add api to format disks and create datastores

This commit is contained in:
Dietmar Maurer 2020-06-10 11:02:37 +02:00
parent fab2413741
commit d4f2397d4c
4 changed files with 229 additions and 0 deletions

View File

@ -15,6 +15,8 @@ use crate::server::WorkerTask;
use crate::api2::types::{UPID_SCHEMA, NODE_SCHEMA, BLOCKDEVICE_NAME_SCHEMA};
pub mod directory;
#[api(
protected: true,
input: {

View File

@ -0,0 +1,214 @@
use anyhow::{Error};
use ::serde::{Deserialize, Serialize};
use proxmox::api::{api, Permission, RpcEnvironment, RpcEnvironmentType};
use proxmox::api::section_config::SectionConfigData;
use proxmox::api::router::Router;
use crate::config::acl::{PRIV_SYS_AUDIT, PRIV_SYS_MODIFY};
use crate::tools::disks::{
DiskManage, FileSystemType,
create_file_system, create_single_linux_partition, get_fs_uuid,
};
use crate::tools::systemd::{self, types::*};
use crate::server::WorkerTask;
use crate::api2::types::*;
#[api(
properties: {
"filesystem": {
type: FileSystemType,
optional: true,
},
},
)]
#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all="kebab-case")]
/// Datastore mount info.
pub struct DatastoreMountInfo {
/// The path of the mount unit.
pub unitfile: String,
/// The mount path.
pub path: String,
/// The mounted device.
pub device: String,
/// File system type
pub filesystem: Option<String>,
/// Mount options
pub options: Option<String>,
}
#[api(
protected: true,
input: {
properties: {
node: {
schema: NODE_SCHEMA,
},
}
},
returns: {
description: "List of systemd datastore mount units.",
type: Array,
items: {
type: DatastoreMountInfo,
},
},
access: {
permission: &Permission::Privilege(&["system", "disks"], PRIV_SYS_AUDIT, false),
},
)]
/// List systemd datastore mount units.
fn list_datastore_mounts() -> Result<Vec<DatastoreMountInfo>, Error> {
lazy_static::lazy_static! {
static ref MOUNT_NAME_REGEX: regex::Regex = regex::Regex::new(r"^mnt-datastore-(.+)\.mount$").unwrap();
}
let mut list = Vec::new();
let basedir = "/etc/systemd/system";
for item in crate::tools::fs::scan_subdir(libc::AT_FDCWD, basedir, &MOUNT_NAME_REGEX)? {
let item = item?;
let name = item.file_name().to_string_lossy().to_string();
let unitfile = format!("{}/{}", basedir, name);
let config = systemd::config::parse_systemd_mount(&unitfile)?;
let data: SystemdMountSection = config.lookup("Mount", "Mount")?;
list.push(DatastoreMountInfo {
unitfile,
device: data.What,
path: data.Where,
filesystem: data.Type,
options: data.Options,
});
}
Ok(list)
}
#[api(
protected: true,
input: {
properties: {
node: {
schema: NODE_SCHEMA,
},
name: {
schema: DATASTORE_SCHEMA,
},
disk: {
schema: BLOCKDEVICE_NAME_SCHEMA,
},
"add-datastore": {
description: "Configure a datastore using the directory.",
type: bool,
optional: true,
},
filesystem: {
type: FileSystemType,
optional: true,
},
}
},
returns: {
schema: UPID_SCHEMA,
},
access: {
permission: &Permission::Privilege(&["system", "disks"], PRIV_SYS_MODIFY, false),
},
)]
/// Create a Filesystem on an unused disk. Will be mounted under '/mnt/datastore/<name>'.".
fn create_datastore_disk(
name: String,
disk: String,
add_datastore: Option<bool>,
filesystem: Option<FileSystemType>,
rpcenv: &mut dyn RpcEnvironment,
) -> Result<String, Error> {
let to_stdout = if rpcenv.env_type() == RpcEnvironmentType::CLI { true } else { false };
let username = rpcenv.get_user().unwrap();
let upid_str = WorkerTask::new_thread(
"dircreate", Some(name.clone()), &username.clone(), to_stdout, move |worker|
{
worker.log(format!("create datastore '{}' on disk {}", name, disk));
let add_datastore = add_datastore.unwrap_or(false);
let filesystem = filesystem.unwrap_or(FileSystemType::Ext4);
let manager = DiskManage::new();
let disk = manager.clone().disk_by_name(&disk)?;
let partition = create_single_linux_partition(&disk)?;
create_file_system(&partition, filesystem)?;
let uuid = get_fs_uuid(&partition)?;
let uuid_path = format!("/dev/disk/by-uuid/{}", uuid);
let mount_unit_name = create_datastore_mount_unit(&name, filesystem, &uuid_path)?;
if add_datastore {
unimplemented!(); // fixme
}
systemd::reload_daemon()?;
systemd::enable_unit(&mount_unit_name)?;
systemd::start_unit(&mount_unit_name)?;
Ok(())
})?;
Ok(upid_str)
}
pub const ROUTER: Router = Router::new()
.get(&API_METHOD_LIST_DATASTORE_MOUNTS)
.post(&API_METHOD_CREATE_DATASTORE_DISK);
fn create_datastore_mount_unit(
datastore_name: &str,
fs_type: FileSystemType,
what: &str,
) -> Result<String, Error> {
let mount_point = format!("/mnt/datastore/{}", datastore_name);
let mut mount_unit_name = systemd::escape_unit(&mount_point, true);
mount_unit_name.push_str(".mount");
let mount_unit_path = format!("/etc/systemd/system/{}", mount_unit_name);
let unit = SystemdUnitSection {
Description: format!("Mount datatstore '{}' under '{}'", datastore_name, mount_point),
..Default::default()
};
let install = SystemdInstallSection {
WantedBy: Some(vec!["multi-user.target".to_string()]),
..Default::default()
};
let mount = SystemdMountSection {
What: what.to_string(),
Where: mount_point,
Type: Some(fs_type.to_string()),
Options: Some(String::from("defaults")),
..Default::default()
};
let mut config = SectionConfigData::new();
config.set_data("Unit", "Unit", unit)?;
config.set_data("Install", "Install", install)?;
config.set_data("Mount", "Mount", mount)?;
systemd::config::save_systemd_mount(&mount_unit_path, &config)?;
Ok(mount_unit_name)
}

View File

@ -927,6 +927,15 @@ impl std::fmt::Display for FileSystemType {
}
}
impl std::str::FromStr for FileSystemType {
type Err = serde_json::Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
use serde::de::IntoDeserializer;
Self::deserialize(s.into_deserializer())
}
}
/// Create a file system on a disk or disk partition
pub fn create_file_system(disk: &Disk, fs_type: FileSystemType) -> Result<(), Error> {

View File

@ -126,6 +126,10 @@ pub fn parse_systemd_timer(filename: &str) -> Result<SectionConfigData, Error> {
parse_systemd_config(&TIMER_CONFIG, filename)
}
pub fn parse_systemd_mount(filename: &str) -> Result<SectionConfigData, Error> {
parse_systemd_config(&MOUNT_CONFIG, filename)
}
fn save_systemd_config(config: &SectionConfig, filename: &str, data: &SectionConfigData) -> Result<(), Error> {
let raw = config.write(filename, &data)?;