fix #4856: tui: bootdisk: use correct defaults in advanced dialog

The size of the install disk was set to the size of the first disk,
regardless of what disk was selected. This only happened if the advanced
options dialog was never opened, and only a disk was selected in the
main bootdisk dialog.

Properly solving this involved restructuring the LVM advanced bootdisk
dialog, to also hold the selected disks, like the ZFS and Btrfs dialogs.
In addition to that, the `BootdiskOptionsRef` needs quite some passing
around, to cover all the cases, since the dialog also needs to be
"reentrant-safe".

I tested (among other things):
  * Only select disk, don't open the advanced dialog, go to summary,
    then back to the bootdisk dialog -> selected disk should be kept
  * Select disk, open advanced dialog but leave everything as is, go to
    summary, then go back again -> selected disk should be kept
  * Same as previous, but change the "Total size" for the disk, go to
    summary and back -> selected disk and size should be kept
  * Same as previous, but additionally change filesystem to XFS -> disk,
    filesystem and size should be kept
  * Same as previous, but then create a ZFS RAID, go to summary & back,
    ZFS RAID should be kept with all parameters
  * etc ..

Further I also verified that the correct disk size(s) get written into
the setup structure for the low-level installer.

Signed-off-by: Christoph Heiss <c.heiss@proxmox.com>
This commit is contained in:
Christoph Heiss 2023-11-09 10:40:56 +01:00 committed by Thomas Lamprecht
parent d81cffcbb4
commit eda9fa0c6c

View File

