14.1. Planning a Storage Change

Storage changes fail in predictable ways: a container is reduced while the data inside it still extends past the new boundary, a device name changes and the boot configuration does not, or there is no record of the original layout to restore to. Planning prevents all three. This section builds the practice stack - a GPT partition, a RAID1 array under LUKS and LVM, and three separate filesystems - captures its layout, metadata, and content into john's workspace, and classifies which operations can run while the system is serving and which need a maintenance window. Every later section changes this stack, and each change begins from the records made here.

14.1.1. Mapping the Storage Stack and Supported Operations

Draw the stack before changing it. Each layer has a different operation for growth, a different operation for reduction, and sometimes no safe reduction at all. The practice stack deliberately includes all of the common layers so that the same change can be followed from the disk to the mounted filesystem.

The layered capacity model, with the growth command for each layer and the rule that an outer layer can only use space already visible below it.

Figure 14.1. The practice stack and the direction of growth. The diagram is a conceptual schematic of the layer order; layers are not drawn to scale.

The following table is the planning reference for this chapter. The "grow" column names the command that makes the layer larger; the "reduce" column names the supported operation, or the migration that replaces it.

Layer Grow Reduce Important constraint
Disk or LUN Provider or hypervisor expands the device; rescan the transport Not applicable The guest may cache the old size until it is rescanned
GPT partition sgdisk -e, parted resizepart, partprobe Repartition after the filesystem is smaller A mounted partition cannot always be reread safely
md RAID mdadm --grow --size=max mdadm --grow --size= or --array-size=, after the filesystem shrinks Capacity is limited by the smallest member
LUKS2 cryptsetup resize cryptsetup resize --size= only when the data below is already smaller The mapping cannot expose more than the underlying device
LVM PV pvresize pvmove then vgreduce, pvremove The VG must have free extents to receive moved data
LVM LV lvextend, then grow the filesystem lvreduce only after the filesystem is smaller LVM checks the filesystem, but the order still matters
ext4 resize2fs online resize2fs offline, then reduce the container Shrinking requires an unmounted filesystem and a check
XFS xfs_growfs online Migration is the supported route The experimental -D path is not a routine reduction
Btrfs btrfs filesystem resize btrfs filesystem resize, btrfs device remove, balance Chunk allocation can block a shrink even when df shows free space

The comparison between filesystems matters at this point:

Requirement ext4 XFS Btrfs
Online grow Yes Yes Yes
Supported shrink Offline only Migration; experimental -D path only Online per device, with allocation constraints
Container reduction after shrink LV or partition New filesystem and mount Device resize or device removal
Metadata and data checksums Metadata checksums; no data checksums Metadata checksums; no data checksums Data and metadata checksums
Practical recovery from a full volume Remove files, grow, or migrate Remove files, grow, or migrate Delete snapshots, balance, or add a device

Build the practice stack from the bottom up. On the SCSI disk, create a GPT and a 1 GiB partition:

# parted -s /dev/sda mklabel gpt

The command is silent.

# parted -s /dev/sda unit MiB mkpart partdata ext4 1 1025

The command is silent.

# partprobe /dev/sda

The command is silent, but it asks the kernel to reread the table. The partition ends at 1025 MiB, leaving roughly 1 GiB of the 2 GiB disk unallocated for the growth exercise. Check the result before putting a filesystem on it:

# lsblk -o NAME,SIZE,TYPE,FSTYPE /dev/sda
NAME   SIZE TYPE FSTYPE
sda      2G disk 
└─sda1   1G part 

mkfs.ext4 writes the first filesystem:

# mkfs.ext4 -L partdata /dev/sda1
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: 5a9a116f-1396-4436-afd3-6df6eedfe994
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376

Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done

Selected output; the redrawn progress counters and the version banner that mke2fs prints on standard error are omitted.

Mount it and give john a place to write the file that will be checked after every layer change:

# mkdir -p /srv/part
# mount /dev/sda1 /srv/part
# chown john:john /srv/part

All three commands are silent. As john, write 64 MiB of random data:

