Create Binary Image with Linux
To create an image file on Linux we have various options to do the job. We have commands like dd, truncate, mkfile and others. On most linux systems we have fallocate installed by default. The command fallocate is supported only on certain file systems such ext4, xfs, ocfs2 and btrfs. The best choice to create a large file on a Linux system is the command fallocate. Fallocate only allocates/reserves blocks and marking them as uninitialized without requiring I/O and CPU time. This is much faster than creating a file by filling it with zeros like the command dd does.
To make sure we have sufficient disk space to create an image file, we use the command df.
root@OSMIOM:/var/www# df -h /var
Filesystem Size Used Avail Use% Mounted on /dev/mapper/OSMIOM-var 6.0G 1.3G 4.4G 22% /var root@OSMIOM:/var/www#
We can see, that we have enough disk space to create some image files. To create an image file we use the command fallocate.
fallocate -d [-o offset] [-l length] filename
Example to create a 100 Megabyte file:
root@OSMIOM:/var/www# fallocate -l 100M myfile.img
Example to create a 1 Gigabyte file:
root@OSMIOM:/var/www# fallocate -l 1G myfile.img
Example to create a 10 Gigabyte file:
root@OSMIOM:/var/www# fallocate -l 10G myfile.img
Let’s verify our new file.:
root@OSMIOM:/var/www# ls -lh myfile.img
-rw-r--r-- 1 root www-data 1.0G Mar 22 11:05 myfile.img root@OSMIOM:/var/www#