Resizing luks and LVM Root Disk

From Hack Sphere Labs Wiki
Jump to: navigation, search
  • +5G means powers of 1024
  • gdisk shows partition sizes in powers of 1024
  • 1024

Making it smaller

Its pretty simple but there is a lot of room to mess up. I would use the 3 links below to figure things out...the commands that are posted here are to jog my memory when I get to certain places and may have some issues because I compiled them after I did the entire thing. A lot of the guides get to the step of resizing the luks container without realizing it can resize itself if you modify the partition first. It sounds crazy but it works and it makes it so you do not have to guess sector sizes to get the proper real world size. Just be sure not to have anything at the end of that luks container when you change the partition size, and resize the luks.

The concept goes (at least here): mount encrypted disk, resize LVM stuff (make them much smaller then what you want in the end), change partition table, resize luks (automagic), test...the regrow the filesystem out.

I was having problems doing the sector size the manual way, fdisk and gdisk would both add sectors to my command. I would issue something like +137155219 (sectors) and it would show when I listed the partitions the number of sectors to be 137155220 and I do not know why right now. It was one of the reasons I changed the partition table before I resized the container and just issued the general resize command with no sector specifications to cryptsetup and it would adjust to its partition size.

All the other guides want you to edit the partition table, then match the luks container to it. I even had a problem where when I would do this, the cryptsetup status bla command would still show the old sector count. I think the right sector count persisted after reboot but why did it not update after issue in the status command?

cryptsetup luksOpen /dev/sda3 bla
vgchange -a y
lvdisplay /dev/lvmpool/root
fsck.ext4 /dev/lvmpool/root
e2fsck -f /dev/lvmpool/root
resize2fs -p /dev/lvmpool/root 50G #(small, small, small!)
lvreduce -L 50G /dev/lvmpool/root
lvdisplay /dev/lvmpool/root

Now you have to resize your PVs. It seems the swap partition was at the end or something. So either move it (can you do that) (looks like they want you to backup the partition, delete, recreate, and restore) or delete it. I chose to delete it. It will not let you resize your PV without doing that.

lvremove /dev/lvmpool/swap
pvresize --setphysicalvolumesize 55G /dev/mapper/lvm #( I make the entire lvm way smaller then the 60GB luks I want in the end)
pvchange -x y /dev/mapper/lvm

make your swap again

lvcreate -L 5G -n swap lvmpool
mkswap -L swap /dev/lvmpool/swap

Update your fstab with the new uuid, or do it after reboot if you do not need swap atm.

  • mount the root fs
  • nano /etc/fstab
  • replace uuid
  • unmount
pvchange -x n /dev/mapper/lvm

This is where the order thing comes in...fdisk/gdisk,then luks or luks, then fdisk/gdisk. In the ubuntu guide it seemed like he was deleting more then one partition...all I did was resize one partition because my lvm is contained in the luks, not the other way around.

vgchange -an (you must issue this before you close your container...once again, unless your container is inside of an lvm...)
sudo cryptsetup luksClose crypt1
  • My Way
gdisk /dev/sda

You can backup with the b command, issue gdisk -l (take a picture with your phone, or write it down, or save it)

d (delete)
3 (my luks partition with the container on it)
n (new)
+60G (how big I want the luks partition to really be)
  • I just put Linux Filesystem in because my partition is not a LVM partition but instead a luks container with lvm inside and also...well it works. The Ubuntu guide said to use 8e in fdisk but even when I was trying the fdisk way that was still just "Linux Filesystem" not Linux LVM. I think the ubuntu way puts luks containers inside lvms....So anyways:
w (write)
partprobe /dev/sda (I did not do this....I was not resizing live, I just rebooted, or I might not have.  You should probably try it.)

Anyways, it got to a point where I was using the fdisk stuff and the sectors were not lining would always add sectors in the fidsk command...I made the LVM pv way smaller then the luks so I had some breathing room. I started with a 73 gb luks and I was going to keep going down from there before I discovered how luks would auto resize to its partition. When I realized this...I booted into the live cd, changed the size of the partitions with gdisk, opened the luks container, and then ran the resize command...and it works. I had a luks of 73, a lvm pv of 57, and file systems of 55 total in the lvm....and the sector thing was driving me nuts.

I made the partition 60GB with the above gdisk commands and ran this:

cryptsetup luksOpen /dev/sda3 lvm
cryptsetup resize lvm
sync #(why not)
vgchange -an
cryptsetup luksClose lvm

Above may be incomplete, make a backup before you try (yeh right!...aint nobody got time for that), use the 3 links that I have above to figure different things out. It is simple, you just have to understand how your system is created.

The one thing that would have screwed me is if the LVM PV was bigger then the partition or if the LVM started at the end of the container and went backwards, but it does not look like it does. What a mess.

Making it Bigger

I am not going to make the luks container bigger in this one...I have it set to the size I want. I just want to grow the LVM inside it.

pvresize /dev/mapper/lvm
lvremove /dev/mapper/lvm-swap


pvchange -x y /dev/mapper/lvm
lvresize -L +5G /dev/mapper/lvmpool-root
lvcreate -L +100%FREE -n swap lvmpool
mkswap -L swap /dev/lvmpool/swap

(fstab either now or later for swap...)(if I did not do it now, systemd would wait 1.5 mins to try and load, I could still do it later)

lsblk -f (helps)


pvchange -x y /dev/mapper/lvm

Resize FS

e2fsck -f /dev/mapper/lvmpool-root
resize2fs -p /dev/mapper/lvmpool-root
vgchange -an
cryptsetup luksClose lvm