KVM和KVM安装黑群晖

Table of Contents

Overview

KVM是基于硬件的完全虚拟化,其虚拟化需要硬件支持(如Intel VT技术或者 AMD-V技术)。

少量机器的管理,发行版+Kvm,使用libvirtd管理,virt-manager远程管理, lvm做存储后端,是最简单、可控性最高、性能也较好的方案。

首先检查自己的机器是否支持VT,

egrep -o '(vmx|svm)' /proc/cpuinfo

install

sudo apt-get install qemu-kvm libvirt-bin bridge-utils \
virtinst
# virt-manager: GUI管理窗口
# bridge-utils:用于网络桥接
# virtinst: support virt-install
# 验证KVM内核是否加载成功:
   lsmod | grep kvm
# 证实KVM正常运行
   virsh -c qemu:///system list
# 如果提示你没权限操作,则需要将当前操作账户添加进libvirtd组,命令为:
   sudo usermod -G libvirtd -a username

安装完后默认会生成一个网桥virbr0, 这里自己创建一个NAT的网桥br0

/etc/network/interfaces

auto br0
iface br0 inet dhcp
        bridge_ports eth0
        bridge_stp off
        bridge_fd 0
        bridge_maxwait 0

EOF

或者手动临时测试配置:

$ sudo ifconfig eth0 0.0.0.0 up
$ sudo brctl addbr br0
$ sudo brctl addif br0 eth0
$ sudo dhclient br0

Configure and install DSM

如果安装 virt-manager 直接启动 virt-manager 来GUI操作KVM.

The KVM hypervisor supports qcow2. qcow2 images support compression, snapshots and a few other nice things like growing on demand (thin provisioning, sparse file) and a read only base image. There was a performance overhead but nowdays that is almost negligent. To create an 10GB qcow2 image:

qemu-img create -f qcow2 ./dsm.qcow2 10G
virt-install \
--name DSM \
--ram 1024 \
--disk path=./dsm.qcow2,format=qcow2 \
--os-type linux \
--network bridge=br0,mac=00:11:22:33:44:55 \
--graphics vnc,port=5999 \
--console pty,target_type=serial \
--cdrom ./XPEnoboot_DS3615xs_5.2-5967.1.iso

https://www.synology.com/en-us/support/download/DS3615xs 下载 Synology Assistant. 或者 http://find.synology.com/ .

# stop
virsh destroy DSM
# start
virsh start DSM

如果迟迟没有看到新设备,可以用vnc客户端连接host的5999端口,查看虚拟机的屏幕。

More

virt-install examples1

Debian 8

virt-install \
--name debian8 \
--ram 1024 \
--disk path=./debian8.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://ftp.nl.debian.org/debian/dists/jessie/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'

CentOS 7

virt-install \
--name centos7 \
--ram 1024 \
--disk path=./centos7.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant centos7 \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://mirror.i3d.net/pub/centos/7/os/x86_64/' \
--extra-args 'console=ttyS0,115200n8 serial'

Ubuntu 14.04

virt-install \
--name ubuntu1404 \
--ram 1024 \
--disk path=./ubuntu1404.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://archive.ubuntu.com/ubuntu/dists/trusty/main/installer-amd64/' \
--extra-args 'console=ttyS0,115200n8 serial'

OpenSUSE 13

virt-install \
--name opensuse13 \
--ram 1024 \
--disk path=./opensuse13.qcow2,size=8 \
--vcpus 1 \
--os-type linux \
--os-variant generic \
--network bridge=virbr0 \
--graphics none \
--console pty,target_type=serial \
--location 'http://download.opensuse.org/distribution/13.2/repo/oss/' \
--extra-args 'console=ttyS0,115200n8 serial'

Generic ISO Download an ISO file and give the filename to the --cdrom= parameter. This is used instead of --location. A VNC console is available on localhost, port 5999 for you to use.

virt-install \
--name freebsd10 \
--ram 1024 \
--disk path=./freebsd10.qcow2,size=8 \
--vcpus 1 \
--os-type generic \
--os-variant generic \
--network bridge=virbr0 \
--graphics vnc,port=5999 \
--console pty,target_type=serial \
--cdrom ./FreeBSD-10.1-RELEASE-amd64-dvd1.iso \

os-variant

You can get a list of supported operating system variants with the osinfo-query os command

sudo apt-get install libosinfo-bin
$ osinfo-query os
 Short ID             | Name                                               | Version  | ID                                      
----------------------+----------------------------------------------------+----------+-----------------------------------------
 altlinux1.0          | Mandrake RE Spring 2001                            | 1.0      | http://altlinux.org/altlinux/1.0        
 altlinux2.0          | ALT Linux 2.0                                      | 2.0      | http://altlinux.org/altlinux/2.0        
 altlinux2.2          | ALT Linux 2.2                                      | 2.2      | http://altlinux.org/altlinux/2.2        
 altlinux2.4          | ALT Linux 2.4                                      | 2.4      | http://altlinux.org/altlinux/2.4        
 altlinux3.0          | ALT Linux 3.0                                      | 3.0      | http://altlinux.org/altlinux/3.0        
 altlinux4.0          | ALT Linux 4.0                                      | 4.0      | http://altlinux.org/altlinux/4.0        
 altlinux4.1          | ALT Linux 4.1                                      | 4.1      | http://altlinux.org/altlinux/4.1        
....

start, stop and remove VM

$ virsh start centos7
$ virsh list --all
# To stop a VM, you give the (unintuitive) command
$ virsh destroy centos7
#  remove the VM from the virsh list
$ virsh undefine centos7
# Reboot (soft & safe reboot) a vm/domain called openbsd
$ virsh reboot name
$ virsh shutdown foo
# Reset (hard reset/not safe) a vm/domain called openbsd
$ virsh reset openbsd

misc

# connect to your hypervisor. This can be local, or even remote.
$ virsh connect qemu:///system
# export the XML of an existing virtual machine:
 virsh dumpxml centos7 > ./centos7.xml
# libvirt XML format: http://libvirt.org/format.html
$ virt-xml-validate /path/to/XML/file

Once you have an XML file describing the new virtual machine you want to create, import it into virsh, and run it immediately:

$ virsh create /tmp/foo_new.xml 
# Alternatively, if you want to define it, but not run it, you could have used:
$ virsh define /tmp/foo_new.xml

Console:

$ virsh console foo

To view network information for a specific virtual network:

virsh net-dumpxml [vnet name]

Reference:

Footnotes:

Author: Shi Shougang

Created: 2017-02-25 Sat 00:12

Emacs 24.3.1 (Org mode 8.2.10)

Validate