Images can serve as file storage resources, LiveCD, LiveUSB, snapshots. It can be mounted, encrypted and the filesystem can be created on it. This article is about how to create iso, img images. We can install any filesystem on the images, therefore in this article as an example we will install ext2, ISO 9660 CD-ROM filesystems on the images.
Creating img with specific filesystem on it
First we need to create an empty img file. Many internet resources are advised to use /dev/zero but it takes a lot of time for large files. Therefore, we used a fast method that is able to create large files in a second using /dev/null:
dd if=/dev/null of=./image.img bs=100M count=1 seek=1 ls -lh image.img ... 100M image.img
This command creates a file with 100 megabytes in size. We can use any desired size. If seek=1 option is not used then the file will be empty, 0 bytes.
We can now check the file with file command:
file image.img ... image.img: data
By now, it is a unspecific file with random data. Therefore, we need to create a filesystem that will organize files on it. For example, let’s create ext2 filesystem on our img image:
sudo mkfs.ext2 ./image.img
Then check it with file utility:
file image.img ... image.img: Linux rev 1.0 ext2 filesystem data
Now it can be mounted and used to store files. We can make chmod 777 command then each user will be able to write files to this image:
sudo mount -t ext2 -o loop ./image.img /media/img/ sudo chmod 777 /media/img
Now we can copy files to our img image, for example a music directory:
cp -r ~/Music/ /media/img/Music
We can view the image state by the following command:
df -h /media/img/ ... Filesystem Size Used Avail Use% Mounted on /dev/loop0 97M 1.6M 91M 2% /media/img
When we are done with the img image, we can safely unmount it:
sudo umount /media/img
Now this image contains all the files we saved on it. But in fact, img images occupy a more important role. Snapshots, LiveUSB, data encryption… And this is an incomplete list of what it can be used for.
Creating ISO 9660 CD-ROM filesystem
This is a read-only format. Once the iso file is created, it will be read-only when mounted. VirtualBox supports this format without additional plugins. Therefore, it can also be used to transfer files from the host system to virtual machines.
First, we need to install a simple commandline utility:
sudo apt install --no-install-recommends genisoimage
It contains a couple of unnecessary dependencies, so I recommend installing it with the command above.
Now the program is installed and we can create an iso image. After -o option specify your path where to put iso image. Then specify the path to the files and subdirectories which must to be written to the image:
genisoimage -r -D -o ~/Images/songs_on_cd.iso ~/Music/genre/
In simple terms, -r option makes all files readable on client side and will delete all unnecessary owner bits from the files. And -D option means to pack directories in the way we see them. When the directory is located deeply, relocation is used on this file system, with -D option we can disable it. The man page says it works without errors in most cases.
Now we can check iso image with file utility:
file songs_on_cd.iso ... songs_on_cd.iso: ISO 9660 CD-ROM filesystem data 'CDROM'
Now it can be mounted like this (in Linux):
sudo mount -o loop ./songs_on_cd.iso /media/cdrom/
And in this way we can mount it in FreeBSD:
sudo mount -t cd9660 ./songs_on_cd.iso /mnt/cdroom
If we use iso image as an optical disk in Virtual Box it must be mounted from a disk device (FreeBSD way):
sudo mount -t cd9660 /dev/cd0 /mnt/cdroom