虚拟机有关操作脚本

来源:互联网 发布:网页预览pdf的js插件 编辑:程序博客网 时间:2024/06/11 04:33

编写脚本:
 vim /bin/vm_create.sh
  #!/bin/bash
  case "$1" in
  start)
  virsh start $2 &> /dev/null
  virt-viewer $2 &> /dev/null
  ;;
  stop)
  virsh destroy $2
  ;;
  mksnapshot)
  virsh destroy $2
  virsh undefine $2
  qemu-img create -f qcow2 -b /var/lib/libvirt/images/$2.qcow2 /var/lib/libvirt/images/$3.qcow2
  virt-install \
  --name $3 \
  --ram 1024 \
  --file /var/lib/libvirt/images/$3.qcow2 \
  --import &
  ;;
  reset)
  virsh destroy $2
  rm -fr /var/lib/libvirt/images/$2.qcow2
  qemu-img create -f qcow2 -b /var/lib/libvirt/images/$3.qcow2 /var/lib/libvirt/images/$2.qcow2
  virsh start $2
  virt-viewer $2
  ;;
  remove)
  virsh destroy $2
  virsh undefine $2
  rm -fr /var/lib/libvirt/images/$2.qcow2
  ;;
  install)
  virt-install \
  --name $2 \
  --ram 1024 \
  --file /var/lib/libvirt/images/$2.qcow2 \
  --file-size 8 \
  --location http://172.25.254.62/rhel7.2 \
  --extra-args "ks=http://172.25.254.62/silent.cfg" &
  ;;
  esac
 chmod +x /bin/vm_create.sh ##给脚本可执行权限

注:安装虚拟机之前需配置好system-config-kickstart文件,并存放到/var/www/html/下

1.开启虚拟机
 vm_create.sh start desktop

2.关闭虚拟机
 vm_create.sh stop desktop

3.建立镜像虚拟机(快照)
 vm_create.sh mksnapshot westos westos_1

4.重置虚拟机
 vm_create.sh reset westos_1 westos

5.删除虚拟机
 vm_create.sh remove westos_1

6.自动安装虚拟机
 vm_create.sh install westos

 
0 0
原创粉丝点击