Sunday, 17 January 2016

Resize encrypted lvm partitions in Fedora23

In this article I will describe how to shrink one Logical Volume and extend another one in the same volume group.

My setup

During Fedora23 installation I let the installer to partition my disk automatically. I also checked that I want to encrypt my data.
The installer created Logical Volumes: 50GB root partition, 60GB home and 8GB swap.

After some time of using the system I found out that there is only 15GB left in /home and that there is 40GB of free space in / (root). I've decided that I will extend home and shrink root.

Preparation for the action

I spent some time reading about lvm and also LUKS disk encryption in fedora, and then I found this great page about resizing of lvm encrypted partitions - exactly what I needed, even little bit more.

I will try to describe exactly how it went in my case, so this article should be little bit shorter then the one I linked.

Initialization

When you want to shrink a partition you have to do it in unmounted state. For the root partition it means to boot from a live CD. I used Fedora23 CD for that.

Everybody warns you that you should backup your data before doing this, because you can easily loose them if something goes wrong. As I have all my important data in clouds (Dropbox, Google drive) I just restarted my laptop and booted Fedora live.



Brief description

When I skip few minor, but also important commands, then it went like this:
  1. Boot from the live CD
  2. Check if lvm2 and cryptsetup are installed
  3. Decrypt the encrypted file system with cryptsetup
  4. Check sizes of partitions and unmount them - I did this simply in file filesystem GUI - PCManFM in my case (fedora LDXE spin), I guess you can use umount in the terminal
  5. Reduce the the root filesystem with resize2fs
  6. Reduce the root logical volume with lvreduce
  7. Enlarge the home logical volume with lvextend
  8. Enlarge the home filesystem with resize2fs
  9. Reboot

Step by step howto

Prepare

Boot the live CD and open terminal

Switch to super user as all commands need to be started with sudo. Check that lvm and cryptsetup are installed.
sudo su
dnf install lvm2 cryptsetup
Check your partition setup
fdisk -l
Load the cryptsetup module.
modprobe dm-crypt
Decrypt the filesystem. Instead of sda9 use your desired partition. You can keep the cript1 name. 
cryptsetup luksOpen /dev/sda9 crypt1
Activate the LVM
sudo vgscan --mknodes
sudo vgchange -ay

Now you should be able to see your partitions in /dev/mapper location
ls -al /dev/mapper

# you should see something like:
lrwxrwxrwx.  1 root root       7 Jan 16 17:17 crypt1 -> ../dm-0
lrwxrwxrwx.  1 root root       7 Jan 16 17:17 fedora-home -> ../dm-3
lrwxrwxrwx.  1 root root       7 Jan 16 17:17 fedora-root -> ../dm-1
lrwxrwxrwx.  1 root root       7 Jan 16 17:17 fedora-swap -> ../dm-2

Shrink the root

Before we resize the filesystem it's good to say that it's good to shrink the filesystem more then you want to shrink the lvm and then after lvm is reduced extend the filesystem to the lvm size. That way you will avoid corruption of the filesystem and loose of your data. So be sure that you don't shrink your lvm more then your filesystem!

And also unmount it now, if it's mounted. I just opened the file explorer GUI (PCManFM) and saw that there are icons for unmount next to the root and home, so I unmounted both.

To be able to reduce the ext4 (ext2/ext3) filesystem, you have to check it before you can resize it. I shrinked my fedora-root filesystem to 25 Gigabytes. Adapt the size to your needs.
e2fsck -f /dev/mapper/fedora-root
resize2fs -p /dev/mapper/redora-root 25g
Check the filesystem again.
e2fsck -f /dev/mapper/fedora-root
Display Logical Volumes with lvdisplay.
lvdisplay
My fedora-root lvm had 50GiB, I wanted to shrink it to 32GiB, it means minus 18GiB.
lvreduce -L -18G /dev/fedora/root
Ok, done. If you use lvdisplay once more, you can see that root lvm is reduced.
lvdisplay
Now extend the filesystem from the 25GB to the lvm size (32GiB). Which means resize2fs without size parameter.
resize2fs /dev/mapper/fedora-root 

# output:
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/mapper/fedora-root to 8388608 (4k) blocks.
The filesystem on /dev/mapper/fedora-root is now 8388608 (4k) blocks long.
And check it.
e2fsck -f /dev/mapper/fedora-root

# output:
e2fsck 1.42.13 (17-May-2015)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/mapper/fedora-root: 177536/2097152 files (0.2% non-contiguous), 1483068/8388608 blocks

Ok great, root is reduced, now extend the home.

Extend the home

At first, we have to unlock the physical volume with pvdisplay command. Check it's name - it should be /dev/mapper/crypt1 if you decrypted it the same was as I did.
pvdisplay
You should see something like:
--- Physical volume ---
  PV Name               /dev/mapper/crypt1
  VG Name               fedora
  PV Size               118.67 GiB / not usable 4.00 MiB
  Allocatable           yes 
  PE Size               4.00 MiB
  Total PE              30379
  Free PE               4609
  Allocated PE          25770
And unlock. (I'm not sure if it did something in my case :)
pvchange -x y /dev/mapper/crypt1

# output was:
  Physical volume "/dev/mapper/crypt1" is already allocatable.
  Physical volume /dev/mapper/crypt1 not changed
  0 physical volumes changed / 1 physical volume not changed
Now check the volumes names to be sure that you extend the right one - home in my case.
lvdisplay
And then extend the home to fill the free space.
lvextend -l +100%FREE /dev/mapper/fedora-home 

# output:
  Size of logical volume fedora/home changed from 60.91 GiB (15594 extents) to 78.92 GiB (20203 extents).
  Logical volume home successfully resized.
Lock the pysical volume
sudo pvchange -x n /dev/mapper/crypt1 

# output:
  Physical volume "/dev/mapper/crypt1" changed
  1 physical volume changed / 0 physical volumes not changed

Resize the filesystem on the home lvm to fill the new space. Be sure that the filesystem is not mounted when you are resizing it.

Check first.
e2fsck -f /dev/mapper/fedora-home
Extend filesystem to fill all the free space on the lvm.
sudo resize2fs -p /dev/mapper/fedora-home 

# output:
resize2fs 1.42.13 (17-May-2015)
Resizing the filesystem on /dev/mapper/fedora-home to 20687872 (4k) blocks.
The filesystem on /dev/mapper/fedora-home is now 20687872 (4k) blocks long.
Check after.
e2fsck -f /dev/mapper/fedora-home

Ok, thats it. Now reboot and hopefully your data are still there ;)

2 comments:

  1. This guide is AMAZING. really, huge thanks! The nervousness you get during this whole process is scary :D

    ReplyDelete