How to use the loop device on Unix?
English · Français
January 21, 2012
Antre du Tryphon

How to use the loop device on Unix?

January 21, 2012 · ctheroux Ubuntu 11.10 Unix Ubuntu 11.04

The loop device on Unix lets access a file as a block device.  Disks are block devices.  So, it is possible to use a file as a disk.  The first step, create a file that will be used as a disk.

dd if=/dev/zero bs=1M count=100 of=/tmp/mydiskfile

Format your file as a file system.  If /dev/loop0 is already used, use /dev/loop1.

losetup /dev/loop0 /tmp/mydiskfile
mkfs.ext4 /dev/loop0
losetup -d /dev/loop0

losetup -d /dev/loop0 deletes the loop device

mount -o loop /tmp/mydiskfile /mnt

The file /tmp/mydiskfile  can now be used as a disk with /mnt as the mount point!


Leave a Comment

Replying to
Your email address will not be published.

Copyright 2012–2026, Claude "Tryphon" Théroux