The Linux Newbie Guide  ⇒    Fundamentals     Advanced     Supplement   Command Index   ENG⇒中
All rights reserved, please indicate the source when citing
  

Setting Up Virtual Devices with "losetup"




The losetup(loop devices setup) command is used to set up loop devices, which are virtual devices that can simulate disk space using files. In Linux, loop devices are represented by names like "/dev/loop#" (where "#" is a number), and each loop device can simulate a hard disk.

Example:
# dd if=/dev/zero of=disk-image bs=100M count=1 ←Create a 100MB image file
# losetup /dev/loop0 disk-image ←Use losetup to simulate "/dev/loop0" as a disk
# mkfs -t ext4 /dev/loop0 ←Format "/dev/loop0"
# mount /dev/loop0 /mnt ←Mount "/dev/loop0"
# umount /dev/loop0 ←Unmount
# losetup -d /dev/loop0 ←Detach "/dev/loop0" from the image file

After the second step of losetup, operating on "/dev/loop0" is just like working with a real disk. You can format, mount, and store files on it. For more information on losetup usage, operations, and examples, refer to the provided link.