[dm-crypt] cryptsetup

test532 at codingninjas.org test532 at codingninjas.org
Wed Sep 2 04:42:29 CEST 2009

Hi There,

I don't know anything about the internals of dmcrypt, but I was in an 
inquisitive mood, so I did some testing, and this is what I found:

I had success, and it appears that you don't have to do anything special with 
dmcrypt/cryptsetup. I did luksClose before resizing the 'partition' however, 
which is either required or at least a good idea to be safe.

Here is what I did:

1.I created a test file in my home directory:

# dd if=/dev/zero of=test bs=10000000 count=1

2. I then created a loop back device for it:

# losetup -f test

(this created /dev/loop0)

3. I then did a luksFormat on it:

# cryptsetup luksFormat /dev/loop0

4. Opened it.

# cryptsetup luksOpen /dev/loop0 test

5. formatted it

# mkfs.ext2 /dev/mapper/test

6. mounted it

# mkdir /mnt/test && mount /dev/mapper/test /mnt/test

7. filled it up

# dd if=/dev/urandom of=/mnt/test/fill

(resulting file was ~9megs, df reports 0 space left on drive)

8. md5sum on file to make sure it doesn't get corrupted from the next steps.

# md5sum /mnt/test/fill

(25d32f5e6a66e3ad7d7979b7785d7f02  fill)

9. unmounted

# umount /mnt/test

10. luksClosed it.

# cryptsetup luksClose /dev/mapper/test

11. detached the loop back device.

# losetup -d /dev/loop0

12. made the 'partition' bigger. (doubled the size of the file, by adding 
10megs of zeros to the end, not overwriting the first original 10 megs)

# dd if=/dev/zero of=test bs=10000000 seek=1 count=1

13. losetup'ed it.

# losetup -a test

14. luksOpened it, no special other luks stuff needed.

# cryptsetup luksOpen /dev/loop0 test

15. expanded the ext2 filesystem to fill the new larger size of the 

# fsck.ext2 -f /dev/mapper/test && resize2fs /dev/mapper/test

(resize wants you to first run a fsck on it)

16. Mounted it,

# mount /dev/mapper/test /mnt/test

17. Filled it some more.

# dd if=/dev/urandom of=/mnt/test/fill2

(Resulted in another ~9meg file, thus proving that the filesystem resized 

18. checksumed the first fill file again to make sure it didn't change (Ie. 
get corrupted).

# md5sum fill

(25d32f5e6a66e3ad7d7979b7785d7f02  fill)

As you can see everything worked fine.

Here are the versions of my cryptsetup and dmsetup:

cryptsetup --version
cryptsetup 1.0.6-pre1

dmsetup --version
Library version:   1.02.27 (2008-06-25)
Driver version:    4.14.0

So based upon my testing, you should be fine. It shouldn't mater if you are 
resizing a loop back device like I did to test, or are resizing a partition, 
or are resizing a logical volume (LVM). What is important is it seems a 
luksFormat does not store any info regarding the size of the blockdevice. So 
if you close, resize and open, it will take advantage of the new space. I 
cannot say about shrinking though. If you are trying to shrink, try the same 
thing I did above, but with shrinking. I would assume that shrinking is more 
risky as who knows how cryptsetup stores the data, it might put stuff at the 
end of the drive that then you would be truncating away.

As for growing, as long as you do the things in the order I did, everything 
should be fine. (Like make sure to luksClose the partition/logical_volume 
before resizing it).

Good Luck!


> can anyone say if this command is needed when resizing a partition?  What
>  if I just used resize2fs? how could it know about the change of size? 
>  Also its a shame information like this has to be a secret, no one seems to
>  know or say anything about it.
> Thanks,

More information about the dm-crypt mailing list