$ dd if=/dev/urandom of=/srv/part/keep.bin bs=1M count=64 status=none

status=none suppresses dd's transfer summary, so the command prints nothing.

# sha256sum /srv/part/keep.bin > /home/john/debian-handbook/storage-migration/part-keep.sha256

The redirection runs in the root shell, so the checksum file is root-owned; the workspace directory remains readable to john. The checksum file is the reader's record of the original content; later commands compare against it with sha256sum -c.

The RAID1 array uses /dev/vdc and /dev/vdd. mdadm asks whether to enable its write-intent bitmap, then starts the array:

# mdadm --create /dev/md0 --level=1 --raid-devices=2 --metadata=1.2 /dev/vdc /dev/vdd
To optimalize recovery speed, it is recommended to enable write-indent bitmap, do you want to enable it now? [y/N]? y
mdadm: array /dev/md0 started.

The prompt is mdadm's own wording; answer y for a write-intent bitmap, which makes a later interrupted resynchronization cheaper to resume. The array performs an initial resync in the background. Wait for it to become idle before putting an encrypted layer on it, then read the result:

# mdadm --detail /dev/md0
/dev/md0:
           Version : 1.2
     Creation Time : Fri Nov 20 17:54:31 2026
        Raid Level : raid1
     Array Size : 2094080 (2045.00 MiB 2144.34 MB)
     Used Dev Size : 2094080 (2045.00 MiB 2144.34 MB)
      Raid Devices : 2
     Total Devices : 2
   Persistence : Superblock is persistent

     Intent Bitmap : Internal

     Update Time : Fri Nov 20 17:55:22 2026
           State : clean 
    Active Devices : 2
   Working Devices : 2
   Failed Devices : 0
    Spare Devices : 0

Consistency Policy : bitmap

          Name : migrate:0  (local to host migrate)
          UUID : f30c8347:1fed8a73:5bd7fe0f:3bf6dcfb
        Events : 25

    Number   Major   Minor   RaidDevice State
       0     254       32        0      active sync   /dev/vdc
       1     254       48        1      active sync   /dev/vdd

The array size is about 2 GiB, slightly below the raw member size because md stores its superblock and alignment metadata. That difference is the first of several overheads that make the final filesystem smaller than the disk printed on the label. The UUID line is the array's identity; Section 14.1.2 records it so the array can be reassembled by name if the member names change.

Format the array as LUKS2 with the key file and open the mapping. Both commands are silent when they succeed:

# cryptsetup luksFormat --type luks2 --pbkdf-memory 262144 --key-file /etc/luks-keys/stack.key /dev/md0

The command is silent when it succeeds.

# cryptsetup open --key-file /etc/luks-keys/stack.key /dev/md0 cryptstack

The mapping is created silently. The options that matter here are:

Option Description
--type luks2 Use the LUKS2 header format, which supports Argon2 and flexible keyslots
--key-file FILE Read the passphrase from FILE; the file must remain readable at boot or unlock
--pbkdf-memory KiB Set the Argon2 memory cost; the default is chosen for the machine, not for the smallest machine
--key-size, --cipher Override the default cipher and key size; the defaults are suitable for this exercise

The mapping is now the device that LVM will use. Check that it is active and note its payload offset:

# cryptsetup status cryptstack
/dev/mapper/cryptstack is active.
  type:    LUKS2
  cipher:  aes-xts-plain64
  keysize: 512 bits
  key location: keyring
  device:  /dev/md0
  sector size:  512
  offset:  32768 sectors
  size:    4155392 sectors
  mode:    read/write

The offset is the 16 MiB area reserved for the LUKS2 header; the mapping's usable size is therefore smaller than /dev/md0. Build the volume group and logical volume on the mapping:

# pvcreate /dev/mapper/cryptstack
  Physical volume "/dev/mapper/cryptstack" successfully created.
# vgcreate stackvg /dev/mapper/cryptstack
  Volume group "stackvg" successfully created
# lvcreate -L 1G -n stacklv stackvg
  Logical volume "stacklv" created.

