guestfs-recipes(1) libguestfs, guestfish および仮想化ツールのレシピ

説明

このページには libguestfs, guestfish(1) および仮想化ツールを使用してできることのレシピとリンクがあります。

Access a remote disk image using guestfish

If the disk image is on a remote server which is accessible using SSH, HTTP, FTP, NBD, iSCSI, or similar, then you can open it directly. See ``ADDING REMOTE STORAGE'' in guestfish(1) for several examples. This requires libguestfs ≥ 1.22 and qemu ≥ 1.5.

仮想マシンの setuid ファイルを監査します

関連項目: ``例'' in virt-ls(1)。

Audit a virtual machine for vulnerabilities and security problems

See: https://rwmj.wordpress.com/2013/05/16/scanning-offline-guests-using-openscap-and-guestmount/#content

Windows XP 仮想マシンの背景画像を変更します

以下のリンクは、Windows XP 仮想マシンのユーザーの背景画像を変更するために guestfish(1) を使用する方法について説明しています。残念ながら、このテクニックは Windows のバージョンによりわずかに異なります。

https://lists.fedoraproject.org/pipermail/virt/2011-May/002655.html https://lists.fedoraproject.org/pipermail/virt/2011-May/002658.html

ディスクイメージの中にあるファイルまたはデバイスのチェックサム

デバイス全体、またはディスクイメージの中にあるパーティションや論理ボリュームなどについてチェックサムを確認するには:

 guestfish --ro -a disk.img run : checksum-device md5 /dev/sda1

Replace "md5" with the type of checksum you want. See ``guestfs_checksum_device'' in guestfs(3) for a list of supported types.

/dev/sda1 means ``the first partition''. You could use /dev/sda to checksum the whole disk image, or the name of a logical volume or RAID device.

単一サポートをチェックサムするには:

 guestfish --ro -a disk.img -i checksum sha256 /etc/passwd

または、Windows ゲストには:

 guestfish --ro -a disk.img -i \
   checksum sha256 'win:\windows\system32\config\SOFTWARE'

仮想マシンのクローン方法

cp(1), dd(1), and virt tools like virt-sysprep(1), virt-sparsify(1) および virt-resize(1) のようなツールの組み合わせを使用します。

詳細は次を参照してください: ``コピー方法およびクローン方法'' in virt-sysprep(1)。

CD-ROM / DVD / ISO を tar ファイルに変換します

This converts input cd.iso to output cd.tar.gz:

 guestfish --ro -a cd.iso -m /dev/sda tgz-out / cd.tar.gz

To export just a subdirectory, eg. /files, do:

 guestfish --ro -a cd.iso -m /dev/sda tgz-out /files cd.tar.gz

Convert from one format/filesystem to another

If you have a data disk in one format / filesystem / partition / volume manager, you can convert it another using this technique.

In this example, we start with a data disk that has a single partition containing a filesystem, and we want to create another disk that contains the same files but on an ext3 filesystem embedded in a logical volume on a sparse raw-format disk.

First create the formatted-but-empty target disk:

 truncate -s 10G target.img
 virt-format -a target.img --partition=mbr --lvm --filesystem=ext3

Now, pipe two guestfish instances together to transfer the old data to the new disk:

 guestfish --ro -a source.img -m /dev/sda1  -- tar-out / - | \
 guestfish --rw -a target.img -m /dev/VG/LV -- tar-in - /

To browse the final disk image, do:

 guestfish --ro -a target.img -m /dev/VG/LV
 ><fs> ll /

This technique is quite powerful, allowing you for example to split up source directories over the target filesystems.

Note this won't work (at least, not directly) for bootable virtual machine disks because it doesn't copy over the boot loader.

Convert Xen-style partitionless image to partitioned disk image

