Moving from plain LUKS to LVM on LUKS
At installation I forgot to create a logical volume group in LUKS container. I won’t reinstall the system. So how do I create LVG in LUKS container without data loss?
My plan is:
-
Install
lvm2
package -
Backup
/
usingrsync
to another drive -
Create logical volumes I need, for example
VolumeGroup/root
,VolumeGroup/home
andVolumeGroup/swap
-
Transfer all data from backup to
VolumeGroup/root
-
Change kernel parameters from
cryptdevice=PARTUUID=...:root root=/dev/mapper/root
tocryptdevice=PARTUUID=...:cryptlvm root=/dev/VolumeGroup/root
-
Add
lvm
hook to HOOKS in/etc/mkinitcpio.conf
-
Chroot into new root
-
Re-gen fstab
-
Run
mkinitcpio -P
Possible implementation:
### On the primary OS
pacman -S lvm2
# Add lvm2 hook to /etc/mkinitcpio.conf, to it looks like this:
# HOOKS=(base udev autodetect keyboard keymap modconf block encrypt filesystems lvm2 fsck)
mkinitcpio -P
### Reboot into live-USB now
### My existing layout:
###
### nvme0n1 259:0 0 931.5G 0 disk
### ├─nvme0n1p1 259:1 0 512M 0 part /boot
### └─nvme0n1p2 259:2 0 931G 0 part
### └─root 254:0 0 931G 0 crypt /
### mounting necessary partitions
rsync -aAXHv /mnt/ /path/to/backup
wipefs -a /dev/nvme0n1p2
cryptsetup luksFormat /dev/nvme0n1p2
cryptsetup open /dev/nvme0n1p2 cryptlvm
pvcreate /dev/mapper/cryptlvm
vgcreate VolumeGroup /dev/mapper/cryptlvm
lvcreate -L 16G VolumeGroup -n swap
lvcreate -L 100G VolumeGroup -n root
lvcreate -l 100%FREE VolumeGroup -n home
lvreduce -l -256M /dev/VolumeGroup/home
mkfs.ext4 /dev/VolumeGroup/root
mkfs.ext4 /dev/VolumeGroup/home
mkswap /dev/VolumeGroup/swap
mount /dev/VolumeGroup/root /mnt
mount --mkdir /dev/VolumeGroup/home /mnt/home
swapon /dev/VolumeGroup/swap
rsync -aAXHv /path/to/backup /mnt
### Now editing kernel parameters so (dots are placeholder for real PARTUUID)
### cryptdevice=PARTUUID=...:cryptlvm root=/dev/VolumeGroup/root
### (*I use systemd-boot, so I change /mnt/etc/kernel/cmdline*)
arch-chroot /mnt
genfstab -U / > /etc/fstab
mkinitcpio -P
### Now reboot and hope all will work
Will it work?
It should work.
The archlinux mkinitcpio hook is lvm2
, not lvm
. Add this hook first. No harm done if no LVM is present yet. One less thing to worry about later.
Backups from a running system can be inconsistent. With --exclude
or -x
, it can be incomplete. Consider using bind mounts, or do it from a Live/Rescue environment (don’t have to exclude /proc /sys /dev
if you don’t mount it in the first place).
Another alternative would be to convert to LVM in-place, there’s a German tutorial on that ("LVM nachträglich einrichten" – ubuntuusers.de Wiki). It works by relocating the first extent to make room for LVM metadata. But it’s risky enough you have to backup everything anyway, so your method is better.