Create the filesystem, mount it, and write a second checksummed file:

# mkfs.ext4 -L stacklv /dev/stackvg/stacklv
Creating filesystem with 262144 4k blocks and 65536 inodes
Filesystem UUID: b0f323be-9876-4714-9c62-6f1d1fec18b5

Selected output; the superblock-backup, progress, and completion lines are the same as the first filesystem's transcript above. The filesystem UUID is the value that fstab will use later.

# mount /dev/stackvg/stacklv /srv/stack
# chown john:john /srv/stack

Both commands are silent.

$ dd if=/dev/urandom of=/srv/stack/keep.bin bs=1M count=64 status=none

As before, dd prints nothing.

# sha256sum /srv/stack/keep.bin > /home/john/debian-handbook/storage-migration/stack-keep.sha256

The redirection runs in the root shell and the file is root-owned. The checksum file is the record that the later reductions and moves compare against.

The remaining practice filesystems are independent. Rather than repeat twenty-one one-line commands, put them in a small script in the workspace and read it before running it. Each mkfs is destructive and targets a different disposable device:

#!/bin/bash
set -e

mkfs.xfs -f -L xfsdata /dev/vdb
mkdir -p /srv/xfs
mount /dev/vdb /srv/xfs
chown john:john /srv/xfs

mkfs.btrfs -L btrdata /dev/vdg /dev/vdh
mkdir -p /srv/btr
mount /dev/vdg /srv/btr
chown john:john /srv/btr

pvcreate /dev/vde /dev/vdf
vgcreate demovg /dev/vde /dev/vdf
lvcreate -L 1G -n appdata demovg /dev/vde
mkfs.ext4 -L appdata /dev/demovg/appdata
mkdir -p /srv/appdata
mount /dev/demovg/appdata /srv/appdata
chown john:john /srv/appdata

mkfs.ext4 -L migsrc /dev/vdk
mkfs.ext4 -L migdst /dev/vdl
mkdir -p /srv/migsrc /srv/migdst
mount /dev/vdk /srv/migsrc
mount /dev/vdl /srv/migdst
chown john:john /srv/migsrc /srv/migdst

Save this as /home/john/debian-handbook/storage-migration/prepare-practice.sh, then run it once from the root shell with the absolute path:

# bash /home/john/debian-handbook/storage-migration/prepare-practice.sh

The script prints each command's ordinary output as it runs: mkfs.xfs and mkfs.btrfs print their geometry and label banners, the two mkfs.ext4 calls print their block and inode counts, each LVM command prints one line per object it creates, and the mkdir, mount, and chown steps are silent. set -e stops the run at the first failing command, leaving the disks in a state that shows where it stopped. Do not run it a second time without erasing the practice disks; mkfs and pvcreate are not idempotent.

Give the practice volumes content. The exact bytes do not matter, but the file count and checksums do:

$ for i in $(seq 1 20); do dd if=/dev/urandom of=/srv/btr/data$i.bin bs=1M count=8 status=none; done

The loop is one shell command that writes twenty 8 MiB files; it prints nothing.

$ for i in $(seq 1 40); do dd if=/dev/urandom of=/srv/appdata/file$i.bin bs=1M count=8 status=none; done

This loop similarly writes forty files for the LVM extent exercise.

$ cd /srv/appdata

Changing directory prints nothing.

$ sha256sum *.bin > SHA256SUMS

The redirection writes the checksums to a file. That file lets the later pvmove verify that moving extents did not alter the data.

14.1.2. Capturing Layouts and Establishing a Restore Point

A layout capture is not a data backup. It records the relationships that are easy to forget under pressure: which partition belongs to which array, which UUID a filesystem uses, which physical volume holds which extent, and which mapping crypttab expects. The data backup itself is the subject of Chapter 15.

As john, write the current block layout to the workspace. The redirection is performed by john's shell, so the file belongs to john:

$ sudo lsblk -o NAME,SIZE,TYPE,FSTYPE,MOUNTPOINTS > layout-before.txt

The command prints nothing; the output went to the file. Read it back:

