创建虚拟机
有两种方式建立虚拟机,根据使用的环境来决定自己建立的方式
- 使用图形界面建立
- 使用命令提示符(vnc方式,console口方式)建立
使用图形界面建立
打开虚拟系统管理器
[root@foundation0 /]# virt-manager
打开左上角图标,进入新建虚拟机窗口
选择使用本地安装介质,点击下一步
点击左下角添加资源池
填入名称,点击下一步
选择存放iso的目录文件,点击完成
点击右下角Choose Volume
确认镜像文件选择无误,点击下一步
选择内存大小和cpu核心数量,这里用的2048MB和2核心,根据所需自行调整,点击下一步
选择映射硬盘的大小,这里选择的是20G的空间,点击下一步
确认下信息有无错误,点击完成进行安装
将光标移动到第一项,回车进入到系统安装流程
接下来的流程就跟普通安装虚拟机一样啦,下列为简化流程,具体请跳转VMware Workstation安装Centos 8教程
选择好时区,安装模式,即可点击开始安装
设置好root密码,静静的等待安装即可
安装完成,点击重启完成最后工作
使用命令提示符建立
使用vnc方式建立
安装实例代码如下
[root@foundation0 kiosk]# virt-install -n rhel7-2 -r 2048 --vcpus 4 -l /content/rhel7.5/x86_64/isos/rhel-server-7.5-x86_64-dvd.iso --os-type=linux --os-variant=rhel7 --disk /var/lib/libvirt/images/rhel7-2.img,format=qcow2,size=8,sparse=false -w network=default --graphics vnc,listen='0.0.0.0',port=8001
常用参数解析
-n 指定虚拟机的名称,不能与已存在的重复 -r 指定虚拟机的内存大小,大小MB --vcpus 指定虚拟CPU的核心个数,不能超过真实机器的大小 -l 指定安装源的位置,可使用iso镜像方式 -c 指定安装源的位置。如: -c /dev/cdrom --ostype 指定安装的系统类型 --os-variant 指定安装的系统版本 --disk 指定虚拟机的虚拟硬盘所在位置和名称,格式(默认为raw格式,可用raw或qcow2),大小,sparse=false表示磁盘空间不立即分配,而是按需分配 -w 指定虚拟机的网络连接,这里桥接的是default网络 --graphics 指定虚拟机图形驱动,这里采用的是vnc的方式输出,监听任意地址,端口8001
接下来的流程就跟普通装机流程一样,并没有太大的区别
使用console方式建立
有时服务器是没有图形界面的,所以并不能使用vnc的方式安装
[root@foundation0 kiosk]# virt-install -n rhel7-3 -r 768 --vcpus 4 -l /content/rhel7.5/x86_64/isos/rhel-server-7.5-x86_64-dvd.iso --os-type=linux --os-variant=rhel7 --disk /var/lib/libvirt/images/rhel7-3.img,format=qcow2,size=8,sparse=false -w network=default --extra-args="text console=tty0 console=ttyS0,115200" --nographics
console参数解析,相同的上文有介绍过了,就不再写一遍了
text console=tty0 console=ttyS0,115200 tt0是指物理机上的当前进程bash的控制终端,ttyS0是指虚拟机上的控制终端,两者建立联系,则虚拟机可以输出信息到达物理机控制终端tty0。115200指的是传输速率
接下来会在终端看到这个界面,需要把必要的序号中的信息填写完毕,不然会显示感叹号,其实也就是把图形安装界面换成了代码的形式
将需要修改的信息,选择它的序号并填入,示例:修改时区为亚洲——上海
Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 2 ================================================================================ ================================================================================ Time settings Timezone: not set NTP servers:not configured 1) Set timezone 2) Configure NTP servers Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: 1 ================================================================================ ================================================================================ Timezone settings Available regions 1) Europe 6) Pacific 10) Arctic 2) Asia 7) Australia 11) US 3) America 8) Atlantic 12) Etc 4) Africa 9) Indian 5) Antarctica Please select the timezone. Use numbers or type names directly [b to region list, q to quit]: 2 ================================================================================ ================================================================================ Timezone settings Available timezones in region Asia 9) Baghdad 37) Karachi 64) Shanghai 10) Bahrain 38) Kathmandu 65) Singapore 28) Ho_Chi_Minh Please select the timezone. Use numbers or type names directly [b to region list, q to quit]: 64
修改root密码
Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 3) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 5) [!] Installation Destination 6) [x] Kdump (No disks selected) (Kdump is enabled) 7) [ ] Network configuration 8) [!] Root password (Not connected) (Password is not set.) 9) [!] User creation (No user will be created) Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 8 ================================================================================ ================================================================================ Please select new root password. You will have to type it twice. Password: Password (confirm): ================================================================================ ================================================================================ Question The password you have provided is weak: The password fails the dictionary check - it is too simplistic/systematic. Would you like to use it anyway? Please respond 'yes' or 'no': yes
修改默认硬盘安装方式,无特殊要求,默认即可,输入c开始继续下一步
================================================================================ ================================================================================ Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 3) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 5) [!] Installation Destination 6) [x] Kdump (No disks selected) (Kdump is enabled) 7) [ ] Network configuration 8) [x] Root password (Not connected) (Password is set.) 9) [ ] User creation (No user will be created) Please complete all spokes before continuing Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: 5 Probing storage... Installation Destination [x] 1) : 8192 MiB (vda) 1 disk selected; 8192 MiB capacity; 8192 MiB free ... Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: c Autopartitioning Options [ ] 1) Replace Existing Linux system(s) [x] 2) Use All Space [ ] 3) Use Free Space Installation requires partitioning of your hard drive. Select what space to use for the install target. Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: c ================================================================================ ================================================================================ Partition Scheme Options [ ] 1) Standard Partition [ ] 2) Btrfs [x] 3) LVM [ ] 4) LVM Thin Provisioning Select a partition scheme configuration. Please make your choice from above ['q' to quit | 'c' to continue | 'r' to refresh]: c Generating updated storage configuration Checking storage configuration...
最后全部感叹号解决完毕,输入b正式开始安装
Installation 1) [x] Language settings 2) [x] Time settings (English (United States)) (Asia/Shanghai timezone) 3) [x] Installation source 4) [x] Software selection (Local media) (Minimal Install) 5) [x] Installation Destination 6) [x] Kdump (Automatic partitioning (Kdump is enabled) selected) 8) [x] Root password 7) [ ] Network configuration (Password is set.) (Not connected) 9) [ ] User creation (No user will be created) Please make your choice from above ['q' to quit | 'b' to begin installation | 'r' to refresh]: b
最后静静的等待安装即可,可以按Ctrl+【将安装界面推入到后台安装,不影响继续操作
Virt控制指令
虚拟机的控制
virsh,打开虚拟化的交互式终端,平时使用也可以不使用交互式界面
[root@foundation0 kiosk]# virsh Type: 'help' for help with commands 输入help+指令就可以获得该指令的帮助信息 'quit' to quit 输入quit退出 virsh # destroy rhel7-3 域rhel7-4已被关闭 virsh # start rhel7-4 域rhel7-4已开始 virsh # suspend rhel7-4 域rhel7-4被挂起 virsh # resume rhel7-4 域rhel7-4被重新恢复 virsh # autostart rhel7-4 域rhel7-4标记为自动挂起 virsh # autostart --disable rhel7-4 域rhel7-4取消标记为自动挂起 virsh # quit 退出
查看各域的状态
[root@foundation0 kiosk]# virsh list Id Name State ---------------------------------------------------- 1 classroom running 9 rhel7-4 running
查看所有域的状态
root@foundation0 kiosk]# virsh list --all Id Name State ---------------------------------------------------- 1 classroom running 9 rhel7-4 running - desktop shut off - rhel7-2 shut off - rhel7-3 shut off - rhel7.5 shut off - server shut off [root@foundation0 kiosk]# virsh shutdown 9 支持使用序号来进行操作,域 9 已被关机 [root@foundation0 kiosk]# virsh destroy 9 域 9 已被强制关机 [root@foundation0 kiosk]# virsh start rhel7-4 域 rhel7-4已被开启,关机之后序号会消失,启动时需要打全称
调整资源
内存部分
调整rhel7-3的mem资源,查看rhel7-3内存使用情况,可看到rhel7-3的总内存为739M
[root@rhel7-3 ~]# free -h total used free shared buff/cache available Mem: 739M 90M 510M 5.1M 138M 506M Swap: 819M 0B 819M
在关机状态,将内存扩容到1024M
[root@foundation0 /]# virsh Welcome to virsh, the virtualization interactive terminal. virsh # destroy rhel7-3 Domain rhel7-3 destroyed [root@foundation0 /]#virsh setmaxmem rhel7-3 1024M --config 设置最大内存限制为1024M [root@foundation0 /]#virsh setmem rhel7-3 1024M --config 设置实际内存大小 [root@foundation0 /]#virsh start rhel7-3 Domain rhel7-3 started
现在在rhel7-3上重新查看内存大小,已成功扩容
[root@rhel7-3 ~]# free -h total used free shared buff/cache available Mem: 991M 95M 774M 6.7M 120M 749M Swap: 819M 0B 819M
在机器启动的状态下,将内存动态调整到600M大小
[root@foundation0 /]#virsh setmem rhel7-3 600M --config --live
现在在rhel7-3上重新查看内存大小,已成功缩小
[root@rhel7-3 ~]# free -h total used free shared buff/cache available Mem: 567M 95M 350M 6.7M 120M 325M Swap: 819M 0B 819M
硬盘部分
查看虚拟器管理器里面的存储池列表
[root@foundation0 /]#virsh pool-list Name State Autostart ------------------------------------------- default active yes iso active yes
查看default这个存储池的磁盘卷列表
[root@foundation0 /]#virsh vol-list --pool default Name Path ------------------------------------------------------------------------------ rhel7-2.img /var/lib/libvirt/images/rhel7-2.img rhel7-3.img /var/lib/libvirt/images/rhel7-3.img rhel7-4.img /var/lib/libvirt/images/rhel7-4.img rhel7.5.qcow2 /var/lib/libvirt/images/rhel7.5.qcow2
可以使用dd qemu-img创建磁盘卷,也可以使用virsh vol-create-as,下面采用的是第一种
[root@foundation0 /]#virsh vol-create-as default rhel7-3.1.img 2G [root@foundation0 /]#virsh attach-disk rhel7-3 /var/lib/libvirt/images/rhel7-3.1.img vdb --config
在rhel7-3上查看硬盘情况,已经增加了一块vdb的磁盘
[root@rhel7-3 ~]# fdisk -l|grep vdb Disk /dev/vdb: 2147 MB, 2147483648 bytes, 4194304 sectors
还可以使用脚本的方式创建磁盘卷
[root@foundation0 /]# qemu-img create /var/lib/libvirt/images/rhel7-3.2.img 1G [root@foundation0 /]# vi newdisk-01.xml <disk type='file' device='disk'> <dirver name='qemu' type='raw' cache='none'/> <source file='/var/lib/libvirt/images/rhel7-3.2.img'/> <target dev='vdc' bus='virtio'/> </disk> :wq [root@foundation0 /]# virsh attach-device --persistent rhel7-3 newdisk-01.xml
在rhel7-3上查看硬盘情况,已经增加了一块vdc的磁盘
[root@rhel7-3 ~]# fdisk -l|grep vdc Disk /dev/vdc: 1073 MB, 1073741824 bytes, 2097152 sectors
分离rhel7-3上的vdc硬盘
[root@foundation0 /]# virsh detach-disk rhel7-3 vdc --persistent
NIC部分
查看当前的网络接口
[root@foundation0 /]#virsh iface-list Name State MAC Address --------------------------------------------------- br0 active fe:54:00:00:00:fe lo active 00:00:00:00:00:00
查看虚拟系统管理器内的虚拟网络列表
[root@foundation0 /]#virsh net-list Name State Autostart Persistent ---------------------------------------------------------- br1 active yes yes default active yes yes privbr0 active yes yes privnet active yes yes
给rhel7-3增加网卡,第一块网卡连接到default网络,第二块网卡连接到br0桥接到物理网卡(可添加–mod参数选择需要的模式,不填即是默认)
[root@foundation0 /]#virsh attach-interface rhel7-3 network default --config [root@foundation0 /]#virsh attach-interface rhel7-3 bridge br0 --config
使用domiflist查看,已经看到存在和新添加的两块新网卡的MAC地址
[root@foundation0 /]#virsh domiflist rhel7-3 Interface Type Source Model MAC ------------------------------------------------------- vnet2 network default virtio 52:54:00:c5:b2:81 vnet3 bridge br0 rtl8139 52:54:00:6e:37:f9 vnet4 network default rtl8139 52:54:00:5e:fd:e8
注意通过virsh attach-interface命令增加的网卡时及时生效的,但是在虚拟机重启后新增加的网卡会消失。
那是因为该命令新增加的网卡,没有更新到虚拟机的配置文件中。要使该网卡永久生效,我们可以使用virsh dumpxml命令把该配置保存到虚拟机配置文件中。如下:
[root@foundation0 /]# virsh dumpxml rhel7-3 > /etc/libvirt/qemu/rhel7-3.xml [root@foundation0 /]# virsh define /etc/libvirt/qemu/rhel7-3.xml
若不想用了,删除掉第二张网卡
[root@foundation0 /]#virsh detach-interface rhel7-3 bridge 52:54:00:67:c2:39 --config
网络连接方式
nat
默认情况下,虚拟机的网卡连接的是Default网络,使用nat技术
在虚拟机窗口点击灯泡按钮,选中网卡即可看到对应选项
查看网络基本信息
查看虚拟网络,虚拟机管理器中点击Edit——Connection Details
可看到default网络的详细信息
手动添加其他虚拟网络
点击左下角的加号,输入网络名称,点击下一步
填入自己所需的网段,点击下一步
到ipv6选项时,默认下一步即可
设置通过任意物理网卡做nat模式,点击完成
每多一个虚拟网络,在物理机上就会增加一块virbrx的网卡
每多一台虚拟机guestX,在物理机上就会增加一块vnetX的网卡
[root@foundation0 /]# ifconfig |grep virbr2 virbr2: flags=4099<UP,BROADCAST,MULTICAST> mtu 1500 [root@foundation0 /]# ifconfig |grep vnet4 vnet4: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
桥接
配置虚拟网络的桥接,必须先停掉NetworkManager服务,因为该服务不支持桥接
[root@foundation0 /]# systemctl stop NetworkManager [root@foundation0 /]# systemctl disable NetworkManager
编辑网卡配置文件
[root@foundation0 /]# cd /etc/sysconfig/network-scripts/ [root@foundation0 network-scripts]# vi ifcfg-ens33 DEVICE=ens33 NM_CONTROLLED=yes USERCTL=no BOOTPROTO=none BRIDGE=br0 ONBOOT=yes TYPE=Ethernet NAME="System ens33" BRIDGE=br0 :wq 保存退出 [root@foundation0 network-scripts]# vi ifcfg-br0 DEVICE=br0 NAME="Bridge br0" ONBOOT=yes NM_CONTROLLED=yes USERCTL=no BOOTPROTO=none IPADDR0=172.25.254.250 PREFIX0=24 DEFROUTE=yes ZONE=trusted IPADDR1=172.25.0.250 PREFIX1=24 DNS1=172.25.254.250 DOMAIN="ilt.example.com example.com" IPV6INIT=no PEERNTP=no PEERDNS=yes TYPE=Bridge DELAY=0 STP=no :wq 保存退出
重启网络服务
[root@foundation0 network-scripts]# systemctl restart network
查看桥接相关的信息
[root@foundation0 /]# brctl show bridge name bridge id STP enabled interfaces br0 8000.000c29412b4b no ens33 vnet0
快照
生成快照
如果是raw格式的磁盘,是不支持做快照的,需要将其转换为qocw2格式
# qemu-img convert -f raw -O qcow2 disk_name.raw disk_name.qcow2
额外操作:qcow2格式的磁盘转raw的磁盘:
# qemu-img convert -f qcow2 -O raw disk_name.qcow2 disk_name.raw
格式转化的时候查看进度
# qemu-img convert - p -f qcow2 -O raw disk_name.qcow2 disk_name.raw
查看硬盘的基本情况,可看出已经是qcow2格式的了,就不需要转换了
[root@foundation0 /]# qemu-img info /var/lib/libvirt/images/rhel7-3.img image: /var/lib/libvirt/images/rhel7-3.img file format: qcow2 virtual size: 8.0G (8589934592 bytes) disk size: 1.3G cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: true
但是发现之前额外怎加的rhel7-3.1.img硬盘是raw格式的,不能生成快照,需要转换
开始转换为qcow2格式,再查看硬盘相关信息,已经变成了qcow2格式
[root@foundation0 /]# qemu-img convert -p -f raw -O qcow2 /var/lib/libvirt/images/rhel7-3.1.img /var/lib/libvirt/images/rhel7-3.1.img (100.00/100%) [root@foundation0 /]# qemu-img info /var/lib/libvirt/images/rhel7-3.1.img image: /var/lib/libvirt/images/rhel7-3.1.img file format: qcow2 virtual size: 2.0G (2147483648 bytes) disk size: 512K cluster_size: 65536 Format specific information: compat: 1.1 lazy refcounts: false [root@foundation0 /]#
转换之后还是不够,配置文件并没有动态的修改,需要手动修改。找到<disk></disk>硬盘区域,修改type为qcow2
[root@foundation0 /]# virsh edit rhel7-3 <disk type='file' device='disk'> <driver name='qemu' type='qcow2'/> <source file='/var/lib/libvirt/images/rhel7-3.1.img'/> <target dev='vdb' bus='virtio'/> <address type='pci' domain='0x0000' bus='0x00' slot='0x08' function='0x0'/> </disk> :wq 保存退出
生成快照
[root@foundation0 /]# virsh snapshot-create-as rhel7-3 kz01
查看rhel7-3的快照相关信息
[root@foundation0 /]# virsh snapshot-list rhel7-3 Name Creation Time State ------------------------------------------------------------ kz01 2021-10-27 11:14:11 +0800 shutoff
测试快照
在rhel7-3创建一堆文件
[root@rhel7-3 /]# touch test{1..100} [root@foundation0 kiosk]# virsh destroy rhel7-3 关闭rhel7-3 [root@foundation0 kiosk]# virsh snapshot-revert rhel7-3 kz01 将rhel7-3还原到快照kz01的状态 [root@foundation0 kiosk]# virsh start rhel7-3 重新开启rhel7-3 返回到rhel7-3查看根目录的情况 [root@rhel7-3 ~]# cd / [root@rhel7-3 /]# ls 已经变回正常了 bin dev home lib64 mnt proc run srv tmp var boot etc lib media opt root sbin sys usr
虚拟机的克隆
虚拟机必须挂起或关机状态才可以被克隆
[root@foundation0 /]# virsh destroy rhel7-3
查看虚拟机状态
[root@foundation0 /]# virsh list --all Id Name State ---------------------------------------------------- 1 classroom running - desktop shut off - rhel7-2 shut off - rhel7-3 shut off - rhel7-4 shut off - rhel7.5 shut off - server shut off
开始克隆虚拟机
[root@foundation0 /]# virt-clone -o rhel7-3 -n rhel7-3-clone1 -f /var/lib/libvirt/images/rhel7-3-clone1.qcow2.img --auto-clone
常用参数解析
-o 指定被克隆虚拟机名称 -n 指定克隆出来的虚拟机名称 -f 指定新的虚拟机磁盘的存储位置和名称 --auto-clone 其他没有指定选择则自动决定
打开新的克隆虚拟机测试
[root@foundation0 /]# virsh start rhel7-3-clone1
若是其他端口冲突之类的,可以手动修改起配置文件
[root@foundation0 /]# virsh edit +操作失败的虚拟机