配置CentOS 阿里云源

1
2
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

安装开发工具

1
yum install kernel-headers-`uname -r` kernel-devel-`uname -r` vim gcc make automake autoconf gcc-c++ wget curl libcurl 

安装kvm

1
yum install -y libvirt-admin.x86_64 libvirt-client.x86_64 libvirt-daemon.x86_64 libvirt-daemon-config-network.x86_64 libvirt-daemon-config-nwfilter.x86_64 libvirt-daemon-driver-interface.x86_64 libvirt-daemon-driver-lxc.x86_64 libvirt-daemon-driver-network.x86_64 libvirt-daemon-driver-nodedev.x86_64 libvirt-daemon-driver-nwfilter.x86_64 libvirt-daemon-driver-qemu.x86_64 libvirt-daemon-driver-secret.x86_64 libvirt-daemon-driver-storage.x86_64 libvirt-daemon-driver-storage-core.x86_64 libvirt-dbus.x86_64 libvirt-devel.x86_64 libvirt-gconfig.i686 libvirt-gconfig.x86_64 libvirt-gconfig-devel.x86_64 libvirt-glib.x86_64 libvirt-glib-devel.x86_64 libvirt-gobject.x86_64 libvirt-gobject-devel.x86_64 libvirt-java.noarch libvirt-java-devel.noarch libvirt-java-javadoc.noarch libvirt-nss.x86_64 libvirt-python.x86_64 libvirt-snmp.x86_64 nbdkit-plugin-libvirt.x86_64 ocaml-libvirt.x86_64 ocaml-libvirt-devel.x86_64 php-libvirt.x86_64 php-libvirt-doc.noarch python36-libvirt.x86_64 rubygem-ruby-libvirt.x86_64 rubygem-ruby-libvirt-doc.noarch collectd-virt.x86_64 fence-agents-virsh.x86_64 libvirt.x86_64 libvirt-bash-completion.x86_64 libvirt-daemon-driver-storage-disk.x86_64 libvirt-daemon-driver-storage-gluster.x86_64 libvirt-daemon-driver-storage-iscsi.x86_64 libvirt-daemon-driver-storage-logical.x86_64 libvirt-daemon-driver-storage-mpath.x86_64 libvirt-daemon-driver-storage-rbd.x86_64 libvirt-daemon-driver-storage-scsi.x86_64 libvirt-daemon-kvm.x86_64 libvirt-daemon-lxc.x86_64 libvirt-docs.x86_64 libvirt-libs.x86_64 libvirt-lock-sanlock.x86_64 libvirt-login-shell.x86_64 perl-Sys-Virt.x86_64 virt-manager.noarch

安装磁盘工具qemu

1
yum install -y centos-release-qemu-ev.noarch ipxe-roms-qemu.noarch libvirt-daemon-driver-qemu.x86_64 qemu.x86_64 qemu-common.x86_64 qemu-guest-agent.x86_64 qemu-img.x86_64 qemu-kvm.x86_64 qemu-kvm-common.x86_64 qemu-system-alpha.x86_64 qemu-system-arm.x86_64 qemu-system-cris.x86_64 qemu-system-lm32.x86_64 qemu-system-m68k.x86_64 qemu-system-microblaze.x86_64 qemu-system-mips.x86_64 qemu-system-moxie.x86_64 qemu-system-or32.x86_64 qemu-system-s390x.x86_64 qemu-system-sh4.x86_64 qemu-system-unicore32.x86_64 qemu-system-x86.x86_64 qemu-system-xtensa.x86_64 qemu-user.x86_64 libvirt-lock-sanlock.x86_64 qemu-kvm-tools.x86_64

安装网络工具

1
yum install -y bridge-utils.x86_64 cockpit-bridge.x86_64 console-bridge-devel.x86_64 net-tools iproute
1
yum install libguestfs-bash-completion.noarch libguestfs-benchmarking.x86_64 libguestfs-devel.x86_64 libguestfs-gfs2.x86_64 libguestfs-gobject.x86_64 libguestfs-gobject-devel.x86_64 libguestfs-java.x86_64 libguestfs-java-devel.x86_64 libguestfs-javadoc.noarch libguestfs-man-pages-ja.noarch libguestfs-man-pages-uk.noarch libguestfs-rsync.x86_64 libguestfs-xfs.x86_64 ocaml-libguestfs.x86_64 ocaml-libguestfs-devel.x86_64 python-libguestfs.x86_64 ruby-libguestfs.x86_64 libguestfs.x86_64 libguestfs-inspect-icons.noarch libguestfs-rescue.x86_64 libguestfs-tools.noarch libguestfs-tools-c.x86_64 libguestfs-winsupport.x86_64 lua-guestfs.x86_64 nbdkit-plugin-guestfs.x86_64 perl-Sys-Guestfs.x86_64

补全功能

1
yum install -y bash-completion.noarch bash-completion-extras.noarch libguestfs-bash-completion.noarch libvirt-bash-completion.x86_64
1
yum install -y virt-dib.x86_64 virt-install.noarch virt-manager.noarch virt-manager-common.noarch virt-p2v-maker.x86_64 virt-v2v.x86_64 virt-viewer.x86_64 virt-who.noarch
1
2
3
4
5
6
7
egrep '(vmx|svm)' /proc/cpuinfo
modprobe kvm
modprobe kvm_intel

systemctl start libvirtd
systemctl enable libvirtd

virt-install

1
2
3
4
5
6
7
8
9
10
11
12
13
–name 指定虚拟机的名称
–memory 指定分配给虚拟机的内存资源大小
maxmemory 指定可调节的最大内存资源大小,因为KVM支持热调整虚拟机的资源
–vcpus 指定分配给虚拟机的CPU核心数量
maxvcpus 指定可调节的最大CPU核心数量
–os-type 指定虚拟机安装的操作系统类型
–os-variant 指定系统的发行版本
–location 指定ISO镜像文件所在的路径,支持使用网络资源路径,也就是说可以使用URL
–disk path 指定虚拟硬盘所存放的路径及名称,size 则是指定该硬盘的可用大小,单位是G
–bridge 指定使用哪一个桥接网卡,也就是说使用桥接的网络模式
–graphics 指定是否开启图形
–console 定义终端的属性,target_type 则是定义终端的类型
–extra-args 定义终端额外的参数
1
virt-install --name=gurobi --memory=512,maxmemory=1024 --vcpus=1,maxvcpus=2 --os-type=linux --os-variant=rhel7 --location=/tmp/CentOS-7-x86_64-DVD-1708.iso --disk path=/kvm_data/gurobi.img,size=10 --bridge=br0 --graphics=none --console=pty,target_type=serial --extra-args="console=tty0 console=ttyS0"
1
2
3
4
5
6
7
8
9
virsh console xxx # 进入指定的虚拟机,进入的时候还需要按一下回车
virsh start xxx # 启动虚拟机
virsh shutdown xxx # 关闭虚拟机
virsh destroy xxx # 强制停止虚拟机
virsh undefine xxx # 彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘
virsh autostart xxx # 设置宿主机开机时该虚拟机也开机
virsh autostart --disable xxx # 解除开机启动
virsh suspend xxx # 挂起虚拟机
virsh resume xxx # 恢复挂起的虚拟机