Linux循环设备/dev/loop

  在进行某些功能测试的时候,有时候需要新建一些分区等,可以通过loop伪设备来实现循环挂载,循环设备与现存文件系统上的文件相关联。这种关联将提供给用户一个应用程序接口,接口将允许文件视为块特殊文件使用。即文件中包含一个完整的文件系统,那么这个文件就能如同磁盘设备被挂载。


root@DevOps:/root # tldr losetup

  losetup

  Set up and control loop devices.
  More information: https://manned.org/losetup.

  - List loop devices with detailed info:
    losetup -a

  - Attach a file to a given loop device:
    sudo losetup /dev/loop /path/to/file

  - Attach a file to a new free loop device and scan the device for partitions:
    sudo losetup --show --partscan -f /path/to/file

  - Attach a file to a read-only loop device:
    sudo losetup --read-only /dev/loop /path/to/file

  - Detach all loop devices:
    sudo losetup -D

  - Detach a given loop device:
    sudo losetup -d /dev/loop

root@DevOps:/root # tldr dd
  dd
  Convert and copy a file.
  More information: https://www.gnu.org/software/coreutils/dd.

  - Make a bootable USB drive from an isohybrid file (such like `archlinux-xxx.iso`) and show the progress:
    dd if=file.iso of=/dev/usb_drive status=progress

  - Clone a drive to another drive with 4 MiB block, ignore error and show progress:
    dd if=/dev/source_drive of=/dev/dest_drive bs=4M conv=noerror status=progress

  - Generate a file of 100 random bytes by using kernel random driver:
    dd if=/dev/urandom of=random_file bs=100 count=1

  - Benchmark the write performance of a disk:
    dd if=/dev/zero of=file_1GB bs=1024 count=1000000

  - Generate a system backup into an IMG file and show the progress:
    dd if=/dev/drive_device of=path/to/file.img status=progress

  - Restore a drive from an IMG file and show the progress:
    dd if=path/to/file.img of=/dev/drive_device status=progress

  - Check progress of an ongoing dd operation (Run this command from another shell):
    kill -USR1 $(pgrep ^dd)

root@DevOps:/root # dd if=/dev/zero of=/data/.ponfey.ext4 bs=1M count=2048 # 创建一个镜像文件,大小为2G,开始之前可以通过 losetup -a 查询列出镜像文件
root@DevOps:/root # mkfs.ext4 /data/.ponfey.ext4 # 对镜像文件进行ext4格式化
root@DevOps:/root # mount /data/.ponfey.ext4 /data/tmp/ponfey 

root@DevOps:/root # umount /dev/loop0
root@DevOps:/root # dd if=/dev/zero of=/data/.ponfey.ext4 bs=1M conv=notrunc oflag=append count=2048 # 对镜像文件进行扩容
root@DevOps:/root # losetup -c /dev/loop0
root@DevOps:/root # resize2fs -p /dev/loop0 4G # 调整loop配置大小

root@DevOps:/root # umount /dev/loop0
root@DevOps:/root # resize2fs -p /dev/loop0 4G 
root@DevOps:/root # mount /data/.ponfey.ext4 /data/tmp/ponfey 

root@DevOps:/root # umount /dev/loop0
root@DevOps:/root # e2fsck -f /data/.ponfey.ext4
root@DevOps:/root # resize2fs /data/.ponfey.ext4 1G
root@DevOps:/root # ll -h /data/.ponfey.ext4 # 可通过 dd if=/dev/zero of=/data/.ponfey.ext4 bs=1M seek=1024 count=0 进行缩减
上一篇
下一篇
/*
Theme Argon
*/