1 检查CPU虚拟化支持
egrep 'svm|vmx' /proc/cpuinfo
有输出说明cpu支持虚拟化,在BIOS中打开inter-VT
2 宿主机网桥设置
bridge不支持NetworkManager,所有在下面的操作前确保NetworkManager处于关闭状态
cat /etc/NetworkManager/NetworkManager.conf
[main]
plugins=ifupdown,keyfile,ofono
dns=dnsmasq
[ifupdown]
managed=false
配置managed=false
systemctl stop NetworkManager.service
配置网桥
安装bridge-utils
sudo apt-get install bridge-utils
创建网桥
brctl addbr br0
brctl addif br0 eno1
ifconfig eno1 0.0.0.0
ifconfig br0 192.168.100.20/24
添加默认网关
route add -net 0.0.0.0 netmask 0.0.0.0 gw 192.168.100.1
若要永久生效,则如下配置
cat /etc/network/interfaces
# interfaces(5) file used by ifup(8) and ifdown(8)
auto lo
iface lo inet loopback
auto br0
iface br0 inet static
bridge_ports eno1
address 192.168.100.20
netmask 255.255.255.0
gateway 192.168.100.1
bridge_stp off
bridge_fd 0
dns-nameservers 8.8.8.8
重启网络后,查看网络
ifconfig
br0 Link encap:Ethernet HWaddr 1c:b7:2c:aa:3d:89
inet addr:192.168.100.20 Bcast:192.168.100.255 Mask:255.255.255.0
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:2007114 errors:0 dropped:3179 overruns:0 frame:0
TX packets:2547163 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:14447174274 (14.4 GB) TX bytes:11544824281 (11.5 GB)
eno1 Link encap:Ethernet HWaddr 1c:b7:2c:aa:3d:89
UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1
RX packets:11663933 errors:0 dropped:8384 overruns:0 frame:0
TX packets:2472983 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:16484732144 (16.4 GB) TX bytes:237579422 (237.5 MB)
Interrupt:20 Memory:f7f00000-f7f20000
lo Link encap:Local Loopback
inet addr:127.0.0.1 Mask:255.0.0.0
UP LOOPBACK RUNNING MTU:65536 Metric:1
RX packets:5895380 errors:0 dropped:0 overruns:0 frame:0
TX packets:5895380 errors:0 dropped:0 overruns:0 carrier:0
collisions:0 txqueuelen:1000
RX bytes:9039764297 (9.0 GB) TX bytes:9039764297 (9.0 GB)
virbr0 Link encap:Ethernet HWaddr 00:00:00:00:00:00
inet addr:192.168.122.1 Bcast:192.168.122.255 Mask:255.255.255.0
UP BROADCAST MULTICAST MTU:1500 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:1000
RX bytes:0 (0.0 B) TX bytes:0 (0.0 B)
以上说明网桥设置ok
2 安装软件包
apt install kvm qemu qemu-kvm
通过kvm-ok检测kvm是否安装好了
kvm-ok
INFO: /dev/kvm exists
KVM acceleration can be used
这样说明已经安装好了
apt install virtinst libvirt-bin python-libvirt virt-viewer virt-manager (管理工具)
apt install bridge-utils (网桥管理)
3 创建虚拟磁盘
qemu-img create -f qcow2 /vm/kvm/instance-1.img 20G
创建一个20G大小,名称为instance-001,格式为qcow2的虚拟磁盘
4 安装GuestOS
本文采用的是非交互文本方式进行安装,所以做一下操作
4.1 挂载iso镜像
sudo mount /home/wangpei/iso/CentOS-7-x86_64-DVD-1708.iso /mnt/os -o loop
4.2 搭建ftp,允许匿名登录,匿名用户家目录指向/mnt
apt instal vsftp
vsftpd配置如下:
anonymous_enable=YES
anon_root=/mnt
4.3 在/mnt下建ks.cfg文件内容如下:
bootloader --location=mbr --driveorder=vda --append="console=ttyS0 crashkernel=auto"
clearpart --all --initlabel
install
url --url=ftp://192.168.100.20/os
lang en_US.UTF-8
keyboard us
network --onboot yes --device eth0 --bootproto static --ip 192.168.100.115 --netmask 255.255.255.0 --gateway 192.168.100.1 --ipv6 auto --nameserver 8.8.8.8
rootpw --iscrypted $6$1Y6QA0TPid69iYMp$ygY0OFrw06w1oHuOC8D666fO5E9NNUMvCZuL7Uewcq8/Kz5RikrwJ9yOdouSQbTbeEIpzugqllTlSpIBVboQk0
#firewall --service=ssh
firewall --disabled
authconfig --enableshadow --passalgo=sha512
selinux --disabled
timezone --utc Asia/Shanghai
#zerombr
autopart
repo --name="CentOS" --baseurl=ftp://192.168.100.20/os --cost=100
%packages
@core
net-tools
vim
%end
reboot
5 创建虚拟机
sudo virt-install
--connect qemu:///system
--virt-type kvm
--name instance-1
--metadata description="192.168.100.115"
--ram 2048
--vcpus 2
--disk "/vm/kvm/instance-1.img"
--location ftp://192.168.100.20/os
--extra-arg="ks=ftp://192.168.100.20/ks.cfg"
--nographic
-x console=ttyS0
--force
等到出现localhost login: 说明安装好了
用virsh list --all查看虚机实例
virsh list --all
Id Name State
57 centos7 running
71 instance-1 running
78 instance-2 running
80 instance-3 running
84 instance-4 running
87 ubuntu16.04 running
97 instance-5 running
99 instance-6 running
网友评论