2

I can create a regular file on an existing file-system (say, ext4), make it into a block device (via losetup), and format it with an ext4 file-system of its own.

But the above hard-disk is of a fixed size.

Question:

  1. VirtualBox allows you to create VDI, VMDK, and VHD types of hard-disks that can be either fixed- or dynamically-sized. Is there any way to make a file-based dynamically-sized hard-disk for use by a Linux host OS?

  2. Is it possible to employ any of these three types of file-based, dynamically-sized hard-disks outside of VirtualBox, on a Linux host OS?

I'm on Ubuntu 16.04.

  • There are tools to resize the partitions but I don't get how you plan to leave parts of the hard drive unallocated. This makes much more sense on a VM. – Julie Pelletier Jul 4 '16 at 2:57
  • What does "employ" mean in this context? – Deltik Jul 4 '16 at 4:07
  • @Deltik By "employ" I mean, using any of those file-based hard-disk formats on the host OS without VirtualBox. Eg, if I mount this file-based hdisk on my host OS, and if a host program (eg, cat) writes to a file sitting in this hdisk, then this hdd should automatically expand if needed. – Harry Jul 4 '16 at 5:18
  • If you mean writing to a mount of a partition in a raw image and the raw image was originally created sparsely, then yes, it will expand as data is written to the raw image. – Deltik Jul 4 '16 at 5:22
  • @Deltik Great! Then, is using qemu-img necessary? I think, the key point in your answer is sparse files, which I did not know ext4 supported. I could create a sparse file, and use losetup and mke2fs, right? – Harry Jul 4 '16 at 5:26
5
  1. Yes, if your filesystem supports sparse files. (ext4 supports sparse files.)

    Here's how you create a 64GiB raw image disk.img that initially takes up almost no space:

    truncate -s 64G disk.img
    
  2. You can now use the disk.img as a loopback block device, and it will expand as you write to it.


Bonus Features

If you want to convert a disk image disk.img to another format, like to a physical disk /dev/sdb, use qemu-img:

qemu-img convert -O raw disk.img /dev/sdb

This is useful if you want to convert VDI, VMDK, VHD, or other container formats into RAW so that you can manipulate the output image using built-in tools like losetup/kpartx.

It doesn't matter what format the source image is. qemu-img can work with more than VDI, VMDK, VHD, and raw images. To see a list of supported images, run this command:

    root@node51 [~]# qemu-img --help | grep 'Supported formats:'
    Supported formats: bochs vvfat rbd vpc parallels tftp ftp ftps raw https qcow http dmg qcow2 quorum null-aio cloop vdi null-co vhdx blkverify file vmdk host_cdrom blkdebug host_device sheepdog iscsi qed nbd
  • +1 for truncate. The name of the command doesn't do full justice to it as it can expand the thing as well, and that too, sparsely! – Harry Jul 4 '16 at 5:30

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.