How to increase the size of a LUKS file-container

There are many tips on how to resize (increase) a LUKS2 encrypted device / partition / LVM volume. But how to increase the size of the LUKS container created in the file?

I once created:

dd if=/dev/random of=/some file bs=1M count=100

cryptsetup luksFormat /some-file

cryptsetup luksOpen /some-file some-mount

mkfs.ext4 /dev/mapper/some-mount

Now this container has run out of space and I need to increase its size. How to do it?

Asked By: DarekH


You can just set a new file size with truncate, then cryptsetup resize and resize2fs.

For example, setting it to 200M:

truncate -s 200M cryptfile.img

Alternatively, if you prefer dd or other tools, you can just append another 100M of random data:

head -c 100M /dev/urandom >> cryptfile.img

Warning: if you truncate to a too small size, or if you typo and use > instead of >>, your data would be lost. dd in particular likes to truncate files to 0 bytes by default, and it’s easy to make mistakes, so I don’t really recommend using it here.

The difference between truncate and appending random data is that truncate makes sparse files (zero, unallocated) while appending properly allocates space and initializes it with random data.

Then you can online resize the mapping (this might ask for your passphrase again):

cryptsetup resize cryptname

This would also happen automatically next time you cryptsetup open the container normally.

At this point the encrypted block device (loop device, image file) should be properly resized to 200M (minus 16M or whatever is the size of your LUKS header).

That leaves the filesystem:

resize2fs /dev/mapper/cryptname
Answered By: frostschutz