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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章