linux----虚拟机的创建
1.安装虚拟机
[[email protected] ~]# cd /var/lib/libvirt/images/ ----> 切换到虚拟机硬盘文件目录下
[[email protected] images]# ll -s
total 5926192
78148 -rw-r--r-- 1 qemu qemu 80084992 Oct 23 22:03 kai.qcow2
57864 -rw-r--r-- 1 qemu qemu 59310080 Oct 20 11:18 rh124-desktop-vda.ovl
1174208 -rw-r--r-- 1 qemu qemu 1202388992 Oct 13 12:19 rh124-desktop-vda.qcow2
196 -rw-r--r-- 1 qemu qemu 197120 Oct 18 11:03 rh124-desktop-vdb.ovl
[[email protected] images]# cd /etc/libvirt/qemu/ ----> 切换到虚拟机前端管理文件目录下
[[email protected] qemu]# ll -s
total 20
4 -rw------- 1 root root 2502 Oct 13 12:19 desktop.xml
4 -rw------- 1 root root 3838 Oct 23 21:47 kai.xml
[[email protected] mnt]# vim createvm.sh ----> 编辑安装脚本安装
#!/bin/bash
virt-install \
--cdrom /home/kiosk/Desktop/linux923/isos/rhel-server-7.0-x86_64-dvd.iso \
--ram 1024 \
--cpus 1 \
--disk /var/lib/libvirt/images/test.qcow2,bus=virtio,size=10 \
--name test \
--network bridge=br0,model=virtio &
2.虚拟机管理
virt-manager ----> 开启图形管理工具
virt-viewer vmname ----> 显示虚拟机,vmname表示虚拟机名称
virsh list ----> 列出正在运行的vm
virsh list --all ----> 列出所有vm
virsh start vmname ----> 运行指定vm
virsh shutdown vmname ----> 正常关闭指定vm
virsh destroy vmname ----> 强行结束指定vm
virsh create vmname.xml ----> 临时恢复指定vm,vmname表示前端管理文件
virsh define vmname.xml ----> 永久恢复vm
virsh undefine vmname ----> 删除vm的前端管理,不会删除硬盘文件
3.拷贝虚拟机
#### (1)
Tips:从A主机拷贝虚拟机(以拷贝的虚拟机叫node1为例)到B主机上
拷贝A主机硬盘文件 :cp/var/lib/libvirt/images/node1.qcow2 U盘dir
拷贝A主机前端管理文件:cp/etc/libvirt/qemu/node1.xml U盘dir (xml:可扩展标记语言,用作数据封装)
将硬盘文件移动到B主机指定路径下:mv U盘dir/node1.qcow2 /var/lib/libvirt/images/
将硬盘文件移动到B主机指定路径下:mv U盘dir/node1.xml /etc/libvirt/qemu/
永久恢复vm:virsh definenode.xml
#### (2)
准备一个已经安装好的虚拟机
mv /etc/libvirt/qemu/test.xml /mnt/
mv /var/lib/libvirt/images/test.qcow2 /mnt/
在虚拟机管理器中删除这个装好的虚拟机
模拟恢复过程
cd /mnt
virsh create test.xml ----> 用硬件信息启动虚拟机
在操作过程中会有以下错误:
error:Cannot access storage file '/var/lib/libvirt/images/test.qcow2' (a07,gid:107):No such file or directory
解决方案:
mv /mnt/test.qcow2/var/lib/libvirt/images/
4.制作虚拟机快照
[[email protected] images]# qemu-img create -f qcow2 -b /var/lib/libvirt/images/rhel7.0.qcow2 /var/lib/libvirt/images/kai.qcow2
Formatting '/var/lib/libvirt/images/kai.qcow2', fmt=qcow2 size=9663676416 backing_file='/var/lib/libvirt/images/rhel7.0.qcow2' encryption=off cluster_size=65536 lazy_refcounts=off
[[email protected] images]# ll
total 5848240
-rw-r--r-- 1 root root 197120 Oct 23 21:23 kai.qcow2
注意:这个只是创建了硬盘文件而没有前端管理文件,我们可以采用图形
的方式新建前端管理文件;操作如下
[[email protected] mnt]# vim kaizhao.sh ----> 编辑快照脚本安装
#!/bin/bash
qemu-img create -f qcow2 -b \
/var/lib/libvirt/images/$1.qcow2 \
/var/lib/libvirt/images/$2.qcow2
virt-install \
--name $2 \
--ram 1024 \
--cpus 1 \
--disk /var/lib/libvirt/images/$2.qcow2,bus=virtio \
--network bridge=br0,model=virtio \
--import &
[[email protected] mnt]# chmod +x kaizhao.sh
[[email protected] mnt]# ./kaizhao.sh yu hao
[[email protected] images]# du -sh hao.qcow2 ----> 查看虚拟机实际大小
72M hao.qcow2
[[email protected] images]# du -sh yu.qcow2
3.3G yu.qcow2
5.快照restart
[[email protected] mnt]# vim restart.sh ----> 编辑快照restart脚本
#!/bin/bash
virsh destroy $1
rm -rf /var/lib/libvirt/images/$1.qcow2
qemu-img create -f qcow2 -b /var/lib/libvirt/images/$2.qcow2 \
/var/lib/libvirt/images/$1.qcow2
virsh start $1
virt-viewer $1 &
[[email protected] mnt]# chmod 700 restart.sh ----> 给文件添加执行权限
[[email protected] mnt]# ./restart.sh hao yu ----> 以vm (yu)为模版重新编辑vm (hao)