Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use the common command losetup in Linux

2025-04-05 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Development >

Share

Shulou(Shulou.com)06/01 Report--

Editor to share with you how to use the Linux command losetup. I hope you will get something after reading this article. Let's discuss it together.

Linux common commands losetup commands are used to set up loop devices, which virtualize files into block devices to simulate the entire file system, so that users can treat them as hard disk drives, optical drives, floppy drives, etc., and mount them as directories

Syntax losetup [- d] [- e] [- o] [loop device code] [file] parameter:

-d remove the equipment.

-e start encryption coding.

-o sets the number of data translations.

Instance (1) creates an empty disk image file, where a 1.44m floppy disk is created.

$dd if=/dev/zero of=floppy.img bs=512 count=2880 (2) use losetup to virtualize disk image files into block devices

$losetup / dev/loop1 floppy.img (3) Mount block devices

After going through the above three steps, $mount / dev/loop0 / tmp, we can access the disk image file floppy.img through the / tmp directory as if it were a real block device.

(4) Uninstall the loop device

$umount / tmp $losetup-d / dev/loop1 A complete test example

\ 1. First create an empty file with a size of 1G:

# 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. Format the file into ext4 format:

# mkfs.ext4 loopfile.img . \ 3. Use the file command to view the formatted file type:

# 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 files:

The-o loop option of the # mkdir / mnt/loopback # mount-o loop loopfile.img / mnt/loopbackmount command allows you to 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 so in fact, mount-o loop already mounts files and / dev/loop0 internally by default.

However, the first method (mount-o loop) is not suitable for all scenarios. For example, if we want to create a hard disk file, partition the file, and then mount one of the subpartitions, we can't use the-o loop method. Therefore, the following must be done:

# losetup / dev/loop1 loopfile.img # fdisk / dev/loop1\ 6. Uninstall the mount point:

# umount / mnt/loopback after reading this article, I believe you have some understanding of "how to use Linux commands losetup". If you want to know more about it, you are welcome to follow the industry information channel. Thank you for reading!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Development

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report