一、查看内核是否支持KVM虚拟化
针对ARM架构服务器,若/dev/kvm 和 /sys/module/kvm任意一个不存在,都说明内核不支持KVM虚拟化
bash
[root@localhost ~]# ls -l /dev/kvm
crw-rw----+ 1 root kvm 10, 232 May 6 09:18 /dev/kvm
[root@localhost ~]# ls /sys/module/kvm
parameters uevent
扩展说明:
针对X86服务器,查看是否支持KVM虚拟化的命令如下:
bash
#若是intel cpu,命令执行结果是vmx;若是amd cpu,命令执行结果是svm;
[root@localhost ~]# egrep -o 'vmx|svm' /proc/cpuinfo
二、关闭核心防护和防火墙
bash
[root@localhost ~]# sed -i 's/enforcing/disabled/' /etc/selinux/config
[root@localhost network-scripts]# getenforce
Enforcing
bash
[root@localhost ~]# systemctl disable firewalld
[root@localhost ~]# systemctl disable iptables
三、配置网桥br0和静态IP
- 使用ifconfig命令查看当前处于UP状态并且处于RUNNING状态的网卡接口
bash
[root@localhost network-scripts]# ifconfig | grep RUNNING
enahisic2i1: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
lo: flags=73<UP,LOOPBACK,RUNNING> mtu 65536
-
基于网卡 enahisic2i1 的网络配置文件,复制生成网桥br0的网络配置文件
bashcp /etc/sysconfig/network-scripts/ifcfg-enahisic2i1 /etc/sysconfig/network-scripts/ifcfg-br0
-
修改网桥br0的网络配置文件,其中需要配置宿主机的静态IP
bash
#Type使用桥接
TYPE=Bridge
#IP分配协议改为静态分配
BOOTPROTO=static
#修改NAME为br0
NAME=br0
#修改DEVICE为br0
DEVICE=br0
ONBOOT=yes
#宿主机的静态IP
IPADDR=192.168.110.66
#子网掩码
PREFIX=24
#网关
GATEWAY=192.168.110.1
#DNS
DNS1=114.114.114.114
- 修改处于UP状态且处于RUNNING状态的网卡 enahisic2i1 的网络配置文件
bash
TYPE=Ethernet
#Type使用Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
#IP分配协议改为静态分配
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
IPV6_ADDR_GEN_MODE=eui64
NAME=enahisic2i1
UUID=2205bd6f-9005-4c6e-92a6-ca7ad19e974f
DEVICE=enahisic2i1
ONBOOT=yes
BRIDGE=br0
#指定桥接设备为br0
- 重启网络
bash
#华为openeuler系统使用该命令,其他linux系统可以使用systemctl restart network
[root@localhost ~]# nmcli con reload; nmcli con up enahisic2i1
- 查看网络连接
bash
##如果配置成功,会有以下输出:
[root@localhost network-scripts]# brctl show
bridge name bridge id STP enabled interfaces
br0 8000.48570264e74c no enahisic2i1
virbr0 8000.5254006e8ff6 yes
-
设置ip转发
##修改内核参数
[root@localhost ~]# vi /etc/sysctl.conf
net.ipv4.ip_forward=1
四、安装GNOME桌面环境
bash
##更新系统
[root@localhost ~]# yum update -y
##安装字库
[root@localhost ~]# yum install dejavu-fonts liberation-fonts \
gnu-*-fonts google-*-fonts -y
##安装Xorg
[root@localhost ~]# yum install xorg-* -y
##这可能会安装很多没用的包,可以使用下面的命令安装必要的xorg相关包
[root@localhost ~]# yum install xorg-x11-apps xorg-x11-drivers xorg-x11-drv-ati \
xorg-x11-drv-dummy xorg-x11-drv-evdev xorg-x11-drv-fbdev xorg-x11-drv-intel \
xorg-x11-drv-libinput xorg-x11-drv-nouveau xorg-x11-drv-qxl \
xorg-x11-drv-synaptics-legacy xorg-x11-drv-v4l xorg-x11-drv-vesa \
xorg-x11-drv-vmware xorg-x11-drv-wacom xorg-x11-fonts xorg-x11-fonts-others \
xorg-x11-font-utils xorg-x11-server xorg-x11-server-utils xorg-x11-server-Xephyr \
xorg-x11-server-Xspice xorg-x11-util-macros xorg-x11-utils xorg-x11-xauth \
xorg-x11-xbitmaps xorg-x11-xinit xorg-x11-xkb-utils -y
##安装GNOME及组件
[root@localhost ~]# yum install adwaita-icon-theme atk atkmm at-spi2-atk \
at-spi2-core baobab \
abattis-cantarell-fonts cheese clutter clutter-gst3 clutter-gtk cogl dconf \
dconf-editor devhelp eog epiphany evince evolution-data-server file-roller folks \
gcab gcr gdk-pixbuf2 gdm gedit geocode-glib gfbgraph gjs glib2 glibmm24 \
glib-networking gmime30 gnome-autoar gnome-backgrounds gnome-bluetooth \
gnome-builder gnome-calculator gnome-calendar gnome-characters \
gnome-clocks gnome-color-manager gnome-contacts gnome-control-center \
gnome-desktop3 gnome-disk-utility gnome-font-viewer gnome-getting-started-docs \
gnome-initial-setup gnome-keyring gnome-logs gnome-menus gnome-music \
gnome-online-accounts gnome-online-miners gnome-photos gnome-remote-desktop \
gnome-screenshot gnome-session gnome-settings-daemon gnome-shell \
gnome-shell-extensions gnome-software gnome-system-monitor gnome-terminal \
gnome-tour gnome-user-docs gnome-user-share gnome-video-effects \
gnome-weather gobject-introspection gom grilo grilo-plugins \
gsettings-desktop-schemas gsound gspell gssdp gtk3 gtk4 gtk-doc gtkmm30 \
gtksourceview4 gtk-vnc2 gupnp gupnp-av gupnp-dlna gvfs json-glib libchamplain \
libdazzle libgdata libgee libgnomekbd libgsf libgtop2 libgweather libgxps libhandy \
libmediaart libnma libnotify libpeas librsvg2 libsecret libsigc++20 libsoup \
mm-common mutter nautilus orca pango pangomm libphodav python3-pyatspi \
python3-gobject rest rygel simple-scan sushi sysprof tepl totem totem-pl-parser \
tracker3 tracker3-miners vala vte291 yelp yelp-tools \
yelp-xsl zenity -y
##启动gdm显示管理器
[root@localhost ~]# systemctl enable gdm
##设置系统默认以图形界面登录
[root@localhost ~]# systemctl set-default graphical.target
##查询当前开机运行级别
[root@localhost ~]# systemctl get-default
##重启生效
[root@localhost ~]# reboot
五、 安装KVM及所需组件
**说明:**如果宿主机没有安装图形桌面,则无需安装virt-manager。virt-manager(Virtual Machine Manager)是图形化虚机管理器,类似于Esxi的Web Console管理页面。
bash
##安装KVM及所需组件
[root@localhost ~]# yum install virt-manager virt-install bridge-utils \
libvirt qemu-kvm virt-viewer libcanberra-gtk2 libiscsi \
dbus-devel virt-clone dejavu-lgc-sans-fonts qemu-img \
libvirt-client virt-v2v libguestfs-tools xorg-x11-xauth -y
##查看virt-manager版本号
[root@localhost ~]# virt-manager --version
##查看virsh版本号
[root@localhost ~]# virsh --version
##查看qemu-img版本号
[root@localhost ~]# qemu-img --version
##查看virt-install版本号
[root@localhost ~]# virt-install --version
六、修改sshd配置文件
bash
[root@localhost ~]# sudo vi /etc/ssh/sshd_config
##确保该配置文件有如下内容
X11Forwarding yes
X11DisplayOffset 10
X11UseLocalhost no
PermitTunnel yes
##重启 sshd 服务
[root@localhost ~]# systemctl restart sshd
七、启动libvirt并设置开机自启动
bash
##启动
[root@localhost ~]# systemctl start libvirtd
##设置开机自启
[root@localhost ~]# systemctl enable libvirtd
##查看libvirt服务是否启动成功
##若服务处于running状态,说明服务启动成功
##可以正常使用libvirt提供的virsh命令行工具
[root@localhost ~]# systemctl status libvirtd
八、创建镜像目录和磁盘文件目录
bash
##查看物理机磁盘空间
##通过df -hT发现/home目录空间最大
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
devtmpfs devtmpfs 4.0M 0 4.0M 0% /dev
tmpfs tmpfs 220G 0 220G 0% /dev/shm
tmpfs tmpfs 88G 60M 88G 1% /run
tmpfs tmpfs 4.0M 0 4.0M 0% /sys/fs/cgroup
/dev/mapper/openeuler-root ext4 69G 15G 51G 23% /
tmpfs tmpfs 220G 0 220G 0% /tmp
/dev/sda2 ext4 974M 204M 704M 23% /boot
/dev/sda1 vfat 599M 6.0M 593M 2% /boot/efi
/dev/mapper/openeuler-home ext4 1.7T 174G 1.5T 11% /home
tmpfs tmpfs 44G 72K 44G 1% /run/user/1003
##建立目录用来存放iso镜像文件
[root@localhost ~]# mkdir /home/iso
##建立目录用来存放qcow2磁盘文件
[root@localhost ~]# mkdir /home/qcow2
九、创建虚拟机的磁盘文件
##进入磁盘文件目录
root@localhost \~\]# cd /home/qcow2 ##创建虚拟机的磁盘文件 ##这里创建了一个名为vm1.qcow2,空间为400GB的动态扩展的qcow2格式的磁盘 ##这里的400GB只是磁盘的最大值,没有被写入数据前只有不到200KB大小 [root@localhost ~]# qemu-img create -f qcow2 vm1.qcow2 400G Formatting 'vm1.qcow2', fmt=qcow2 cluster_size=65536 extended_l2=off compression_type=zlib size=429496729600 lazy_refcounts=off refcount_bits=16 cache=writeback ##查看磁盘文件vm1.qcow2的卷信息,里面disk size是其实际占用的宿主机磁盘大小 ##上面的400GB只是磁盘的最大值,这里可以看到,没有被写入数据前只有200KB大小 [root@localhost ~]# qemu-img info vm1.qcow2 image: vm1.qcow2 file format: qcow2 virtual size: 400 GiB (429496729600 bytes) disk size: 200 KiB cluster_size: 65536 Format specific information: compat: 1.1 compression type: zlib lazy refcounts: false refcount bits: 16 corrupt: false extended l2: false ### 十、图形化创建虚拟机 跳过创建过程 开始安装,配置语言、时区、安装源、网络、密码等信息。 ```bash Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 2) [!] Installation source 4) [!] Software selection (Processing...) (Processing...) 3) [!] Installation Destination 6) [!] Network configuration (Processing...) (Not connected) 4) [!] Root password 8) [!] User creation (Root account is disabled) (No user will be created) Please make a selection from the above ['b' to begin installation, 'q' to quit, 'r' to refresh]: 5 <=======输入5然后按Enter键,设置安装目录 Probing storage... ================================================================================ Installation Destination 1) [x] QEMU HARDDISK: 400 GiB (sda) 1 disk selected; 400 GiB capacity; 400 GiB free Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: c <=======输入c继续 ================================================================================ Partitioning Options 1) [ ] Replace Existing Linux system(s) 2) [x] Use All Space 3) [ ] Use Free Space 4) [ ] Manually assign mount points Installation requires partitioning of your hard drive. Select what space to use for the install target or manually assign mount points. Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: c <=======输入c继续 ================================================================================ Partition Scheme Options 1) [ ] Standard Partition 2) [ ] Btrfs 3) [x] LVM 4) [ ] LVM Thin Provisioning Select a partition scheme configuration. Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: 1 <=======输入1然后按Enter键,选择Standard Partition ================================================================================ Partition Scheme Options 1) [x] Standard Partition 2) [ ] Btrfs 3) [ ] LVM 4) [ ] LVM Thin Provisioning Select a partition scheme configuration. Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: c <=======输入c继续 ================================================================================ Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 2) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 3) [x] Installation Destination 6) [ ] Network configuration (Automatic partitioning (Not connected) selected) 4) [!] Root password 8) [!] User creation (Root account is disabled) (No user will be created) Please make a selection from the above ['b' to begin installation, 'q' to quit, 'r' to refresh]: 7 <=======输入7然后按Enter键,设置root账号的密码 ================================================================================ Root password 1) [ ] SM3 encrypt Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: 1 <=======输入1然后按Enter键,选择SM3加密方式 ================================================================================ Root password 1) [x] SM3 encrypt Please make a selection from the above ['c' to continue, 'q' to quit, 'r' to refresh]: c <=======输入c继续 ================================================================================ Root password Please select new root password. You will have to type it twice. Password: <=======输入密码然后按Enter键,这里不会反显 Password (confirm): <=======再次输入确认密码,然后按Enter键,这里也不会反显 ================================================================================ Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 2) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 3) [x] Installation Destination 6) [ ] Network configuration (Automatic partitioning (Not connected) selected) 4) [x] Root password 8) [ ] User creation (Root password is set) (No user will be created) Please make a selection from the above ['b' to begin installation, 'q' to quit, 'r' to refresh]: b <=======输入b然后按Enter键就开始安装 ================================================================================ ...... ...... Installation complete Use of this product is subject to the license agreement found at: /usr/share/openEuler-release/EULA Installation complete. Press ENTER to quit:<=======安装完成,按Enter键重启虚拟机 重启后就进入到虚拟机的登录界面 ...... [ OK ] Finished OpenEuler Security Tool. [FAILED] Failed to start Network Manager Wait Online. <======= 该问题可以忽略,登陆虚拟机,配置网络后即可解决 See 'systemctl status NetworkManager-wait-online.service' for details. [ OK ] Reached target Network is Online. Starting Crash recovery kernel arming... Starting Notify NFS peers of a restart... Starting System Logging Service... [ OK ] Started Notify NFS peers of a restart. [ OK ] Started System Logging Service. [ OK ] Reached target Multi-User System. Starting Record Runlevel Change in UTMP... [ OK ] Finished Record Runlevel Change in UTMP. [ OK ] Finished Crash recovery kernel arming. localhost login: 登录成功后,这时,如果要退出虚拟机,按 Ctrl + ] 即可。 ``` ### 十一、KVM虚拟机常见管理命令 ```bash ## 从虚拟机退出到宿主机 [root@localhost ~]# Ctrl + ] ## 查看KVM进程 [root@localhost ~]# ps axu |grep kvm ## 查看libvirt的版本信息 [root@localhost ~]# virsh version ## 列出当前有多少个虚拟机,以及其状态 [root@localhost ~]# virsh list ## 列出当前有多少个虚拟机,包括关机状态的虚拟机 [root@localhost ~]# virsh list --all ## 进入指定的虚拟机,进入的时候还需要按一下回车 [root@localhost ~]# virsh console vm1 ## 编辑虚拟机的配置 [root@localhost ~]# virsh edit vm1 ## 使用XML文件创建一个虚拟机 [root@localhost ~]# virsh create vm1.xml ## 使用XML文件定义一个虚拟机,但是不启动 [root@localhost ~]# virsh define vm1.xml ## 启动虚拟机 [root@localhost ~]# virsh start vm1 ## 重启虚拟机 [root@localhost ~]# virsh reboot vm1 ## 关闭虚拟机,比较优雅地做法,按部就班的关闭虚拟机 [root@localhost ~]# virsh shutdown vm1 ## 强制停止虚拟机,是比较暴力的做法,相当于物理机的直接关闭电源 [root@localhost ~]# virsh destroy vm1 ## 彻底销毁虚拟机,会删除虚拟机配置文件,但不会删除虚拟磁盘 [root@localhost ~]# virsh undefine vm1 --nvram ## 设置宿主机开机时该虚拟机也开机 [root@localhost ~]# virsh autostart vm1 ## 解除开机启动 [root@localhost ~]# virsh autostart --disable vm1 ## 列出开机自动启动的虚拟机 [root@localhost ~]# virsh list --autostart ## 挂起虚拟机 [root@localhost ~]# virsh suspend vm1 ## 恢复挂起的虚拟机 [root@localhost ~]# virsh resume vm1 ## 当机器是运行状态时,卸载虚拟磁盘命令(只是当前有效,重启后又回来了) [root@localhost ~]# virsh detach-disk vm1 sdb ## 永久添加磁盘 [root@localhost ~]# virsh attach-disk vm1 \ /var/lib/libvirt/images/disk1/additional/vm1/file1 sdb \ --subdriver=qcow2 --config --live --persistent ## 查看虚拟机有几块网卡 [root@localhost ~]# virsh domiflist vm1 ## 查看虚拟机的磁盘 [root@localhost ~]# virsh domblklist vm1 ## 查看虚拟机的cpu数量 [root@localhost ~]# virsh vcpucount vm1 ```