$ cat layout-before.txt
NAME                   SIZE TYPE  FSTYPE            MOUNTPOINTS
sda                      2G disk                    
└─sda1                   1G part  ext4              /srv/part
vda                    100G disk                    
└─vda1                 100G part  ext4              /
vdb                      2G disk  xfs               /srv/xfs
vdc                      2G disk  linux_raid_member 
└─md0                    2G raid1 crypto_LUKS       
  └─cryptstack           2G crypt LVM2_member       
    └─stackvg-stacklv    1G lvm   ext4              /srv/stack
vdd                      2G disk  linux_raid_member 
└─md0                    2G raid1 crypto_LUKS       
  └─cryptstack           2G crypt LVM2_member       
    └─stackvg-stacklv    1G lvm   ext4              /srv/stack
vde                      2G disk  LVM2_member       
└─demovg-appdata         1G lvm   ext4              /srv/appdata
vdf                      4G disk  LVM2_member       
vdg                      2G disk  btrfs             /srv/btr
vdh                      2G disk  btrfs             
vdi                      2G disk                    
vdj                      4G disk                    
vdk                      2G disk  ext4              /srv/migsrc
vdl                      2G disk  ext4              /srv/migdst
vdm                      2G disk                    

The listing shows the dependency order directly: /dev/vdc and /dev/vdd feed md0; md0 feeds cryptstack; cryptstack feeds stackvg-stacklv; and that volume is mounted at /srv/stack. It also shows that /dev/vdb is an XFS filesystem with no partition table, while /dev/sda1 is a partition. Both are valid; the difference changes the commands used to grow them.

Capture the LVM identities separately, because lsblk does not print volume-group UUIDs:

$ sudo pvs -o pv_name,pv_uuid,vg_name,pv_size > pvs-before.txt

The redirection writes the report to the file; the command itself prints nothing. Read it back:

$ cat pvs-before.txt
  PV                     PV UUID                                VG      PSize 
  /dev/mapper/cryptstack O7LURd-Pzp1-9wSP-H6Ki-qN0R-x9x5-kEobaT stackvg  1.98g
  /dev/vde               6cMgVE-fy0V-Rm0u-4NR9-4tgm-lCh6-jb5ThI demovg  <2.00g
  /dev/vdf               VZsNHV-kvEu-vK4B-bplr-t1xt-mIcH-F5YxBB demovg  <4.00g

The UUIDs are the values that LVM can use to find the physical volumes if device names change.

Now create the restore material. vgcfgbackup writes a textual copy of the volume-group metadata:

# vgcfgbackup -f /home/john/debian-handbook/storage-migration/stackvg.vg stackvg
  Volume group "stackvg" successfully backed up.

Back up the LUKS header separately. The header contains the keyslots; a damaged header can make the data unrecoverable even when the ciphertext is intact:

# cryptsetup luksHeaderBackup /dev/md0 --header-backup-file /home/john/debian-handbook/storage-migration/cryptstack.header

The command is silent.

# chmod 600 /home/john/debian-handbook/storage-migration/cryptstack.header /home/john/debian-handbook/storage-migration/stackvg.vg

The command is silent. The header backup is as sensitive as the key file, because it holds the keyslots; mode 600 keeps each artifact readable only by its owner.

# chown john:john /home/john/debian-handbook/storage-migration/cryptstack.header /home/john/debian-handbook/storage-migration/stackvg.vg

Ownership moves to john so the restore material can be read from john's account later. The ls -l below shows both results.

Back up the partition table with sgdisk, record the md array configuration, and copy the two files that determine boot-time assembly:

# sgdisk --backup=/home/john/debian-handbook/storage-migration/sda.gpt /dev/sda
The operation has completed successfully.
# mdadm --detail --scan > /home/john/debian-handbook/storage-migration/mdadm.scan

The redirection writes the array line and prints nothing. Read it back:

# cat /home/john/debian-handbook/storage-migration/mdadm.scan
ARRAY /dev/md0 metadata=1.2 UUID=f30c8347:1fed8a73:5bd7fe0f:3bf6dcfb

