16.2. Accessing the Installed System
With a shell in a rescue environment, the work proceeds from the bottom of the storage stack upward: identify the block device, unlock any encryption layer, activate LVM, assemble RAID if present, mount the root, mount the separate boot filesystem and EFI System Partition, and only then enter the system with a chroot. Mounting is not the same as repairing; the first pass should be read-only wherever the tools allow it, because every write to a damaged filesystem reduces the chance of recovering its contents.
Figure 16.2 shows three storage arrangements used in this chapter. The application volume is a normal LVM stack; the recovery copy is a GPT disk with a BIOS boot partition, an ESP, and a root filesystem; and the encrypted container is a separate LUKS layer whose contents are not mounted until it is opened.
Figure 16.2. The example machine's data volume, encrypted container, and disposable recovery copy. The diagram is a conceptual map; the command output in the text carries the actual UUIDs and sizes.
16.2.1. Identifying and Unlocking the Storage Stack
lsblk shows the block-device tree, but blkid shows the signatures that the
kernel and the recovery tools will act on. Start with the practice disks:
$ sudo blkid /dev/vdb /dev/vdc2 /dev/vdc3 /dev/vdd
/dev/vdb: UUID="IDDoFF-b1nz-1QW0-yNKb-5KEf-jZcj-3xtJQy" TYPE="LVM2_member"
/dev/vdc2: LABEL_FATBOOT="RECOVERYESP" LABEL="RECOVERYESP" UUID="D166-6DF7" BLOCK_SIZE="512" TYPE="vfat" PARTLABEL="ESP" PARTUUID="46f36977-871d-4d36-9811-cdbd96d5dbeb"
/dev/vdc3: LABEL="recoveryroot" UUID="bb532cbe-ae1c-4501-9d1e-17dc0e284919" BLOCK_SIZE="4096" TYPE="ext4" PARTLABEL="root" PARTUUID="cf822c03-427f-4cd4-9eb2-3e173c9bdbd4"
/dev/vdd: UUID="78f570f6-b31e-4d57-a6df-c942bc125f37" LABEL="recoverycrypt" TYPE="crypto_LUKS"
TYPE="LVM2_member" says that /dev/vdb is an LVM physical volume, not a
filesystem; there is nothing to mount on it directly. TYPE="crypto_LUKS" is
the LUKS signature; the inner filesystem appears only after the mapping is
opened. The ESP is vfat, and the PARTUUID values identify the partitions
independently of their filesystem UUIDs. Record both kinds of identifier: the
filesystem UUID is used by fstab and crypttab, while a PARTUUID can be
used when the filesystem itself is too damaged to read.
For an encrypted volume, confirm the LUKS signature and read its UUID before opening it:
$ sudo cryptsetup luksUUID /dev/vdd
78f570f6-b31e-4d57-a6df-c942bc125f37
cryptsetup isLuks performs the same check and is silent on success:
$ sudo cryptsetup isLuks /dev/vdd
The command prints nothing and exits 0; a nonzero exit means the device does
not carry a LUKS header. The UUID should match the crypttab entry you
recorded. The practice container is opened with the key file created in the
chapter's setup, under the mapping name cryptdata:
$ sudo cryptsetup open --key-file=/etc/luks/recovery.key /dev/vdd cryptdata
The command normally prints nothing. A production system usually omits
--key-file and enters the passphrase at the prompt; the rest of the command
is the same. The key file must be readable only by root and must not be the
only copy of the material needed to open the volume. If the passphrase is
lost, the LUKS header, not the filesystem, is the problem; Chapter
13 covers header backups and the limited recovery paths.
The mapping is a device-mapper target named cryptdata:
$ sudo cryptsetup status cryptdata
/dev/mapper/cryptdata is active.
type: LUKS2
cipher: aes-xts-plain64
keysize: 512 bits
key location: keyring
device: /dev/vdd
sector size: 512
offset: 32768 sectors
size: 2064384 sectors
mode: read/write
The status reports the cipher, key size, backing device, and the data offset.
The offset is not free space to reuse; it is where the encrypted payload
starts. lsblk now shows the mapping as a child of the physical device:
$ lsblk -f /dev/vdd
NAME FSTYPE FSVER LABEL UUID FSAVAIL FSUSE% MOUNTPOINTS
vdd crypto_LUKS 2 recoverycrypt 78f570f6-b31e-4d57-a6df-c942bc125f37
└─cryptdata
A mapping with no FSTYPE and no MOUNTPOINTS is expected at this point: LUKS
is a block layer, and whatever filesystem or LVM metadata lives above it is
not visible until that layer is read. A system being recovered usually has
that layer already; the practice container was formatted empty, so build a
small LVM stack on the mapping to give the inventory commands below something
real to show. The procedures are the ones from
Chapter 13, and the device argument is the mapping
/dev/mapper/cryptdata, never the raw /dev/vdd - writing LVM metadata to
the raw device would destroy the LUKS header. Create the physical volume and
the volume group:
$ sudo pvcreate /dev/mapper/cryptdata
Physical volume "/dev/mapper/cryptdata" successfully created.
$ sudo vgcreate cryptvg /dev/mapper/cryptdata
Volume group "cryptvg" successfully created
Carve a logical volume from the group:
$ sudo lvcreate -L 256M -n secret cryptvg
Logical volume "secret" created.
-L 256M fixes the size and -n names the volume; the same naming and
sizing rules as any other LVM volume apply. Put a filesystem on it and mount
it:
$ sudo mkfs.ext4 -q -L secret /dev/cryptvg/secret
$ sudo mkdir -p /mnt/secret
$ sudo mount /dev/cryptvg/secret /mnt/secret
The three commands print nothing on success. Write a note through the encrypted layer from a root shell and read it back as john:
$ sudo -i
# printf "inside the encrypted layer\n" > /mnt/secret/note.txt
# exit
$ cat /mnt/secret/note.txt
inside the encrypted layer
The write and the exit are silent. The file passed through the kernel's
crypto layer on its way to and from the disk; to everything above the mapping,
the unlocked container is an ordinary block device. When a real system
already has a volume group on the mapping, LVM usually activates it
automatically after the device appears; if it does not, vgchange -ay scans
and activates the groups. The inventory commands now show both groups:
$ sudo vgs
VG #PV #LV #SN Attr VSize VFree
cryptvg 1 1 0 wz--n- 1004.00m 748.00m
datavg 1 1 0 wz--n- <4.00g <3.00g
The #PV, #LV, and #SN columns count physical volumes, logical volumes,
and snapshots. A volume group whose physical volume is missing shows a
different attribute and requires the recovery procedure in Chapter
13; do not run vgreduce --removemissing until the
missing device is understood. lvs shows the logical volumes and their
activation state:
$ sudo lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
secret cryptvg -wi-ao---- 256.00m
appdata datavg -wi-ao---- 1.00g
The Attr field begins with -wi-ao: the leading hyphen is the allocation
policy, w means writable, i means inherited, and ao means active and
open. An inactive logical volume has -wi----- and its device node under
/dev/mapper disappears until it is activated. The datavg-appdata volume is
the application data used in the lab; cryptvg/secret is the practice volume
created on the mapping above. The output is selected: the trailing spaces are omitted here.
The practice stack has served its purpose. Dismantle it from the top down
before closing the mapping, because cryptsetup close cannot remove a mapping
that a volume group still occupies - even after its filesystem has been
unmounted:
$ sudo umount /mnt/secret
$ sudo lvremove -f /dev/cryptvg/secret
Logical volume "secret" successfully removed.
$ sudo vgremove -f cryptvg
Volume group "cryptvg" successfully removed
$ sudo pvremove -f /dev/mapper/cryptdata
Labels on physical volume "/dev/mapper/cryptdata" successfully wiped.
The -f flag answers the removal confirmation automatically, which is what a
recovery script wants; run the commands without it to be asked first. With the
layers above it gone, the mapping can be closed:
$ sudo cryptsetup close cryptdata
cryptsetup close is silent on success. If a layer is still active, the
command refuses with Device cryptdata is still in use.; lsblk
/dev/mapper/cryptdata shows any logical volume still stacked on the mapping,
and lsof /dev/mapper/cryptdata names a process that holds the device node
itself open. Closing the mapping makes the encrypted contents inaccessible
until the volume is opened again.
A wrong key fails before any LUKS data is exposed. Create a second key file with different content so the failure is certain:
$ sudo -i
# printf 'not-the-passphrase\n' > /etc/luks/wrong.key
# chmod 600 /etc/luks/wrong.key
# exit
The two writes are silent, and exit returns to john's shell. Now try to
open the container with the wrong credential:
$ sudo cryptsetup open --key-file=/etc/luks/wrong.key /dev/vdd wrongdata
No key available with this passphrase.
The command exits nonzero and no /dev/mapper/wrongdata node is created. A
"keyslot operation could fail as it requires more than available memory"
warning can precede the error on a small machine; it concerns the Argon2 key
derivation, not the password check. Treat any success with an unexpected key
as a reason to stop: it may mean the key file was replaced or the container is
not the one you think it is.
The useful options for these commands are summarized in Table 16.1.
| Option | Description |
|---|---|
cryptsetup open --key-file=FILE |
Unlock using a key file instead of an interactive passphrase. |
cryptsetup open --readonly |
Create a read-only mapping for inspection or imaging. |
cryptsetup open --type luks |
Select the LUKS device type explicitly when detection is ambiguous. |
cryptsetup status NAME |
Show the mapping's cipher, device, offset, and state. |
cryptsetup close NAME |
Remove the mapping; fails if it is still in use. |
cryptsetup luksUUID DEVICE |
Print the LUKS UUID stored in the header. |
cryptsetup isLuks DEVICE |
Exit successfully if the device has a LUKS header; silent on success. |
16.2.2. Mounting Root, Boot, and the EFI System Partition
The recovery copy on /dev/vdc has the layout of a UEFI-capable Debian disk:
a 1 MiB BIOS boot partition for legacy GRUB, a 512 MiB FAT32 ESP, and an ext4
root. The BIOS boot partition has no filesystem and is never mounted. Mount the
root first, then the ESP at the directory that the installed system uses for
it, normally /boot/efi:
$ sudo mount /dev/vdc3 /mnt/recovery
The command is silent on success. If the root filesystem is damaged, mount it
read-only first with mount -o ro /dev/vdc3 /mnt/recovery; a read-only mount
allows inspection and copying without replaying the journal or completing a
partial write. Remount it read-write only when the repair requires it. If the
root lives on LVM, use /dev/mapper/datavg-appdata or the appropriate
/dev/VG/LV path; if it is inside LUKS, open the mapping first and use
/dev/mapper/cryptdata or the volume group above it.
Mount the ESP inside the mounted root, not at the rescue environment's own
/boot/efi:
$ sudo mount /dev/vdc2 /mnt/recovery/boot/efi
The command is silent. The nested mount matters because GRUB and the kernel
package scripts running in the chroot expect the ESP at /boot/efi relative
to the installed root. Mounting it at the rescue system's /boot/efi would
write the wrong files. findmnt -R shows the resulting tree:
$ sudo findmnt -R /mnt/recovery
TARGET SOURCE FSTYPE OPTIONS
/mnt/recovery /dev/vdc3 ext4 rw,relatime
└─/mnt/recovery/boot/efi /dev/vdc2 vfat rw,relatime,fmask=0022,dmask=0022,codepage=437,iocharset=ascii,shortname=mixed,utf8,errors=remount-ro
The tree lists the ext4 root and the vfat ESP mounted beneath it. The ESP's
fmask and dmask options keep ordinary users from modifying boot code; do
not replace them with defaults just to make a manual copy easier. The root directory of the mounted system is visible next:
$ ls /mnt/recovery
bin
boot
dev
etc
home
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
srv
sys
tmp
usr
var
The list is the ordinary Debian root layout. lost+found belongs to the ext4
filesystem and is not evidence of a previous repair by itself. The mount is
read-write in this example because the following sections edit the copy; on a
production system, make the read-only inspection pass first and note the
ro mount option in the incident record. The copy's fstab describes its own
root and ESP:
$ sudo cat /mnt/recovery/etc/fstab
UUID=bb532cbe-ae1c-4501-9d1e-17dc0e284919 / ext4 errors=remount-ro 0 1
UUID=D166-6DF7 /boot/efi vfat umask=0077 0 1
Both UUIDs match the devices mounted above. If they did not, the correct repair
would be to fix the file, not to mount by a different device and hope the boot
uses the same one. The root entry uses errors=remount-ro, so an ext4 error
will remount the filesystem read-only rather than continuing to write through
the corruption. The ESP entry uses umask=0077, which gives root-only access to
the mount point. Chapter 11 discusses the dump and
pass fields; the 0 1 values here mean "do not dump" and "check this
filesystem first" in the traditional ordering.
If /boot is a separate filesystem, mount it between the root and the ESP:
mount /dev/vdc2 /mnt/recovery/boot when vdc2 is the boot filesystem, then
mount the ESP at /mnt/recovery/boot/efi. Do not mount a separate /boot
over a directory that already contains the kernel images from the root
filesystem; check findmnt and ls first. On an LVM installation the root is
usually /dev/mapper/VG-root, /boot is a plain ext4 partition, and the ESP is
the FAT32 partition. The names differ; the order does not.
16.2.3. Preparing a chroot with Required Virtual Filesystems
A chroot changes the pathname root for the process, but it does not create a
new mount namespace or provide kernel filesystems. The installed system's
tools need /proc, /sys, /dev, and /dev/pts to see processes, devices,
and terminal state. Bind the rescue environment's copies into the mounted
root:
$ sudo mount --bind /proc /mnt/recovery/proc
$ sudo mount --bind /sys /mnt/recovery/sys
$ sudo mount --bind /dev /mnt/recovery/dev
$ sudo mount --bind /dev/pts /mnt/recovery/dev/pts
Each command is silent on success. /proc and /sys expose the running
kernel's view; /dev supplies device nodes such as /dev/null, /dev/tty,
and the block devices; /dev/pts supplies pseudo-terminals for interactive
programs. If package management inside the chroot needs DNS, copy the rescue
environment's /etc/resolv.conf into the mounted root or bind-mount it
read-only; do not assume the installed system's resolver is reachable from the
rescue network. /run is sometimes needed for services and sockets; bind it
only when a specific tool requires it, because the rescue environment's
runtime state is not the installed system's.
Enter the chroot with a shell. The installed system's /etc/debian_version
confirms that the pathname root really changed:
$ sudo chroot /mnt/recovery /bin/bash -c 'cat /etc/debian_version'
13.5
The command form runs one command and returns; sudo chroot /mnt/recovery
without -c opens an interactive shell, which is left with exit. The
chroot is not a security boundary: a root process inside it can escape if it
can access the right device nodes or mount points. Use it only for repair, and
do not expose it to untrusted users. The package database can be checked
before changing anything:
$ sudo chroot /mnt/recovery /bin/bash -c 'dpkg --audit'
The command prints nothing when dpkg has no half-installed or half-configured
packages. If it prints a package name, note the package and its state before
running dpkg --configure -a; the repair is covered in Section 16.4.2. The
same chroot can run passwd, visudo, update-initramfs, and grub-install
against the mounted root, which is why the bind mounts are established before
any of those tasks.
Figure 16.3 shows the relationship between the rescue environment's kernel filesystems and the paths inside the chroot.
Figure 16.3. The chroot sees the mounted root as /; /proc, /sys, and
/dev must be bind-mounted into that root before the installed system's tools
can use them.
Keep the bind mounts in place for the repairs in Sections 16.3 and 16.4; every
chroot command that follows depends on them. Do not unmount the root or the
ESP while the chroot is in use. The teardown sequence is at the end of Section
16.4.1, after the last chroot command.
16.2.4. Preserving Data Before Attempting Repairs
The most important recovery command is the one that copies data before the
repair starts. A filesystem check, a package operation, or a boot-loader
rewrite can all make a damaged system worse; a copy on independent media
preserves the option to change course. Preserve the configuration first,
because it is small and explains how the system was supposed to work. From the
rescue environment, with the installed root mounted at /mnt/recovery, create
a tar archive in the reader's workspace:
$ sudo tar --acls --xattrs --numeric-owner -cpf /home/john/debian-handbook/recovery/etc-before-repair.tar -C /mnt/recovery etc
The command is silent on success. -c creates an archive, -p preserves
permissions and timestamps, -f names the archive, and -C changes to the
mounted root before reading etc. --acls and --xattrs preserve POSIX ACLs
and extended attributes; --numeric-owner stores numeric user and group IDs
rather than resolving names through the rescue environment's account database.
Those options matter for files such as /etc/shadow, sudoers drop-ins, and
service configuration that carries capabilities or ACLs. Preserve the logs
separately:
$ sudo tar --acls --xattrs --numeric-owner -cpf /home/john/debian-handbook/recovery/var-log-before-repair.tar -C /mnt/recovery var/log
The log archive is often larger than /etc; it contains the evidence that
explains the failure. If the disk is failing, copy it before running any
filesystem check or fsck repair, because a check can rewrite metadata and
overwrite the evidence. For a block device that is producing I/O errors,
ddrescue from the gddrescue package is the appropriate imaging tool; it
reads the healthy regions first, retries the bad areas, and writes a map file
so a later pass can resume where the earlier one stopped. The package is not
in the list above; install it when a device starts reporting read errors,
and image to a different, healthy disk. The principle is the same: image
first, repair the image or restore from the image second.
List the archive to verify that the expected file is present:
$ tar -tf etc-before-repair.tar | grep '^etc/fstab$'
etc/fstab
The grep filters a potentially large listing to the exact path. If the
archive were empty or truncated, the command would print nothing and exit
nonzero; check the exit status, not only the presence of a file. Compute a
checksum and record it with the incident notes:
$ sha256sum etc-before-repair.tar var-log-before-repair.tar
a7c11596e4713eff41384ac33eea047b9c12aeafd75f1c6f3cfb83d048ada047 etc-before-repair.tar
f8bff36881989777989f395499d1a7df699d03d41600a8a7972511da511b709f var-log-before-repair.tar
The hashes are specific to these archives; a later copy is a different archive even if the file names are reused. Store the checksums outside the archive, or in a file whose own hash is recorded. A checksum detects accidental corruption, not tampering; for evidence that may be used in an investigation, record who collected it, when, and from which device, and keep the original media write-protected.
For application data, the backup strategy from Chapter
15 applies: a filesystem-consistent copy, preferably
from a snapshot or after fsfreeze, with the application's own dump if it has
one. A database copied while running may be structurally inconsistent even
though every file is readable. Preserve the database dump alongside the file
archive, and verify that the dump restores into a scratch database before
trusting it. The practical lab in Section 16.6 uses a small SQLite database,
which is copied as a file while no process is writing it; a production
PostgreSQL or MariaDB instance would need the corresponding dump or snapshot
procedure from Chapter 15.