KVM ( 1 ) ~ ゲスト OS インストール ~

September 24, 2017

CPU が仮想化機構に対応しているか確認

まずは、利用している CPU が仮想化支援機構に対応しているかの確認を行います。
/proc/cpuinfofalgs の部分に vmx もしくは svm が記載されていれば OK です。

$ grep -E '(vmx|svm)' /proc/cpuinfo
flags		: fpu vme de pse tsc msr pae mce cx8 apic sep mtrr pge mca cmov pat pse36 clflush dts acpi mmx fxsr sse sse2 ss ht tm pbe syscall nx pdpe1gb rdtscp lm constant_tsc art arch_perfmon pebs bts rep_good nopl xtopology nonstop_tsc aperfmperf eagerfpu pni pclmulqdq dtes64 monitor ds_cpl vmx est tm2 ssse3 fma cx16 xtpr pdcm pcid sse4_1 sse4_2 x2apic movbe popcnt tsc_deadline_timer aes xsave avx f16c rdrand lahf_lm abm 3dnowprefetch intel_pt tpr_shadow vnmi flexpriority ept vpid fsgsbase tsc_adjust bmi1 avx2 smep bmi2 erms invpcid mpx rdseed adx smap clflushopt xsaveopt xsavec xgetbv1 dtherm arat pln pts hwp hwp_notify hwp_act_window hwp_epp

...

vmx が含まれているので、Intel VT-x 拡張機能を備えた Intel プロセッサであることがわかる。ちなみに svm は AMD-V を表す。

環境及び必要なソフトウェアのインストール

$ cat /etc/centos-release
CentOS Linux release 7.4.1708 (Core) 
$ sudo yum -y install libguestfs libvirt libvirt-client python-virtinst qemu-kvm virt-manager virt-top virt-viewer virt-who virt-install bridge-utils

libvirtd の起動

$ sudo systemctl start libvirtd
$ sudo systemctl enable libvirtd
$ sudo systemctl status libvirtd
● libvirtd.service - Virtualization daemon
   Loaded: loaded (/usr/lib/systemd/system/libvirtd.service; enabled; vendor preset: enabled)
   Active: active (running) since 日 2017-09-24 01:00:59 JST; 10s ago
     Docs: man:libvirtd(8)
           http://libvirt.org
 Main PID: 15749 (libvirtd)
   CGroup: /system.slice/libvirtd.service
           ├─15749 /usr/sbin/libvirtd
           ├─15824 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper
           └─15825 /usr/sbin/dnsmasq --conf-file=/var/lib/libvirt/dnsmasq/default.conf --leasefile-ro --dhcp-script=/usr/libexec/libvirt_leaseshelper

 924 01:00:59 localhost.localdomain systemd[1]: Started Virtualization daemon.
 924 01:01:00 localhost.localdomain dnsmasq[15824]: started, version 2.76 cachesize 150
 924 01:01:00 localhost.localdomain dnsmasq[15824]: compile time options: IPv6 GNU-getopt DBus no-i18n IDN DHCP DHCPv6 no-Lua TFTP no-conntrack ipset auth no-DNSSEC loop-detect inotify
 924 01:01:00 localhost.localdomain dnsmasq-dhcp[15824]: DHCP, IP range 192.168.122.2 -- 192.168.122.254, lease time 1h
 924 01:01:00 localhost.localdomain dnsmasq-dhcp[15824]: DHCP, sockets bound exclusively to interface virbr0
 924 01:01:00 localhost.localdomain dnsmasq[15824]: reading /etc/resolv.conf
 924 01:01:00 localhost.localdomain dnsmasq[15824]: using nameserver 192.168.3.1#53
 924 01:01:00 localhost.localdomain dnsmasq[15824]: read /etc/hosts - 2 addresses
 924 01:01:00 localhost.localdomain dnsmasq[15824]: read /var/lib/libvirt/dnsmasq/default.addnhosts - 0 addresses
 924 01:01:00 localhost.localdomain dnsmasq-dhcp[15824]: read /var/lib/libvirt/dnsmasq/default.hostsfile

$ sudo reboot

ブリッジの設定

$ sudo cp /etc/sysconfig/network-scripts/ifcfg-enp1s0 /etc/sysconfig/network-scripts/ifcfg-br0

$ cat /etc/sysconfig/network-scripts/ifcfg-br0
TYPE="Bridge"
NAME="br0"
BOOTPROTO="dhcp"
DEVICE="br0"
ONBOOT="yes"

$ cat /etc/sysconfig/network-scripts/ifcfg-enp1s0
TYPE="Ethernet"
BRIDGE=br0
PROXY_METHOD="none"
BROWSER_ONLY="no"
BOOTPROTO="dhcp"
DEFROUTE="yes"
IPV4_FAILURE_FATAL="no"
IPV6INIT="yes"
IPV6_AUTOCONF="yes"
IPV6_DEFROUTE="yes"
IPV6_FAILURE_FATAL="no"
IPV6_ADDR_GEN_MODE="stable-privacy"
NAME="enp1s0"
UUID="248731b7-e4e6-46a0-9007-1d2291970d46"
DEVICE="enp1s0"
ONBOOT="yes"

$ sudo systemctl restart network
$ brctl show
bridge name	bridge id		STP enabled	interfaces
br0		8000.309c23275b93	no		enp1s0

$ ip addr show br0
3: br0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc noqueue state UP qlen 1000
    link/ether 30:9c:23:27:5b:93 brd ff:ff:ff:ff:ff:ff
    inet 192.168.3.200/24 brd 192.168.3.255 scope global dynamic br0
       valid_lft 86312sec preferred_lft 86312sec
    inet6 2400:2410:a562:2400:329c:23ff:fe27:5b93/64 scope global mngtmpaddr dynamic
       valid_lft 86313sec preferred_lft 14313sec
    inet6 fe80::329c:23ff:fe27:5b93/64 scope link
       valid_lft forever preferred_lft forever

ゲスト OS の用意

$ cd /tmp
$ curl -LO http://ftp.iij.ad.jp/pub/linux/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1708.iso
$ sudo qemu-img create -f qcow2 /var/lib/libvirt/images/centos7.img 20G
Formatting '/var/lib/libvirt/images/centos7.img', fmt=qcow2 size=21474836480 encryption=off cluster_size=65536 lazy_refcounts=off 

$ sudo virt-install --connect=qemu:///system \
--name=virt-centos7 \
--vcpus=1 \
--ram=512 \
--accelerate \
--hvm \
--disk path=/var/lib/libvirt/images/centos7.img,size=20,format=qcow2 \
--location='/tmp/CentOS-7-x86_64-Minimal-1708.iso' \
--network bridge=br0 \
--nographics \
--extra-args='console=tty0 console=ttyS0,115200n8'

 © 2023, Dealing with Ambiguity