Copy the two configuration files:

# cp -p /etc/fstab /home/john/debian-handbook/storage-migration/fstab.before
# cp -p /etc/crypttab /home/john/debian-handbook/storage-migration/crypttab.before

Both commands are silent; -p preserves the original modes and timestamps.

Artifact What it preserves Restore command
layout-before.txt, pvs-before.txt Human-readable map of the stack Read and compare after the change
stackvg.vg LVM metadata for stackvg vgcfgrestore -f stackvg.vg stackvg
cryptstack.header LUKS2 header and keyslots cryptsetup luksHeaderRestore /dev/md0 --header-backup-file cryptstack.header
sda.gpt GPT primary and backup headers sgdisk --load-backup=sda.gpt /dev/sda
mdadm.scan Array UUID, level, and member metadata mdadm --assemble --scan after installing the line in mdadm.conf
fstab.before, crypttab.before Mount and unlock configuration Copy back and run systemctl daemon-reload

Check that the files exist and have sensible sizes and modes:

# ls -l /home/john/debian-handbook/storage-migration
total 16436
-rw------- 1 john john 16777216 Nov 20 17:57 cryptstack.header
-rw-r--r-- 1 root root       54 Nov 20 17:52 crypttab.before
-rw-r--r-- 1 root root      336 Nov 20 17:48 fstab.before
-rw-rw-r-- 1 john john     1399 Nov 20 17:57 layout-before.txt
-rw-r--r-- 1 root root       69 Nov 20 17:57 mdadm.scan
-rw-r--r-- 1 root root       85 Nov 20 17:54 part-keep.sha256
-rw-rw-r-- 1 john john      316 Nov 20 17:57 pvs-before.txt
-rw-r--r-- 1 root root    17920 Nov 20 17:57 sda.gpt
-rw-r--r-- 1 root root       86 Nov 20 17:56 stack-keep.sha256
-rw------- 1 john john     1342 Nov 20 17:57 stackvg.vg

The header is 16 MiB, which is the size of the LUKS2 keyslot area for this layout. A header backup taken before a keyslot change will not contain the later keyslot; take a fresh one after changing passphrases or keys.

14.1.3. Choosing Online Work or a Maintenance Window

Whether a change can be online is a property of the layer and the operation, not of the whole system. The same host can grow an LV and its ext4 filesystem online, while the ext4 shrink on that LV requires an unmount. The following decision list is the one to apply before touching a production stack:

Operation Usual mode Why
Add capacity to a LUN or virtual disk Online The provider changes the backing device; the guest still needs a rescan
Grow a GPT partition Maintenance or careful online reread A busy partition table may not be reread; partx -u can update the kernel in some cases
Grow md RAID component size Online The array resynchronizes in the background
Grow a LUKS mapping Online The mapping exposes more of the same ciphertext
Grow a PV, LV, and ext4 or XFS Online pvresize, lvextend, resize2fs, and xfs_growfs support mounted filesystems
Grow or shrink Btrfs Online per device Btrfs relocates chunks if a device becomes smaller
Shrink ext4 Maintenance resize2fs requires an unmounted filesystem for reduction
Shrink an LV, PV, partition, or md array Maintenance The filesystem must already be smaller; the container must not be busy
Replace an md member or Btrfs device Online if redundancy exists The array or filesystem keeps serving during the rebuild or relocation
Move LVM extents with pvmove Online LVM copies extents while the LV stays active
Migrate a root filesystem Maintenance The boot loader and initramfs must be updated and the old root kept for rollback

A maintenance window is not only about unmounting. It also gives you time to stop applications, take a backup, verify the new layout, and roll back. When the change touches the root filesystem, the window must cover a reboot and a test boot; a chroot validation is useful but does not prove that the firmware will find the new disk.

Online work still needs a consistency point. Growing a filesystem is metadata work and is generally safe while the filesystem is mounted, but moving data between devices is not the same as resizing. Section 14.4.4 covers the final synchronization; Chapter 15 covers a backup that can be restored after a failed migration.