国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 學院 > 操作系統(tǒng) > 正文

掛載KVM Guest操作系統(tǒng)磁盤

2024-06-28 13:24:03
字體:
來源:轉載
供稿:網(wǎng)友
掛載KVM Guest操作系統(tǒng)磁盤

使用虛擬機時, 發(fā)現(xiàn)想要修改虛擬機中的文件非常麻煩, 需要啟動虛擬機, 然后再登錄進去修改.

對于已經關閉的虛擬機, 為了修改一個文件而啟動, 非常耽誤時間.

對于一個無法啟動的虛擬機(比如啟動文件損壞), 則束手無策.

因此, 掌握在主機中掛載 Guest 的操作系統(tǒng)磁盤, 并修改其中的內容的方法在某些時候會非常有用.

1. 掛載 Guest 操作系統(tǒng)磁盤的方法

掛載 Guest 操作系統(tǒng)磁盤的方法主要有以下3種:

  1. guestfish : 支持lvm, 速度較慢
  2. lomount : 不支持lvm, 速度快
  3. kpartx : 支持lvm, 速度快

使用上述3種工具之前, 我先安裝了2個 kvm 虛擬機, 都是debian7.6 x86_64, 一個是普通分區(qū), 一個是LVM分區(qū).

2個系統(tǒng)的磁盤在默認路徑中

root@debian-113:~# ll /var/lib/libvirt/imagestotal 16777320-rw------- 1 root root 8589934592 Sep 18 12:48 debian7.6.img-rw------- 1 root root 8589934592 Sep 18 13:08 debian7.6-lvm.img

2. 方法1 - guestfish2.1 使用 guestfish 對 普通Guest的磁盤進行一些基本的操作
root@debian-113:~# apt-get install guestfish            <-- 安裝 guestfish 工具包, 中途跳出一個選擇畫面, 選擇noroot@debian-113:~# guestfish                            <-- 進入 guestfishWelcome to guestfish, the libguestfs filesystem interactive shell forediting virtual machine filesystems.Type: 'help' for help on commands      'man' to read the manual      'quit' to quit the shell><fs> add /var/lib/libvirt/images/debian7.6.img         <-- 掛載 debian7.6的系統(tǒng)磁盤(非LVM分區(qū))><fs> launch                                            <-- 啟動Guest, 這里會慢一些, 如果有錯誤, 參見下面的 *注1* 100% ?------------------? 00:00><fs> list-devices                                      <-- 查看 Guest 中的磁盤/dev/vda><fs> sfdisk-l /dev/vda                                 <-- 查看 Guest 中的磁盤分區(qū)Disk /dev/vda: 16644 cylinders, 16 heads, 63 sectors/trackUnits = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0   Device Boot Start     End   #cyls    #blocks   Id  System/dev/vda1   *      2+   5115-   5114-   2577408   83  linux           <-- 這個應該是根分區(qū) //dev/vda2       5117+  16642-  11525-   5808129    5  Extended/dev/vda3          0       -       0          0    0  Empty/dev/vda4          0       -       0          0    0  Empty/dev/vda5       5117+   5741-    624-    314368   82  Linux swap / Solaris/dev/vda6       5743+  16642-  10899-   5492736   83  Linux           <-- 這個應該是home分區(qū)  /home><fs> mount /dev/vda6 /                                               <-- 掛載 Guest 操作系統(tǒng)磁盤><fs> ls /                                                            <-- 顯示掛載的內容, 就是Guest的 /homelost+foundwangyubin# Guest磁盤上新建文件><fs> touch /wangyubin/guestfish                                      <-- Guest的磁盤上新建一個文件><fs> vi /wangyubin/guestfish                                         <-- 編輯這個文件, 輸入 test guestfish# 將Host上的hostname文件上傳到Guest的 /home/wangyubin/from-host><fs> upload /etc/hostname /wangyubin/from-host# 卸載 Guest 的/home分區(qū), 重新掛載Guest的 / 分區(qū)><fs> mounts/dev/vda6><fs> umount /dev/vda6><fs> mount /dev/vda1 /><fs> ls /binbootdevetchomeinitrd.imgliblib64lost+foundmediamntoptPRocrootrunsbinselinuxsrvsystmpusrvarvmlinuz# 將 Guest上的 hostname 文件下載到 Host的 /home/from-guest><fs> download /etc/hostname /home/from-guest# 卸載Guest磁盤, 退出 guestfish><fs> mounts /dev/vda1><fs> umount /dev/vda1 ><fs> quit

*注1* guestfish中, launch時出現(xiàn)錯誤的解決方法:

><fs> launch febootstrap-supermin-helper: ext2: parent directory not found: /lib: File not found by ext2_lookuplibguestfs: error: external command failed, see earlier error messages><fs> quitroot@debian:~# update-guestfs-appliance

2.2 驗證 guestfish 對普通磁盤的修改結果

1. HOST - 驗證是否有 Guest的hostname文件, 并且內容正確?

root@debian-113:~# cat /home/from-guest      <-- 內容正確, 參見下面cat出的Guest上的hostnamedebianroot@debian-113:~# cat /etc/hostname         <-- Host的hostname內容, 用于驗證 from-host的內容debian-113

2. GUEST - 驗證是否有新建的文件? 是否有 Host的hostname文件, 并且內容正確?

# virt-manager 啟動 guest之后, 登錄進去執(zhí)行以下命令root@debian:~# ls /home/wangyubin/               <-- 新建的文件和upload的文件都在from-host  guestfishroot@debian:~# cat /home/wangyubin/from-host     <-- 內容正確, 參見上面cat出的Host上的hostnamedebian-113root@debian:~# cat /home/wangyubin/guestfish     <-- 新建的文件內容正確test guestfishroot@debian:~# cat /etc/hostname                 <-- Guest的hostname內容, 用于驗證 from-guest的內容debian

2.3 使用 guestfish 對 LVM分區(qū)的Guest磁盤進行一些基本的操作
root@debian-113:~# guestfish Welcome to guestfish, the libguestfs filesystem interactive shell forediting virtual machine filesystems.Type: 'help' for help on commands      'man' to read the manual      'quit' to quit the shell><fs> add /var/lib/libvirt/images/debian7.6-lvm.img><fs> launch ><fs> list-devices /dev/vda><fs> sfdisk-l /dev/vdaDisk /dev/vda: 16644 cylinders, 16 heads, 63 sectors/trackUnits = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0   Device Boot Start     End   #cyls    #blocks   Id  System/dev/vda1   *      2+    495-    494-    248832   83  Linux/dev/vda2        497+  16642-  16145-   8136705    5  Extended/dev/vda3          0       -       0          0    0  Empty/dev/vda4          0       -       0          0    0  Empty/dev/vda5        497+  16642-  16145-   8136704   8e  Linux LVM         <-- 這里看出是LVM><fs> lvs                 <-- 查看LVM的邏輯卷/dev/debian/home/dev/debian/root/dev/debian/swap_1><fs> mount /dev/debian/home /   <-- 掛載 /home 對應的LVM邏輯卷><fs> ls /lost+foundwangyubin# Guest磁盤上新建文件 - 步驟同上面普通磁盤><fs> touch /wangyubin/guestfish-lvm                           <-- Guest的磁盤上新建一個文件><fs> vi /wangyubin/guestfish-lvm                              <-- 編輯這個文件, 輸入 test guestfish lvm# 將Host上的hostname文件上傳到Guest的 /home/wangyubin/from-host-lvm><fs> upload /etc/hostname /wangyubin/from-host-lvm# 卸載 Guest 的/home分區(qū), 重新掛載Guest的 / 分區(qū) 都是LVM格式><fs> mounts/dev/debian/home><fs> umount /dev/debian/home ><fs> lvs/dev/debian/home/dev/debian/root/dev/debian/swap_1><fs> mount /dev/debian/root /><fs> ls /binbootdevetchomeinitrd.imgliblib64lost+foundmediamntoptprocrootrunsbinselinuxsrvsystmpusrvarvmlinuz# 將 Guest上的 hostname 文件下載到 Host的 /home/from-guest-lvm><fs> download /etc/hostname /home/from-guest-lvm# 卸載Guest磁盤, 退出 guestfish><fs> mounts /dev/debian/root><fs> umount /dev/debian/root ><fs> quit

2.4 驗證 guestfish 對LVM磁盤的修改結果

1. HOST - 驗證是否有 Guest的hostname文件, 并且內容正確?

root@debian-113:~# cat /home/from-guest-lvm      <-- 內容正確, 參見下面cat出的Guest上的hostnamedebianroot@debian-113:~# cat /etc/hostname             <-- Host的hostname內容, 用于驗證 from-host的內容debian-113

2. GUEST - 驗證是否有新建的文件? 是否有 Host的hostname文件, 并且內容正確?

# virt-manager 啟動 guest之后, 登錄進去執(zhí)行以下命令root@debian:~# ls /home/wangyubin/                   <-- 新建的文件和upload的文件都在from-host-lvm  guestfish-lvmroot@debian:~# cat /home/wangyubin/from-host-lvm     <-- 內容正確, 參見上面cat出的Host上的hostnamedebian-113root@debian:~# cat /home/wangyubin/guestfish-lvm     <-- 新建的文件內容正確test guestfish lvmroot@debian:~# cat /etc/hostname                     <-- Guest的hostname內容, 用于驗證 from-guest的內容debian

3. 方法2 - lomount

lomount是Xen環(huán)境下的工具, 掛載Guest的操作系統(tǒng)磁盤時比guestfish要快很多, 但是不支持lvm.

由于沒有Xen的環(huán)境, 這里就不嘗試了.

4. 方法3 - kpartx

首先, 安裝 kaprtx

root@debian-113:~# apt-get install kpartx

4.1 使用 kpartx 對 普通分區(qū)的Guest磁盤進行一些基本的操作
root@debian-113:~# kpartx -av /var/lib/libvirt/images/debian7.6.img add map loop0p1 (254:0): 0 5154816 linear /dev/loop0 2048      <-- 這個是根分區(qū) /add map loop0p2 (254:1): 0 11616258 linear /dev/loop0 5158910add map loop0p5 : 0 628736 linear 254:1 2add map loop0p6 : 0 10985472 linear 254:1 630786               <-- 這個是分區(qū) /home# 掛載磁盤root@debian-113:~# mount /dev/mapper/loop0p1 /mnt/             <-- *注2*root@debian-113:~# ll /mnt/total 96drwxr-xr-x  2 root root  4096 Sep 18 12:38 bindrwxr-xr-x  3 root root  4096 Sep 18 12:40 bootdrwxr-xr-x  3 root root  4096 Sep 18 11:26 devdrwxr-xr-x 72 root root  4096 Sep 18 19:50 etcdrwxr-xr-x  2 root root  4096 Sep 18 11:23 homelrwxrwxrwx  1 root root    30 Sep 18 11:27 initrd.img -> /boot/initrd.img-3.2.0-4-amd64drwxr-xr-x 13 root root  4096 Sep 18 12:31 libdrwxr-xr-x  2 root root  4096 Sep 18 12:30 lib64drwx------  2 root root 16384 Sep 18 11:23 lost+founddrwxr-xr-x  3 root root  4096 Sep 18 11:23 mediadrwxr-xr-x  2 root root  4096 Jun 12 05:07 mntdrwxr-xr-x  2 root root  4096 Sep 18 11:23 optdrwxr-xr-x  2 root root  4096 Jun 12 05:07 procdrwx------  3 root root  4096 Sep 18 12:46 rootdrwxr-xr-x  2 root root  4096 Sep 18 12:45 rundrwxr-xr-x  2 root root  4096 Sep 18 12:45 sbindrwxr-xr-x  2 root root  4096 Jun 10  2012 selinuxdrwxr-xr-x  2 root root  4096 Sep 18 11:23 srvdrwxr-xr-x  2 root root  4096 Jul 15  2013 sysdrwxrwxrwt  2 root root  4096 Sep 18 19:52 tmpdrwxr-xr-x 10 root root  4096 Sep 18 11:23 usrdrwxr-xr-x 11 root root  4096 Sep 18 11:23 varlrwxrwxrwx  1 root root    26 Sep 18 11:27 vmlinuz -> boot/vmlinuz-3.2.0-4-amd64# 卸載磁盤root@debian-113:~# umount /mnt

*注2* kpartx 可以將Guest的磁盤掛載到主機的某個目錄下 (比如上述例子就是 /mnt),

不像 guestfish 只能將Guest的磁盤掛載到 /

因此, 對于 kpartx 來說, 不需要 download 和 upload 命令, 操作 Guest 磁盤中的文件就和掛載了U盤一樣.

4.2 使用 kpartx 對 LVM分區(qū)的Guest磁盤進行一些基本的操作
root@debian-113:~# kpartx -av /var/lib/libvirt/images/debian7.6-lvm.img add map loop1p1 (254:4): 0 497664 linear /dev/loop1 2048add map loop1p2 (254:5): 0 16273410 linear /dev/loop1 501758add map loop1p5 : 0 16273408 linear 254:5 2   <-- 這是 lvm 分區(qū)root@debian-113:~# vgscan                     <-- 通過 lvm 相關命令查看 lvm分區(qū)信息  Reading all physical volumes.  This may take a while...  Found volume group "debian" using metadata type lvm2root@debian-113:~# vgs                        <-- 通過 lvm 相關命令查看 lvm分區(qū)信息  VG     #PV #LV #SN Attr   VSize VFree  debian   1   3   0 wz--n- 7.76g    0 root@debian-113:~# lvs                        <-- 通過 lvm 相關命令查看 lvm分區(qū)信息  LV     VG     Attr     LSize   Pool Origin Data%  Move Log Copy%  Convert  home   debian -wi-----   5.06g                                             root   debian -wi-----   2.40g                                             swap_1 debian -wi----- 300.00m              root@debian-113:~# vgchange -ay debian        <-- 激活lvm分區(qū)  3 logical volume(s) in volume group "debian" now activeroot@debian-113:~# mount /dev/debian/root /mnt/   <-- 掛載 lvm中的根分區(qū) /root@debian-113:~# ll /mnt/total 96drwxr-xr-x  2 root root  4096 Sep 18 12:53 bindrwxr-xr-x  2 root root  4096 Sep 18 11:28 bootdrwxr-xr-x  3 root root  4096 Sep 18 11:31 devdrwxr-xr-x 73 root root  4096 Sep 18 20:54 etcdrwxr-xr-x  2 root root  4096 Sep 18 11:28 homelrwxrwxrwx  1 root root    30 Sep 18 11:32 initrd.img -> /boot/initrd.img-3.2.0-4-amd64drwxr-xr-x 13 root root  4096 Sep 18 12:43 libdrwxr-xr-x  2 root root  4096 Sep 18 12:42 lib64drwx------  2 root root 16384 Sep 18 11:28 lost+founddrwxr-xr-x  3 root root  4096 Sep 18 11:28 mediadrwxr-xr-x  2 root root  4096 Jun 12 05:07 mntdrwxr-xr-x  2 root root  4096 Sep 18 11:28 optdrwxr-xr-x  2 root root  4096 Jun 12 05:07 procdrwx------  3 root root  4096 Sep 18 13:08 rootdrwxr-xr-x  2 root root  4096 Sep 18 13:06 rundrwxr-xr-x  2 root root  4096 Sep 18 13:06 sbindrwxr-xr-x  2 root root  4096 Jun 10  2012 selinuxdrwxr-xr-x  2 root root  4096 Sep 18 11:28 srvdrwxr-xr-x  2 root root  4096 Jul 15  2013 sysdrwxrwxrwt  2 root root  4096 Sep 18 20:17 tmpdrwxr-xr-x 10 root root  4096 Sep 18 11:28 usrdrwxr-xr-x 11 root root  4096 Sep 18 11:28 varlrwxrwxrwx  1 root root    26 Sep 18 11:32 vmlinuz -> boot/vmlinuz-3.2.0-4-amd64root@debian-113:~# umount /mnt     <-- 卸載分區(qū)

5. 總結

lomount 由于沒有環(huán)境, 沒有實驗.

對于 guestfish 和 kpartx來說, 感覺無論是速度還是易用性上, 都是 kpartx 勝出.


發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
主站蜘蛛池模板: 陆丰市| 滕州市| 顺义区| 澄迈县| 桃园县| 桂东县| 清水县| 虎林市| 巴彦淖尔市| 汉源县| 新余市| 枞阳县| 奉节县| 柞水县| 红桥区| 阳原县| 微博| 肃宁县| 吴旗县| 汉源县| 尚志市| 襄汾县| 通河县| 广宁县| 东乌珠穆沁旗| 杭锦旗| 东明县| 阿鲁科尔沁旗| 大城县| 星子县| 潜江市| 普洱| 东宁县| 杨浦区| 遵化市| 抚宁县| 惠安县| 五华县| 昭通市| 军事| 化隆|