脚本安装虚拟机
#!/bin/bash
 2 /usr/sbin/virt-install
 3     --vnc \
 4     --noautoconsole \
 5     --name=rhel6base \
 6     --ram=1024 \
 7     --arch=x86_64 \
 8     --vcpus=1 \
 9     --os-type=linux \
10     --os-variant=rhel6 \
11     --hvm \
12     --accelerate \
13     --disk=/dev/vgtest/lvtest \
14     -m 52:54:00:00:01:00 \
15     -w bridge=br0 \
16     --location=ftp://192.168.1.1/rhel6 \
17     --extra-args="ks=ftp://192.168.1.1/ks.cfg"
~                                          



实现能够创建多台虚拟机的脚本(虚拟机克隆):
#!/bin/bash

IMG_DIR=/var/lib/libvirt/p_w_picpaths
BASE_VM=rhel6base
E_NOINPUT=65
E_NOTNUM=66
E_OUT_OF_RANGE=67
E_VM_EXISTS=68

read -p "please input a vm number: " NEW_VMNU
if [ -z $NEW_VMNU ]; then
 echo "You must input a number."
 exit $E_NOINPUT
fi

if [ $NEW_VMNU != $(echo "$NEW_VMNU*1" | bc) ]; then
 echo "You must input a number."
 exit $E_NOTNUM
fi

if [ $NEW_VMNU -lt 1 -o $NEW_VMNU -gt 254 ]; then
 echo "You must input a number between 1 and 254"
 exit $E_OUT_OF_RANGE
fi

NEW_VM=${BASE_VM}node${NEW_VMNU}

if [ -e $IMG_DIR/$NEW_VM.img ]; then
 echo "$NEW_VM already exists"
 exit $E_VM_EXISTS
fi

echo -en "creating disk p_w_picpath......\t\t"
qemu-img create -b $IMG_DIR/$BASE_VM.img -f qcow2 $IMG_DIR/$NEW_VM.img &> /dev/null
echo -e "\e[32m[OK]\e[0m"
echo

virsh dumpxml $BASE_VM > /tmp/$BASE_VM.xml

MAC_ADDR=$(echo "obase=16;$NEW_VMNU" | bc)

sed -i "/$BASE_VM/s/$BASE_VM/$NEW_VM/" /tmp/$BASE_VM.xml
sed -i "//s/.\{36\}/$(uuidgen)/" /tmp/$BASE_VM.xml
sed -i "/$BASE_VM\.img/s/$BASE_VM\.img/$NEW_VM\.img/" /tmp/$BASE_VM.xml
sed -i "/mac address/s/00'/$MAC_ADDR'/" /tmp/$BASE_VM.xml

virsh define /tmp/$BASE_VM.xml &> /dev/null

echo "$NEW_VM created."