Commit 7ab115ff authored by Heinz N. Gies's avatar Heinz N. Gies

Create snapshot and clone for create

parent 19230deb
{
"cpu" : 100,
"cpu_cap" : 100,
"image_uuid": "ec66180b-f07a-fb57-b9dc-245f97f74e4e",
"hostname": "test",
"uuid" : "fe0b9b05-1f3e-4b11-b0ae-8494bb6ecd53",
"ram" : 1024,
"disk" : 100,
"max_physical_memory" : 1024,
"quota" : 100,
"alias" : "test"
}
......@@ -16,11 +16,12 @@ use config::Config;
pub struct JailConfig {
#[serde(default = "new_uuid")]
pub uuid: String,
pub image_uuid: String,
alias: String,
hostname: String,
ram: u64,
cpu: u64,
disk: u64,
max_physical_memory: u64,
cpu_cap: u64,
quota: u64,
#[serde(default = "bfalse")]
autostart: bool,
}
......@@ -36,7 +37,7 @@ fn bfalse() -> bool {
#[derive(Debug, Serialize, Deserialize)]
pub struct IdxEntry {
version: u32,
uuid: String,
pub uuid: String,
pub root: String,
state: String,
jail_type: String,
......@@ -211,7 +212,7 @@ impl<'a> JDB<'a> {
"{:37} {:5} {:8} {:17} {}",
conf.uuid,
"OS",
conf.ram,
conf.max_physical_memory,
entry.state,
conf.alias
);
......
......@@ -106,9 +106,13 @@ fn list(conf: &Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
fn create(conf: &Config, _matches: &clap::ArgMatches) -> Result<i32, Box<Error>> {
let mut db = JDB::open(conf)?;
let conf: jdb::JailConfig = serde_json::from_reader(io::stdin())?;
let entry = db.insert(conf)?;
zfs::create(entry.root.as_str())?;
let jail: jdb::JailConfig = serde_json::from_reader(io::stdin())?;
let mut dataset = conf.settings.pool.clone();
dataset.push('/');
dataset.push_str(jail.image_uuid.clone().as_str());
let entry = db.insert(jail)?;
let snap = zfs::snapshot(dataset.as_str(), entry.uuid.as_str())?;
zfs::clone(snap.as_str(), entry.root.as_str())?;
Ok(0)
}
......
......@@ -32,12 +32,9 @@ pub fn list(pool: &str) -> Result<Vec<ZFSEntry>, Box<Error>> {
}
/// reads the zfs datasets in a pool
pub fn get(pool: &str, dataset: &str) -> Result<ZFSEntry, Box<Error>> {
let mut path = String::from(pool);
path.push('/');
path.push_str(dataset);
pub fn get(dataset: &str) -> Result<ZFSEntry, Box<Error>> {
let output = Command::new("zfs")
.args(&["list", "-p", "-H", path.as_str()])
.args(&["list", "-p", "-H", dataset])
.output()
.expect("zfs list failed");
if output.status.success() {
......@@ -61,6 +58,37 @@ pub fn create(dataset: &str) -> Result<i32, Box<Error>> {
}
}
/// create a zfs snapshot of a dataset
pub fn snapshot(dataset: &str, snapshot: &str) -> Result<String, Box<Error>> {
let mut snap = String::from(dataset);
snap.push('@');
snap.push_str(snapshot);
let output = Command::new("zfs")
.args(&["snapshot", snap.as_str()])
.output()
.expect("zfs create snapshot");
println!("zfs snapshot {}", snap);
if output.status.success() {
Ok(snap)
} else {
Err(GenericError::bx("Failed create snapshot"))
}
}
/// clones a zfs snapshot
pub fn clone(snapshot: &str, dataset: &str) -> Result<i32, Box<Error>> {
let output = Command::new("zfs")
.args(&["clone", snapshot, dataset])
.output()
.expect("zfs create snapshot");
println!("zfs clone {} {}", snapshot, dataset);
if output.status.success() {
Ok(0)
} else {
Err(GenericError::bx("Failed to clone dataset"))
}
}
/// destroy the zfs datasets in a pool
pub fn destroy(dataset: &str) -> Result<i32, Box<Error>> {
let output = Command::new("zfs")
......
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