linux 挂载img文件 的方法
一,有些文件可以直接挂载,不是启动文件,只是img文件,
losetup /dev/loop1 zenith.img
kpartx -av /dev/loop1
mount /dev/loop1 /mnt/zenith/
二,如果是虚机的系统磁盘,直接挂载可能会有问题,选择挂载对应的lv
# fdisk -ul xxx.img
Disk 3059.img: 4294 MB, 4294967296 bytes
255 heads, 63 sectors /track , 522 cylinders, total 8388608 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: 0x000bdbdc
Device Boot Start End Blocks Id System
3059.img1 * 63 7903979 3951958+ 83 Linux
3059.img2 7903980 8385929 240975 5 Extended
3059.img5 7904043 8385929 240943+ 82 Linux swap / Solaris
从上面的信息可以看到,从63开始,其中lvm的磁盘从7904043开始。
由这条信息可以的出,扇区大小为512
Units = sectors of 1 * 512 = 512 bytes
所以我们需要从512*63出开始挂载
最后执行:
(sudo) mount -o loop,offset=32256 xxx.img /mnt/xxx……
# ls /mnt/
config-2.6.32-279.el6.x86_64 initramfs-2.6.32-279.el6.x86_64.img System.map-2.6.32-279.el6.x86_64
efi lost+found vmlinuz-2.6.32-279.el6.x86_64
grub symvers-2.6.32-279.el6.x86_64.gz
这样挂载了第一个磁盘,可能不是我们想要的系统。所以应该挂载下面的lvm
[root@localhost file]# fdisk -lu centos.img
You must set cylinders.
You can do this from the extra functions menu.
Disk centos.img: 0 MB, 0 bytes
255 heads, 63 sectors/track, 0 cylinders, total 0 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: 0x0001905c
Device Boot Start End Blocks Id System
centos.img1 * 2048 1026047 512000 83 Linux
Partition 1 does not end on cylinder boundary.
centos.img2 1026048 62914559 30944256 8e Linux LVM
Partition 2 has different physical/logical endings:
phys=(1023, 254, 63) logical=(3916, 63, 51)
[root@localhost file]# echo $((1026048*512))
525336576
[root@localhost file]# losetup /dev/loop3 centos.img -o 525336576
[root@localhost file]# pvscan
PV /dev/mapper/loop0p2 VG VolGroup lvm2 [29.51 GiB / 0 free]
Total: 1 [29.51 GiB] / in use: 1 [29.51 GiB] / in no VG: 0 [0 ]
[root@localhost file]# vgchange -ay VolGroup
2 logical volume(s) in volume group "VolGroup" now active
[root@localhost file]# lvs
LV VG Attr LSize Pool Origin Data% Move Log Cpy%Sync Convert
lv_root VolGroup -wi-a----- 27.54g
lv_swap VolGroup -wi-a----- 1.97g
[root@localhost file]# mount /dev/VolGroup/lv_root /mnt/
[root@localhost file]# ls /mnt/
bin dev home lib64 media opt root selinux sys usr
boot etc lib lost+found mnt proc sbin srv tmp var
这样就挂载到了对应系统的LVM 然后就可以修改系统文件了
上例中,最后几步,是通过查看配置文件区确实是否是某台KVM主机。挂载使用完成后,可以通过下面的方法进行卸载和删除
[root@localhost file]# umount /mnt/
[root@localhost file]# vgchange -an VolGroup
0 logical volume(s) in volume group "VolGroup" now active
[root@localhost file]# losetup -d /dev/loop3