一、下载虚拟机
1、下载虚拟机
[root@westos_student12 ~]# mount /isos/rhel-8.2-x86_64-dvd.iso /var/www/html/westos ##挂载镜像
mount: /var/www/html/westos: WARNING: device write-protected, mounted read-only.
[root@westos_student12 ~]# dnf group list --hidden
[root@westos_student12 ~]# dnf group install "Virtualization Client" "Virtualization Hypervisor" "Virtualization Tools"
[root@westos_student12 ~]# systemctl enable --now libvirtd.service
Created symlink /etc/systemd/system/multi-user.target.wants/libvirt-guests.service → /usr/lib/systemd/system/libvirt-guests.service.
[root@westos_student12 ~]# virt-manager ##启动虚拟机
Virtualization Client -------虚拟化客户端
Virtualization Tools --------虚拟化工具
Virtualization Hypervisor ------虚拟化核心套件
2、虚拟化相关信息
服务名称 | libvirtd |
---|---|
虚拟化核心 | qemu/kvm |
虚拟化存储目录 | /var/lib/libvirt/images/westos.qcow2 |
虚拟化硬件信息 | /etc/libvirt/qemu/westos.xml |
二、虚拟机管理命令
virt-viewer westos | 显示 westos 虚拟机 |
---|---|
virt-manager | 打开虚拟机控制器 |
virsh list | 列出运行的虚拟机 |
virsh list --all | 列出所有虚拟机 |
virsh start westos | 开启虚拟机 |
virsh shutdown westos | 正常关闭虚拟机 |
virsh destroy westos | 断电 westos 虚拟机 |
[root@westoslinux images]# virsh shutdown node1
[root@westoslinux images]# virsh start node1
[root@westoslinux images]# virt-viewer node1
[root@westos_student12 ~]# virt-viewer node1 &> /dev/null & %不显示警告信息
[2] 7875
三、建立虚拟机
四、建立虚拟机快照
什么是快照?
磁盘“快照”是虚拟机磁盘文件(VMDK)在某个点及时的复本。系统崩溃或系统异常,可以通过使用恢复到快照来保持磁盘文件系统和系统存储。当升级应用和服务器及给它们打补丁的时候,快照是救世主。
简而言之:就是类似于照相中照片一样,我们对照片进行涂改(写入数据),如果发现出错,重新再拍一张就行,而不直接对虚拟机本身进行操作。
[root@westos_student12 ~]# cd /var/lib/libvirt/images
[root@westos_student12 images]# qemu-img create -f qcow2 -b lee.qcow2 node1.qcow2
##虚拟机快照创建命令 -f 表示快照格式(fmt) -b 要备份的虚拟机镜像文件(母盘) 虚拟机快照文件
[root@westos_student12 images]# ls
dan.qcow2 node1.qcow2
[root@westos_student12 images]# file node1.qcow2
node1.qcow2: QEMU QCOW Image (v3), has backing file (path dan.qcow2), 8589934592 bytes
虚拟机快照被损坏后,可以删除原文件,重新进行快照。
[root@westos_student12 images]# rm -fr node1.qcow2
[root@westos_student12 images]# qemu-img create -f qcow2 -b lee.qcow2 node1.qcow2
[root@westos_student12 images]# ls
lee.qcow2 node1.qcow2
五、复制其它主机的虚拟机
[root@westos_student12 images]# systemctl enable --now sshd
[root@westos_student12 ~]# ssh root@172.25.254.3 ##连接他人电脑 从别的主机迁移到自己主机里
[root@westos_student3 ~]# cd /var/lib/libvirt/images/
[root@westos_student3 images]# ls
linux1.qcow2
[root@westos_student3 images]# scp linux1.qcow2 root@172.25.254.12:/var/lib/libvirt/image
[root@westos_student3 ~]# cd /etc/libvirt/qemu
[root@westos_student3 qemu]# ls
rhel8.2.xml
[root@westos_student3 qemu]# scp rhel8.2.xml root@172.25.254.12:/var/lib/libvirt/image
[root@westos_student12 images]# ls
lee.qcow2 linux1.qcow2 node1.qcow2 rhel8.2.xml
[root@westos_student12 images]# virsh creat rhel8.2.xml ##一次性打开
[root@westos_student12 images]# virsh define rhel8.2.xml ##永久打开
[root@westos_student12 images]# virt-viewer linux1.qcow2
六、运用脚本创建虚拟机及快照
1、运用脚本创建虚拟机
[root@westos_student12 images]# man virt-install ##查看帮助
[root@westos_student12 ~]# vim create_vm.sh
1 #!/bin/bash
2 virt-install \
3 --cdrom /isos/rhel-8.2-x86_64-dvd.iso \
4 --memory 2048 \
5 --vcpus 1 \
6 --disk /var/lib/libvirt/images/$*.qcow2,size=8,bus=virtio \
7 --name $*
[root@westos_student12 ~]# sh create_vm.sh test ## 运用脚本安装虚拟机
2、运用脚本创建虚拟机快照
2.1 运用脚本创建虚拟机快照
[root@westos_student12 mnt]# vim snapshoot_vm.sh
#!/bin/bash
qemu-img create \
-f qcow2 \
-b /var/lib/libvirt/images/lee.qcow2 \
/var/lib/libvirt/images/$*.qcow2
virt-install \
--memory 2048 \
--vcpus 1 \
--name $* \
--disk /var/lib/libvirt/images/$*.qcow2 \
--import
[root@westoslinux mnt]# sh snapshoot_vm.sh node2
2.2 运用脚本重置虚拟机快照
[root@westos_student12 mnt]# vim vm_reset.sh
#!/bin/bash
virsh destroy $*
rm -fr /var/lib/libvirt/images/$*.qcow2
qemu-img create \
-f qcow2 \
-b /var/lib/libvirt/images/lee.qcow2 \
/var/lib/libvirt/images/$*.qcow2
virsh start $*
virt-viewer $* &> /dev/null &
[root@westos_student12 mnt]# sh vm_reset.sh node2
七、虚拟机桥接上网
[root@westos_student12 ~]# cd /etc/sysconfig/network-scripts/
[root@westos_student12 network-scripts]# ls
[root@westos_student12 network-scripts]# mv * /mnt/
[root@westos_student12 network-scripts]# vim ifcfg-enp2s0
DEVICE=enp2s0 ##设备 网卡名字 ifconfig 查看
ONBOOT=yes ##在网络启动服务的时候网卡自动激活
BOOTPROTO=none ##网卡工作方式为静态的
BRIDGE=br0 ##网卡开启的桥接接口名称为br0
[root@westos_student12 network-scripts]# vim ifcfg-br0
DEVICE=br0
ONBOOT=yes
BOOTPROTO=none
IPADDR=172.25.254.12
NETMASK=255.255.255.0 子网掩码
TYPE=Bridge
[root@westos_student12 network-scripts]# reboot
[root@localhost ~]# nm-connection-editor ##在虚拟机里执行
[root@localhost ~]# ping 172.25.254.12