创建磁盘映像。以下命令将创建一个10G 稀疏映像:
# dd if=/dev/zero of=mydisk.img bs=1 count=0 seek=10G
0+0 records in
0+0 records out
0 bytes (0 B) copied, 1.6554e-05 s, 0.0 kB/s
# ls -lh mydisk.img
-rw-r--r--. 1 root root 10G Jun 17 15:27 mydisk.img
用fdisk分区映像:
# fdisk mydisk.img
确保您创建的分区至少与您所成像的分区一样大!对于此示例,我创建了以下布局:
# fdisk -l mydisk.img
Disk mydisk.img: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders, total 20971520 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x5519250f
Device Boot Start End Blocks Id System
mydisk.img1 2048 2099199 1048576 83 Linux
mydisk.img2 2099200 4196351 1048576 83 Linux
使用kpartx
创建对应的分区中的每一个设备:
# kpartx -av mydisk.img
add map loop0p1 (253:3): 0 2097152 linear /dev/loop0 2048
add map loop0p2 (253:4): 0 2097152 linear /dev/loop0 2099200
这将在/dev/mapper
以下位置创建条目:
# ls -l /dev/mapper
lrwxrwxrwx. 1 root root 7 Jun 17 15:33 loop0p1 -> ../dm-3
lrwxrwxrwx. 1 root root 7 Jun 17 15:33 loop0p2 -> ../dm-4
现在,您可以将分区映像复制到分区上:
# dd if=image_of_partition_1.img of=/dev/mapper/loop0p1 bs=1M
# dd if=image_of_partition_2.img of=/dev/mapper/loop0p2 bs=1M
现在删除设备映射:
# kpartx -dv mydisk.img
del devmap : loop0p2
del devmap : loop0p1
loop deleted : /dev/loop0
一切准备就绪!
笔记