Commit ed1e80fd authored by Heinz N. Gies's avatar Heinz N. Gies

First use of brand tools

parent 8789199c
Pipeline #12341952 failed with stage
in 4 minutes and 42 seconds
......@@ -9,7 +9,7 @@
# {brand_root} - root of this brand
# {hostname} - hostname of the jail
modname=jail
modname="jail"
# install is executed after cloning
[install]
......@@ -25,10 +25,11 @@ args=["{jail_root}", "{brand_root}"]
# boot the inner zone
[boot]
cmd="{brand_root}/boot"
args=["{jail_uuid}" "{hostname}"]
args=["{jail_uuid}", "{hostname}"]
# Halt the jail executed on the host before
# rctl and interfaces are torn down
# rctl and interfaces are torn down, the outer
# jail is then shut down
[halt]
cmd="{brand_root}/halt"
args=["{jail_root}", "{inner_id}"]
......
heinz@Schrodinger.6083
\ No newline at end of file
use std;
use std::io::Read;
use std::fs::File;
use std::path::PathBuf;
use std::process::{Command, Output};
use toml;
use jails::Jail;
use config::Config;
#[derive(Debug, Deserialize)]
pub struct Step {
pub cmd: String,
pub args: Vec<String>
}
fn expand(fmt: String, jail: &Jail, conf: &Config) -> String {
let inner_id = match jail.inner {
Some(e) => e.id,
_ => 0
};
let outer_id = match jail.outer {
Some(e) => e.id,
_ => 0
};
let mut brand_root = PathBuf::from(conf.settings.brand_dir.as_str());
brand_root.push(jail.config.brand.as_str());
let mut jail_root = String::new();
jail_root.push('/');
jail_root.push_str(jail.idx.root.as_str());
fmt.clone()
.replace("{inner_id}", inner_id.to_string().as_str())
.replace("{ounter_id}", outer_id.to_string().as_str())
.replace("{jail_uuid}", jail.idx.uuid.hyphenated().to_string().as_str())
.replace("{jail_root}", jail_root.as_str())
.replace("{brand_root}", brand_root.to_string_lossy().as_ref())
.replace("{hostname}", jail.config.hostname.as_str())
}
impl Step {
fn cmd(&self, jail: &Jail, conf: &Config) -> String{
expand(self.cmd.clone(), jail, conf)
}
fn args(&self, jail: &Jail, conf: &Config) -> Vec<String>{
self.args.clone().into_iter().map(
|arg| expand(arg, jail, conf)
).collect()
}
#[cfg(not(target_os = "freebsd"))]
pub fn run(&self, jail: &Jail, conf: &Config) -> Result<Output, std::io::Error> {
let command = self.cmd(jail, conf);
let args = self.args(jail, conf);
debug!("[BRAND] Running command";
"command" => command.clone(),
"args" => args.clone().join(" "),
"scope" => "brand",
"brand" => jail.config.brand.as_str());
Command::new("echo").args(args).output()
}
#[cfg(target_os = "freebsd")]
pub fn run(&self, jail: &Jail, conf: &Config) -> Result<Output, std::io::Error> {
let command = self.cmd(jail, conf);
let args = self.args(jail, conf);
debug!("[BRAND] Running command";
"command" => command.clone(),
"args" => args.clone().join(" "),
"scope" => "brand",
"brand" => jail.config.brand.as_str());
Command::new(command).args(args).output()
}
}
#[derive(Debug, Deserialize)]
pub struct Brand {
modname: String,
pub install: Step,
pub init: Step,
pub boot: Step,
pub halt: Step,
pub login: Step,
}
impl Brand {
fn from_file(brand: &str) -> Result<Self, Box<std::error::Error>> {
let mut file = File::open(brand)?;
let mut contents = String::new();
file.read_to_string(&mut contents).expect(
"Failed to read brand file.",
);
let result: Brand = toml::from_str(contents.as_str())?;
Ok(result)
}
pub fn load(brand: &str, conf: &Config) -> Result<Self, Box<std::error::Error>> {
let mut brand_root = PathBuf::from(conf.settings.brand_dir.as_str());
brand_root.push(brand);
brand_root.push("config.toml");
Brand::from_file(brand_root.to_string_lossy().as_ref())
}
}
#[cfg(test)]
mod tests {
use brand::Brand;
#[test]
fn jail() {
match Brand::from_file("rel/pkgng/deploy/usr/local/lib/brand/jail/config.toml") {
Ok(_) => assert!(true),
Err(e) => {
println!("{}", e);
assert!(false)
}
}
}
#[test]
fn lx_jail() {
match Brand::from_file("rel/pkgng/deploy/usr/local/lib/brand/lx_jail/config.toml") {
Ok(_) => assert!(true),
Err(e) => {
println!("{}", e);
assert!(false)
}
}
}
}
......@@ -20,8 +20,11 @@ pub struct Settings {
pub conf_dir: String,
#[serde(default = "default_image_dir")]
pub image_dir: String,
#[serde(default = "default_brand_dir")]
pub brand_dir: String,
#[serde(default = "devfs_ruleset")]
pub devfs_ruleset: u32,
pub networks: Map<String, String>,
}
......@@ -39,6 +42,10 @@ fn default_conf_dir() -> String {
"/usr/local/etc/vmadm".to_string()
}
fn default_brand_dir() -> String {
"/usr/local/lib/brand".to_string()
}
fn default_image_dir() -> String {
"/var/imgadm/images".to_string()
}
......
//! Wrapper around the freebsd jail commands
use std::error::Error;
use std::path::PathBuf;
use errors::GenericError;
use std::collections::HashMap;
use std::process::Command;
......@@ -9,6 +10,7 @@ use config::Config;
use uuid::Uuid;
use jdb::IdxEntry;
use jail_config::JailConfig;
use brand::Brand;
#[derive(Debug)]
/// Basic information about a ZFS dataset
......@@ -63,11 +65,13 @@ impl<'a> Jail<'a> {
/// starts a jail
pub fn start(&self, config: &Config) -> Result<i32, Box<Error>> {
self.set_rctl()?;
self.mount_devfs()?;
let brand = Brand::load(self.config.brand.as_str(), config)?;
if self.config.brand == "lx-jail" {
self.mount_lxfs()?;
}
brand.init.run(self, config)?;
// self.mount_devfs()?;
// if self.config.brand == "lx-jail" {
// self.mount_lxfs()?;
// }
let CreateArgs { args, ifs } = create_args(config, self)?;
debug!("Start jail"; "vm" => self.idx.uuid.hyphenated().to_string(), "args" => args.clone().join(" "));
......@@ -148,11 +152,14 @@ impl<'a> Jail<'a> {
}
}
/// stops a jail
pub fn stop(&self) -> Result<i32, Box<Error>> {
pub fn stop(&self, config: &Config) -> Result<i32, Box<Error>> {
debug!("Dleting jail"; "vm" => self.idx.uuid.hyphenated().to_string());
let brand = Brand::load(self.config.brand.as_str(), config)?;
brand.halt.run(self, config)?;
let output = Command::new(JAIL)
.args(&["-r", self.idx.uuid.hyphenated().to_string().as_str()])
.output()
......@@ -164,7 +171,7 @@ impl<'a> Jail<'a> {
let _ = self.umount_devfs();
if self.config.brand == "lx-jail" {
if self.config.brand == "lx-jail" {
let _ = self.umount_lxfs();
}
......@@ -321,6 +328,7 @@ fn create_args(config: &Config, jail: &Jail) -> Result<CreateArgs, Box<Error>> {
let uuid = jail.idx.uuid.hyphenated().to_string();
let mut name = String::from("name=");
name.push_str(uuid.as_str());
let mut idx_file = PathBuf::from(config.settings.conf_dir.as_str());
let mut path = String::from("path=/");
path.push_str(jail.idx.root.as_str());
path.push_str("/root");
......
......@@ -56,6 +56,7 @@ use aud::{Failure, Adventure, Saga};
use std::process::Command;
mod brand;
mod zfs;
mod images;
mod jails;
......@@ -255,7 +256,7 @@ fn reboot(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>>
}
Ok(jail) => {
println!("Rebooting jail {}", uuid);
jail.stop()?;
jail.stop(conf)?;
jail.start(conf)
}
}
......@@ -343,7 +344,7 @@ fn stop(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
}
Ok(jail) => {
println!("Stopping jail {}", uuid);
jail.stop()
jail.stop(conf)
}
}
}
......@@ -528,7 +529,7 @@ fn delete(conf: &Config, matches: &clap::ArgMatches) -> Result<i32, Box<Error>>
Ok(jail) => {
if jail.outer.is_some() {
println!("Stopping jail {}", uuid);
jail.stop()?;
jail.stop(conf)?;
};
let origin = zfs::origin(jail.idx.root.as_str());
match zfs::destroy(jail.idx.root.as_str()) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment