Yesterday I wrote about How to create an encrypted LUKS disk image.
Today I wanted to experiment more with it. I wanted to try resizing a LUKS volume, and after one failed attempt I found a way. If you still have the disk image of yesterday’s example, just follow me. 🙂
- Safety first! Unmount, luksClose and then backup your image file.
sudo umount /dev/mapper/myluksvol1 && sudo cryptsetup luksClose myluksvol1 cp /home/shaakunthala/luksvolume1 /home/shaakunthala/luksvolume1.bak
- Now, make more room in the container.
dd if=/dev/zero of=/home/shaakunthala/luksvolume1 bs=1M count=1024 oflag=append conv=notrunc
Yesterday we created a 512 MB disk image, now let’s add one more gigabyte. Conv and oflag options tell dd not to overwrite or truncate the image but append zero data.
- Open the encrypted volume.
sudo cryptsetup luksOpen /home/shaakunthala/luksvolume1 myluksvol1
- Repair ext4 file system to make sure it’s clean and then resize to claim ‘unallocated’ 1 GB.
sudo fsck -fvy /dev/mapper/myluksvol1 sudo resize2fs /dev/mapper/myluksvol1 sudo fsck -fvy /dev/mapper/myluksvol1 # One last sanity check
- Now, if there are no errors the filesystem has successfully grown to the maximum available size of the encrypted partition.
sudo mount /dev/mapper/myluksvol1 /mnt df -h /mnt
I’m still experimenting this and next thing I would like to try out on my own is how to shrink a LUKS encrypted disk image.
Thank you for your posts on LUKS encryption. Very useful! Not overloaded with unnecessary information.
lovely! many thanks for the info!
On point 4, use instead:
cryptsetup resize myluksvol1
Then mount the luks disk:
mount /dev/mapper/myluksvol1 /mnt
and resize the FS hosted by the luks disk: (ext4 in your case):
resize2fs /dev/mapper/myluksvol1 (not sure about the syntax of this command)
or for XFS:
xfs_growfs /dev/mapper/myluksvol1
Job done.