]> git.proxmox.com Git - pve-installer.git/commitdiff
tui: switch to `f64` for disk sizes
authorStefan Sterz <s.sterz@proxmox.com>
Thu, 22 Jun 2023 09:20:38 +0000 (11:20 +0200)
committerThomas Lamprecht <t.lamprecht@proxmox.com>
Thu, 22 Jun 2023 12:34:24 +0000 (14:34 +0200)
previously the tui used `u64` internally to represent the disk size.
since the perl-based installer expects GiB as floats and that is also
what is displayed in the tui that meant a lot of converting back and
forth. it also lead to an error where the disk sizes that were set
seemed to not have been persisted, even though the sizes were
correctly set. this commit refactors the installer to convert the size
once in the beginning and then stick to `f64`.

Signed-off-by: Stefan Sterz <s.sterz@proxmox.com>
Reviewed-by: Christoph Heiss <c.heiss@proxmox.com>
Tested-by: Christoph Heiss <c.heiss@proxmox.com>
proxmox-tui-installer/src/options.rs
proxmox-tui-installer/src/setup.rs
proxmox-tui-installer/src/views/mod.rs

index 5f3d29548f8274ab54b2ce085cb7d54129e6b7e5..e1218df7a24675b7f260f9166be2cf195ce1a4a9 100644 (file)
@@ -86,11 +86,11 @@ pub const FS_TYPES: &[FsType] = {
 
 #[derive(Clone, Debug)]
 pub struct LvmBootdiskOptions {
-    pub total_size: u64,
-    pub swap_size: Option<u64>,
-    pub max_root_size: Option<u64>,
-    pub max_data_size: Option<u64>,
-    pub min_lvm_free: Option<u64>,
+    pub total_size: f64,
+    pub swap_size: Option<f64>,
+    pub max_root_size: Option<f64>,
+    pub max_data_size: Option<f64>,
+    pub min_lvm_free: Option<f64>,
 }
 
 impl LvmBootdiskOptions {
@@ -107,7 +107,7 @@ impl LvmBootdiskOptions {
 
 #[derive(Clone, Debug)]
 pub struct BtrfsBootdiskOptions {
-    pub disk_size: u64,
+    pub disk_size: f64,
 }
 
 impl BtrfsBootdiskOptions {
@@ -180,7 +180,7 @@ pub struct ZfsBootdiskOptions {
     pub compress: ZfsCompressOption,
     pub checksum: ZfsChecksumOption,
     pub copies: usize,
-    pub disk_size: u64,
+    pub disk_size: f64,
 }
 
 impl ZfsBootdiskOptions {
@@ -202,12 +202,12 @@ pub enum AdvancedBootdiskOptions {
     Btrfs(BtrfsBootdiskOptions),
 }
 
-#[derive(Clone, Debug, Eq, PartialEq)]
+#[derive(Clone, Debug, PartialEq)]
 pub struct Disk {
     pub index: String,
     pub path: String,
     pub model: Option<String>,
-    pub size: u64,
+    pub size: f64,
 }
 
 impl fmt::Display for Disk {
@@ -219,11 +219,7 @@ impl fmt::Display for Disk {
             // FIXME: ellipsize too-long names?
             write!(f, " ({model})")?;
         }
-        write!(
-            f,
-            " ({:.2} GiB)",
-            (self.size as f64) / 1024. / 1024. / 1024.
-        )
+        write!(f, " ({:.2} GiB)", self.size)
     }
 }
 
@@ -233,6 +229,8 @@ impl From<&Disk> for String {
     }
 }
 
+impl cmp::Eq for Disk {}
+
 impl cmp::PartialOrd for Disk {
     fn partial_cmp(&self, other: &Self) -> Option<cmp::Ordering> {
         self.index.partial_cmp(&other.index)
index 43e4b0d8ed02198e5c756bba20a5d17c2ffce6fb..1c5ff3ece4498d330855f8903387049587870cae 100644 (file)
@@ -109,15 +109,15 @@ pub struct InstallConfig {
 
     #[serde(serialize_with = "serialize_fstype")]
     filesys: FsType,
-    hdsize: u64,
+    hdsize: f64,
     #[serde(skip_serializing_if = "Option::is_none")]
-    swapsize: Option<u64>,
+    swapsize: Option<f64>,
     #[serde(skip_serializing_if = "Option::is_none")]
-    maxroot: Option<u64>,
+    maxroot: Option<f64>,
     #[serde(skip_serializing_if = "Option::is_none")]
-    minfree: Option<u64>,
+    minfree: Option<f64>,
     #[serde(skip_serializing_if = "Option::is_none")]
-    maxvz: Option<u64>,
+    maxvz: Option<f64>,
 
     #[serde(skip_serializing_if = "Option::is_none")]
     zfs_opts: Option<InstallZfsOption>,
@@ -153,7 +153,7 @@ impl From<InstallerOptions> for InstallConfig {
             autoreboot: options.autoreboot as usize,
 
             filesys: options.bootdisk.fstype,
-            hdsize: 0,
+            hdsize: 0.,
             swapsize: None,
             maxroot: None,
             minfree: None,
@@ -243,13 +243,13 @@ fn deserialize_disks_map<'de, D>(deserializer: D) -> Result<Vec<Disk>, D::Error>
 where
     D: Deserializer<'de>,
 {
-    let disks = <Vec<(usize, String, u64, String, u64, String)>>::deserialize(deserializer)?;
+    let disks = <Vec<(usize, String, f64, String, f64, String)>>::deserialize(deserializer)?;
     Ok(disks
         .into_iter()
         .map(
             |(index, device, size_mb, model, logical_bsize, _syspath)| Disk {
                 index: index.to_string(),
-                size: size_mb * logical_bsize,
+                size: (size_mb * logical_bsize) / 1024. / 1024. / 1024.,
                 path: device,
                 model: (!model.is_empty()).then_some(model),
             },
index ee96398de9bdb9fed8960da7523cbdd965695150..faa005264f31ee94b90a6c7984e9a306f052655e 100644 (file)
@@ -189,17 +189,15 @@ impl DiskSizeEditView {
         }
     }
 
-    pub fn content(mut self, content: u64) -> Self {
-        let val = (content as f64) / 1024. / 1024. / 1024.;
-
+    pub fn content(mut self, content: f64) -> Self {
         if let Some(view) = self.view.get_child_mut(0).and_then(|v| v.downcast_mut()) {
-            *view = FloatEditView::new().content(val).full_width();
+            *view = FloatEditView::new().content(content).full_width();
         }
 
         self
     }
 
-    pub fn content_maybe(self, content: Option<u64>) -> Self {
+    pub fn content_maybe(self, content: Option<f64>) -> Self {
         if let Some(value) = content {
             self.content(value)
         } else {
@@ -207,20 +205,19 @@ impl DiskSizeEditView {
         }
     }
 
-    pub fn max_value(mut self, max: u64) -> Self {
+    pub fn max_value(mut self, max: f64) -> Self {
         if let Some(view) = self
             .view
             .get_child_mut(0)
             .and_then(|v| v.downcast_mut::<ResizedView<FloatEditView>>())
         {
-            let max = (max as f64) / 1024. / 1024. / 1024.;
             view.get_inner_mut().set_max_value(max);
         }
 
         self
     }
 
-    pub fn get_content(&self) -> Option<u64> {
+    pub fn get_content(&self) -> Option<f64> {
         self.with_view(|v| {
             v.get_child(0)?
                 .downcast_ref::<ResizedView<FloatEditView>>()?
@@ -234,7 +231,6 @@ impl DiskSizeEditView {
                 .flatten()
         })
         .flatten()
-        .map(|val| val as u64)
     }
 }
 
@@ -285,8 +281,8 @@ where
     }
 }
 
-impl FormViewGetValue<u64> for DiskSizeEditView {
-    fn get_value(&self) -> Option<u64> {
+impl FormViewGetValue<f64> for DiskSizeEditView {
+    fn get_value(&self) -> Option<f64> {
         self.get_content()
     }
 }