在 Centos 7.7 上我运行命令:
# dd if=/dev/zero of=/root/500MB.img bs=1M count=500
500+0 records in
500+0 records out
524288000 bytes (524 MB) copied, 0.374844 s, 1.4 GB/s
# losetup --all
# losetup /dev/loop0 ./500MB.img
# losetup --all
/dev/loop0: [64768]:33575036 (/root/500MB.img)
# fdisk -l /dev/loop0
Disk /dev/loop0: 524 MB, 524288000 bytes, 1024000 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
# fdisk /dev/loop0
[here set partition]
# fdisk -l /dev/loop0
Disk /dev/loop0: 524 MB, 524288000 bytes, 1024000 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 label type: dos
Disk identifier: 0x2b7a6a60
Device Boot Start End Blocks Id System
/dev/loop0p1 2048 1023999 510976 83 Linux
#
但是系统中不存在 /dev/loop0p1:
# pvcreate /dev/loop0p1
Device /dev/loop0p1 not found.
# ls -ld /dev/loop0p1
ls: cannot access /dev/loop0p1: No such file or directory
# kpartx -l /dev/loop0
loop0p1 : 0 1021952 /dev/loop0 2048
# ls -ld /dev/loop0p1
ls: cannot access /dev/loop0p1: No such file or directory
仅存在循环磁盘设备:
# ls -ld /dev/loop0
brw-rw----. 1 root disk 7, 0 Apr 23 09:31 /dev/loop0
“fdisk -l”上显示的分区在哪里?
更新:由于斯蒂芬·基特的提示已修复。运行所有步骤来修复:
# losetup -d /dev/loop0
# losetup --all
# ls /dev/loop0
/dev/loop0
# fdisk -l /dev/loop0
fdisk: cannot open /dev/loop0: Inappropriate ioctl for device
# fdisk -l ./500MB.img
Disk ./500MB.img: 524 MB, 524288000 bytes, 1024000 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 label type: dos
Disk identifier: 0x2b7a6a60
Device Boot Start End Blocks Id System
./500MB.img1 2048 1023999 510976 83 Linux
# losetup -P /dev/loop0 ./500MB.img
# losetup --all
/dev/loop0: [64768]:33575036 (/root/500MB.img)
# ls -ld /dev/loop0*
brw-rw----. 1 root disk 7, 0 Apr 23 10:16 /dev/loop0
brw-rw----. 1 root disk 259, 0 Apr 23 10:16 /dev/loop0p1
#
答案1
你需要告诉内核扫描分区;一种方法是使用losetup
,假设您的分区表是在安装设备之前写入的:
fdisk
在你的图像上运行fdisk -l
检查图像上的分区表- 将
-P
选项添加到losetup
:losetup -P /dev/loop0 ./500MB.img
最后一步将扫描循环设备中的分区,您将看到/dev/loop0p1
.