使用 kvm 安装虚拟机
2016 年 3 月 20 日 使用 kvm 安装虚拟机无评论
运行环境
ubuntu server 14.04
安装步骤
参考 How to install KVM on Ubuntu 14.04 LTS Headless Server
apt-get install cpu-checker
kvm-ok
kvm-ok
返回应该为
INFO: /dev/kvm exists
KVM acceleration can be used
KVM acceleration can be used
安装 kvm
apt-get install qemu-kvm libvirt-bin virtinst bridge-utils
安装 centos7
先下载安装镜像,之后运行安装,将系统镜像保存到分区较大的目录
mkdir /home/libvirt
cd /var/lib/libvirt/boot/
wget http://mirrors.kernel.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 1024 \
--vcpus=1 \
--os-variant=rhel7 \
--hvm \
--cdrom=/home/libvirt/boot/CentOS-7-x86_64-Minimal-1511.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/centos7.img,size=20,bus=virtio
cd /var/lib/libvirt/boot/
wget http://mirrors.kernel.org/centos/7/isos/x86_64/CentOS-7-x86_64-Minimal-1511.iso
sudo virt-install \
--virt-type=kvm \
--name centos7 \
--ram 1024 \
--vcpus=1 \
--os-variant=rhel7 \
--hvm \
--cdrom=/home/libvirt/boot/CentOS-7-x86_64-Minimal-1511.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/centos7.img,size=20,bus=virtio
之后输出
Starting install...
Allocating 'centos7.img' | 20 GB 00:00
Creating domain... | 0 B 00:00
WARNING Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
Allocating 'centos7.img' | 20 GB 00:00
Creating domain... | 0 B 00:00
WARNING Unable to connect to graphical console: virt-viewer not installed. Please install the 'virt-viewer' package.
Domain installation still in progress. You can reconnect to
the console to complete the installation process.
确定 vnc 端口号
virsh dumpxml centos7 | grep vnc
使用 ssh 或 haproxy 跳转 tcp 端口,在客户端用 vnc 连接安装系统。
安装完成后运行系统,再次确认 vnc 端口号。
virsh start centos7
virsh list --all
netstat -natp|grep 590
virsh list --all
netstat -natp|grep 590
使用 ssh 或 haproxy 跳转 tcp 端口,在客户端使用 vnc 连接系统。
添加用户、修改密码并启用网络。
修改 /etc/sysconfig/network-scripts/ifcfg-eth0 文件,将 ONBOOT=no 改为 ONBOOT=yes。
重启客户机
virsh destroy centos7
virsh start centos7
virsh start centos7
绑定固定的 nat ip,先确认客户机 mac 地址
cat /var/lib/libvirt/dnsmasq/default.leases
添加固定ip 配置
virsh net-edit default
修改为如下格式
<dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
<host mac='52:54:00:14:5d:fc' name='arch' ip='192.168.122.2'/>
<host mac='52:54:00:de:72:4e' name='centos7' ip='192.168.122.3'/>
</dhcp>
<range start='192.168.122.2' end='192.168.122.254'/>
<host mac='52:54:00:14:5d:fc' name='arch' ip='192.168.122.2'/>
<host mac='52:54:00:de:72:4e' name='centos7' ip='192.168.122.3'/>
</dhcp>
重启服务使设置生效
virsh destroy centos7
service libvirt-bin restart
virsh start centos7
service libvirt-bin restart
virsh start centos7
附录 安装 archlinux 的命令
virt-install \
--virt-type=kvm \
--name arch \
--ram 1024 \
--vcpus=1 \
--hvm \
--cdrom=/home/libvirt/boot/archlinux-2016.03.01-dual.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/arch.img,size=20,bus=virtio
--virt-type=kvm \
--name arch \
--ram 1024 \
--vcpus=1 \
--hvm \
--cdrom=/home/libvirt/boot/archlinux-2016.03.01-dual.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/arch.img,size=20,bus=virtio
如果要移动镜像储存位置,请修改 /etc/libvirt/qemu/arch.xml 中的并重启服务。
安装 win7 命令
virtio-win.iso 需要下载
# win7
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.102/virtio-win-0.1.102.iso
# win10
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.113-1/virtio-win-0.1.113.iso
virt-install \
--virt-type=kvm \
--name win7 \
--ram 2048 \
--vcpus=1 \
--cpuset=auto \
--hvm \
--os-variant=win7 \
--os-type=windows \
--disk device=cdrom,path=/home/libvirt/boot/virtio-win-0.1.102.iso \
--disk device=cdrom,path=/home/libvirt/boot/cn_windows_7_enterprise_with_sp1_x64_dvd_u_677685.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/win7.img,size=50,bus=virtio \
--boot cdrom,hd
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.102/virtio-win-0.1.102.iso
# win10
wget https://fedorapeople.org/groups/virt/virtio-win/direct-downloads/archive-virtio/virtio-win-0.1.113-1/virtio-win-0.1.113.iso
virt-install \
--virt-type=kvm \
--name win7 \
--ram 2048 \
--vcpus=1 \
--cpuset=auto \
--hvm \
--os-variant=win7 \
--os-type=windows \
--disk device=cdrom,path=/home/libvirt/boot/virtio-win-0.1.102.iso \
--disk device=cdrom,path=/home/libvirt/boot/cn_windows_7_enterprise_with_sp1_x64_dvd_u_677685.iso \
--network network=default,model=virtio \
--graphics vnc \
--disk path=/home/libvirt/images/win7.img,size=50,bus=virtio \
--boot cdrom,hd
注意选择驱动安装后才会显示硬盘
安装 win10 时如果出现蓝屏,注意修改 cpu 参数为
<cpu mode='custom' match='exact'>
<model fallback='allow'>core2duo</model>
<feature policy='require' name='nx'/>
</cpu>
<model fallback='allow'>core2duo</model>
<feature policy='require' name='nx'/>
</cpu>
Tags: kvm ubuntu
发表评论