5

I found a very nice tutorial on how to create virtual hard disks, and I am considering using these for my work in order to store reliably and portably large datasets with associated processing results.

Basically, the tutorial consist in doing this:

dd if=/dev/zero of=MyDrive.vhd bs=1M count=500
mkfs -t ext3 MyDrive.vhd
mount -t auto -o loop MyDrive.vhd /some/user/folder

which creates a virtual hard drive of 500MB formatted in ext3 and mounts it somewhere.

Now say I use that file and realise I need more than 500MB, is there a way of "dynamically" resizing the virtual disk? (By dynamically I mean other than creating a new bigger disk and copy the data over.)

3 Answers 3

12

There is better alternative than dd to extend a file. The dd command requires several parameters to run properly (to not corrupt your data). I use truncate instead. Despite of its name it can extend the size of a file as well:

truncate - shrink or extend the size of a file to the specified size

-s, --size=SIZE

set or adjust the file size by SIZE

SIZE is an integer and optional unit (example: 10M is 10*1024*1024). Units > are K, M, G, T, P, E, Z, Y (powers of 1024) or KB, MB, ... (powers of 1000).

SIZE may also be prefixed by one of the following modifying characters: '+' extend by, '-' reduce by, '<' at most, '>' at least, '/' round down to multiple of, '%' round up to multiple of.

thus,

truncate -s +1G MyDrive.vhd

safely expands your file by 1 gigabyte. And, yes, it does sparse expansion when supported by the underlying filesystem, so the actual blocks would be allocated on demand.

When a file is expanded, don't forget to run resize2fs:

resize2fs MyDrive.vhd

Also, the whole thing may be done online (without umount'ing the device) for file systems that support online resize:

losetup -c loopdev

updates in-kernel information on a backing file,

and

 resize2fs loopdev

resizes the mounted file system online

5

Double the size:

umount /some/user/folder
dd if=/dev/zero bs=1M count=500 seek=500 >>MyDrive.vhd
resize2fs MyDrive.vhd
4
  • Sorry if this was a silly question, I just looked up resize2fs and that's exactly what I was looking for!
    – Jonathan H
    Sep 8, 2016 at 12:38
  • I'm just thinking, because it seems necessary to provide the block-size in this command, is there anyway to determine what's the block size? Say if I wasn't the one to create the disk or this information is not available.
    – Jonathan H
    Sep 8, 2016 at 12:41
  • 1
    what blocksize dd use doesn't affect the blocksize of the created ext3 fs. It just append 500M to the file, you can also use bs=1 count=500M or bs=50 count=10M, and so on... it's just a bunch of bytes. Sep 8, 2016 at 12:46
  • 1
    The blocksize basically only determines how dd writes the data. A larger blocksize means fewer individual writes which speeds up the process. (Up to a reasonable limit of course. If you make the block size so big that it no longer fits into free RAM, or even bigger than RAM at all, it will start to evict other stuff or even be swapped out itself.) A couple % of your free RAM are a good rule of thumb. But for only a gigabyte it probably doesn't matter. Sep 8, 2016 at 15:56
-1

$ vboxmanage modifyhd NewVirtualDisk1.vhd --resize 40000 #New size in mb, here 40000mb

1
  • There is no modifyhd subcommand.
    – Kusalananda
    Dec 24, 2022 at 1:03

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .