Skip to content

Instantly share code, notes, and snippets.

@vladan
Created November 15, 2015 21:17
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save vladan/425ba5b351ad5b7c8677 to your computer and use it in GitHub Desktop.
Save vladan/425ba5b351ad5b7c8677 to your computer and use it in GitHub Desktop.
virt helper scripts
#!/bin/sh
if [ $# -eq 0 ]
then
echo "Usage: vmclone orig-name [cloned-vm-name]"
echo "If cloned-vm-name isn't provided '-clone' is appended to the original name."
exit -1
fi
orig=$1
if [ $# -eq 2 ]
then
name=$2
else
name=$orig"-clone"
fi
virt-clone \
--connect qemu:///system \
--original $orig \
--name $name \
--file ~vladan/virt/images/${name}.qcow2
#!/bin/sh
function show_help {
cat << EOF
Usage: ${0##*/} [-m RAM_SIZE] [-d DISK_SIZE] [-t OS_TYPE] VM_NAME ISO_PATH
Install a VM in an image from a specified ISO file.
-h Display this help and exit
-m RAM_SIZE RAM size of the VM in MB (default is 2048)
-d IMG_SIZE Size of the image in GB (default is 20)
-t OS_TYPE Type of the os - debian, arch, fedora (default is debian7)
VM_NAME Name of the VM. The image is created from this name.
ISO_PATH Path to the installation media.
EOF
exit 0
}
if [ $# -eq 0 ]
then
show_help
fi
image_dir=/home/vladan/virt/images
# A POSIX variable
OPTIND=1 # Reset in case getopts has been used previously in the shell.
# Initialize our own variables:
ram=2048
size=20
os='debian7'
while getopts ":h:m:n:d:t:i:" opt; do
case "$opt" in
h)
show_help
;;
m)
ram=${OPTARG}
;;
d)
size=${OPTARG}
;;
t)
os=${OPTARG}
;;
'?')
show_help
;;
esac
done
name=${@:$OPTIND:1}
iso=${@:$OPTIND+1:1}
shift $((OPTIND-1))
[ "$1" = "--" ] && shift
# check for missing postitional args
if [ -z "$name" ]; then echo "NAME IS EMPTY"; show_help; fi
if [ -z "$iso" ]; then echo "ISO IS EMPTY"; show_help; fi
image=${image_dir}/${name}.qcow2
if [ -f ${image} ];
then
echo
echo "Image file ${image} exists on filesystem, aborting!!!"
echo
while true; do
read -p "Do you wish to remove it? [y/n] " yn
case $yn in
[Yy]* ) rm -f ${image} && exit;;
[Nn]* ) exit;;
* ) echo "Please answer yes or no.";;
esac
done
fi
qemu-img create ${image} -f qcow2 ${size}G
echo
echo "virt-install --name=${name} --arch=x86_64 --vcpus=1 --ram=${ram} --os-type=linux --os-variant=${os} --hvm --connect=qemu:///system --cdrom=${iso} --disk path=${image},size=${size} --accelerate --vnc --network bridge=virbr0 --noautoconsole --keymap=us"
echo
virt-install \
--name=${name} \
--arch=x86_64 \
--vcpus=1 \
--ram=${ram} \
--os-type=linux \
--os-variant=${os} \
--hvm \
--connect=qemu:///system \
--cdrom=${iso} \
--disk path=${image},size=${size} \
--accelerate \
--vnc \
--network bridge=virbr0 \
--noautoconsole \
--keymap=us
#!/bin/sh
name=$1
mac=$(virsh dumpxml $name | egrep -io '([0-9a-f]{2}:){5}[0-9a-f]{2}')
echo $(arp | grep $mac | cut -d " " -f 1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment