Image 掛載與編輯

  1. 查看 image 檔。
    [root@dywmac ~]# cd /usr/src
    [root@dywmac src]# file crt15g.img 
    crt15g.img: x86 boot sector; GRand Unified Bootloader, stage1 version 0x3, boot drive 0x80, 1st sector stage2 0x6298, GRUB version 0.94; partition 1: ID=0x83, active, starthead 32, startsector 2048, 262144 sectors; partition 2: ID=0x8e, starthead 113, startsector 264192, 28672000 sectors, code offset 0x48
    
  2. 無法直接掛載 image 檔。
    [root@dywmac src]# mount crt15g.img /mnt/test
    mount: you must specify the filesystem type
    
  3. 查看 image 檔,包含兩個檔案系統,第一個檔案系統從 sector 2048 開始。
    [root@dywmac src]# fdisk -luc crt15g.img 
    You must set cylinders.
    You can do this from the extra functions menu.
    
    Disk crt15g.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: 0x0006315d
    
         Device Boot      Start         End      Blocks   Id  System
    crt15g.img1   *        2048      264191      131072   83  Linux
    
  4. 必須位移 2048 個 sectors,每個 sector 512 bytes,以 loop 及 offset 位移 1048576 (2048x512) 參數掛掛載 image 檔。
    [root@dywmac src]# mount -o rw,loop,offset=1048576 crt15g.img /mnt/loop/
    [root@dywmac src]# mount | grep crt15
    /usr/src/crt15g.img on /mnt/loop type ext4 (rw,loop=/dev/loop0,offset=1048576)
    [root@dywmac src]# ls /mnt/loop/
    config-2.6.32-573.el6.x86_64             symvers-2.6.32-573.el6.x86_64.gz
    config-4.8.1-1.el6.elrepo.x86_64         symvers-4.8.1-1.el6.elrepo.x86_64.gz
    efi                                      System.map-2.6.32-573.el6.x86_64
    grub                                     System.map-4.8.1-1.el6.elrepo.x86_64
    initramfs-2.6.32-573.el6.x86_64.img      vmlinuz-2.6.32-573.el6.x86_64
    initramfs-4.8.1-1.el6.elrepo.x86_64.img  vmlinuz-4.8.1-1.el6.elrepo.x86_64
    lost+found
    
  5. 掛載 image 檔的第二個檔案系統,第二個檔案系統從 sector 264192 開始 (264192x512=135266304)。不能直接掛載 LVM 檔案系統,要使用 losetup 命令設定 loop 裝置。
    [root@dywmac src]# mount -o rw,loop,offset=135266304 crt15g.img /mnt/loop/
    mount: unknown filesystem type 'LVM2_member'
    [root@dywmac src]# losetup --offset 135266304 /dev/loop1 crt15g.img
    
  6. image 檔的第二個檔案系統是 LVM 格式,無法直接掛載。
    [root@dywmac src]# mount /dev/loop1 /mnt/loop1
    mount: unknown filesystem type 'LVM2_member'
    
  7. losetup 設定 image 檔的第二個檔案系統為 loop device 後,vgscan 掃描 vg,出現 vg_crt。
    [root@dywmac src]# vgscan 
      Reading all physical volumes.  This may take a while...
      Found volume group "dywmac" using metadata type lvm2
      Found volume group "vg_crt" using metadata type lvm2
    
  8. 啟動 vg_crt vg。
    [root@dywmac ~]# vgchange -ay vg_crt
      3 logical volume(s) in volume group "vg_crt" now active
    
  9. 掛載 lv vg_crt-root。
    [root@dywmac ~]# mount /dev/mapper/vg_crt-root /mnt/root/
    
  10. 卸載 lv vg_crt-root。
    [root@dywmac ~]# umount /mnt/root/
    
  11. 移除 lv vg_crt-root, vg_crt-home, vg_crt-swap mapping。
    [root@dywmac ~]# dmsetup remove vg_crt-{root,home,swap}
    
  12. 卸載 lv loop1 裝置。
    [root@dywmac ~]# losetup -d /dev/loop1