Show Table of Contents
4.2. デモンストレーション: コマンドラインユーティリティーによるゲストの作成および管理
ここでは、CLI で仮想化タスクを実行する方法を実証するために、新しいゲスト仮想マシンを作成し、そのマシンに OS をインストールした後にゲストと通信し、CLI コマンドを使用して仮想マシンを管理する例を説明します。
4.2.1. インストール
以下の例では、
guest1-rhel7 という名前の新しいゲストを作成し、Red Hat Enterprise Linux 7 ワークステーションの ISO イメージから OS のインストールを開始します。このイメージは、 カスタマーポータル から取得でき、この例では ~/Downloads/ フォルダーにあります。ゲストには 2 つの仮想 CPU、2048 MB の RAM、および 8 GB のディスク領域が割り当てられています。
# virt-install --name guest1-rhel7 --memory 2048 --vcpus 2 --disk size=8 --cdrom /home/username/Downloads/rhel-workstation-7.4-x86_64-dvd.iso --os-variant rhel7
Starting install...
Allocating 'guest1-rhel7.qcow2' | 8.0 GB 00:00:00注記
グラフィカルインターフェースにアクセスできないホストシステムでは、以下のような
virt-install コマンドを実行し、 テキストベースの Anaconda を使用してゲスト OS をインストールすることができます。
# virt-install -name rhel7anaconda-guest -r 1024 --location=/home/jherrman/Downloads/rhel-workstation-7.4-x86_64-dvd.iso --disk size=8 --nographics --extra-args="console=tty0 console=ttyS0,115200n8"
インストールが正常に終了すると、コマンドラインに以下が表示されます。
Domain creation completed. Restarting guest.
これで、ゲストの設定が可能になりましたが、ゲストの設定を安全に管理するため、最初にゲストをシャットダウンすることが推奨されます。
# virsh shutdown guest1-rhel7
Domain guest1-rhel7 is being shutdown4.2.2. デバイスの接続
ゲストがホストに接続する USB デバイスを検出し、使用するようにするには (この例では Samsung の携帯電話)、最初にホストで
lsusb コマンドを実行し、デバイスの ID を取得します。
# lsusb
[...]
Bus 003 Device 007: ID 04e8:6860 Samsung Electronics Co., Ltd Galaxy (MTP)
その後、お好きなテキストエディターをホストで使用して、デバイス (この例では
samsung_USB_device.xml) の XML ファイルを作成します。このファイルにベンダーおよび製品 ID を入力します。
# vim samsung_USB_device.xml
<hostdev mode='subsystem' type='usb' managed='yes'>
<source>
<vendor id='0x04e8'/>
<product id='0x6860'/>
</source>
</hostdev>
最後に
virsh attach-device コマンドを使用してデバイスをゲストに接続します。
# virsh attach-device guest1-rhel7 --file samsung_USB_device.xml --config
Device attached successfully注記
稼働中のゲストにデバイスを接続することも可能です。それには、
--live オプションを使用します。
4.2.3. ゲストとの対話
guest1-rhel7 ゲストを起動してから使用します。
# virsh start guest1-rhel7
Domain guest1-rhel7 started
ホストシステムでグラフィック表示を使用できるかどうかに応じて、virt-viewer アプリケーションまたは SSH シェルのいずれかを使用してゲストと対話できます。
- グラフィック表示を使用できるシステムでは、virt-viewer を使用します。
#
virt-viewer guest1-rhel7
その後、物理マシン上で OS の GUI を使用するように、virt-viewer ウインドウの画面出力と対話できます。たとえば、サブスクリプションマネージャーアプリケーションを使用して Red Hat Enterprise Linux のゲスト OS を登録できます。
- ホストまたはゲストでテキスト専用インターフェースを使用できる場合は、SSH を使用します。これには、ゲストの IP アドレスを知っている必要があります。IP アドレスが分からない場合は、
virsh domifaddrコマンドを使用して取得することができます。#
virsh domifaddr guest1-rhel7Name MAC address Protocol Address ------------------------------------------------------------------------------- vnet0 52:54:00:65:29:21 ipv4 10.34.3.125/24 #ssh root@10.34.3.125root@10.34.3.125's password: Last login: Wed Jul 19 18:27:10 2017 from 192.168.122.1 [root@localhost ~]#注記
virsh domifaddrが動作するには、ゲストが動作し、ネットワーク上でアクセスできる必要があります。また、アクティブ化にQEMU ゲストエージェントが必要になることがあります。その後、ゲストマシンでターミナルを使用するのと同様に、ホストターミナルと対話することができます。たとえば、subscription-manager ユーティリティーを使用して、Red Hat Enterprise Linux のゲスト OS を使用することができます。[root@localhost ~]#
subscription-manager registerRegistering to: subscription.rhsm.redhat.com:443/subscription Username: username@sample.com Password: The system has been registered with ID: 30b5e666-67f9-53bb-4b90-c2a88e5be789
4.2.4. 診断
ゲストの状態に関する一般情報を表示します。
# virsh dominfo guest1-rhel7
Id: 1
Name: guest1-rhel7
UUID: ec0c0122-fb63-4a54-b602-5cf84f5e2dfd
OS Type: hvm
State: running
CPU(s): 2
CPU time: 33.4s
Max memory: 2097152 KiB
Used memory: 2097152 KiB
Persistent: yes
Autostart: disable
Managed save: no
Security model: selinux
Security DOI: 0
Security label: unconfined_u:unconfined_r:svirt_t:s0:c102,c792 (enforcing)4.2.5. スナップショットの作成
ゲストの状態をバックアップするには、
virsh snapshot-create コマンドを使用することができます。
# virsh snapshot-create guest1-rhel7
Domain snapshot 1500563241 created
現在のスナップショットとそれぞれの XML 設定を表示することができます。
#virsh snapshot-list guest1-rhel7Name Creation Time State ------------------------------------------------------------ 1500563241 2017-07-20 17:07:21 +0200 shutoff #virsh snapshot-dumpxml guest1-rhel7 1500563241<domainsnapshot> <name>1500563241</name> <state>shutoff</state> <creationTime>1500563241</creationTime> <memory snapshot='no'/> <disks> <disk name='vda' snapshot='internal'/> [...]
このスナップショットを後でロードすると、ゲストをこのスナップショットが保存された状態に戻すことができます。
# virsh snapshot-revert guest1-rhel7 --snapshotname 150056324

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.