Skip to content

Instantly share code, notes, and snippets.

@whitekid
Last active December 17, 2015 01:09
Show Gist options
  • Save whitekid/5526141 to your computer and use it in GitHub Desktop.
Save whitekid/5526141 to your computer and use it in GitHub Desktop.
KVM 인스턴스 생성 스크립트
#!/bin/bash
set -e
function usage(){
echo "Usage: `basename $0` [domain]"
echo " -m memory as Mb"
echo " -s disk as Gb"
echo " -n comma separated network"
echo " -f first delete image file"
echo
}
function fatal(){
echo $@
exit
}
function free_loop(){
for dev in `ls /dev/loop[0-9]*`; do
if losetup -a | grep -q "$dev"; then
continue
fi
echo $dev
return
done
fatal 'no available loop device'
}
while getopts "m:s:n:f" opt; do
case $opt in
m)
MEM="$OPTARG"
;;
s)
SIZE="$OPTARG"
;;
n)
NET="$OPTARG"
;;
f)
recreate=1
;;
\?)
exit
;;
esac
done
shift $((OPTIND-1))
domain=$1
if [ -z "$domain" ]; then
usage
fatal 'domain name required'
fi
MEM=${MEM:-1024}
SIZE=${SIZE:-10}
NET=${NET:-br0}
for net in ${NET//,/ }; do
net_opt="$net_opt --network bridge:$net,model=virtio "
done
IMAGE_DIR=`dirname $0`/images
image=${IMAGE_DIR}/${domain}.qcow2
if [ "$recreate" == "1" ]; then
virsh destroy $domain || true
virsh undefine $domain || true
rm -f ${image}
fi
qemu-img create -f qcow2 $image ${SIZE}G
boot_opt="--cdrom ${IMAGE_DIR}/ubuntu-12.04.4-server-amd64.iso"
virt-install \
--name="$domain" \
--vcpus=1 \
--ram=${MEM} \
--os-type=linux \
--os-variant=ubuntuprecise \
$net_opt \
$boot_opt \
--disk path=${image},format=qcow2,bus=virtio,cache=none \
--graphics vnc,listen=0.0.0.0 \
--accelerate \
--noautoconsole
virsh vncdisplay $domain
# vim: nu ai ts=4 sw=4
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment