Jan 05, 2017 15:54
The story behind that is that I had to re-setup already installed Ubuntu Server equipped with 140+Gigs RAM and ~550Gigs of storage (4*140G). Server uses LVM for all the filesystems except /boot.
Ubuntu installer created 540G big LVM VG and 2 LVs on it. 140G swap + 400G root. Taking into consideration that I plan running LXD on that machine, I had to free some space in order to create another LV there.
I decided to shrink the root file system from 400G to 45G and swap from 140G to 10G.
However, resizing the root (/) volume is tricky since you need to unmount the volume to do file system checks, and that is impossible if you are using it.
As far as I got there are 2 way to achieve non-mounted root.
1) boot into rescue mode from installation ISO
2) use pivot_root
Taking into consideration that I have IPKVM I proceeded to the first option.
1. Boot from Ubuntu Server ISO, select "Rescue a Broken System".
2. When it gets to the point of asking you to mount a file system, tell it not to mount any file system and run from the install root.
3. From the command line, type lvmdiskscan
4. This will show you all the disks. You will see your root volume. In my case I had to change /dev/lxc-vg/root and /dev/lxc-vg/swap_1
5. Run e2fsck -f /dev/lxc-vg/root
6. After that completes, run resize2fs -p /dev/lxc-vg/root 40G
7. Now we can put the rest of the space back into the LV pool. Run lvresize /dev/lxc-vg/root --size 45G
Word of caution, I intentionally made FS smaller at step 6 in order not to bother with calculation of blocks amount.
8. Now, we are extending root FS to use whole logical volume.
resize2fs /dev/lxc-vg0/root
At this point it is possible to reboot back to OS.
9. Unmount swap sudo swapoff -a
10. Resize swap lvresize /dev/lxc-vg/swap_1 --size 10G
11. Fix swap filesystem mkswapfs /dev/lxc-vg/swap_1
12. Mount it back sudo swapon -a
Now sudo lvs output should tell you that you have 45G root and 10g swap_1.
ubuntu,
lvresize,
lvm,
resize2fs