From d4f2397d4ceac719b2badc11c843c77f3cab6ee1 Mon Sep 17 00:00:00 2001 From: Dietmar Maurer Date: Wed, 10 Jun 2020 11:02:37 +0200 Subject: [PATCH] add api to format disks and create datastores --- src/api2/node/disks.rs | 2 + src/api2/node/disks/directory.rs | 214 +++++++++++++++++++++++++++++++ src/tools/disks.rs | 9 ++ src/tools/systemd/config.rs | 4 + 4 files changed, 229 insertions(+) create mode 100644 src/api2/node/disks/directory.rs diff --git a/src/api2/node/disks.rs b/src/api2/node/disks.rs index f90e8681..05e48b1b 100644 --- a/src/api2/node/disks.rs +++ b/src/api2/node/disks.rs @@ -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: { diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs new file mode 100644 index 00000000..ed9fbdf7 --- /dev/null +++ b/src/api2/node/disks/directory.rs @@ -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, + /// Mount options + pub options: Option, +} + +#[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, 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/'.". +fn create_datastore_disk( + name: String, + disk: String, + add_datastore: Option, + filesystem: Option, + rpcenv: &mut dyn RpcEnvironment, +) -> Result { + + 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 { + + 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) +} diff --git a/src/tools/disks.rs b/src/tools/disks.rs index 0e1364dd..07ad1341 100644 --- a/src/tools/disks.rs +++ b/src/tools/disks.rs @@ -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 { + 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> { diff --git a/src/tools/systemd/config.rs b/src/tools/systemd/config.rs index 68d9c229..9526a525 100644 --- a/src/tools/systemd/config.rs +++ b/src/tools/systemd/config.rs @@ -126,6 +126,10 @@ pub fn parse_systemd_timer(filename: &str) -> Result { parse_systemd_config(&TIMER_CONFIG, filename) } +pub fn parse_systemd_mount(filename: &str) -> Result { + parse_systemd_config(&MOUNT_CONFIG, filename) +} + fn save_systemd_config(config: &SectionConfig, filename: &str, data: &SectionConfigData) -> Result<(), Error> { let raw = config.write(filename, &data)?;