YouTip LogoYouTip

Linux Comm Losetup

# Linux losetup Command [![Image 3: Linux Command Manual](#) Linux Command Manual](#) The Linux losetup command is used to set up loop devices. A loop device can virtualize a file into a block device, thereby simulating an entire file system, allowing users to treat it as a hard drive, CD-ROM, or floppy drive, and mount it as a directory for use. ### Syntax losetup **Parameters**: * -d Detach the device. * -e Enable encryption encoding. * -o Set the number of data offsets. ### Examples (1) Create an empty disk image file, here creating a 1.44M floppy disk $ dd if=/dev/zero of=floppy.img bs=512 count=2880 (2) Use losetup to virtualize the disk image file into a block device $ losetup /dev/loop1 floppy.img (3) Mount the block device $ mount /dev/loop0 /tmp After the above three steps, we can access the disk image file floppy.img through the /tmp directory, just like accessing a real block device. (4) Unmount the loop device $ umount /tmp $ losetup -d /dev/loop1 A complete test example 1. First, create a 1G empty file: # dd if=/dev/zero of=loopfile.img bs=1G count=11+0 records in1+0 records out1073741824 bytes (1.1 GB) copied, 69.3471 s, 15.5 MB/s 2. Format the file to ext4 format: # mkfs.ext4 loopfile.img。。。。 3. Use the file command to check the file type after formatting: # file loopfile.img loopfile.img: Linux rev 1.0 ext4 filesystem data, UUID=a9dfb4a0-6653-4407-ae05-7044d92c1159 (extents) (large files) (huge files) 4. Prepare to mount the above file: # mkdir /mnt/loopback# mount -o loop loopfile.img /mnt/loopback The -o loop option of the mount command can mount any loopback file system. The above mount command is actually equivalent to the following two commands: # losetup /dev/loop0 loopfile.img# mount /dev/loop0 /mnt/loopback Therefore, in practice, mount -o loop internally defaults to mounting the file and /dev/loop0. However, the first method (mount -o loop) is not applicable to all scenarios. For example, if we want to create a hard drive file, then partition the file, and then mount one of the sub-partitions, we cannot use the -o loop method. Therefore, we must do the following: # losetup /dev/loop1 loopfile.img# fdisk /dev/loop1 6. Unmount the mount point: # umount /mnt/loopback [![Image 4: Linux Command Manual](#) Linux Command Manual](#)
← Linux Comm MkfsLinux Comm Fdisk β†’