挂载KVM Guest操作系统磁盘

本文介绍如何使用guestfish、lomount及kpartx三种工具挂载Guest操作系统的磁盘,并进行文件修改等操作。详细对比了各工具的优缺点,重点介绍了kpartx在速度与易用性方面的优势。

使用虚拟机时, 发现想要修改虚拟机中的文件非常麻烦, 需要启动虚拟机, 然后再登录进去修改.

对于已经关闭的虚拟机, 为了修改一个文件而启动, 非常耽误时间.

对于一个无法启动的虚拟机(比如启动文件损坏), 则束手无策.

 

因此, 掌握在主机中挂载 Guest 的操作系统磁盘, 并修改其中的内容的方法在某些时候会非常有用.

 

1. 挂载 Guest 操作系统磁盘的方法

挂载 Guest 操作系统磁盘的方法主要有以下3种:

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

 

使用上述3种工具之前, 我先安装了2个 kvm 虚拟机, 都是debian7.6 x86_64, 一个是普通分区, 一个是LVM分区.

2个系统的磁盘在默认路径中

root@debian-113:~# ll /var/lib/libvirt/images
total 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 - guestfish

2.1 使用 guestfish 对 普通Guest的磁盘进行一些基本的操作
root@debian-113:~# apt-get install guestfish            <-- 安装 guestfish 工具包, 中途跳出一个选择画面, 选择no
root@debian-113:~# guestfish                            <-- 进入 guestfish

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing 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的系统磁盘(非LVM分区)
><fs> launch                                            <-- 启动Guest, 这里会慢一些, 如果有错误, 参见下面的 *注1*
 100% ⟦------------------⟧ 00:00
><fs> list-devices                                      <-- 查看 Guest 中的磁盘
/dev/vda

><fs> sfdisk-l /dev/vda                                 <-- 查看 Guest 中的磁盘分区

Disk /dev/vda: 16644 cylinders, 16 heads, 63 sectors/track
Units = 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           <-- 这个应该是根分区 /
/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分区  /home

><fs> mount /dev/vda6 /                                               <-- 挂载 Guest 操作系统磁盘
><fs> ls /                                                            <-- 显示挂载的内容, 就是Guest的 /home
lost+found
wangyubin

# 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分区, 重新挂载Guest的 / 分区
><fs> mounts
/dev/vda6
><fs> umount /dev/vda6
><fs> mount /dev/vda1 /
><fs> ls /
bin
boot
dev
etc
home
initrd.img
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
selinux
srv
sys
tmp
usr
var
vmlinuz

# 将 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时出现错误的解决方法:

><fs> launch 
febootstrap-supermin-helper: ext2: parent directory not found: /lib: File not found by ext2_lookup
libguestfs: error: external command failed, see earlier error messages
><fs> quit

root@debian:~# update-guestfs-appliance

 

2.2 验证 guestfish 对普通磁盘的修改结果

1. HOST - 验证是否有 Guest的hostname文件, 并且内容正确?

root@debian-113:~# cat /home/from-guest      <-- 内容正确, 参见下面cat出的Guest上的hostname
debian
root@debian-113:~# cat /etc/hostname         <-- Host的hostname内容, 用于验证 from-host的内容
debian-113

 

2. GUEST - 验证是否有新建的文件? 是否有 Host的hostname文件, 并且内容正确?

# virt-manager 启动 guest之后, 登录进去执行以下命令
root@debian:~# ls /home/wangyubin/               <-- 新建的文件和upload的文件都在
from-host  guestfish
root@debian:~# cat /home/wangyubin/from-host     <-- 内容正确, 参见上面cat出的Host上的hostname
debian-113
root@debian:~# cat /home/wangyubin/guestfish     <-- 新建的文件内容正确
test guestfish
root@debian:~# cat /etc/hostname                 <-- Guest的hostname内容, 用于验证 from-guest的内容
debian

 

2.3 使用 guestfish 对 LVM分区的Guest磁盘进行一些基本的操作
root@debian-113:~# guestfish 

Welcome to guestfish, the libguestfs filesystem interactive shell for
editing 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/vda

Disk /dev/vda: 16644 cylinders, 16 heads, 63 sectors/track
Units = 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+found
wangyubin

# 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分区, 重新挂载Guest的 / 分区 都是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 /
bin
boot
dev
etc
home
initrd.img
lib
lib64
lost+found
media
mnt
opt
proc
root
run
sbin
selinux
srv
sys
tmp
usr
var
vmlinuz

# 将 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上的hostname
debian
root@debian-113:~# cat /etc/hostname             <-- Host的hostname内容, 用于验证 from-host的内容
debian-113

 

2. GUEST - 验证是否有新建的文件? 是否有 Host的hostname文件, 并且内容正确?

# virt-manager 启动 guest之后, 登录进去执行以下命令
root@debian:~# ls /home/wangyubin/                   <-- 新建的文件和upload的文件都在
from-host-lvm  guestfish-lvm
root@debian:~# cat /home/wangyubin/from-host-lvm     <-- 内容正确, 参见上面cat出的Host上的hostname
debian-113
root@debian:~# cat /home/wangyubin/guestfish-lvm     <-- 新建的文件内容正确
test guestfish lvm
root@debian:~# cat /etc/hostname                     <-- Guest的hostname内容, 用于验证 from-guest的内容
debian

 

3. 方法2 - lomount

lomount是Xen环境下的工具, 挂载Guest的操作系统磁盘时比guestfish要快很多, 但是不支持lvm.

由于没有Xen的环境, 这里就不尝试了.

 

4. 方法3 - kpartx

首先, 安装 kaprtx

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

 

4.1 使用 kpartx 对 普通分区的Guest磁盘进行一些基本的操作
root@debian-113:~# kpartx -av /var/lib/libvirt/images/debian7.6.img 
add map loop0p1 (254:0): 0 5154816 linear /dev/loop0 2048      <-- 这个是根分区 /
add map loop0p2 (254:1): 0 11616258 linear /dev/loop0 5158910
add map loop0p5 : 0 628736 linear 254:1 2
add map loop0p6 : 0 10985472 linear 254:1 630786               <-- 这个是分区 /home

# 挂载磁盘
root@debian-113:~# mount /dev/mapper/loop0p1 /mnt/             <-- *注2*
root@debian-113:~# ll /mnt/
total 96
drwxr-xr-x  2 root root  4096 Sep 18 12:38 bin
drwxr-xr-x  3 root root  4096 Sep 18 12:40 boot
drwxr-xr-x  3 root root  4096 Sep 18 11:26 dev
drwxr-xr-x 72 root root  4096 Sep 18 19:50 etc
drwxr-xr-x  2 root root  4096 Sep 18 11:23 home
lrwxrwxrwx  1 root root    30 Sep 18 11:27 initrd.img -> /boot/initrd.img-3.2.0-4-amd64
drwxr-xr-x 13 root root  4096 Sep 18 12:31 lib
drwxr-xr-x  2 root root  4096 Sep 18 12:30 lib64
drwx------  2 root root 16384 Sep 18 11:23 lost+found
drwxr-xr-x  3 root root  4096 Sep 18 11:23 media
drwxr-xr-x  2 root root  4096 Jun 12 05:07 mnt
drwxr-xr-x  2 root root  4096 Sep 18 11:23 opt
drwxr-xr-x  2 root root  4096 Jun 12 05:07 proc
drwx------  3 root root  4096 Sep 18 12:46 root
drwxr-xr-x  2 root root  4096 Sep 18 12:45 run
drwxr-xr-x  2 root root  4096 Sep 18 12:45 sbin
drwxr-xr-x  2 root root  4096 Jun 10  2012 selinux
drwxr-xr-x  2 root root  4096 Sep 18 11:23 srv
drwxr-xr-x  2 root root  4096 Jul 15  2013 sys
drwxrwxrwt  2 root root  4096 Sep 18 19:52 tmp
drwxr-xr-x 10 root root  4096 Sep 18 11:23 usr
drwxr-xr-x 11 root root  4096 Sep 18 11:23 var
lrwxrwxrwx  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分区的Guest磁盘进行一些基本的操作
root@debian-113:~# kpartx -av /var/lib/libvirt/images/debian7.6-lvm.img 
add map loop1p1 (254:4): 0 497664 linear /dev/loop1 2048
add map loop1p2 (254:5): 0 16273410 linear /dev/loop1 501758
add map loop1p5 : 0 16273408 linear 254:5 2   <-- 这是 lvm 分区

root@debian-113:~# vgscan                     <-- 通过 lvm 相关命令查看 lvm分区信息
  Reading all physical volumes.  This may take a while...
  Found volume group "debian" using metadata type lvm2
root@debian-113:~# vgs                        <-- 通过 lvm 相关命令查看 lvm分区信息
  VG     #PV #LV #SN Attr   VSize VFree
  debian   1   3   0 wz--n- 7.76g    0 
root@debian-113:~# lvs                        <-- 通过 lvm 相关命令查看 lvm分区信息
  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分区
  3 logical volume(s) in volume group "debian" now active

root@debian-113:~# mount /dev/debian/root /mnt/   <-- 挂载 lvm中的根分区 /
root@debian-113:~# ll /mnt/
total 96
drwxr-xr-x  2 root root  4096 Sep 18 12:53 bin
drwxr-xr-x  2 root root  4096 Sep 18 11:28 boot
drwxr-xr-x  3 root root  4096 Sep 18 11:31 dev
drwxr-xr-x 73 root root  4096 Sep 18 20:54 etc
drwxr-xr-x  2 root root  4096 Sep 18 11:28 home
lrwxrwxrwx  1 root root    30 Sep 18 11:32 initrd.img -> /boot/initrd.img-3.2.0-4-amd64
drwxr-xr-x 13 root root  4096 Sep 18 12:43 lib
drwxr-xr-x  2 root root  4096 Sep 18 12:42 lib64
drwx------  2 root root 16384 Sep 18 11:28 lost+found
drwxr-xr-x  3 root root  4096 Sep 18 11:28 media
drwxr-xr-x  2 root root  4096 Jun 12 05:07 mnt
drwxr-xr-x  2 root root  4096 Sep 18 11:28 opt
drwxr-xr-x  2 root root  4096 Jun 12 05:07 proc
drwx------  3 root root  4096 Sep 18 13:08 root
drwxr-xr-x  2 root root  4096 Sep 18 13:06 run
drwxr-xr-x  2 root root  4096 Sep 18 13:06 sbin
drwxr-xr-x  2 root root  4096 Jun 10  2012 selinux
drwxr-xr-x  2 root root  4096 Sep 18 11:28 srv
drwxr-xr-x  2 root root  4096 Jul 15  2013 sys
drwxrwxrwt  2 root root  4096 Sep 18 20:17 tmp
drwxr-xr-x 10 root root  4096 Sep 18 11:28 usr
drwxr-xr-x 11 root root  4096 Sep 18 11:28 var
lrwxrwxrwx  1 root root    26 Sep 18 11:32 vmlinuz -> boot/vmlinuz-3.2.0-4-amd64

root@debian-113:~# umount /mnt     <-- 卸载分区

 

5. 总结

lomount 由于没有环境, 没有实验.

对于 guestfish 和 kpartx来说, 感觉无论是速度还是易用性上, 都是 kpartx 胜出.



本文转自wang_yb博客园博客,原文链接:http://www.cnblogs.com/wang_yb/p/3980599.html,如需转载请自行联系原作者


### 如何在麒麟操作系统上启用和配置KVM虚拟化 #### 启用CPU中的虚拟化支持 为了使KVM能够正常工作,必须先确认物理服务器的处理器是否启用了硬件辅助虚拟化技术。这通常涉及到进入系统的BIOS/UEFI界面来激活Intel VT-x或AMD-V选项[^1]。 #### 安装必要的软件包 一旦确保了硬件层面的支持,在麒麟V10这样的Linux发行版中启动KVM服务之前还需要安装一系列工具和服务程序: ```bash sudo apt-get update && sudo apt-get install qemu-kvm libvirt-daemon-system \ libvirt-clients bridge-utils virt-manager -y ``` 这段命令会下载并安装QEMU-KVM模拟器、Libvirt守护进程及其客户端库以及图形化的管理前端Virt-Manager,并且包含了用于网络桥接配置所需的`bridge-utils`组件[^2]。 #### 设置默认存储池 对于新建立起来的环境来说,默认情况下是没有准备好用来保存镜像文件的数据卷组的。可以通过下面这条指令快速初始化一个基于本地磁盘空间的位置作为后续创建实例时使用的容器位置: ```bash virsh pool-define-as default dir - - - - "/var/lib/libvirt/images" virsh pool-build default virsh pool-start default virsh pool-autostart default ``` 这些操作定义了一个名为"default"类型的目录型资源池指向指定路径,并将其标记为开机自动挂载状态。 #### 调整网络连接模式 为了让来宾系统可以顺利访问外部互联网或者内部局域网,往往需要调整宿主机器上的网络接口参数。这里以搭建NAT方式为例说明具体做法: ```bash virsh net-edit default ``` 编辑打开后的XML描述文档里找到对应于<ip>标签的部分修改成如下所示的形式(假设子网掩码长度为24位): ```xml <ip address='192.168.122.1' netmask='255.255.255.0'> </ip> ``` 最后重启整个虚拟网络服务让更改生效即可。 #### 创建首个客户操作系统实例 当以上准备工作全部完成后就可以着手准备构建第一个Guest OS了。借助GUI应用程序Virt-Manager能极大简化这一过程;当然也支持通过CLI形式执行相同任务,比如利用`virt-install`工具按照提示逐步输入各项必要信息直至最终完成部署。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值