用文件模拟成一个磁盘

Posted by Remilia Scarlet on December 13, 2022

创建模拟磁盘的文件

先创建一个大文件

dd if=/dev/zero of=disk.img bs=1G count=1 status=progress

使用fdisk格式化

fdisk disk.img

其中m为帮助,g为创建GTP分区表,n为添加一个新的分区,t为mkfs,w为保存

假设我要制作4个分区,如下所示:

Command (m for help): g
Created a new GPT disklabel (GUID: 3CA4937E-24BD-774D-B7E6-96B83866825B).

Command (m for help): n
Partition number (1-128, default 1):
First sector (2048-2097118, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-2097118, default 2097118): +200M
Created a new partition 1 of type 'Linux filesystem' and of size 200 MiB.

Command (m for help): n
Partition number (2-128, default 2):
First sector (411648-2097118, default 411648):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (411648-2097118, default 2097118): +100M
Created a new partition 2 of type 'Linux filesystem' and of size 100 MiB.

Command (m for help): n
Partition number (3-128, default 3):
First sector (616448-2097118, default 616448):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (616448-2097118, default 2097118): +200M
Created a new partition 3 of type 'Linux filesystem' and of size 200 MiB.

Command (m for help): n
Partition number (4-128, default 4):
First sector (1026048-2097118, default 1026048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (1026048-2097118, default 2097118):
Created a new partition 4 of type 'Linux filesystem' and of size 523 MiB. 

Command (m for help): t
Partition number (1-4, default 4): 1
Partition type (type L to list all types): ext4
Type of partition 1 is unchanged: Linux filesystem.


Command (m for help): t
Partition number (1-4, default 4): 2
Partition type (type L to list all types): ext4
Type of partition 2 is unchanged: Linux filesystem.


Command (m for help): t
Partition number (1-4, default 4): 3
Partition type (type L to list all types): ext4
Type of partition 3 is unchanged: Linux filesystem.


Command (m for help): t
Partition number (1-4, default 4): 4
Partition type (type L to list all types): ext4
Type of partition 4 is unchanged: Linux filesystem.   

Command (m for help): w
The partition table has been altered.
Syncing disks.

至此,已经完成了一个完整的磁盘,接下来还需要将其挂载到目录里,不然是没法使用的

挂载到loop设备并显示其分区

挂载

sudo losetup -v -f disk.img

sudo losetup -a
/dev/loop0: []: (/tmp/test/disk.img)

sudo partx --show /dev/loop0
NR   START     END SECTORS SIZE NAME UUID
1    2048  411647  409600 200M      20558895-ceba-c847-bb22-9846ef025550
2  411648  616447  204800 100M      7c09d01e-4895-7e4f-8c7e-4dcf4f34a353
3  616448 1026047  409600 200M      79678ff9-e7d8-ce41-87fe-5b5d842ea3b4
4 1026048 2097118 1071071 523M      49ad0c66-4416-5f4d-8acf-a48af3e3ca6f

sudo partx -v --add /dev/loop0
partition: none, disk: /dev/loop0, lower: 0, upper: 0
/dev/loop0: partition table type 'gpt' detected
range recount: max partno=4, lower=0, upper=0
/dev/loop0: partition #1 added
/dev/loop0: partition #2 added
/dev/loop0: partition #3 added
/dev/loop0: partition #4 added 

sudo blkid /dev/loop0*
/dev/loop0: PTUUID="3ca4937e-24bd-774d-b7e6-96b83866825b" PTTYPE="gpt"
/dev/loop0p1: PARTUUID="20558895-ceba-c847-bb22-9846ef025550"
/dev/loop0p2: PARTUUID="7c09d01e-4895-7e4f-8c7e-4dcf4f34a353"
/dev/loop0p3: PARTUUID="79678ff9-e7d8-ce41-87fe-5b5d842ea3b4"
/dev/loop0p4: PARTUUID="49ad0c66-4416-5f4d-8acf-a48af3e3ca6f"

接下来把/dev/loop0p*用mount命令挂载到/mnt目录下就行了

卸载

先确认已经卸载/mnt目录

sudo partx -v -d /dev/loop0 
sudo losetup -D

参考链接

https://dustymabe.com/2012/12/15/mounting-a-partition-within-a-disk-image/


| 访问量: