创建文件系统并挂载

  1. 在上一步创建的 /dev/loop0p1 分区上创建 ext4 文件系统。
# mkfs.ext4 /dev/loop0p1
mke2fs 1.42.9 (28-Dec-2013)
Discarding device blocks: done                            
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
128016 inodes, 512000 blocks
25600 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=34078720
63 block groups
8192 blocks per group, 8192 fragments per group
2032 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
  1. 将文件系统挂载到所需目录。
# mkdir /loopfs
# mount -o loop /dev/loop0p1 /loopfs
  1. 验证挂载点的大小和文件系统类型。
# df -hP  /loopfs
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop1      477M  2.3M  445M   1% /loopfs
# mount | grep loopfs
/dev/loop0p1 on /loopfs type ext4 (rw,relatime,seclabel,data=ordered)
如何在环回设备内创建分区

在大多数情况下,我们只需使用“losetup”创建一个环回设备并使用“-o loopback”选项安装它。
但是如果要创建loopback文件,要对其进行分区,最后挂载一个子分区,这个选项就不能用了。
让我们看看如何在环回镜像中创建分区。

创建环回设备(loopback device)

  1. 首先使用“dd”命令创建一个大小约为 1GB 的文件。
# dd if=/dev/zero of=loopbackfile.img bs=100M count=10
10+0 records in
10+0 records out
1048576000 bytes (1.0 GB) copied, 1.26748 s, 827 MB/s
  1. 在上一步创建的文件之上创建一个环回设备。
# losetup -fP loopbackfile.img
  1. 要打印使用上述命令生成的循环设备,请使用“losetup -a”。
# losetup -a
/dev/loop0: [64769]:4199216 (/root/loopbackfile.img)
on  it road.com

使用 fdisk 在环回镜像内创建分区

  1. 使用 fdisk 命令在环回设备 /dev/loop0 上创建分区。
    创建大小为 500MB 的主分区,如下所示。
# fdisk /dev/loop0
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x4d455ea1.
Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-2047999, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2047999, default 2047999): +500M
Partition 1 of type Linux and of size 500 MiB is set
  1. 保存分区表并退出 fdisk 实用程序。
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
日期:2020-09-17 00:13:27 来源:oir作者:oir