|
| 1 | +// Copyright 2024 Amazon.com, Inc. or its affiliates. All Rights Reserved. |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +use std::fmt; |
| 5 | + |
| 6 | +/// Errors associated with actions on `RootfsConfig`. |
| 7 | +#[derive(Debug)] |
| 8 | +pub enum RootfsConfigError { |
| 9 | + /// Invalid device specification. |
| 10 | + InvalidDevice(String), |
| 11 | + /// Invalid filesystem type. |
| 12 | + InvalidFsType(String), |
| 13 | + /// Invalid mount flags. |
| 14 | + InvalidMountFlags(String), |
| 15 | +} |
| 16 | + |
| 17 | +impl fmt::Display for RootfsConfigError { |
| 18 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 19 | + use self::RootfsConfigError::*; |
| 20 | + match *self { |
| 21 | + InvalidDevice(ref e) => write!(f, "Invalid device specification: {}", e), |
| 22 | + InvalidFsType(ref e) => write!(f, "Invalid filesystem type: {}", e), |
| 23 | + InvalidMountFlags(ref e) => write!(f, "Invalid mount flags: {}", e), |
| 24 | + } |
| 25 | + } |
| 26 | +} |
| 27 | + |
| 28 | +type RootfsResult<T> = std::result::Result<T, RootfsConfigError>; |
| 29 | + |
| 30 | +/// Strongly typed data structure used to configure the root filesystem |
| 31 | +/// kernel arguments for the microvm. |
| 32 | +#[derive(Clone, Debug, Eq, PartialEq, Default)] |
| 33 | +pub struct RootfsConfig { |
| 34 | + /// The device on which the root filesystem should be found. |
| 35 | + /// Defaults to "/dev/vda1" if not specified. |
| 36 | + pub device: Option<String>, |
| 37 | + /// The type of the filesystem on the device. |
| 38 | + /// Defaults to "virtiofs" if not specified. |
| 39 | + pub fs_type: Option<String>, |
| 40 | + /// Additional mount flags to be used when mounting the root filesystem. |
| 41 | + pub mount_flags: Option<String>, |
| 42 | + /// Whether to mount the root filesystem read-only. |
| 43 | + /// If false, mount read-write. |
| 44 | + pub read_only: bool, |
| 45 | +} |
| 46 | + |
| 47 | +impl RootfsConfig { |
| 48 | + /// Create a new RootfsConfig with default values |
| 49 | + pub fn new() -> Self { |
| 50 | + Self::default() |
| 51 | + } |
| 52 | + |
| 53 | + /// Set the device for the root filesystem |
| 54 | + pub fn with_device<S: Into<String>>(mut self, device: S) -> RootfsResult<Self> { |
| 55 | + let device_str = device.into(); |
| 56 | + if device_str.is_empty() { |
| 57 | + return Err(RootfsConfigError::InvalidDevice( |
| 58 | + "Device cannot be empty".to_string(), |
| 59 | + )); |
| 60 | + } |
| 61 | + self.device = Some(device_str); |
| 62 | + Ok(self) |
| 63 | + } |
| 64 | + |
| 65 | + /// Set the filesystem type |
| 66 | + pub fn with_fs_type<S: Into<String>>(mut self, fs_type: S) -> RootfsResult<Self> { |
| 67 | + let fs_type_str = fs_type.into(); |
| 68 | + if fs_type_str.is_empty() { |
| 69 | + return Err(RootfsConfigError::InvalidFsType( |
| 70 | + "Filesystem type cannot be empty".to_string(), |
| 71 | + )); |
| 72 | + } |
| 73 | + self.fs_type = Some(fs_type_str); |
| 74 | + Ok(self) |
| 75 | + } |
| 76 | + |
| 77 | + /// Set the mount flags |
| 78 | + pub fn with_mount_flags<S: Into<String>>(mut self, mount_flags: S) -> RootfsResult<Self> { |
| 79 | + self.mount_flags = Some(mount_flags.into()); |
| 80 | + Ok(self) |
| 81 | + } |
| 82 | + |
| 83 | + /// Set whether the filesystem should be mounted read-only |
| 84 | + pub fn with_read_only(mut self, read_only: bool) -> Self { |
| 85 | + self.read_only = read_only; |
| 86 | + self |
| 87 | + } |
| 88 | + |
| 89 | + /// Get the device, with default fallback |
| 90 | + pub fn get_device(&self) -> &str { |
| 91 | + self.device.as_deref().unwrap_or("/dev/vda1") |
| 92 | + } |
| 93 | + |
| 94 | + /// Get the filesystem type, with default fallback |
| 95 | + pub fn get_fs_type(&self) -> &str { |
| 96 | + self.fs_type.as_deref().unwrap_or("virtiofs") |
| 97 | + } |
| 98 | + |
| 99 | + /// Get the mount flags, if any |
| 100 | + pub fn get_mount_flags(&self) -> Option<&str> { |
| 101 | + self.mount_flags.as_deref() |
| 102 | + } |
| 103 | + |
| 104 | + /// Generate kernel command line arguments from this config |
| 105 | + pub fn to_kernel_args(&self) -> String { |
| 106 | + let mut args = Vec::new(); |
| 107 | + |
| 108 | + // Add root device |
| 109 | + args.push(format!("root={}", self.get_device())); |
| 110 | + |
| 111 | + // Add filesystem type |
| 112 | + args.push(format!("rootfstype={}", self.get_fs_type())); |
| 113 | + |
| 114 | + // Add mount flags if specified |
| 115 | + if let Some(flags) = self.get_mount_flags() { |
| 116 | + if !flags.is_empty() { |
| 117 | + args.push(format!("rootflags={}", flags)); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + // Add read-only or read-write |
| 122 | + if self.read_only { |
| 123 | + args.push("ro".to_string()); |
| 124 | + } else { |
| 125 | + args.push("rw".to_string()); |
| 126 | + } |
| 127 | + |
| 128 | + args.join(" ") |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +#[cfg(test)] |
| 133 | +mod tests { |
| 134 | + use super::*; |
| 135 | + |
| 136 | + #[test] |
| 137 | + fn test_default_config() { |
| 138 | + let config = RootfsConfig::new(); |
| 139 | + assert_eq!(config.get_device(), "/dev/vda1"); |
| 140 | + assert_eq!(config.get_fs_type(), "virtiofs"); |
| 141 | + assert_eq!(config.get_mount_flags(), None); |
| 142 | + assert!(!config.read_only); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn test_custom_config() { |
| 147 | + let config = RootfsConfig::new() |
| 148 | + .with_device("/dev/vdb1") |
| 149 | + .unwrap() |
| 150 | + .with_fs_type("ext4") |
| 151 | + .unwrap() |
| 152 | + .with_mount_flags("noatime,discard") |
| 153 | + .unwrap() |
| 154 | + .with_read_only(true); |
| 155 | + |
| 156 | + assert_eq!(config.get_device(), "/dev/vdb1"); |
| 157 | + assert_eq!(config.get_fs_type(), "ext4"); |
| 158 | + assert_eq!(config.get_mount_flags(), Some("noatime,discard")); |
| 159 | + assert!(config.read_only); |
| 160 | + } |
| 161 | + |
| 162 | + #[test] |
| 163 | + fn test_to_kernel_args_default() { |
| 164 | + let config = RootfsConfig::new(); |
| 165 | + let args = config.to_kernel_args(); |
| 166 | + assert_eq!(args, "root=/dev/vda1 rootfstype=virtiofs rw"); |
| 167 | + } |
| 168 | + |
| 169 | + #[test] |
| 170 | + fn test_to_kernel_args_custom() { |
| 171 | + let config = RootfsConfig::new() |
| 172 | + .with_device("/dev/vdb1") |
| 173 | + .unwrap() |
| 174 | + .with_fs_type("ext4") |
| 175 | + .unwrap() |
| 176 | + .with_mount_flags("noatime,discard") |
| 177 | + .unwrap() |
| 178 | + .with_read_only(true); |
| 179 | + |
| 180 | + let args = config.to_kernel_args(); |
| 181 | + assert_eq!( |
| 182 | + args, |
| 183 | + "root=/dev/vdb1 rootfstype=ext4 rootflags=noatime,discard ro" |
| 184 | + ); |
| 185 | + } |
| 186 | + |
| 187 | + #[test] |
| 188 | + fn test_invalid_device() { |
| 189 | + let result = RootfsConfig::new().with_device(""); |
| 190 | + assert!(result.is_err()); |
| 191 | + } |
| 192 | + |
| 193 | + #[test] |
| 194 | + fn test_invalid_fs_type() { |
| 195 | + let result = RootfsConfig::new().with_fs_type(""); |
| 196 | + assert!(result.is_err()); |
| 197 | + } |
| 198 | +} |
0 commit comments