Softpanorama

May the source be with you, but remember the KISS principle ;-)
Home Switchboard Unix Administration Red Hat TCP/IP Networks Neoliberalism Toxic Managers
(slightly skeptical) Educational society promoting "Back to basics" movement against IT overcomplexity and  bastardization of classic Unix

Linux Loopback filesystem

News

Recommended Books

Recommended Links Grub Linux filesystems Mounting Linux filesystems
Linux Swap filesystem Swap file Filesystem mount options /etc/fstab/  ext2 attributes Ext3 filesystem
udev Partition labels Managing Disks by UUID Linux tmpfs    
The Linux Logical Volume Manager Linux Logical Volume Snapshots Linux Disk Partitioning   Humor Etc

With this handy tool, you can mount image of DD backup of the partition or ISO file and operate it as if they are "real" filesystems only mounted read-only. In case of recovery of data form damaged filesystem operating with DD image leave your "real" file system alone and safe. It is most often is used for mounting ISO files.

To create an empty filesystem you can use the dd command to create a file. for example

dd if=/dev/zero of=/tmp/test-img bs=1024 count=10000

The shell responds with

10000+0 records in
10000+0 records out

Now we need to make the system think the file is a block device instead of an ASCII file, so we use losetup, a utility that associates loop devices with regular files or block devices. You will use the loopback device /dev/loop0. losetup /dev/loop0 /tmp/test-img.

Then format the file with an ext3 file system:

mkfs -t ext3 -q /tmp/test-img

If prompted that test.img is not a block device, enter y to proceed anyway.

Create a mount point:

mkdir /mnt/image

Now we can mount it:

mount -o loop /tmp/test.img /mnt/image

After mounting the file system, look at it with the df command:

df -h /mnt/image

And get this response:

Filesystem        Size    Used    Avail    Use%    Mounted on
/tmp/test-img     10M     1.1M    9M       2%      /mnt/image

To unmount the image:

umount /mnt/image

You can even back up the image, in case something happens while you're playing:

cp /tmp/test-img test-img.bak

When you've confirmed that you have a mounted image file, you can create directories, copy files to it, delete files, attempt to recover them, and, generally speaking, do anything you want with this file system. It's a playpen where you can learn valuable lessons with no risk. If you somehow irreparably damage the file system on the image, unmount it, delete it, and start over, perhaps with that backup you just made. So have fun!

Let's now discuss how to mount a read-only partition on a running system.

Mounting a Read-Only Partition on a Running System

From time to time, you may need to mount a partition in your file system in such a manner that you can view the data that it contains but not be able to make changes to it. This can be accomplished by mounting the partition in read-only mode. You can do this in two ways. The first is with the mount command. At the shell prompt, enter

mount -r device mount_point

For example, if you had a partition at /dev/sda3 and you wanted to mount it at the /extra directory, you would enter the following:

mount -r /dev/sda3 /extra

Notice that the mounting for /dev/sda3 is shown with a (ro) designation, indicating that the partition is mounted read-only.

In addition to using mount, you can automatically mount the partition read-only using your fstab file. Simply add ro to the mount options field

Recommended Links

Loop device - Wikipedia, the free encyclopedia