kvm初体验之三:vm的安装及管理

Host: CentOS release 6.4 (Final)

Guest: CentOS release 6.6 (Final)

全程以root身份操作

1. host上创建桥br0

参考《KVM Virtualization in RHEL 6 Made Easy》2.6 Networking

我的/etc/sysconfig/network-scripts/ifcfg-br0文件内容为:

DEVICE=br0
BOOTPROTO=dhcp
IPV6INIT=yes
IPV6_AUTOCONF=yes
NM_CONTROLLED=no
ONBOOT=yes
TYPE=Bridge
DELAY=

我的/etc/sysconfig/network-scripts/ifcfg-eth0文件内容为:

DEVICE=eth0
TYPE=Ethernet
UUID=20008b30-343b--b908-af2b0ca5b545
ONBOOT=yes
NM_CONTROLLED=no
BOOTPROTO=dhcp
HWADDR=3C::0E:5C:E4:2E
DNS1=192.168.1.1
BRIDGE=br0

重启network服务后,ifconfig出现br0接口,并且br0获取到了IP:

[root@tang* ~]# ifconfig
br0 Link encap:Ethernet HWaddr 3C::0E:5C:E4:2E
inet addr:192.168.1.6 Bcast:192.168.1.255 Mask:255.255.255.0
inet6 addr: fe80::3e97:eff:fe5c:e42e/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (39.3 MiB) TX bytes: (1.4 MiB) eth0 Link encap:Ethernet HWaddr 3C::0E:5C:E4:2E
inet6 addr: fe80::3e97:eff:fe5c:e42e/ Scope:Link
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (39.7 MiB) TX bytes: (1.4 MiB)
Interrupt: Memory:f2500000-f2520000 lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
inet6 addr: ::/ Scope:Host
UP LOOPBACK RUNNING MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (1.0 KiB) TX bytes: (1.0 KiB) virbr0 Link encap:Ethernet HWaddr :::::C1
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU: Metric:
RX packets: errors: dropped: overruns: frame:
TX packets: errors: dropped: overruns: carrier:
collisions: txqueuelen:
RX bytes: (0.0 b) TX bytes: (0.0 b)

ping一下网关及外网,可以连通:

[root@tang* ~]# ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) () bytes of data.
bytes from 192.168.1.1: icmp_seq= ttl= time=0.978 ms
bytes from 192.168.1.1: icmp_seq= ttl= time=0.861 ms
^C
--- 192.168.1.1 ping statistics ---
packets transmitted, received, % packet loss, time 1080ms
rtt min/avg/max/mdev = 0.861/0.919/0.978/0.065 ms
[root@tang* ~]# ping www.baidu.com
PING www.a.shifen.com (115.239.210.25) () bytes of data.
bytes from 115.239.210.25: icmp_seq= ttl= time=7.46 ms
bytes from 115.239.210.25: icmp_seq= ttl= time=7.37 ms
^C
--- www.a.shifen.com ping statistics ---
packets transmitted, received, % packet loss, time 9850ms
rtt min/avg/max/mdev = 7.372/7.416/7.460/0.044 ms
[root@tang* ~]#

brctl show可以看到host上存在的桥:

[root@tang* ~]# brctl show
bridge name bridge id STP enabled interfaces
br0 .3c970e5ce42e no eth0
virbr0 .5254002590c1 yes virbr0-nic

2. 设置SELinux

参考《KVM
Virtualization in RHEL 6 Made Easy
》2.7
SELinux

3.
安装vm

参考《KVM
Virtualization in RHEL 6 Made Easy
》3.
Creating VMs

virt-install --name vm1 --ram= --vcpus= --disk path=/vm-images/vm1,size= --cdrom /root/iso/CentOS-6.6-x86_64-minimal.iso --network bridge:br0

关于virt-install参数的详细解释参考:

Installing a KVM Guest OS from the Command-line (virt-install)

安装过程同linux系统的正常安装过程。

kvm初体验之三:vm的安装及管理

安装完成之后输入用户名密码可以进入vm:

kvm初体验之三:vm的安装及管理

4. 管理vm

常用的vm管理命令有:

列出所有vm:virsh list –all

挂起:virsh suspend

恢复:virsh resume

关机:virsh shutdown

启动:virsh start

强制关机:virsh destroy

销毁:virsh undefine

查看vm信息:virsh dominfo

查看virsh全部的管理命令:virsh help

查看virsh某个管理命令的用法:virsh help xxx

kvm初体验之三:vm的安装及管理

kvm初体验之三:vm的安装及管理

注:virsh shutdown命令执行后vm往往仍处于running状态,这时可以用virsh destroy强制关机。

virsh undefine销毁一个vm前,要先将vm关机。

上一篇:kvm初体验之五:vm连接网络的两种方式:bridge和nat


下一篇:【CCPC-Wannafly Winter Camp Day4 (Div1) D】欧拉回路(分类讨论)