IOTTMCO

Intuitively Obvious to the Most Casual Observer

Resizing a LUKS-encrypted partition

Since gparted does not support encryption, the resizing process for LUKS-encrypted partitions has to be completely manual. It looks scarier than it is, since the sizes of the partition, the encrypted container, and the file-system will be mismatched during the resize. I’ll assume the goal is to enlarge the partition - if shrinking it, the steps should be reversed. The rule is that no object may be larger than it’s enclosing object. For example, the file-system may not be larger than the LUKS container.

For btrfs file-systems, it may be possible to complete all three steps even with the partition in use (so, no live-boot necessary); however, I wouldn’t recommend trying.

The first step, enlarging the partition, really just means changing the MBR or GPT data, without touching any actual data. Assuming GPT, use gdisk to first make a backup.

[root@localhost ~]# gdisk
GPT fdisk (gdisk) version 0.8.5

Type device filename, or press <Enter> to exit: /dev/sda
Partition table scan:
  MBR: protective
  BSD: not present
  APM: not present
  GPT: present

Found valid GPT with protective MBR; using GPT.

Command (? for help): b
Enter backup filename to save: gpt.backup
The operation has completed successfully.

Then delete the old partition and make a new one starting in the same place.

Command (? for help): d
Partition number (1-5): 2

Command (? for help): n
Partition number (2-128, default 2): 
First sector (34-1465149134, default = 8390656) or {+-}size{KMGTP}: 
Last sector (8390656-1037330431, default = 1037330431) or {+-}size{KMGTP}: +256G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 
Changed type of partition to 'Linux filesystem'

Although the data has been changed on the disk, the operating system hasn’t been informed, so we’ll poke it with partprobe (which is in the GNU Parted distribution):

[root@localhost ~]# partprobe /dev/sda

The next step is to enlarge the LUKS container. First you need to open it, and then run the resize command.

[root@localhost ~]# cryptsetup luksOpen /dev/sda2 root
...
[root@localhose ~]# cryptsetup resize root

If you want to resize to a specific size (necessary if shrinking), then use --size. The default is to use the size of the underlying block device, which we set with gdisk.

The last step is to resize the file-system itself. For ext2 and derivatives, use resize2fs on the block device. For btrfs, first mount the filesystem.

[root@localhost ~]# mount /dev/sda2 /mnt
[root@localhost ~]# btrfs filesystem resize max /mnt

And it’s done. Hopefully LUKS functionality will be added to gparted soon.