linux系统中快速安装虚拟机(镜像,快照,)

1.本地镜像安装

见此博客
https://blog.csdn.net/ninimino/article/details/107771513

在这里插入图片描述

注意几点问题:

1.安装时间较长
2.内存大于2G,否则图形打不开
3.cpu  1
4.硬盘选7g以上,本人经常选8G
5.磁盘分区的时候
  custom
 /boot    500M
 /swap  500M
 /    剩下的所有空间
          

在这里插入图片描述

脚本安装,跳过到装机界面

man virt-install 查看虚拟机安装命令的帮助

在这里插入图片描述

没设置网桥时

[root@westos_student4 ~]# cat vm_create.sh 
#!/bin/bash
virt-install \
--cdrom /isos/rhel-8.2-x86_64-dvd.iso \      ####选择安装类型
--memory 2048 \    ##内存
--vcpus 1 \    ###cpu
--disk /var/lib/libvirt/images/$*.qcow2,size=8,bus=virtio \   ##disk路径,大小,类型
--name $*    

在这里插入图片描述

设置网桥后

[root@westos_student4 mnt]# cat vm_create.sh 
#!/bin/bash
virt-install \
  --cdrom /root/rhel-8.0-x86_64-dvd\(1\).iso \
  --memory 2048 \
  --vcpus 1 \
  --disk path=/var/lib/libvirt/$*.qcow2,size=8,bus=virtio \
  --network bridge=br0,model=virtio \
  --os-variant rhel8.0 \
  --name $*

网桥
在这里插入图片描述

跳到装机界面

在这里插入图片描述

2.快照

建立母盘本地镜像安装好lzy虚拟机,将lzy虚拟机作为母盘,(删除的时候不删除硬盘信息,如图
在这里插入图片描述
在这里插入图片描述

[root@westos_student4 ~]# cd /var/lib/libvirt/images/
[root@westos_student4 images]# ls
lzy.qcow2
[root@westos_student4 images]# virsh undefine lzy 删掉虚拟机,不删除硬件信息
Domain lzy has been undefined
##或者在图形界面删除虚拟机的时候不勾选硬件信息选项
[root@westos_student4 images]# ls
lzy.qcow2
lzy.qcow2作为母盘,建立快照,node1,在创建新的虚拟机node1的时候选择node1.qcow2,当此虚拟机出现故障的时候,直接重新快照
在这里插入图片描述在这里插入图片描述

在这里插入图片描述在这里插入图片描述在这里插入图片描述在这里插入图片描述

快照脚本(打开虚拟机)

没设置网桥前

[root@westos_student4 ~]# cat snapshot_vm.sh 
#!/bin/bash
qemu-img create \
-f qcow2 \
-b /var/lib/libvirt/images/lzy.qcow2 \   ###lzy.qcow2母盘
/var/lib/libvirt/images/$*.qcow2

virt-install \
--import \
--memory 2048 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$*.qcow2,size=8,bus=virtio \
--name $*

设置网桥后

[root@westos_student4 mnt]# cat snapshot_vm.sh 
#!/bin/bash
qemu-img create \
-f qcow2 \
-b /var/lib/libvirt/images/lzy.qcow2 \
/var/lib/libvirt/images/$*.qcow2 \

virt-install \
--import \
--memory 2048 \
--vcpus 1 \
--disk /var/lib/libvirt/images/$*.qcow2,size=8,bus=virtio \
--network bridge=br0,model=virtio \
--os-variant rhel8.0 \
--name $* 

在这里插入图片描述

3. 传输别人的虚拟机,硬件文件打开虚拟机

/etc/libvirt/qemu **.xml ---->硬件信息

/var/lib/libvirt/images---->硬盘信息
传输别人的虚拟机硬件信息***.xml文件硬盘信息***.qcow2到本机的/var/lib/libvirt/images/就可以打开虚拟机,注意:如果别人的虚拟机通过快照建立,也要把母盘传过来``
或者复制别人的母盘,在自己的系统中进行快照,建立虚拟机
如果别人的虚拟机不是通过快照建立的,则可以直接复制硬件信息和硬盘文件,就可以打开虚拟机

————————————————————————
[root@westos_student4 images]#scp [email protected]:/etc/libvirt/qemu/Yang.xml /var/lib/libvirt/images/
[email protected]’s password:
Yang.xml 100% 5690 4.6MB/s 00:00

[root@westos_student4 images]#scp [email protected]:/var/lib/libvirt/images/Yang.qcow2 /var/lib/libvirt/images/
[email protected]’s password:
Yang.qcow2 100% 384KB 44.1MB/s 00:00

[root@westos_student4 images]# scp [email protected]:/var/lib/libvirt/images/Mark.qcow2 /var/lib/libvirt/images/ ##复制母本(yang虚拟机是通过快照建立的
[email protected]’s password:
Mark.qcow2 100% 8194MB 108.7MB/s 01:15

[root@westos_student4 images]# ls
lzy.qcow2 Mark.qcow2 Yang.qcow2 Yang.xml

[root@westos_student4 images]# virsh create Yang.xml ##通过硬件信息打开虚拟机,虚拟机关闭后消失
Domain Yang created from Yang.xml

[root@westos_student4 images]# virsh define Yang.xml ####通过硬件信息文件恢复虚拟机,(打开)
Domain Yang defined from Yang.xml

virsh undefine westos 删掉

4.虚拟机恢复脚本

[root@westos_student4 ~]# cat vm_reset.sh 
#!/bin/bash

virsh destroy $*
rm -fr /var/lib/libvirt/images/$*.qcow2

qemu-img create \
-f qcow2 \
-b /var/lib/libvirt/images/lzy.qcow2 \
/var/lib/libvirt/images/$*.qcow2

virsh start $*
virt-viewer $* &>/dev/null &

在这里插入图片描述
在这里插入图片描述
恢复虚拟机脚本
在这里插入图片描述
在这里插入图片描述

猜你喜欢

转载自blog.csdn.net/ninimino/article/details/109334621