public inbox for pve-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Thomas Lamprecht <t.lamprecht@proxmox.com>
To: pve-devel@lists.proxmox.com
Subject: [PATCH installer 3/5] chroot enter: unlock the LUKS containers of an encrypted installation
Date: Wed, 18 Nov 2026 00:42:27 +0100	[thread overview]
Message-ID: <20261117234330.801935-5-t.lamprecht@proxmox.com> (raw)
In-Reply-To: <20261117234330.801935-1-t.lamprecht@proxmox.com>

In a rescue shell, the LUKS containers of an encrypted ext4, xfs or
Btrfs installation are closed, so preparing the chroot failed with a
volume group or Btrfs file system that could not be found, without a
hint at the encryption. For native ZFS encryption, it already asks for
the passphrase.

If the volume group or the Btrfs file system cannot be found, open the
containers of an installation, found by their labels, which match the
device mapper names. cryptsetup asks for the passphrase of each on the
terminal. This leaves the containers alone right after an installation,
where they are still open, and installations without encryption, also
if an encrypted disk of another installation is around. If a label is
found more than once, as with an older installation on another disk,
the right container has to be opened by hand. If a container cannot be
opened or the installation cannot be mounted, the containers opened
before are closed again.

Cleanup closes the containers that prepare opened, after deactivating
the volume group on top, so that the installer can be started
afterwards, which refuses to create a container under a name that is
in use.

Signed-off-by: Thomas Lamprecht <t.lamprecht@proxmox.com>
---
 proxmox-chroot/src/main.rs | 258 +++++++++++++++++++++++++++++++++----
 1 file changed, 233 insertions(+), 25 deletions(-)

diff --git a/proxmox-chroot/src/main.rs b/proxmox-chroot/src/main.rs
index d6cd48b7..5178710d 100644
--- a/proxmox-chroot/src/main.rs
+++ b/proxmox-chroot/src/main.rs
@@ -8,7 +8,9 @@
 use anyhow::{Result, bail};
 use serde::Deserialize;
 use std::{
-    env, fs, io,
+    collections::BTreeMap,
+    env, fs,
+    io::{self, IsTerminal},
     path::{self, Path, PathBuf},
     process::{self, Command},
 };
@@ -24,6 +26,8 @@ static BINDMOUNTS: [&str; 4] = ["dev", "proc", "run", "sys"];
 const TARGET_DIR: &str = "/target";
 const ZPOOL_NAME: &str = "rpool";
 const CD_INFO_PATH: &str = "/cdrom/.cd-info";
