Creating a loop device for testing filesystems in Linux

Creating a loop device for testing filesystems in Linux

If you wanted to have a disk for testing different file systems, but were not able to add a disk to your system, then you might be stuck. By creating a filesystem within a file, you can use a loop mounted device, and you do not have to be stuck!

For example, you could create a 100M /var/lib/testdisk file with the following command:

dd if=/dev/zero of=/var/lib/testdisk bs=1M count=100

Then, you can set up that file as a loop device:

losetup --find # to find the next available loop device, typically /dev/loop0 as shown used below
losetup /dev/loop0 /var/lib/testdisk

Next, you can format the loop device with a filesystem:
mkfs -t ext4 /dev/loop0

Use the filesystem by creating a directory and mounting it:

mkdir /test-ext4
mount /dev/loop0 /test-ext4

In the future to mount this filesystem image, you will be able to just use the command:

mount -o loop /var/lib/testdisk /test-ext4

To make this device permanent you could add the following lines to /etc/fstab:

/var/lib/testdisk       /test-ext4              ext4    loop,nodev      0 0

No comments:

About Me - WrightRocket

My photo

I've worked with computers for over 30 years, programming, administering, using and building them from scratch.

I'm an instructor for technical computer courses, an editor and developer of training manuals, and an Android developer.