8

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.

7
  • 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. Commented Jul 4, 2016 at 2:57
  • What does "employ" mean in this context?
    – Deltik
    Commented Jul 4, 2016 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
    Commented Jul 4, 2016 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
    Commented Jul 4, 2016 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
    Commented Jul 4, 2016 at 5:26

1 Answer 1

7
  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
13
  • +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
    Commented Jul 4, 2016 at 5:30
  • Not a dynamic. read this stackoverflow.com/a/3530654/8747573
    – acgbox
    Commented Nov 3, 2021 at 0:37
  • @ajcg: This answer does create a dynamically growing disk image from a regular file. Perhaps you are confusing "dynamically allocated" with "preallocated"?
    – Deltik
    Commented Nov 3, 2021 at 2:18
  • "So, when doing this, you will be deferring physical allocation until the file is accessed" From “Dan McAllister” on stackoverflow.com/questions/257844/…
    – acgbox
    Commented Nov 3, 2021 at 12:37
  • 1
    @ajcg: I did exactly what you said, and the sparse file still takes up only as much disk space as there are contents in it. Note that the difference of size (1.3MiB vs. 5.3MiB) is because I have compression on the source ZFS file system, which is not available on the tmpfs. The file remains sparse. There is no "gotcha" here. The sparse disk image is indeed dynamic as desired by the question.
    – Deltik
    Commented Nov 6, 2021 at 19:38

You must log in to answer this question.

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