@ -41,12 +41,16 @@ pub struct BootdiskOptionsView {
impl BootdiskOptionsView { impl BootdiskOptionsView {
pub fn new(siv: &mut Cursive, runinfo: &RuntimeInfo, options: &BootdiskOptions) -> Self { pub fn new(siv: &mut Cursive, runinfo: &RuntimeInfo, options: &BootdiskOptions) -> Self {
let advanced_options = Rc::new(RefCell::new(options.clone()));
let bootdisk_form = FormView::new() let bootdisk_form = FormView::new()
.child( .child(
"Target harddisk", "Target harddisk",
SelectView::new() target_bootdisk_selectview(
.popup() &runinfo.disks,
.with_all(runinfo.disks.iter().map(|d| (d.to_string(), d.clone()))), advanced_options.clone(),
options.disks.first(),
),
) )
.with_name("bootdisk-options-target-disk"); .with_name("bootdisk-options-target-disk");
@ -55,8 +59,6 @@ impl BootdiskOptionsView {
.map(|state| state.setup_info.config.clone()) .map(|state| state.setup_info.config.clone())
.unwrap(); // Safety: InstallerState must always be set .unwrap(); // Safety: InstallerState must always be set
let advanced_options = Rc::new(RefCell::new(options.clone()));
let advanced_button = LinearLayout::horizontal() let advanced_button = LinearLayout::horizontal()
.child(DummyView.full_width()) .child(DummyView.full_width())
.child(Button::new("Advanced options", { .child(Button::new("Advanced options", {
@ -89,20 +91,10 @@ impl BootdiskOptionsView {
} }
pub fn get_values(&mut self) -> Result<BootdiskOptions, String> { pub fn get_values(&mut self) -> Result<BootdiskOptions, String> {
let mut options = (*self.advanced_options).clone().into_inner(); // The simple disk selector, as well as the advanced bootdisk dialog save their
// info on submit directly to the shared `BootdiskOptionsRef` - so just clone() + return
if [FsType::Ext4, FsType::Xfs].contains(&options.fstype) { // it.
let disk = self let options = (*self.advanced_options).clone().into_inner();
.view
.get_child_mut(0)
.and_then(|v| v.downcast_mut::<NamedView<FormView>>())
.map(NamedView::<FormView>::get_mut)
.and_then(|v| v.get_value::<SelectView<Disk>, _>(0))
.ok_or("failed to retrieve bootdisk")?;
options.disks = vec![disk];
}
check_disks_4kn_legacy_boot(self.boot_type, &options.disks)?; check_disks_4kn_legacy_boot(self.boot_type, &options.disks)?;
Ok(options) Ok(options)
} }
@ -117,9 +109,14 @@ struct AdvancedBootdiskOptionsView {
} }
impl AdvancedBootdiskOptionsView { impl AdvancedBootdiskOptionsView {
fn new(runinfo: &RuntimeInfo, options: &BootdiskOptions, product_conf: ProductConfig) -> Self { fn new(
runinfo: &RuntimeInfo,
options_ref: BootdiskOptionsRef,
product_conf: ProductConfig,
) -> Self {
let filter_btrfs = let filter_btrfs =
|fstype: &&FsType| -> bool { product_conf.enable_btrfs || !fstype.is_btrfs() }; |fstype: &&FsType| -> bool { product_conf.enable_btrfs || !fstype.is_btrfs() };
let options = (*options_ref).borrow();
let fstype_select = SelectView::new() let fstype_select = SelectView::new()
.popup() .popup()
@ -136,17 +133,25 @@ impl AdvancedBootdiskOptionsView {
.position(|t| *t == options.fstype) .position(|t| *t == options.fstype)
.unwrap_or_default(), .unwrap_or_default(),
) )
.on_submit(Self::fstype_on_submit); .on_submit({
let options_ref = options_ref.clone();
move |siv, fstype| {
Self::fstype_on_submit(siv, fstype, options_ref.clone());
}
});
let mut view = LinearLayout::vertical() let mut view = LinearLayout::vertical()
.child(DummyView.full_width()) .child(DummyView.full_width())
.child(FormView::new().child("Filesystem", fstype_select)) .child(FormView::new().child("Filesystem", fstype_select))
.child(DummyView.full_width()); .child(DummyView.full_width());
// Create the appropriate (inner) advanced options view
match &options.advanced { match &options.advanced {
AdvancedBootdiskOptions::Lvm(lvm) => { AdvancedBootdiskOptions::Lvm(lvm) => view.add_child(LvmBootdiskOptionsView::new(
view.add_child(LvmBootdiskOptionsView::new(lvm, &product_conf)) &options.disks[0],
} lvm,
&product_conf,
)),
AdvancedBootdiskOptions::Zfs(zfs) => { AdvancedBootdiskOptions::Zfs(zfs) => {
view.add_child(ZfsBootdiskOptionsView::new(runinfo, zfs, &product_conf)) view.add_child(ZfsBootdiskOptionsView::new(runinfo, zfs, &product_conf))
} }
@ -158,20 +163,44 @@ impl AdvancedBootdiskOptionsView {
Self { view } Self { view }
} }
fn fstype_on_submit(siv: &mut Cursive, fstype: &FsType) { /// Called when a new filesystem type is choosen by the user.
/// It first creates the inner (filesystem-specific) options view according to the selected
/// filesytem type.
/// Further, it replaces the (outer) bootdisk selector in the main dialog, either with a
/// selector for LVM configurations or a simple label displaying the chosen RAID for ZFS and
/// Btrfs configurations.
///
/// # Arguments
/// * `siv` - Cursive instance
/// * `fstype` - The chosen filesystem type by the user, for which the UI should be
/// updated accordingly
/// * `options_ref` - [`BootdiskOptionsRef`] where advanced disk options should be saved to
fn fstype_on_submit(siv: &mut Cursive, fstype: &FsType, options_ref: BootdiskOptionsRef) {
let state = siv.user_data::<InstallerState>().unwrap(); let state = siv.user_data::<InstallerState>().unwrap();
let runinfo = state.runtime_info.clone(); let runinfo = state.runtime_info.clone();
let product_conf = state.setup_info.config.clone(); let product_conf = state.setup_info.config.clone();
// Only used for LVM configurations, ZFS and Btrfs do not use the target disk selector
let selected_lvm_disk = siv
.find_name::<FormView>("bootdisk-options-target-disk")
.and_then(|v| v.get_value::<SelectView<Disk>, _>(0));
// Update the (inner) options view
siv.call_on_name("advanced-bootdisk-options-dialog", |view: &mut Dialog| { siv.call_on_name("advanced-bootdisk-options-dialog", |view: &mut Dialog| {
if let Some(AdvancedBootdiskOptionsView { view }) = if let Some(AdvancedBootdiskOptionsView { view }) =
view.get_content_mut().downcast_mut() view.get_content_mut().downcast_mut()
{ {
view.remove_child(3); view.remove_child(3);
match fstype { match fstype {
FsType::Ext4 | FsType::Xfs => view.add_child( FsType::Ext4 | FsType::Xfs => {
LvmBootdiskOptionsView::new_with_defaults(&runinfo.disks[0], &product_conf), // Safety: For LVM setups, the bootdisk SelectView always exists, thus
), // there will also always be a value.
let selected_disk = selected_lvm_disk.clone().unwrap();
view.add_child(LvmBootdiskOptionsView::new_with_defaults(
&selected_disk,
&product_conf,
));
}
FsType::Zfs(_) => view.add_child(ZfsBootdiskOptionsView::new_with_defaults( FsType::Zfs(_) => view.add_child(ZfsBootdiskOptionsView::new_with_defaults(
&runinfo, &runinfo,
&product_conf, &product_conf,
@ -183,15 +212,21 @@ impl AdvancedBootdiskOptionsView {
} }
}); });
// The "bootdisk-options-target-disk" view might be either a `SelectView` (if ext4 of XFS
// is used) or a label containing the filesytem/RAID type (for ZFS and Btrfs).
// Now, unconditionally replace it with the appropriate type of these two, depending on the
// newly selected filesystem type.
siv.call_on_name( siv.call_on_name(
"bootdisk-options-target-disk", "bootdisk-options-target-disk",
|view: &mut FormView| match fstype { move |view: &mut FormView| match fstype {
FsType::Ext4 | FsType::Xfs => { FsType::Ext4 | FsType::Xfs => {
view.replace_child( view.replace_child(
0, 0,
SelectView::new() target_bootdisk_selectview(
.popup() &runinfo.disks,
.with_all(runinfo.disks.iter().map(|d| (d.to_string(), d.clone()))), options_ref,
selected_lvm_disk.as_ref(),
),
); );
} }
other => view.replace_child(0, TextView::new(other.to_string())), other => view.replace_child(0, TextView::new(other.to_string())),
@ -213,15 +248,14 @@ impl AdvancedBootdiskOptionsView {
.ok_or("Failed to retrieve advanced bootdisk options view".to_owned())?; .ok_or("Failed to retrieve advanced bootdisk options view".to_owned())?;
if let Some(view) = advanced.downcast_mut::<LvmBootdiskOptionsView>() { if let Some(view) = advanced.downcast_mut::<LvmBootdiskOptionsView>() {
let advanced = view let (disk, advanced) = view
.get_values() .get_values()
.map(AdvancedBootdiskOptions::Lvm)
.ok_or("Failed to retrieve advanced bootdisk options")?; .ok_or("Failed to retrieve advanced bootdisk options")?;
Ok(BootdiskOptions { Ok(BootdiskOptions {
disks: vec![], disks: vec![disk],
fstype, fstype,
advanced, advanced: AdvancedBootdiskOptions::Lvm(advanced),
}) })
} else if let Some(view) = advanced.downcast_mut::<ZfsBootdiskOptionsView>() { } else if let Some(view) = advanced.downcast_mut::<ZfsBootdiskOptionsView>() {
let (disks, advanced) = view let (disks, advanced) = view
@ -263,14 +297,14 @@ impl ViewWrapper for AdvancedBootdiskOptionsView {
struct LvmBootdiskOptionsView { struct LvmBootdiskOptionsView {
view: FormView, view: FormView,
disk: Disk,
has_extra_fields: bool, has_extra_fields: bool,
} }
impl LvmBootdiskOptionsView { impl LvmBootdiskOptionsView {
fn new(options: &LvmBootdiskOptions, product_conf: &ProductConfig) -> Self { fn new(disk: &Disk, options: &LvmBootdiskOptions, product_conf: &ProductConfig) -> Self {
let show_extra_fields = product_conf.product == ProxmoxProduct::PVE; let show_extra_fields = product_conf.product == ProxmoxProduct::PVE;
// TODO: Set maximum accordingly to disk size
let view = FormView::new() let view = FormView::new()
.child( .child(
"Total size", "Total size",
@ -299,15 +333,16 @@ impl LvmBootdiskOptionsView {
Self { Self {
view, view,
disk: disk.clone(),
has_extra_fields: show_extra_fields, has_extra_fields: show_extra_fields,
} }
} }
fn new_with_defaults(disk: &Disk, product_conf: &ProductConfig) -> Self { fn new_with_defaults(disk: &Disk, product_conf: &ProductConfig) -> Self {
Self::new(&LvmBootdiskOptions::defaults_from(disk), product_conf) Self::new(disk, &LvmBootdiskOptions::defaults_from(disk), product_conf)
} }
fn get_values(&mut self) -> Option<LvmBootdiskOptions> { fn get_values(&mut self) -> Option<(Disk, LvmBootdiskOptions)> {
let min_lvm_free_id = if self.has_extra_fields { 4 } else { 2 }; let min_lvm_free_id = if self.has_extra_fields { 4 } else { 2 };
let max_root_size = self let max_root_size = self
@ -319,13 +354,16 @@ impl LvmBootdiskOptionsView {
.then(|| self.view.get_value::<DiskSizeEditView, _>(3)) .then(|| self.view.get_value::<DiskSizeEditView, _>(3))
.flatten(); .flatten();
Some(LvmBootdiskOptions { Some((
total_size: self.view.get_value::<DiskSizeEditView, _>(0)?, self.disk.clone(),
swap_size: self.view.get_value::<DiskSizeEditView, _>(1), LvmBootdiskOptions {
max_root_size, total_size: self.view.get_value::<DiskSizeEditView, _>(0)?,
max_data_size, swap_size: self.view.get_value::<DiskSizeEditView, _>(1),
min_lvm_free: self.view.get_value::<DiskSizeEditView, _>(min_lvm_free_id), max_root_size,
}) max_data_size,
min_lvm_free: self.view.get_value::<DiskSizeEditView, _>(min_lvm_free_id),
},
))
} }
} }
@ -625,12 +663,11 @@ fn advanced_options_view(
) -> impl View { ) -> impl View {
Dialog::around(AdvancedBootdiskOptionsView::new( Dialog::around(AdvancedBootdiskOptionsView::new(
runinfo, runinfo,
&(*options_ref).borrow(), options_ref.clone(),
product_conf, product_conf,
)) ))
.title("Advanced bootdisk options") .title("Advanced bootdisk options")
.button("Ok", { .button("Ok", {
let options_ref = options_ref.clone();
move |siv| { move |siv| {
let options = siv let options = siv
.call_on_name("advanced-bootdisk-options-dialog", |view: &mut Dialog| { .call_on_name("advanced-bootdisk-options-dialog", |view: &mut Dialog| {
@ -666,3 +703,30 @@ fn advanced_options_view(
.with_name("advanced-bootdisk-options-dialog") .with_name("advanced-bootdisk-options-dialog")
.max_size((120, 40)) .max_size((120, 40))
} }
/// Creates a select view for all disks specified.
///
/// # Arguments
///
/// * `avail_disks` - Disks that should be shown in the select view
/// * `options_ref` - [`BootdiskOptionsRef`] where advanced disk options should be saved to
/// * `selected_disk` - Optional, specifies which disk should be pre-selected
fn target_bootdisk_selectview(
avail_disks: &[Disk],
options_ref: BootdiskOptionsRef,
selected_disk: Option<&Disk>,
) -> SelectView<Disk> {
let selected_disk_pos = selected_disk
.and_then(|disk| avail_disks.iter().position(|d| d.index == disk.index))
.unwrap_or_default();
SelectView::new()
.popup()
.with_all(avail_disks.iter().map(|d| (d.to_string(), d.clone())))
.selected(selected_disk_pos)
.on_submit(move |_, disk| {
options_ref.borrow_mut().disks = vec![disk.clone()];
options_ref.borrow_mut().advanced =
AdvancedBootdiskOptions::Lvm(LvmBootdiskOptions::defaults_from(disk));
})
}