14.4. Moving Data to New Storage
Resizing is not always possible: XFS has no supported shrink, a device
may be leaving service, or the new storage may simply be different
hardware. Migration moves the data instead of the boundary, and the
method follows the layer that changes: files can be copied, LVM extents
can be relocated, and md or Btrfs members can be replaced in place. This
section copies a file tree with rsync while preserving the metadata
applications depend on, relocates LVM extents with pvmove, replaces an
md member and a Btrfs device while the filesystems stay mounted, and
closes with the synchronized final pass that makes a copy safe to switch
to.
14.4.1. File-Based Migration with Metadata Preservation
When the new storage is a different filesystem or a different size, the simplest migration is to create the destination filesystem and copy the files. The copy must preserve more than file contents: ownership, mode, timestamps, ACLs, extended attributes, hard links, symbolic links, and sparse holes all matter to a running system.
Figure 14.3. Choosing a migration method. File migration is the only method that changes the filesystem; the other three preserve the existing filesystem and move the block storage under it.
The practice source is on /dev/vdk at /srv/migsrc; the destination is
on /dev/vdl at /srv/migdst. As john, create a small tree with an ACL,
an extended attribute, a hard link, a symbolic link, and a sparse file:
$ cd /srv/migsrc
Changing directory prints nothing.
$ mkdir app
The directory is created silently.
$ cd app
Changing directory prints nothing.
$ printf "%s\n" "quarterly report" "revenue 42" > report.txt
The redirection writes the report; printf prints nothing to the
terminal.
$ dd if=/dev/zero of=sparse.img bs=1M count=0 seek=64 status=none
The dd with seek=64 creates a file with an apparent size of 64 MiB
but no allocated data blocks. Create the two links:
$ ln report.txt report-hardlink.txt
$ ln -s report.txt report-link.txt
Both link commands are silent. Add the ACL and extended attribute, then set the report's mode:
$ setfacl -m u:www-data:r report.txt
$ setfattr -n user.owner -v operations report.txt
$ chmod 640 report.txt
All three commands are silent. ls -li shows the two hard links sharing
an inode and the symbolic link pointing at the report:
# ls -li /srv/migsrc/app
total 8
14 -rw-r-----+ 2 john john 28 Nov 20 17:57 report-hardlink.txt
16 lrwxrwxrwx 1 john john 10 Nov 20 17:57 report-link.txt -> report.txt
14 -rw-r-----+ 2 john john 28 Nov 20 17:57 report.txt
15 -rw-rw-r-- 1 john john 67108864 Nov 20 17:57 sparse.img
The + after the mode marks the ACL. getfacl and getfattr show what
must survive the copy:
# getfacl -p /srv/migsrc/app/report.txt
# file: /srv/migsrc/app/report.txt
# owner: john
# group: john
user::rw-
user:www-data:r--
group::rw- #effective:r--
mask::r--
other::---
# getfattr -d /srv/migsrc/app/report.txt
# file: srv/migsrc/app/report.txt
user.owner="operations"
getfattr strips the leading slash from the path in its comment line
and prints a one-line notice about that on standard error; the value
line is the result.
The first rsync uses one archive mode and explicit preservation flags:
# rsync -aHAXS --numeric-ids /srv/migsrc/ /srv/migdst/
The trailing slash on the source is significant: it copies the contents
of /srv/migsrc, not the directory itself. The options are:
| Option | Description |
|---|---|
-a, --archive |
Preserve permissions, times, group, owner, devices, and recurse |
-H, --hard-links |
Preserve hard-link relationships |
-A, --acls |
Preserve POSIX ACLs |
-X, --xattrs |
Preserve extended attributes |
-S, --sparse |
Turn runs of zero bytes into sparse blocks at the destination |
--numeric-ids |
Keep numeric UID and GID values instead of mapping names |
--delete |
Remove destination files that no longer exist at the source (Section 14.4.4) |
The copy runs without output on success. Verify the destination with the same tools used on the source:
# ls -li /srv/migdst/app
total 8
15 -rw-r-----+ 2 john john 28 Nov 20 17:57 report-hardlink.txt
14 lrwxrwxrwx 1 john john 10 Nov 20 17:57 report-link.txt -> report.txt
15 -rw-r-----+ 2 john john 28 Nov 20 17:57 report.txt
16 -rw-rw-r-- 1 john john 67108864 Nov 20 17:57 sparse.img
The inode numbers differ because the destination is a different
filesystem; the hard-link relationship is what matters. The two names
still share one inode, as the link count 2 and the identical inode
number show. The ACL and xattr also survived:
# getfacl -p /srv/migdst/app/report.txt
# file: /srv/migdst/app/report.txt
# owner: john
# group: john
user::rw-
user:www-data:r--
group::rw- #effective:r--
mask::r--
other::---
# getfattr -d /srv/migdst/app/report.txt
# file: srv/migdst/app/report.txt
user.owner="operations"
The sparse file needs care. A destination that already contains a fully
allocated copy is not changed in place by adding -S; rsync may see the
file as up to date and leave its blocks allocated. The first copy above
used -S, so du reports the apparent size and the allocated size
separately:
# du -h --apparent-size /srv/migdst/app/sparse.img
64M /srv/migdst/app/sparse.img
# du -h /srv/migdst/app/sparse.img
0 /srv/migdst/app/sparse.img
The apparent size is 64 MiB; the allocated size is zero because the file
is all holes. If you copy without -S, the destination can consume 64
MiB on disk even though ls -l shows the same size. Include -S from
the first copy, or recreate the destination file before sparsifying it.
14.4.2. Moving LVM Extents to Replacement Devices
pvmove copies allocated extents off one physical volume and onto free
extents elsewhere in the same volume group. The logical volumes remain
active and the filesystem remains mounted. It is the LVM equivalent of a
block-level migration and preserves the filesystem exactly.
The source in this exercise is /dev/vde; the destination is /dev/vdf,
which has about 4 GiB free in demovg. Run the move:
# pvmove /dev/vde
/dev/vde: Moved: 19.53%
/dev/vde: Moved: 100.00%
The percentages are progress updates; the command exits after all extents have moved. The volume group no longer needs the old PV, so remove it from the group and wipe its label:
# vgreduce demovg /dev/vde
Removed "/dev/vde" from volume group "demovg"
# pvremove /dev/vde
Labels on physical volume "/dev/vde" successfully wiped.
The logical volume now maps to the replacement device:
# lvs -o lv_name,vg_name,lv_size,devices
LV VG LSize Devices
appdata demovg 1.00g /dev/vdf(0)
stacklv stackvg 1.00g /dev/mapper/cryptstack(0)
Verify the data, not just the mapping. The checksum file for this volume
was written from /srv/appdata in Section 14.1.1, so change to that
directory and check the moved files there:
# cd /srv/appdata
Changing directory prints nothing.
# sha256sum -c SHA256SUMS | tail -3
/srv/appdata/file7.bin: OK
/srv/appdata/file8.bin: OK
/srv/appdata/file9.bin: OK
The pipeline is one logical operation: sha256sum -c reads every line
of the checksum file and prints one result per file, and tail -3
keeps the last three of the forty lines. Run it without the filter to
see every file's result.
pvmove options matter when the move is interrupted or when a specific
destination is required:
| Option | Description |
|---|---|
pvmove PV |
Move all allocated extents off PV to any free extents in the VG |
pvmove PV1 PV2 |
Move extents from PV1 to PV2 |
pvmove --abort |
Stop a running move; already-copied extents stay on the destination |
-i SECONDS, --interval |
Set the progress reporting interval |
-b, --background |
Run in the background; monitor with pvmove status or the system log |
--alloc anywhere |
Allow allocation on any PV, including the source, when no strict destination is needed |
pvmove needs free extents at least as large as the allocated extents
being moved. If the destination is too small, the command stops with an
allocation error and leaves the source mapping intact. Check pvs and
vgs before starting, and keep the output of lvs -o devices so the
before and after mappings can be compared.
14.4.3. Replacing MD and Btrfs Devices
An md array with redundancy can replace a member while it continues to serve reads and writes. Add the replacement as a spare first:
# mdadm /dev/md0 --add /dev/vdj
mdadm: added /dev/vdj
Then mark the old member for replacement and name the spare that should take its place:
# mdadm /dev/md0 --replace /dev/vdd --with /dev/vdj
mdadm: Marked /dev/vdd (device 1 in /dev/md0) for replacement
mdadm: Marked /dev/vdj in /dev/md0 as replacement for device 1
The array rebuilds the new member from the surviving copy. The old member stays active until the rebuild finishes, which is why the array remains redundant during the operation. When the rebuild is idle, the old member is marked faulty and the new one is in sync:
# mdadm --detail /dev/md0
/dev/md0:
Version : 1.2
Creation Time : Fri Nov 20 17:54:31 2026
Raid Level : raid1
Array Size : 3142656 (3.00 GiB 3.22 GB)
Used Dev Size : 3142656 (3.00 GiB 3.22 GB)
Raid Devices : 2
Total Devices : 3
Persistence : Superblock is persistent
Intent Bitmap : Internal
State : clean
Active Devices : 2
Working Devices : 2
Failed Devices : 1
Spare Devices : 0
Number Major Minor RaidDevice State
0 254 32 0 active sync /dev/vdc
2 254 144 1 active sync /dev/vdj
1 254 48 - faulty /dev/vdd
Selected output; the update time, consistency policy, name, UUID, and event count lines are omitted.
Remove the failed member and erase its md superblock so it cannot be mistaken for a member later:
# mdadm /dev/md0 --remove /dev/vdd
mdadm: hot removed /dev/vdd from /dev/md0
# mdadm --zero-superblock /dev/vdd
The second command is silent. A later mdadm --examine must not find an
array superblock:
# mdadm --examine /dev/vdd
mdadm: No md superblock detected on /dev/vdd.
The replacement device must be at least as large as the array's component
size. In this exercise /dev/vdj is 4 GiB and the array component is
3 GiB, so the spare has room. A replacement that is smaller than the
component size is rejected even if the array has unused space.
The mdadm options used here are:
| Option | Description |
|---|---|
--add DEVICE |
Add a device as a spare |
--replace OLD |
Mark OLD for replacement; it stays active until the new member is in sync |
--with NEW |
Use NEW as the preferred replacement for the preceding --replace device |
--remove DEVICE |
Remove a failed or replaced member from the array |
--zero-superblock DEVICE |
Erase the md metadata so the device is no longer a member |
--detail DEVICE |
Show member state and rebuild progress |
--grow --size=SIZE |
Change the component size after all members have grown |
Btrfs device replacement works at the chunk level. The filesystem stays
mounted and keeps serving reads and writes while it relocates the chunks
that live on the old device. Replace device 2 (/dev/vdh) with the
same-size /dev/vdi:
# btrfs replace start 2 /dev/vdi /srv/btr
The command starts the replacement in the background and prints nothing. Check it separately:
# btrfs replace status /srv/btr
Started on 20.Nov 18:01:25, finished on 20.Nov 18:01:28, 0 write errs, 0 uncorr. read errs
The status line reports the start and finish times and any read or write errors; this replacement completed with none. The filesystem now lists the new device and no longer lists the old one:
# btrfs filesystem show /srv/btr
Label: 'btrdata' uuid: 30561864-0a3e-4fa1-86c6-f8a471d8a6d3
Total devices 2 FS bytes used 160.31MiB
devid 1 size 2.00GiB used 304.00MiB path /dev/vdg
devid 2 size 2.00GiB used 712.00MiB path /dev/vdi
The old /dev/vdh is no longer a member, and the replacement has
already cleared its Btrfs identity: when a replacement completes, the
kernel scratches the retired device's superblock copies precisely so
the disk cannot be mistaken for a member. wipefs -a is still the
right final step, and it prints nothing because no signature is left
to erase:
# wipefs -a /dev/vdh
The command is silent when it finds nothing. Compare the XFS retirement
in Section 14.5.4, where wipefs reports the four bytes it erases.
Btrfs replacement options follow the same pattern as md, with different names:
| Option | Description |
|---|---|
btrfs replace start SRC DST MOUNT |
Replace SRC (device path or devid) with DST |
btrfs replace status MOUNT |
Show progress and any read/write errors |
btrfs replace cancel MOUNT |
Stop a running replacement |
-r |
Read only from SRC if no other zero-defect mirror exists |
-f |
Force overwriting a target that looks like a valid Btrfs device |
-B |
Run in the foreground instead of the background |
The target must be the same size or larger than the source. If it is
larger, the filesystem does not automatically use the extra space; run
btrfs filesystem resize DEVID:max MOUNT afterward, as in Section
14.2.3.
14.4.4. Final Synchronization and Application Consistency
A first copy is only a snapshot of a moving source. Before switching to the new storage, stop or quiesce the writers and run the copy again. The second pass is deliberately narrower: it copies only changed data, but it still reads the same source tree and still preserves metadata.
Give the final pass something real to do. As john, append one line to the source report, standing in for any change made after the first copy:
$ printf "%s\n" "revised total 43" >> /srv/migsrc/app/report.txt
The redirection appends the line to the file; printf prints nothing
to the terminal. Then, on the destination, create a file the source
does not have, standing in for anything left behind by an earlier copy:
# touch /srv/migdst/app/stale.txt
The command is silent.
Before the final pass, be certain the destination is the one you mean
to replace. --delete removes destination files that the source no
longer has, so a reversed source and destination erases the wrong
tree. When the paths are not obvious, run the same rsync with
--dry-run --itemize-changes first: it lists what would change,
including the deletions, without writing anything.
For a service, quiescing is application-specific: stop the service, put
it in read-only maintenance mode, export a database dump, or create a
snapshot at a point the application can describe. fsfreeze is a
filesystem-level tool for the case where the whole filesystem can be
frozen briefly. It flushes and blocks new writes, then unfreezes with
-u:
# fsfreeze -f /srv/migsrc
The freeze command is silent.
# rsync -aHAXS --delete --numeric-ids /srv/migsrc/ /srv/migdst/
The rsync is silent on success. The frozen source cannot change while it runs, so the destination is an exact copy when the unfreeze completes.
# fsfreeze -u /srv/migsrc
The unfreeze is silent. Freezing a filesystem for the duration of a large copy can block applications for too long; for a large tree, freeze only long enough to create a storage snapshot, copy from the snapshot, and then unfreeze. Chapter 13 covers LVM snapshots and Chapter 15 covers a backup that can be restored if the final copy is wrong.
Two checks close the exercise: the appended line must exist on the destination, and the stale file must be gone.
# cmp /srv/migsrc/app/report.txt /srv/migdst/app/report.txt
cmp prints nothing when the two files are identical, so the silence
is the success. The stale file is gone as well: --delete removed it
during the frozen pass, and ls /srv/migdst/app now lists only the
four names the source has.
| Consistency method | What it guarantees | Limitation |
|---|---|---|
| Stop the service | No application writes during the final pass | Requires downtime |
| Application export/dump | A logically consistent application state | The dump must be restored, not just copied |
fsfreeze -f |
Filesystem-level freeze of writes | Blocks the whole filesystem; must be short |
| LVM snapshot | A stable block view for a longer copy | Snapshot space can fill; writes to the origin continue |
| Storage array snapshot | A stable view without host I/O pause | Depends on the array and its consistency group |