disk image from directory

How to Create Disk Image from Directory

Sometimes you may need to create disk image from folder or ISO file from directory to be able to mount it on other Linux servers. This is very useful for system administrators who want to use a folder of maintenance scripts on different machines. In this article, we will look at how to create disk image from directory. There are many ways to do this and we will look at a couple of them.


How to Create Disk Image from Directory

Here are a couple of ways to create disk image from directory.


1. Using third-party tools

There are many third-party tools such as genisoimage and mkisof built for this purpose. Open terminal and run the following command to install genisoimage.

Ubuntu/Debian

sudo apt-get update -y
sudo apt-get install -y genisoimage

CentOS/Redhat/Fedora

sudo yum install wget -y
sudo wget http://mirror.centos.org/centos/7/os/x86_64/Packages/genisoimage-1.1.11-25.el7.x86_64.rpm
sudo yum install genisoimage-1.1.11-25.el7.x86_64.rpm -y

Once genisoimage is installed, you can create ISO image from directory with the following command.

$ sudo genisoimage -o file_name.iso /path/to/directory

Here is an example

$ sudo genisoimage -o project.iso /home/ubuntu/project

Similarly, you can also use mkisofs command to create ISO image from directory.

$ sudo mkisofs -o file_name.iso /path/to/directory


2. Without Using third-party tools

If you don’t want to use third-party tools then here are set of commands to create image file from directory.

Create folder for disk image

$ sudo mkdir image_folder
$ cd image_folder

Create image file to hold the folder. We will create a file image.ext3 of 750 mb.

dd if=/dev/zero of=image.ext3 bs=1024 count=768000

Use losetup command to setup loop0 device and attach it to above file.

sudo losetup /dev/loop0 image.ext3

Format that loop0 device as ext3

sudo mkfs.ext3 /dev/loop0

Mount it /mnt

sudo mount /dev/loop0 /mnt

Copy files from your directory which you want to use to create image file. For e.g. /home/data

cp -a /home/data/* /mnt

Unmount the device once files are copied.

sudo unmount /mnt

Detach loop0 device.

sudo losetup -d /dev/loop0

Now your disk image will be available at image.ext3.

In this article, we have looked at a couple of ways to create disk image from directory. If you are not familiar with Linux file systems, then it is advisable to use third-party tools like genisoimage or mkisofs for this purpose.

Also read :

How to Mount Drive from Terminal
Shell Script to Concatenate Strings
How to Run Shell Script in Background
How to Mount ISO Files in Ubuntu
How to Create Empty Disk Image in Linux

Leave a Reply

Your email address will not be published. Required fields are marked *