ISO files are a common way to distribute files and folders that would normally be stored on a physical disk. Unlike archive files like tar and zip files, ISO files are meant to be bootable. In this article, I’ll show you how to mount and unmount an ISO file on a Linux system so that you can access its contents without needing to burn the disc onto media like a CD or DVD.
Mounting an ISO File
Let’s say you have an existing ISO file in your directory. We want to mount it to a certain folder. The “mount” command won’t create the folder on its own. We need to create it first using a command like this:
mkdir ./isodirectory/
After the directory has been created, you can mount it using the following command:
sudo mount -o loop ./my_iso.iso ./isodirectory/
If everything goes well, you can then list the contents of the ISO file at the mounted point as shown here:
As you can see, I’ve mounted the “my_iso.iso” file to the newly created “isodirectory” mount point. After mounting it, I was able to list the files in the directory and it displayed the contents of the ISO file. It’s that easy!
Understanding the ISO Mount Parameters
The only parameter that requires an explanation is “-o loop”. The remaining parameters are self-explanatory.
The “-o” argument specifies that a list of comma-separated options will follow. In this scenario, we’re only passing a single one – loop. But we can add multiple options separated with a comma such as “rw”, “noexec”, etc.
The “loop” parameter is crucial to allow a file to be mounted to a drive. Linux uses a trick called a “loop device” that “wraps” the file into it, and makes Linux view the ISO file as a separate device that can be accessed at the mount point. Without this, mounting an ISO file won’t work.
Unmounting the ISO File
After mounting and accessing the contents of the ISO file, you can unmount it using the following command:
sudo umount ./isodirectory
As you can see below, after unmounting, the directory is empty:
It’s a straightforward process.
Why Should You Unmount an ISO File?
It’s not as if mounting an ISO file creates a permanent change in the file system. After you reboot the Linux server, the mount point will be lost and you’ll need to mount it again if you want to access the contents of the file. So why bother unmounting it in the first place? The answer is resource usage.
When you mount an ISO file in the manner described above, the system allocates a certain amount of memory and resources to keep track of the mount point. It creates a virtual device that allows the system to pretend that it’s mounting a real disk, instead of just another file.
Leaving a mounted ISO file after its usefulness can also cause trouble if you try and use the mount point for something else, or try and access it after the system reboots and your scripts still think that the mount point exists. In some cases, you might mount the file as writable, in which case unmounting it can prevent accidental modifications of data.
So after you use the mount point, unmount it just to be safe.
How to Create an ISO File
Creating an ISO file is easy if you have the right tools. Unlike other built-in Linux commands, it’s something that you specifically need to install. The one I use for Ubuntu systems is called “genisoimage”, and you can install it on Ubuntu using the following command:
sudo apt-get install genisoimage
You might need to restart your system or services to complete the installation, but once you install it, using it is easy. To start with, place all the files and folders from which you want to create an ISO into a new folder. The subsequent ISO file will match the structure of the target folder exactly.
Once installed, you can create an ISO file out of the new folder using the following command:
genisoimage -o ~/my_iso.iso -R ~/iso_files
In the above command, replace the ISO file with the name that you want to create, and specify the directory containing the files that you want to convert. And you’re done! Creating an ISO file is as simple as that.
How to Automatically Mount an ISO File on Boot
By default, a mount isn’t permanent. Even though a mount point looks and behaves like a regular folder, when the system reboots, the mount point will vanish. To make it persist, you need to specifically configure Linux to re-initiate the mount point after a reboot.
Editing the fstab File
The fstab is a crucial file in Linux, and it’s meant to define how partitions are allocated when the system boots. If we want to make our mount points permanent, we need to modify the fstab file like this.
Note: This is a dangerous operation and has the potential to brick your server if done incorrectly, particularly if you only have SSH access. So make sure that you take backups of the fstab file, and proceed carefully.
Use the following command to open the fstab file:
sudo vi /etc/fstab
You’ll see something like this:
Now to create the permanent mount point, add the following lines:
/path/to/your.iso /mnt/iso auto loop 0 0
The first parameter is the path to the ISO file, and the second parameter is the path to the destination mount point.
Save your changes and exit.
Why You Should be Cautious with Automatic Mounting
While it seems there are no downsides to automatically mounting an ISO file on system startup, it can cause problems in the future if the ISO file moves is renamed, or is unavailable for whatever reason. In addition, it can slow down the system startup and constantly uses resources. So don’t do it lightly. Mounting an ISO file isn’t hard, as you can see, so it’s a better practice to do it as and when necessary. Maybe if you want, you can create a script that you call, or even create an alias. But try and avoid modifying the fstab to do it automatically on boot.
Mounting Isn’t the Same as Booting
Some people might be confused about the word “mounting”, and conflate it with “booting”. While both these procedures access ISO files, the purposes are very different. Mounting an ISO file simply means accessing its contents by mapping it onto a folder. However, booting from an ISO file means that you want to install or repair an operating system by making the ISO file accessible to Linux when it starts up.
Booting could also mean using virtualization software allowing you to run another operating system in the context of an existing one.
So while both have their uses, they mean very different things.
Conclusion
As you can see, mounting an ISO file in Linux is easy. You can even create an ISO file for easy distribution; others can mount it to view its contents. As long as you’re careful to unmount it after use, and exercise restraint when mounting it automatically when the system boots, you should be fine!
I’m a NameHero team member, and an expert on WordPress and web hosting. I’ve been in this industry since 2008. I’ve also developed apps on Android and have written extensive tutorials on managing Linux servers. You can contact me on my website WP-Tweaks.com!
Leave a Reply