Resizing a LUKS / DM-Crypt / cryptsetup filesystem

April 28, 2007 at 06:26 PM | categories: linux, uncategorized | View Comments

I run Gentoo linux on my laptop. I love it because it's based on a constantly updated rolling release which means I get all the latest software when I want it. One of the side benefits of running Gentoo is that I get to do all of the cool linuxy stuff before the other distros get it: Like encrypting my entire root filesystem.

I used to be running Edgy Eft on my laptop before deciding to go back to Gentoo. So I only installed Gentoo on a 20GB partition and left Edgy in a 60GB partitition. I find myself using Gentoo 100% of the time and have all but abandoned Edgy on the laptop (I have nothing against Ubuntu by the way, it's great, and I maintain several other Ubuntu machines).

So, now that I have a few hours spare time today, it was time to get rid of Edgy and resize the Gentoo filesystem to use the entire hard drive. This is usually done with a fine tool like Gparted, but since this is an encrypted filesystem it's not quite that easy.

First things first, backup the partition to another machine:

Run netcat on another machine where you want to store your backup:

nc -l -p 80000 > /path/to/backups/your_image_name.img

Now on the machine you want to backup (substitute sda1 with your partition name):

dd if=/dev/sda1 | nc hostname_of_2nd_machine 80000

This will transfer an image of the partition to the second machine at an incredibly fast speed (I love netcat!)

Resize the partition:

Boot up on your Gentoo Live CD.

Run fdisk (or your favorite clone), and jot down the start and end block of the partition you want to keep. In my case it was the first partition and it was on block 1.

Now delete all of the partitions you want to delete as well as the encrypted partition. "WTF?" I hear you say? Yes, that's right, DELETE the encrypted partition. This doesn't actually delete any data mind you, and as long as you correctly remembered the start and end block of the partition you are perfectly safe in doing this.

Now create a NEW partition with the same start block as you originally had. Make the end block anything larger than the original end block. Save your changes.

What we've done at this point is resized the partition which is only part of what we need. We now need to resize the filesystem contained inside the partition.

Now decrypt/map your encrypted partiton however you do it. In my case I do the following:

gpg --decrypt key.gpg 2>/dev/null | cryptsetup luksOpen /dev/sda1 root

Run fsck on it:

fsck -f /dev/mapper/root

And resize it with an appropriate tool (like ext2resize for ext3)

resize2fs /dev/mapper/root

resize2fs will resize the filesystem to the size of the containing partition.

And you're done. Reboot your machine and you'll have more hard drive space available.

blog comments powered by Disqus