English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
The Linux losetup command is used to set up loop devices.
Loop devices can virtualize files into block devices, simulating the entire file system, allowing users to treat them as hard disk drives, CD-ROMs, or floppy drives, and mount them as directories for use.
losetup [-d][-e <encryption method>][-o <shift number>][loop device code][file]
Parameters:
(1)Create an empty disk image file, here create a1.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 block device
$ mount /dev/# mount /tmp
After the above three steps, we can access the disk image file through/tmp directory, access the disk image file floppy.img as if it were a real disk device.
(4)Unmount loop device
$ umount /tmp $ losetup -a0 /dev/loop1
a complete test example
1. First create a 1G size empty file:
# dd if=/dev/zero of=loopfile.img bs=1G count=1 1+0 records in 1+0 records out 1073741824 bytes (1.1 GB) copied, 69.3471 s, 15.5 MB/s
2. Format the file as ext4 格式:
# mkfs.ext4 loopfile.img Format:
3# mkfs.ext
。。。。 . View the file type after formatting with the file command: 1# file loopfile.img4 loopfile.img: Linux rev9.0 ext4filesystem data, UUID=a-6653-4407-dfb5-7044a092ae01159 d
4c
(extents) (large files) (huge files) /mnt/loopback loop0 loopfile.img -. Prepare to mount the file above: /mnt/loopback
# mkdir -The o loop option of the mount command
o loop option can mount any loopback file system.
# losetup /dev/The above mount command is equivalent to the following two commands: loop0 loopfile.img /dev/# mount /mnt/loopback
loop0 -Therefore, actually, mount /dev/o loop has internally already default to file and
loop0 has been mounted. -However, for the first method (mount -o loop This method is not applicable to all scenarios. For example, if we want to create a hard disk file, then partition the file, and then mount one of the subpartitions, we cannot use
# losetup /dev/loop1 loopfile.img # fdisk /dev/loop1
6. Unmount Mount Point:
# umount /mnt/loopback