+/// The LUKS containers that `prepare` opened, for `cleanup` to close them again.
+const OPENED_LUKS_PATH: &str = "/run/proxmox-chroot-opened-luks";
 
 /// Arguments for the `prepare` command.
 struct CommandPrepareArgs {
@@ -51,6 +55,7 @@ impl cli::Subcommand for CommandPrepareArgs {
     fn print_usage() {
         eprintln!(
             r#"Mount the root file system and bind mounts in preparation to chroot into the installation.
+An encrypted installation is unlocked, asking for its passphrase.
 
 USAGE:
   {} prepare <OPTIONS>
@@ -86,7 +91,7 @@ impl cli::Subcommand for CommandCleanupArgs {
 
     fn print_usage() {
         eprintln!(
-            r#"Unmount everything. Use once done with the chroot.
+            r#"Unmount everything and lock an encrypted installation again. Use once done with the chroot.
 
 USAGE:
   {} cleanup <OPTIONS>
@@ -137,11 +142,23 @@ fn prepare(args: &CommandPrepareArgs) -> Result<()> {
 
     fs::create_dir_all(TARGET_DIR)?;
 
+    // Containers are only opened if the installation cannot be found otherwise, which leaves them
+    // alone right after the installation, where they are still open, and for an installation
+    // without encryption, also if an encrypted disk of another installation is around.
     match fs {
         Filesystem::Zfs => mount_zpool(args.rpool_id)?,
-        Filesystem::Xfs => mount_fs()?,
-        Filesystem::Ext4 => mount_fs()?,
-        Filesystem::Btrfs => mount_btrfs(args.btrfs_uuid.clone())?,
+        Filesystem::Xfs | Filesystem::Ext4 => {
+            let vg_found = volume_group_exists(&get_product()?.to_string())?;
+            with_luks_containers(fs, !vg_found, mount_fs)?
+        }
+        Filesystem::Btrfs => {
+            let uuids = btrfs_uuids()?;
+            let found = match &args.btrfs_uuid {
+                Some(uuid) => uuids.contains(uuid),
+                None => !uuids.is_empty(),
+            };
+            with_luks_containers(fs, !found, || mount_btrfs(args.btrfs_uuid.clone()))?
+        }
     }
 
     if let Err(e) = bindmount() {
@@ -161,7 +178,16 @@ fn cleanup(args: &CommandCleanupArgs) -> Result<()> {
 
     match fs {
         Filesystem::Zfs => umount_zpool(),
-        Filesystem::Btrfs | Filesystem::Xfs | Filesystem::Ext4 => umount(Path::new(TARGET_DIR))?,
+        Filesystem::Btrfs | Filesystem::Xfs | Filesystem::Ext4 => {
+            umount(Path::new(TARGET_DIR))?;
+            let opened = match fs::read_to_string(OPENED_LUKS_PATH) {
+                Ok(names) => names.lines().map(str::to_string).collect(),
+                Err(err) if err.kind() == io::ErrorKind::NotFound => Vec::new(),
+                Err(err) => bail!("could not read '{OPENED_LUKS_PATH}': {err}"),
+            };
+            close_luks_containers(fs, &opened)?;
+            let _ = fs::remove_file(OPENED_LUKS_PATH);
+        }
     }
 
     println!("Chroot cleanup done. You can now reboot or leave the shell.");
@@ -300,7 +326,7 @@ fn zpool_keystatus_unavailable() -> Result<bool> {
 /// installer's tmpfs, as the caller may be the unattended post-installation hook. Otherwise, for
 /// example in a rescue shell, `zfs` asks for it on the terminal.
 fn load_zpool_key() -> Result<()> {
-    use std::io::{IsTerminal, Write};
+    use std::io::Write;
 
     let passphrase = get_low_level_config()
         .ok()
@@ -346,6 +372,170 @@ fn umount_zpool() {
     }
 }
 
+/// Whether `name` is the device mapper name, and also the label, of a LUKS container that an
+/// encrypted installation with the given filesystem has.
+fn is_luks_container_name(fs: Filesystem, name: &str) -> bool {
+    match fs {
+        Filesystem::Ext4 | Filesystem::Xfs => name == "luks-rpv",
+        Filesystem::Btrfs => name
+            .strip_prefix("luks-rfs")
+            .is_some_and(|n| !n.is_empty() && n.bytes().all(|b| b.is_ascii_digit())),
+        Filesystem::Zfs => false,
+    }
+}
+
+/// Runs `mount`, after opening the LUKS containers of an encrypted installation if `open` is set.
+/// If that fails, it closes the containers it opened, otherwise it records them for `cleanup`.
+fn with_luks_containers(
+    fs: Filesystem,
+    open: bool,
+    mount: impl FnOnce() -> Result<()>,
+) -> Result<()> {
+    let opened = if open {
+        open_luks_containers(fs)?
+    } else {
+        Vec::new()
+    };
+
+    let result = mount().and_then(|()| {
+        if opened.is_empty() {
+            return Ok(());
+        }
+        fs::write(OPENED_LUKS_PATH, opened.join("\n") + "\n")
+            .map_err(|err| anyhow::anyhow!("could not write '{OPENED_LUKS_PATH}': {err}"))
+    });
+    if let Err(err) = result {
+        if let Err(close_err) = close_luks_containers(fs, &opened) {
+            eprintln!("{close_err:#}");
+        }
+        return Err(err);
+    }
+    Ok(())
+}
+
+/// Opens the closed LUKS containers of an encrypted installation, found by their labels, and
+/// returns their names. `cryptsetup` asks for the passphrase of each on the terminal. If one cannot
+/// be opened, the ones opened before are closed again.
+fn open_luks_containers(fs: Filesystem) -> Result<Vec<String>> {
+    #[derive(Deserialize)]
+    struct Entry {
+        path: String,
+        fstype: Option<String>,
+        label: Option<String>,
+    }
+
+    #[derive(Deserialize)]
+    struct LsblkOutput {
+        blockdevices: Vec<Entry>,
+    }
+
+    let output = Command::new("lsblk")
+        .args(["--json", "--list", "--output", "path,fstype,label"])
+        .output()?;
+    if !output.status.success() {
+        bail!(
+            "Error checking for encrypted devices: {}",
+            String::from_utf8_lossy(&output.stderr)
+        );
+    }
+    let out: LsblkOutput = serde_json::from_slice(&output.stdout)?;
+
+    let mut containers: BTreeMap<String, Vec<String>> = BTreeMap::new();
+    for entry in out.blockdevices {
+        if let (Some("crypto_LUKS"), Some(label)) = (entry.fstype.as_deref(), entry.label)
+            && is_luks_container_name(fs, &label)
+            && !Path::new("/dev/mapper").join(&label).exists()
+        {
+            containers.entry(label).or_default().push(entry.path);
+        }
+    }
+
+    // checked for all first, so that nothing is opened in vain
+    for (name, devices) in &containers {
+        // for example from an older installation on another disk
+        if devices.len() > 1 {
+            bail!(
+                "Found several encrypted devices labelled '{name}': {}\nOpen the right one with 'cryptsetup open <device> {name}'",
+                devices.join(", ")
+            );
+        }
+    }
+    if !containers.is_empty() && !io::stdin().is_terminal() {
+        bail!(
+            "cannot unlock the encrypted devices - their passphrase can only be entered on a terminal"
+        );
+    }
+
+    let mut opened = Vec::new();
+    for (name, devices) in containers {
+        let device = &devices[0];
+        println!("unlocking '{device}' as '{name}'");
+        let status = Command::new("cryptsetup")
+            .args(["open", device, &name])
+            .status()
+            .map_err(|err| anyhow::anyhow!("failed to run 'cryptsetup open': {err}"));
+        if !status.as_ref().is_ok_and(|status| status.success()) {
+            if let Err(close_err) = close_luks_containers(fs, &opened) {
+                eprintln!("{close_err:#}");
+            }
+            status?;
+            bail!(
+                "could not unlock '{device}'\nIf it does not belong to the installation, open the ones that do with 'cryptsetup open <device> <name>' and run prepare again"
+            );
+        }
+        opened.push(name);
+    }
+
+    // so that the volume group or Btrfs members on top are known before they are used
+    if !opened.is_empty() {
+        let _ = Command::new("udevadm").arg("settle").status();
+    }
+    Ok(opened)
+}
+
+/// Closes the given LUKS containers, after deactivating the volume group on top, so that the
+/// installer can be started afterwards.
+fn close_luks_containers(fs: Filesystem, names: &[String]) -> Result<()> {
+    if names.is_empty() {
+        return Ok(());
+    }
+
+    if fs != Filesystem::Btrfs {
+        let product = get_product()?;
+        let output = Command::new("vgchange")
+            .arg("-an")
+            .arg(product.to_string())
+            .output()?;
+        if !output.status.success() {
+            bail!(
+                "deactivation of VG '{product}' failed: {}",
+                String::from_utf8_lossy(&output.stderr)
+            );
+        }
+    }
+
+    for name in names {
+        let output = Command::new("cryptsetup").args(["close", name]).output()?;
+        if !output.status.success() {
+            bail!(
+                "closing encrypted device '{name}' failed: {}",
+                String::from_utf8_lossy(&output.stderr)
+            );
+        }
+    }
+    Ok(())
+}
+
+fn volume_group_exists(name: &str) -> Result<bool> {
+    let status = Command::new("vgs")
+        .args(["--noheadings", "--options", "vg_name", name])
+        .stdout(process::Stdio::null())
+        .stderr(process::Stdio::null())
+        .status()
+        .map_err(|err| anyhow::anyhow!("failed to run 'vgs': {err}"))?;
+    Ok(status.success())
+}
+
 fn mount_fs() -> Result<()> {
     // the volume group is named after the product
     let product = get_product()?;
@@ -450,6 +640,24 @@ fn mount_btrfs(btrfs_uuid: Option<String>) -> Result<()> {
 /// If multiple are found, returns a (human-readable) error with all possible
 /// filesystem UUIDs.
 fn get_btrfs_uuid() -> Result<String> {
+    let uuids = btrfs_uuids()?;
+    match uuids.len() {
+        0 => bail!("Could not find any BTRFS UUID"),
+        i if i > 1 => {
+            let uuid_list = uuids
+                .iter()
+                .fold(String::new(), |acc, arg| format!("{acc}\n{arg}"));
+            bail!(
+                "Found {i} UUIDs:{uuid_list}\nPlease specify the UUID to use with the --btrfs-uuid parameter"
+            )
+        }
+        _ => (),
+    }
+    Ok(uuids[0].clone())
+}
+
+/// The UUIDs of all Btrfs file systems present on the system.
+fn btrfs_uuids() -> Result<Vec<String>> {
     let output = Command::new("lsblk")
         .args(["--json", "--fs", "--list", "--output", "fstype,uuid"])
         .output()?;
@@ -475,7 +683,7 @@ fn get_btrfs_uuid() -> Result<String> {
     let out: LsblkOutput = serde_json::from_slice(&output.stdout)?;
     let mut uuids = out
         .blockdevices
-        .iter()
+        .into_iter()
         .filter_map(|entry| match entry {
             Entry {
                 fstype: Some(fstype),
@@ -483,24 +691,11 @@ fn get_btrfs_uuid() -> Result<String> {
             } if fstype == "btrfs" => Some(uuid),
             _ => None,
         })
-        .collect::<Vec<&String>>();
+        .collect::<Vec<String>>();
     // every member device of a multi-disk file system is listed with the same UUID
     uuids.sort();
     uuids.dedup();
-
-    match uuids.len() {
-        0 => bail!("Could not find any BTRFS UUID"),
-        i if i > 1 => {
-            let uuid_list = uuids
-                .iter()
-                .fold(String::new(), |acc, &arg| format!("{acc}\n{arg}"));
-            bail!(
-                "Found {i} UUIDs:{uuid_list}\nPlease specify the UUID to use with the --btrfs-uuid parameter"
-            )
-        }
-        _ => (),
-    }
-    Ok(uuids[0].into())
+    Ok(uuids)
 }
 
 fn do_bindmount(source: &Path, target: &Path) -> Result<()> {
@@ -574,8 +769,21 @@ fn bind_umount() -> Result<()> {
 
 #[cfg(test)]
 mod tests {
-    use super::parse_cd_info_product;
-    use proxmox_installer_types::ProxmoxProduct;
+    use super::{is_luks_container_name, parse_cd_info_product};
+    use proxmox_installer_types::{ProxmoxProduct, answer::Filesystem};
+
+    #[test]
+    fn luks_container_names() {
+        assert!(is_luks_container_name(Filesystem::Ext4, "luks-rpv"));
+        assert!(is_luks_container_name(Filesystem::Xfs, "luks-rpv"));
+        assert!(!is_luks_container_name(Filesystem::Ext4, "luks-rfs0"));
+        assert!(is_luks_container_name(Filesystem::Btrfs, "luks-rfs0"));
+        assert!(is_luks_container_name(Filesystem::Btrfs, "luks-rfs12"));
+        assert!(!is_luks_container_name(Filesystem::Btrfs, "luks-rfs"));
+        assert!(!is_luks_container_name(Filesystem::Btrfs, "luks-rfs1a"));
+        assert!(!is_luks_container_name(Filesystem::Btrfs, "luks-rpv"));
+        assert!(!is_luks_container_name(Filesystem::Zfs, "luks-rpv"));
+    }
 
     #[test]
     fn product_from_cd_info() {
-- 
2.47.3





  parent reply	other threads:[~2026-11-17 23:43 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2026-11-17 23:42 [PATCH proxmox/installer/docs 0/8] installer: add full-disk encryption of the system disks Thomas Lamprecht
2026-11-17 23:42 ` [PATCH proxmox 1/1] installer-types: add full-disk encryption to the disk setup Thomas Lamprecht
2026-11-17 23:42 ` [PATCH installer 1/5] install: encrypt the system disks with LUKS2 or native ZFS encryption Thomas Lamprecht
2026-11-17 23:42 ` [PATCH installer 2/5] auto: support full-disk encryption in answer files Thomas Lamprecht
2026-11-17 23:42 ` Thomas Lamprecht [this message]
2026-11-17 23:42 ` [PATCH installer 4/5] ui: offer full-disk encryption in the graphical and terminal installer Thomas Lamprecht
2026-11-17 23:42 ` [PATCH installer 5/5] assistant: keep the disk passphrase out of prepared ISOs Thomas Lamprecht
2026-11-17 23:42 ` [PATCH docs 1/2] installation: describe full-disk encryption Thomas Lamprecht
2026-11-17 23:42 ` [PATCH docs 2/2] local-zfs: update the limitations of native encryption Thomas Lamprecht

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=20261117234330.801935-5-t.lamprecht@proxmox.com \
    --to=t.lamprecht@proxmox.com \
    --cc=pve-devel@lists.proxmox.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal