Linux下使用guestmount命令挂载/读取/修改qcow2镜像文件|KVM虚拟机|libguestfs-tools

文章目录[隐藏]

前言

qcow2 镜像格式是 QEMU 模拟器支持的一种磁盘镜像。它也是可以用一个文件的形式来表示一块固定大小的块设备磁盘。与普通的 raw 格式的镜像相比,有以下特性:

  1. 更小的空间占用,即使文件系统不支持空洞(holes);
  2. 支持写时拷贝(COW, copy-on-write),镜像文件只反映底层磁盘的变化;
  3. 支持快照(snapshot),镜像文件能够包含多个快照的历史;
  4. 可选择基于 zlib 的压缩方式
  5. 可以选择 AES 加密

但是这个镜像文件并不能简单的通过解压来查看和编辑,这时候就需要使用本文的方法进行挂载后再做读写,特别适合微调修改KVM镜像的使用场景。

安装与使用

安装libguestfs-tools

我们这里用到的是guestmount命令,但是这个命令是内置在libguestfs-tools,如果直接apt install guestmount会报错:



apt install guestmount

Reading package lists... Done

Building dependency tree       

Reading state information... Done

Package guestmount is not available, but is referred to by another package.

This may mean that the package is missing, has been obsoleted, or

is only available from another source

However the following packages replace it:

  libguestfs-tools

E: Package ‘guestmount’ has no installation candidate

正确的安装方法



apt install libguestfs-tools -y

挂载qcow2镜像

首先要建立一个临时挂载的目录,这里用/tmp_mount



mkdir /tmp_mount

然后使用guestmount命令进行挂载,如果事先不知道虚机内部分区的路径信息,则可以使用参数-i来让guestmount自己决定使用哪一个挂接点。
例如这里我们要修改一个CentOS的镜像,增加Rstudio开机脚本,所以直接挂载上去:



guestmount -a /Hel1-S101/kvm/template/Rstudio_3.6.3_CentOS7/Rstudio_3.6.3_CentOS7.qcow2 -i --rw /tmp_mount

检查是否挂载成功:



root@HPC-HEL1-S101 / # guestmount -a /Hel1-S101/kvm/template/Rstudio_3.6.3_CentOS7/Rstudio_3.6.3_CentOS7.qcow2 -i --rw /tmp_mount

root@HPC-HEL1-S101 / # cd /tmp_mount

root@HPC-HEL1-S101 /tmp_mount # ls

bin  boot  dev  etc  home  lib  lib64  lost+found  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

取消挂载



umount /tmp_mount


This article is under CC BY-NC-SA 4.0 license.
Please quote the original link:https://www.liujason.com/article/796.html
Licensed under CC BY-NC-SA 4.0
comments powered by Disqus
Built with Hugo
Theme Stack designed by Jimmy