Create disk image of directory?

You could use a partition, physical or virtual.

A physical partition is unwieldy and not easy to resize.

A virtual partition can for example be created as an encrypted using Veracrypt as described in How to Install and Use Veracrypt on Ubuntu 20.04.

The best solution might be to use a file as a virtual disk. You will find a detailed description in the article How to Create a Virtual HardDisk Volume Using a File in Linux. Below is a summary of the method by example.

Example: creating a VHD volume of size 1GB image

sudo dd if=/dev/zero of=VHD.img bs=1M count=1200
sudo mkfs -t ext4 /media/VHD.img
sudo mkdir /mnt/VHD/
sudo mount -t auto -o loop /media/VHD.img /mnt/VHD/

To mount at system boot, add this to /etc/fstab:

/media/VHD.img  /mnt/VHD/  ext4    defaults        0  0

ZIP -- having to zip/unzip is very inconvenient and slow. I'd like to avoid zip.

You can use the archivefs FUSE filesystem which can present ZIP archives as mountable, although they will remain read-only.

ISO -- the problem with this is that it doesn't really support a native Linux file system, right? I'd like to retain all functionality of ext4. Also not easy to add files.

You can create a disk image using any filesystem that can be put on disk. (Hence "disk image", it's an exact copy of a disk.) So if you want an image file that uses ext4, you can indeed use ext4. It will be writable as long as the filesystem supports being written to; .iso images are only read-only because of the particular filesystem that they contain, which wouldn't apply to ext4.

(It's not that ISO files are limited to containing an ISO9660 filesystem, but rather, the reason they're called "ISO files" is because they have an ISO9660 filesystem.0 There's no particular file format to an .iso file other than it being an 1:1 copy of a CD's data track from /dev/cdrom – doing the same to an ext4 partition will produce an ext4 image.

The next question is whether the OS will be able to use that image regardless of filesystem, and on Linux at least, there's nothing special about ISO9660 – a mounted disk image gets attached as a generic block device, which the kernel then scans for all supported filesystems as with any other block device.1)

So in order to create a mountable disk image containing e.g. an ext4 filesystem, all you need to do is create a blank file of the desired size2 and run mkfs.ext4 on it. Then you can mount it in the same way as an .iso file – either directly using mount foo.img /mnt, or manually via losetup, or by double-clicking the .img file and having GNOME Disks set it up.

(GNOME Disks also supports attaching LUKS-encrypted images, which can be created in mostly the same way, e.g. with cryptsetup luksFormat.)


0 (Or at least, used to be. Even though DVDs and therefore DVD images use the UDF filesystem, the .iso name just stuck around.)

1 (This is not necessarily true for other operating systems; e.g. Windows has special treatment for CDs/DVDs, and an .iso image is attached similar to a CD, so only UDF or ISO9660 will work in it. On the other hand, .vhd images on Windows 10 do work exactly like disks, though they do need to be in the VHD format that's incompatible with Linux.)

2 (Image files don't automatically grow to fit their contents. However, expanding an image is relatively easy – append some more null bytes to the file and run resize2fs.

Additionally, running fstrim on the mounted filesystem will cause that image to become sparse and only occupy as much space as the contents inside, so you can have larger-than-necessary images without actually sacrificing the disk space.)