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:

  1. Install lvm2 package

  2. Backup / using rsync to another drive

  3. Create logical volumes I need, for example VolumeGroup/root, VolumeGroup/home and VolumeGroup/swap

  4. Transfer all data from backup to VolumeGroup/root

  5. Change kernel parameters from cryptdevice=PARTUUID=...:root root=/dev/mapper/root to cryptdevice=PARTUUID=...:cryptlvm root=/dev/VolumeGroup/root

  6. Add lvm hook to HOOKS in /etc/mkinitcpio.conf

  7. Chroot into new root

  8. Re-gen fstab

  9. 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?

Asked By: MPEI_stud

||

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.

Answered By: frostschutz
Categories: Answers Tags: , , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.