テンプレートを作る

インストールしたゲストOSは作成直後のクリーンな段階で、後々仮想マシンを増やす時用のテンプレートとしてディスクイメージをコピーしておくと便利です。 テンプレートとしてとっておいたディスクイメージから新たな仮想マシンを作成する場合は同じように「virt-clone」で複製できます。

[root@host ~]# virsh shutdown www
仮想マシンを停止させる
[root@host ~]# virt-clone --original www --name template --file /home/kvm/images/template.img
Allocating 'template.img'   | 20 GB 01:44
Clone 'template' created successfully.
[root@host ~]# ll /home/kvm/images/template.img     # ディスクイメージ確認
-rwxr-xr-x 1 root root 21474836480 Jul 11 20:05 /home/kvm/images/template.img
[root@host ~]# ll /etc/libvirt/qemu/template.xml     # 定義ファイル確認
-rw------- 1 root root 1340 Jul 11 20:04 /etc/libvirt/qemu/template.xml

テンプレートから仮想マシンを作成する

  • [1] 仮想マシンの作成
# virt-clone --original template --name test --file /home/kvm/images/test.img

or 

# virt-clone -o template -n test -f /home/kvm/images/test.img
  • [2] 仮想マシンの確認
[root@dlp ~]# virsh list --all 

クローン作成した仮想マシンのネットワーク設定を修正

  • [1] ネットワーク設定確認

NICのMACが変更されているので、eth0が無効になっている

# ifconfig

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)
  • [2] udevのruleファイルを編集
# vi /etc/udev/rules.d/70-persistent-net.rules
---
# This file was automatically generated by the /lib/udev/write_net_rules
# program, run by the persistent-net-generator.rules rules file.
#
# You can modify it, as long as you keep each rule on a single
# line, and change only the value of the NAME= key.

# PCI device 0x1af4:0x1000 (virtio-pci)
#SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:c1:e5:e1", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" ← コメントアウト(Clone元のNIC情報なので不要)

# PCI device 0x1af4:0x1000 (virtio-pci)
SUBSYSTEM=="net", ACTION=="add", DRIVERS=="?*", ATTR{address}=="52:54:00:4b:27:b9", ATTR{type}=="1", KERNEL=="eth*", NAME="eth0" ← NAMEをeth1からeth0に変更

  • [3] ifcfg-eth0にHWADDRを変更
# vi /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE="eth0"
BOOTPROTO=none
NM_CONTROLLED="yes"
ONBOOT=yes
TYPE="Ethernet"
UUID="05ea5405-b9e6-49b4-b0d3-0e4eb406372b"
#HWADDR=52:54:00:C1:E5:E1 ←コメントアウト(Clone元のNIC情報なので不要)
HWADDR=52:54:00:4b:27:b9 ← /etc/udev/rules.d/70-persistent-net.rulesのATTR{address}情報を記入
IPADDR=192.168.11.20
PREFIX=24
GATEWAY=192.168.11.1
DNS1=192.168.11.1
DEFROUTE=yes
IPV4_FAILURE_FATAL=yes
IPV6INIT=no
NAME="System eth0"
  • [4] reboot
# reboot
  • [5] ネットワークの確認
# ifconfig

eth0      Link encap:Ethernet  HWaddr 52:54:00:4B:27:B9  
          inet addr:192.168.11.20  Bcast:192.168.11.255  Mask:255.255.255.0
          inet6 addr: 2408:212:2c0:b100:5054:ff:fe4b:27b9/64 Scope:Global
          inet6 addr: fe80::5054:ff:fe4b:27b9/64 Scope:Link
          UP BROADCAST RUNNING MULTICAST  MTU:1500  Metric:1
          RX packets:321 errors:0 dropped:0 overruns:0 frame:0
          TX packets:205 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:1000 
          RX bytes:28925 (28.2 KiB)  TX bytes:27463 (26.8 KiB)

lo        Link encap:Local Loopback  
          inet addr:127.0.0.1  Mask:255.0.0.0
          inet6 addr: ::1/128 Scope:Host
          UP LOOPBACK RUNNING  MTU:16436  Metric:1
          RX packets:0 errors:0 dropped:0 overruns:0 frame:0
          TX packets:0 errors:0 dropped:0 overruns:0 carrier:0
          collisions:0 txqueuelen:0 
          RX bytes:0 (0.0 b)  TX bytes:0 (0.0 b)