fn mount_required_partitions()

in src/prepare_chroot.rs [181:348]


fn mount_required_partitions<'a>(
    distro: &'a distro::Distro,
    cli: &cli::CliInfo,
    partitions: &mut HashMap<&str, &'a PartInfo>,
) -> Result<()> {
    let os_part = distro
        .partitions
        .iter()
        .find(|partition| partition.contains_os)
        .unwrap(); // unwrap is safe as we have always an OS partition
    partitions.insert("os", os_part);

    let efi_part = distro
        .partitions
        .iter()
        .find(|partition| partition.part_type == "EF00");
    if let Some(efi_part) = efi_part {
        partitions.insert("efi", efi_part);
    }

    let boot_part = distro
        .partitions
        .iter()
        .find(|partition| !partition.contains_os && partition.part_type != "EF00");
    if let Some(boot_part) = boot_part {
        partitions.insert("boot", boot_part);
    }

    // A closure is required to build the correct path for the rescue disk
    let cl_get_rescue_disk_path = || -> String {
        if distro.is_ade {
            constants::ADE_OSENCRYPT_PATH.to_string()
        } else {
            format!(
                "{}{}",
                helper::get_recovery_disk_path(cli),
                partitions.get("os").unwrap().number
            )
        }
    };

    debug!("rescue_disk_path : {}", cl_get_rescue_disk_path());
    debug!("os_partition : {:?}", partitions.get("os"));
    debug!("efi_partition : {:?}", partitions.get("efi"));
    debug!("boot_partition : {:?}", partitions.get("boot"));

    // Create the rescue root directory
    mount::mkdir_rescue_root()?;

    // Mount each lv if we have them available
    // This does mount ADE and non ADE partitions/lvs
    if let LogicalVolumesType::Some(lv_set) = &partitions.get("os").unwrap().logical_volumes {
        // First mount the rootlv, otherwise we get mount errors if we continue with the wrong order
        lv_set
            .iter()
            .filter(|root_lv| root_lv.name == "rootvg-rootlv")
            .for_each(|root_lv| {
                let options = if root_lv.fstype == "xfs" {
                    "nouuid"
                } else {
                    ""
                };
                match mount::mount(
                    &format!("{}{}", "/dev/mapper/", root_lv.name),
                    constants::RESCUE_ROOT,
                    options,
                    false,
                ) {
                    Ok(()) => {}
                    Err(e) => {
                        let _ = helper::cleanup(distro, cli);
                        panic!(
                            "Unable to mount the logical volume : {} Error is: {}",
                            root_lv.name, e
                        );
                    }
                }
            });
        lv_set
            .iter()
            .filter(|volume| volume.name != "rootvg-rootlv" && volume.name != "rootvg-tmplv")
            .for_each(|lv| {
                let options = if lv.fstype == "xfs" { "nouuid" } else { "" };
                match mount::mount(
                    &format!("{}{}", "/dev/mapper/", lv.name),
                    &format!(
                        "{}{}",
                        constants::RESCUE_ROOT,
                        lv.name
                            .strip_prefix("rootvg-")
                            .unwrap()
                            .strip_suffix("lv")
                            .unwrap()
                    ),
                    options,
                    false,
                ) {
                    Ok(()) => {}
                    Err(e) => {
                        panic!(
                            "Unable to mount the logical volume : {} Error is: {}",
                            lv.name, e
                        );
                    }
                }
            });
    } else {
        // RAW disks gets mounted here
        // mind the XFS double UUID issue
        let command = format!("lsblk -nf -o FSTYPE {}", cl_get_rescue_disk_path());
        let filesystem = helper::run_fun(&command)?;
        if filesystem.trim() == "xfs" {
            mount::mount(
                &cl_get_rescue_disk_path(),
                constants::RESCUE_ROOT,
                "nouuid",
                false,
            )?;
        } else {
            mount::mount(
                &cl_get_rescue_disk_path(),
                constants::RESCUE_ROOT,
                "",
                false,
            )?;
        }
    }

    // Even if we have an ADE encrpted disk the boot partition and the efi partition are not encrypted
    let rescue_disk_path = helper::get_recovery_disk_path(cli);

    // The order is again important. First /boot then /boot/efi
    // Verify also if we have a boot partition, Ubuntu doesn't have one for example
    if partitions.get("boot").is_some() {
        // mind the XFS double UUID issue
        if let Some(boot_partition) = partitions.get("boot") {
            if partitions.get("boot").unwrap().fstype == "xfs" {
                mount::mount(
                    &format!("{}{}", rescue_disk_path, boot_partition.number),
                    constants::RESCUE_ROOT_BOOT,
                    "nouuid",
                    false,
                )?;
            } else {
                mount::mount(
                    &format!("{}{}", rescue_disk_path, boot_partition.number),
                    constants::RESCUE_ROOT_BOOT,
                    "",
                    false,
                )?;
            }
        }
    }

    // Also be carefull with the efi partition, not all distros have one
    if partitions.get("efi").is_some() {
        if let Some(efi_partition) = partitions.get("efi") {
            mount::mount(
                &format!("{}{}", rescue_disk_path, efi_partition.number),
                constants::RESCUE_ROOT_BOOT_EFI,
                "",
                false,
            )?;
        }
    }

    Ok(())
}