Smokes your problems, coughs fresh air.

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

  1. Raphaël Enrici

    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. halfgaar

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

  3. Raphaël Enrici

    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,
    Raphaël

  4. halfgaar

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

  5. vikki

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

  6. carltm

    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. halfgaar

    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.

© 2024 BigSmoke

Theme by Anders NorenUp ↑