Xen disk images are often partitionless, meaning that the filesystem starts directly at the beginning of the disk with no partition table. You can in fact use these directly in KVM (provided the guest isn't Windows), but some people like to convert them to regular partitioned disk images, and this is required for Windows guests. Here is how to use guestfish to do this:

 guestfish
 ><fs> add-ro input.img
 ><fs> sparse output.img 10G     # adjust the output size
 ><fs> run
 # Create a partition table on the output disk:
 ><fs> part-init /dev/sdb mbr
 ><fs> part-add /dev/sdb p 2048 -2048
 # Copy the data to the target partition:
 ><fs> copy-device-to-device /dev/sda /dev/sdb1 sparse:true
 # Optionally resize the target filesystem.  Use ntfsresize
 # for Windows guests:
 ><fs> resize2fs /dev/sdb1

Such a disk image won't be directly bootable. You may need to boot it with an external kernel and initramfs (see below). Or you can use the guestfish commands "syslinux" or "extlinux" to install a SYSLINUX bootloader.

空のディスクイメージを作成します

virt-format(1) ツールは直接これを実行できます。

中身のあるディスクイメージを作成するには virt-make-fs(1) を使用します。仮想フロッピーディスク (VFD) のような、いくつかの標準的なディスクイメージを作成することもできます。

空のディスクイメージを作成するには guestfish(1) の -N オプションを使用することもできます。以下の有用なガイドが利用可能なオプションを説明しています。

https://rwmj.wordpress.com/2010/09/08/new-guestfish-n-options-in-1-5-9/#content

virt-builder(1) can create minimal guests.

ファイルを削除します (または、他の簡単なファイル操作)

guestfish を使用します。ファイルを削除するには:

 guestfish -a disk.img -i rm /file/to/delete

ファイルをタッチする (ファイルの日付を更新する、または作成する) には:

 guestfish -a disk.img -i touch /file/to/touch

To stat a file. Since this is a read-only operation, we can make it safer by adding the --ro flag.

 guestfish --ro -a disk.img -i stat /file/to/stat

There are dozens of these commands. See guestfish(1) or the output of "guestfish -h"

Diff two guests; compare a snapshot to the current version

Since libguestfs ≥ 1.26, use virt-diff(1) to look for differences between two guests (for example if they were originally cloned from the same source), or between two snapshots from the same guest. In earlier versions of libguestfs, use virt-ls(1).

Disable a systemd service

The following is the equivalent of "systemctl mask ...". To disable the "cloud-init" service so it doesn't start at next boot:

 guestfish -a disk.img -i \
     ln-sf /dev/null /etc/systemd/system/cloud-init.service

To disable tmp-on-tmpfs:

 guestfish -a disk.img -i \
     ln-sf /dev/null /etc/systemd/system/tmp.mount

One problem with the commands above is there is no feedback if you get the name of the service you are trying to mask wrong. But you can use virt-ls(1) to list the available systemd services like this:

 virt-ls -a /tmp/fedora-19.img -R /lib/systemd/system

Drive letters over FUSE

You have a Windows guest, and you want to expose the drive letters as FUSE mountpoints (/C/..., /D/... etc). Instead of guestmount(1), use this Perl script:

 #!/usr/bin/perl -w
 use strict;
 use Sys::Guestfs;
 $| = 1;
 die "usage: $0 mountpoint disk.img" if @ARGV < 2;
 my $mp = shift @ARGV;
 my $g = new Sys::Guestfs;
 $g->add_drive_opts ($_) foreach @ARGV;
 $g->launch;
 my @roots = $g->inspect_os;
 die "$0: no operating system found" if @roots != 1;
 my $root = $roots[0];
 die "$0: not Windows" if $g->inspect_get_type ($root) ne "windows";
 my %map = $g->inspect_get_drive_mappings ($root);
 foreach (keys %map) {
     $g->mkmountpoint ("/$_");
     eval { $g->mount ($map{$_}, "/$_") };
     warn "$@ (ignored)\n" if $@;
 }
 $g->mount_local ($mp);
 print "filesystem ready on $mp\n";
 $g->mount_local_run;
 $g->shutdown;

You can use the script like this:

 $ mkdir /tmp/mnt
 $ ./drive-letters.pl /tmp/mnt windows7.img
 filesystem ready on /tmp/mnt

In another window:

 $ cd /tmp/mnt
 $ ls
 C  D
 $ cd C
 $ ls
 Documents and Settings
 PerfLogs
 ProgramData
 Program Files
 [etc]
 $ cd ../..
 $ guestunmount /tmp/mnt

ディスクイメージまたは仮想マシンの中にあるファイルシステムの内容をそのままダンプします

You can use the guestfish(1) "download" command to extract the raw filesystem content from any filesystem in a disk image or a VM (even one which is encrypted or buried inside an LV or RAID device):

 guestfish --ro -a disk.img run : download /dev/sda1 sda1.img
 guestfish --ro -d Guest run : download /dev/vg_guest/lv_root lv.img

標準出力にダウンロードするには、ファイル名を "-" 文字に置き換えます:

 guestfish --ro -a disk.img run : download /dev/sda1 - | gzip > sda1.gz

ディスクイメージにあるファイルシステムを一覧表示するには virt-filesystems(1) を使用します。

See also ``Uploading raw filesystem content''.

仮想マシンの GRUB 設定を編集します

次の目的のために実行できます:
  • 起動しない仮想マシンを修正します。
  • 仮想マシンの起動時に使用するカーネルを変更します。
  • カーネルのコマンドラインオプションを変更します。

GRUB 設定を編集するには virt-edit(1) を使用します:

 virt-edit -d BrokenGuest /boot/grub2/grub.cfg

もしくは、起動不可能な仮想マシンの中から一般的に修復するために、このように virt-rescue(1) を使用します:

 virt-rescue -d BrokenGuest

仮想マシンから任意のディレクトリをエクスポートします

To export /home from a VM into a local directory use virt-copy-out(1):

 virt-copy-out -d Guest /home .

注:

  • コマンドの最後のドットは表示エラーではありません。現在のディレクトリーにコピーしたいことを意味します。
  • これは現在のディレクトリーに "home" というディレクトリーを作成します。

仮想マシンが Windows ならば、ドライブレターとバックスラッシュを使用できます。しかし、パスを "win:" で始める必要があり、シェルから保護するために引用符でくくる必要があります。このようにします:

 virt-copy-out -d WinGuest 'win:c:\windows\system32\config' .

圧縮された tar ファイルとして出力を取得するには:

 virt-tar-out -d Guest /home - | gzip --best > home.tar.gz

Although it sounds tempting, this is usually not a reliable way to get a backup from a running guest. See the entry in the FAQ: http://libguestfs.org/FAQ.html#backup

Export external kernel and initramfs (initrd)

If a Linux guest doesn't have a boot loader or it is broken, then you can usually boot it using an external kernel and initramfs. In this configuration, the hypervisor acts like a bootloader, loading the kernel from the host disk into guest memory and jumping straight into the kernel.

However you may wonder how to get the right kernel corresponding to the disk image you have. Since libguestfs ≥ 1.24 virt-builder(1) can get the latest kernel and corresponding initramfs for you:

 mkdir outputdir
 virt-builder --get-kernel disk.img -o outputdir
 ls -lh outputdir

もっとも領域を使用しているユーザーを検索します

この簡単なスクリプトは、ホームディレクトリーにおいてもっとも領域を使用しているユーザーを検索するために、Linux 仮想マシンを検査します。

 #!/bin/sh -
 
 set -e
 
 vm="$1"
 dir=/home
 
 eval $(guestfish --ro -d "$vm" -i --listen)
 
 for d in $(guestfish --remote ls "$dir"); do
     echo -n "$dir/$d"
     echo -ne '\t'
     guestfish --remote du "$dir/$d";
 done | sort -nr -k 2
 
 guestfish --remote exit

仮想マシンから DHCP アドレスを取得します

以下のリンクは、仮想マシンに対して最後に割り当てられた DHCP アドレスを取得するために、多くの異なる利用可能なテクニックを説明しています。

https://rwmj.wordpress.com/2011/03/31/tip-code-for-getting-dhcp-address-from-a-virtual-machine-disk-image/#content

In the libguestfs source examples directory you will find the latest version of the virt-dhcp-address.c program.

オペレーティングシステムの製品名を取得します

Save the following script into a file called product-name.sh:

 #!/bin/sh -
 set -e
 eval "$(guestfish --ro -d "$1" --i --listen)"
 root="$(guestfish --remote inspect-get-roots)"
 guestfish --remote inspect-get-product-name "$root"
 guestfish --remote exit

スクリプトを実行可能にして、名前付き仮想マシンにおいて実行します:

 # product-name.sh RHEL60x64
 Red Hat Enterprise Linux Server release 6.0 (Santiago)

"xpath" コマンドラインツールを使用して、またはお気に入りのプログラミング言語から、virt-inspector(1) XML において XPath クエリーを使用することもできます:

 # virt-inspector RHEL60x64 > xml
 # xpath '//product_name' < xml
 Found 1 nodes:
 -- NODE --
 <product_name>Red Hat Enterprise Linux Server release 6.0 (Santiago)</product_name>

Linux 仮想マシンのデフォルトの起動カーネルを取得します

以下のリンクに Linux 仮想マシンのデフォルトの起動カーネルを表示するためのプログラムがあります。

https://rwmj.wordpress.com/2010/10/30/tip-use-augeas-to-get-the-default-boot-kernel-for-a-vm/#content

Augeas を使用します。また、以下のように、多くのさまざまな作業に対して適用可能な一般的なテクニックがあります:

  • 仮想マシンのユーザーアカウントの一覧
  • 使用するよう設定されたリポジトリー
  • 接続する NTP サーバー
  • 前回起動時の起動メッセージ
  • 最近ログインしたユーザーの一覧

http://augeas.net/

Hanging guests

There are various ways to use libguestfs to find out why a guest is hanging or unresponsive:
1.
virt-cat を使用してログファイルを読むには:

 virt-cat Guest /var/log/messages | less
2.
Windows イベントログ (Windows Vista またはそれ以降) を読むには:

https://rwmj.wordpress.com/2011/04/17/decoding-the-windows-event-log-using-guestfish/#content

3.
Find out which files were last updated in a guest:

https://rwmj.wordpress.com/2012/02/27/using-libguestfs-to-find-out-why-a-windows-guest-was-hanging/#content

This might give you a clue as to what program is running.

仮想マシンからセクターを 16 進ダンプします

Hex-dump the boot partition (Master Boot Record / first sector):

 guestfish --ro -a disk.img run : pread-device /dev/sda 0x200 0 |
   hexdump -C

(0x200 = 512 bytes which is the size of traditional PC sectors)

To hexdump the N'th partition, substitute a number for "N" in the following command:

 guestfish --ro -a disk.img \
     run : pread-device /dev/sda 0x200 $((N*0x200)) |
   hexdump -C

仮想マシンにおいてセクターを 16 進ダンプします

Hex-edit the boot partition (Master Boot Record / first sector):

 guestfish --rw -a disk.img run : hexedit /dev/sda 0x200

Install packages (RPMs, Debian packages) in a guest

Since libguestfs 1.26, virt-builder(1), virt-customize(1) and virt-sysprep(1) have an --install option for installing packages in Linux guests. (Use virt-customize if you have an existing guest, or virt-builder if you want to create a guest from scratch).

例:

 virt-builder fedora-20 --install emacs

Install packages from an alternate repository

Since libguestfs 1.26, you can use virt-builder(1), virt-customize(1) or virt-sysprep(1) --edit option to edit repository metadata before installing packages

For example this would install packages from the updates-testing repository in Fedora:

 virt-builder fedora-20 \
   --edit '/etc/yum.repos.d/fedora-updates-testing.repo:
             s/enabled=0/enabled=1/' \
   --install emacs

Install SYSLINUX bootloader in a guest

SYSLINUX is a small, easy to configure bootloader for Linux and Windows guests. If your guest is not bootable, you can install the SYSLINUX bootloader using either the guestfish commands "syslinux" (for FAT-based guests) or "extlinux" (for ext2/3/4 and btrfs-based guests).

This guide assumes a Linux guest where /dev/sda1 is /boot, /boot/vmlinuz is the guest kernel, and /dev/sda3 is the root partition. For a Windows guest you would need a FAT-formatted boot partition and you would need to use the "syslinux" command instead.

Create a syslinux.cfg configuration file. You should check the SYSLINUX documentation at http://www.syslinux.org but it may look something like this:

 DEFAULT linux
 LABEL linux
   SAY Booting the kernel
   KERNEL vmlinuz
   INITRD initrd
   APPEND ro root=/dev/sda3

Locate the syslinux master boot record (a file called something like /usr/share/syslinux/mbr.bin).

 guestfish -a disk.img -i
 # Upload the master boot record and configuration file:
 ><fs> upload ..../mbr.bin /boot/mbr.bin
 ><fs> upload ..../syslinux.cfg /boot/syslinux.cfg
 # Put the MBR into the boot sector:
 ><fs> copy-file-to-device /boot/mbr.bin /dev/sda size:440
 # Install syslinux on the first partition:
 ><fs> extlinux /boot
 # Set the first partition as bootable:
 ><fs> part-set-bootable /dev/sda 1 true

See also: http://rwmj.wordpress.com/2013/04/04/new-in-libguestfs-use-syslinux-or-extlinux-to-make-bootable-guests/#content

仮想マシンにインストールされているアプリケーションを一覧表示します

Save the following to a file list-apps.sh:

 #!/bin/sh -
 set -e
 eval "$(guestfish --ro -d "$1" --i --listen)"
 root="$(guestfish --remote inspect-get-roots)"
 guestfish --remote inspect-list-applications "$root"
 guestfish --remote exit

ファイルを実行可能にして、あらゆる名前付き仮想マシンにおいて実行できます:

 # list-apps.sh WinGuest
 [0] = {
   app_name: Mozilla Firefox (3.6.12)
   app_display_name: Mozilla Firefox (3.6.12)
   app_epoch: 0
   app_version: 3.6.12 (en-GB)
   app_release:
   app_install_path: C:\Program Files\Mozilla Firefox
   app_trans_path:
   app_publisher: Mozilla
   app_url: http://www.mozilla.com/en-GB/
   app_source_package:
   app_summary:
   app_description: Mozilla Firefox
 }
 [1] = {
   app_name: VLC media player
   app_display_name: VLC media player 1.1.5
   app_epoch: 0
   app_version: 1.1.5
   app_release:
   app_install_path: C:\Program Files\VideoLAN\VLC
   app_trans_path:
   app_publisher: VideoLAN
   app_url: http://www.videolan.org/
   app_source_package:
   app_summary:
   app_description:
 }

(libvirt 仮想マシンの代わりに)ディスクイメージにおいてスクリプトを実行したいならば、"-d "$1"""-a "$1"" に変更します。virt-inspector(1) 参照。

仮想マシンのファイルおよびディレクトリを一覧表示します

virt-ls(1) を使用します。

Windows 仮想マシンのサービスを一覧表示します

以下のリンクは、Windows 仮想マシンからサービスを、およびそれらのサービスが起動時に実行されるか必要に応じて読み込まれるかを一覧化するために使用できるスクリプトがあります。

https://rwmj.wordpress.com/2010/12/10/tip-list-services-in-a-windows-guest/#content

ディスクイメージをスパースにします

virt-sparsify(1) を使用します。

ディスク使用量を時系列に監視します

時系列で仮想マシンのディスク使用量を監視するために virt-df(1) を使用できます。以下のリンクは仮想マシンを含みます。

http://virt-tools.org/learning/advanced-virt-df/

Windows Vista (またそれ以降)から Windows のイベントログを読み出します

guestfish(1) に加えて以下のリンクに示されるツールは、実行中の Windows Vista およびそれ以降のあらゆる仮想マシンから Windows イベントログを読み出すために使用できます。

https://rwmj.wordpress.com/2011/04/17/decoding-the-windows-event-log-using-guestfish/#content

root のパスワードを削除します (Linux)

virt-edit(1) -e を使用することにより、ファイルの内容を簡単に置き換えることができます。一つの使用方法は Linux 仮想マシンから root パスワードを削除することです。

 virt-edit -d domname /etc/passwd -e 's/^root:.*?:/root::/'
 virt-edit -a disk.img /etc/passwd -e 's/^root:.*?:/root::/'

Administrator のパスワードを削除します (Windows)

以下のリンクは、Windows 仮想マシンから管理者パスワードを削除するためのテクニックを含みます。または、より精細にするために、セキュリティを迂回するために使用でき、次回ログイン時にコマンドプロンプトを出します:

https://mdbooth.wordpress.com/2010/10/18/resetting-a-windows-guests-administrator-password-with-guestfish/

仮想マシンの sysprep 方法 (Windows)

It is possible to do a ``sysprep'' using libguestfs alone, although not straightforward. Currently there is code in the Aeolus Oz project which does this (using libguestfs). It is likely we will add this to virt-sysprep(1) in future.

https://github.com/clalancette/oz https://www.redhat.com/archives/virt-tools-list/2011-May/msg00019.html

Live CD を展開します

Linux live CD は、ロシア人形のように覆われた複数の層のディスクイメージを含みます。以下のガイドに概要が示されたように、これらの複数の層の内側を見るために guestfish(1) を使用できます。

https://rwmj.wordpress.com/2009/07/15/unpack-the-russian-doll-of-a-f11-live-cd/#content

ファイルのアップロード方法およびダウンロード方法

以下のリンクは、仮想マシンにファイルをアップロード、および仮想マシンからファイルをダウンロードすることに関する、一般的なヒントがあります。

https://rwmj.wordpress.com/2010/12/02/tip-uploading-and-downloading/#content

Uploading raw filesystem content

You can use guestfish(1) to upload whole filesystems into a VM, even into a filesystem which is encrypted or buried inside an LV or RAID device:

 guestfish --rw -a disk.img run : upload sda1.img /dev/sda1
 guestfish --rw -d Guest run : upload lv.img /dev/vg_guest/lv_root

One common problem is that the filesystem isn't the right size for the target. If it is too large, there's not much you can do with libguestfs - you have to prepare the filesystem differently. But if the filesystem needs to expand into the target, you can use guestfish to resize it to the right size:

 guestfish --rw -d Guest run : \
   upload lv.img /dev/vg_guest/lv_root : \
   resize2fs /dev/vg_guest/lv_root

(or use "ntfsresize" if the filesystem is NTFS).

VMware ESX 仮想マシンにおいて libguestfs ツールを使用します

以下のリンクは、まず最初に sshfs 経由で VMware VMFS を共有することにより、VMware ESX 仮想マシンにおいて libguestfs, guestfish(1) および virt ツールを使用する方法を説明しています。

https://rwmj.wordpress.com/2011/05/10/tip-use-libguestfs-on-vmware-esx-guests/#content

著者

Richard W.M. Jones ("rjones at redhat dot com")

COPYRIGHT

Copyright (C) 2009-2016 Red Hat Inc.

LICENSE

BUGS

To get a list of bugs against libguestfs, use this link: https://bugzilla.redhat.com/buglist.cgi?component=libguestfs&product=Virtualization+Tools

To report a new bug against libguestfs, use this link: https://bugzilla.redhat.com/enter_bug.cgi?component=libguestfs&product=Virtualization+Tools

When reporting a bug, please supply:

  • The version of libguestfs.
  • Where you got libguestfs (eg. which Linux distro, compiled from source, etc)
  • Describe the bug accurately and give a way to reproduce it.
  • Run libguestfs-test-tool(1) and paste the complete, unedited output into the bug report.