Opening, Mounting, Closing
Outcome: an opened vault with a
filesystem on it, mounted, used, and
closed again — the full round trip.
One honest label before the commands:
this is the chapter the lab could not
run end to end. Its sandbox has no
device-mapper, so cryptsetup open
stops there with an error this chapter
quotes verbatim. Everything up to that
line, and the passphrase test below,
ran for real; the open–mount–close
sequence itself is taught from the
cryptsetup documentation, and each
step is ordinary and checkable on any
real machine. criteria.md records
exactly which is which.
Testing a passphrase without opening
There is one part of opening that needs no kernel help, and the lab ran it constantly:
cryptsetup open --test-passphrase \
--key-file pass1.txt vault.img
Exit 0 means this passphrase unlocks
this vault; a wrong one answers
No key available with this passphrase.
and exit 2. It creates nothing, mounts
nothing and changes nothing, which
makes it the safest check in the whole
guide — after any key operation in
chapters 6 and 7, this is how you
prove the new state before trusting
it. (On a real machine you will
usually omit --key-file and type.)
Opening
sudo cryptsetup open vault.img vault
Read the shape of it: the device
first, then a name of your
choosing — vault here. cryptsetup
asks for a passphrase, unwraps the
master key, and hands it to the
kernel's device-mapper, which creates
/dev/mapper/vault: a virtual block
device that looks exactly like a small
disk, except that every byte written
to it is encrypted before it lands in
vault.img, and every byte read back
is decrypted on the way out. From that
moment the encryption is invisible to
everything above it.
The lab's machine answered instead:
Cannot initialize device-mapper. Is dm_mod kernel module loaded?
Cannot use device vault, name is invalid or still in use.
That is transcript t08, and it is the
sandbox's boundary, drawn by the tool
itself: bookkeeping lives in the file,
but opening lives in the kernel, and
this kernel offers no device-mapper at
all. On your machine, silence and a
returning prompt mean success; check
with ls /dev/mapper or
cryptsetup status vault, which
reports the device, cipher and offset
of an active mapping.
A filesystem, then a mount
An opened vault is a blank device, not a folder. Treat it like a new disk — because to every tool that follows, it is one:
sudo mkfs.ext4 /dev/mapper/vault
sudo mkdir -p /mnt/vault
sudo mount /dev/mapper/vault /mnt/vault
mkfs.ext4 writes an ordinary ext4
filesystem; it neither knows nor cares
that its writes pass through a cipher.
Files copied into /mnt/vault are
just files — until the vault closes,
when the only thing left in
vault.img is ciphertext under a
header whose data segment now holds
an encrypted filesystem instead of
zeros. Nothing about the files'
names, sizes or contents survives in
the clear: filenames live inside the
encrypted area too.
On the lab's 64 MB file the filesystem would have had about 48 MB to live in — the 16 MB header is not negotiable space. Real vaults are disks, so the tax is trivial there; on small files it is why chapter 3 chose 64 MB and not 20.
Closing
sudo umount /mnt/vault
sudo cryptsetup close vault
Order matters and both steps are
required: unmount first, so the
filesystem finishes its writes and
lets go; then close, which removes
/dev/mapper/vault and drops the
master key from the kernel. After
close, the machine holds ciphertext
and a header — the threat model of
chapter 1 is restored. Closing a
vault that is still mounted, or still
open in some program's working
directory, is refused, and the refusal
is the feature: hunt down the user of
the device (lsof /dev/mapper/vault)
rather than forcing it.
The shape to remember
Every LUKS routine in the wild is these four verbs in costume: open (passphrase in, device out), use (the device is a disk; everything else is ordinary Linux), unmount, and close (device gone, key gone). The boot machinery of chapter 9 and the TPM of chapter 10 change only who performs the first verb, and when.