Skip to content

Increasing (encrypted) disk image size with dd

When you have a raw disk image file, you may need to increase its size at some point. With dd, you can do this:

dd conv=notrunc oflag=append if=/dev/zero of=backups bs=1M count=1024

This will add 1GB of zero data to the image file ‘backups’. You can then use resize2fs on it to increase the file system size (that is, if you use ext2/3/4):

e2fsck (-f) backups
resize2fs backups

However, if the disk image is a volume encrypted with cryptsetup + luks, first open it:

losetup /dev/loop/0 backups
cryptsetup luksOpen /dev/loop/0 encrypted-backups

Then resize the encrypted volume:

cryptsetup resize encrypted-backups
e2fsck (-f) /dev/mapper/encrypted-backups
resize2fs /dev/mapper/encrypted-backups

And I know, I still have to add LVMs to my set of skills…


7 Comments ( Add comment / trackback )

  1. (permalink)
    Comment by Raphal Enrici
    On July 19, 2013 at 11:04

    Hi,
    thanks for the tips. However, if you use the dd as mentionned in the first line you are at risk depending on the version of dd you are using.
    conv=notrunc should be added to the command line, otherwise the image file may be overwritten (the documentation for dd is not clear enough regarding this).
    Someting like this:
    dd conv=notrunc oflag=append conv=notrunc if=/dev/zero of=backups bs=1M count=1024

    You can see a discussion concerning on debian bugtracker:
    See bug #373736 at debian.org

    Thanks!

  2. (permalink)
    Comment by halfgaar
    On July 20, 2013 at 12:23

    I don’t get it. I need to specify it twice, once before oflag and after?

  3. (permalink)
    Comment by Raphal Enrici
    On July 21, 2013 at 17:38

    Oh… It seems I was totally blind 🙂 I didn’t see it was their already. Feel free to revert my comment and delete it forever 🙂
    No reason to pollute the network and in particular the earth with all my bits of mistake.
    Thanks,
    Raphal

  4. (permalink)
    Comment by halfgaar
    On July 22, 2013 at 09:13

    I’ll do no such thing :). Your mention of that bug report is useful.

  5. (permalink)
    Comment by vikki
    On April 1, 2014 at 11:16

    can we extend it online, if the file is mounted as loopback device. any idea or suggestion would be greatly appreciated

  6. (permalink)
    Comment by carltm
    On April 4, 2014 at 03:17

    Good stuff!

    I read somewhere else that truncate is the command to use, but I discovered that

    df -h file.img
    incorrectly reported the original size while
    ls -lh file.img
    reported it correctly after using truncate.

    Next time I’ll use this dd command instead.

  7. (permalink)
    Comment by halfgaar
    On April 4, 2014 at 17:51

    du strictly measures disk usage. If you have a sparse file, you can have a file that shows up in ls as 10 GB, and only take a few kB of actual disk space.