added old kvm-tools scripts
This commit is contained in:
parent
89500d6376
commit
129ab908a5
|
@ -0,0 +1 @@
|
||||||
|
#this repository is discontinued.
|
|
@ -0,0 +1,41 @@
|
||||||
|
# **** kvm-tools readme file ****
|
||||||
|
|
||||||
|
what we have here is a litte tool to backup your kvm/libvirt virtual machines really convenient! check it out!
|
||||||
|
|
||||||
|
this readme was first released with kvm-tools.sh v0.1rc on the 21st of april 2011, that's why i call it judgementday release.
|
||||||
|
|
||||||
|
|
||||||
|
# **** installation ****
|
||||||
|
# here on a ubuntu machine, but kvm-tools should
|
||||||
|
# run perfectly on all unix based operating systems
|
||||||
|
# as long as there is a bash and libvirt
|
||||||
|
run:
|
||||||
|
sudo apt-get install git-core (if you do not have git installed already)
|
||||||
|
cd /opt
|
||||||
|
sudo git clone git://git.socialnerds.org/kvm-tools.git
|
||||||
|
sudo git clone git://git.socialnerds.org/bashlib.git
|
||||||
|
|
||||||
|
rename the configfile sample to kvm-tools.conf
|
||||||
|
move it to /etc/kvm-tools.conf
|
||||||
|
and change its content to meet your needs
|
||||||
|
|
||||||
|
after configuration run:
|
||||||
|
cd /opt/kvm-tools
|
||||||
|
sudo ./kvm-tools.sh -f
|
||||||
|
|
||||||
|
finished!
|
||||||
|
|
||||||
|
for usage help type:
|
||||||
|
"kvm-tools -h" or just "kvm-tools"
|
||||||
|
|
||||||
|
|
||||||
|
# **** in progress ****
|
||||||
|
future features
|
||||||
|
- browsevdisk (mounting a virtual disk (img, qcow2) in the hosts filesystem)
|
||||||
|
|
||||||
|
|
||||||
|
hints
|
||||||
|
- a domain uuid should just exist once in the configuration folder
|
||||||
|
- only files with .xml will be read in the configpath
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,44 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#
|
||||||
|
# mount script for qcow2 images
|
||||||
|
# v0.1
|
||||||
|
#
|
||||||
|
|
||||||
|
# read imagename
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
echo 'usage: "browseqcow2 <image.qcow2>"'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
imagename=$1
|
||||||
|
|
||||||
|
|
||||||
|
# im i root
|
||||||
|
if [ $(whoami) != "root" ]; then
|
||||||
|
echo "only root can do this"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
# run unmount
|
||||||
|
if [[ $1 = "--unmount" || $1 = "-u" || $1 = "--umount" ]]; then
|
||||||
|
umount /mnt/
|
||||||
|
qemu-nbd --disconnect /dev/nbd0
|
||||||
|
echo "image unmounted"
|
||||||
|
echo
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
|
||||||
|
# load kernel module
|
||||||
|
modprobe nbd max_part=8
|
||||||
|
|
||||||
|
# gen blockdevice
|
||||||
|
qemu-nbd --connect=/dev/nbd0 $imagename
|
||||||
|
|
||||||
|
# make the actual mount
|
||||||
|
sleep 2
|
||||||
|
mount /dev/nbd0p1 /mnt/
|
||||||
|
|
||||||
|
echo "$imagename is mounted in /mnt"
|
||||||
|
echo 'run "browseqcow2.sh --unmount" when ready'
|
||||||
|
echo
|
|
@ -0,0 +1,58 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
if [ $(whoami) = "root" ]; then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
echo "only root can do this"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
echo "hostname?"
|
||||||
|
read hostname
|
||||||
|
echo "username?"
|
||||||
|
read username
|
||||||
|
echo "architecture? [amd64]"
|
||||||
|
read arch
|
||||||
|
echo "disksize? [8]"
|
||||||
|
read disksize
|
||||||
|
|
||||||
|
if [ -z $disksize ]; then
|
||||||
|
disksize=8192
|
||||||
|
else
|
||||||
|
disksize=$((disksize*1024))
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ -z $arch ]; then
|
||||||
|
arch="amd64"
|
||||||
|
fi
|
||||||
|
|
||||||
|
clear
|
||||||
|
echo
|
||||||
|
echo "a virtual machine with following details will be created in $(pwd)"
|
||||||
|
echo
|
||||||
|
echo "hostname: $hostname"
|
||||||
|
echo "os: lucid lynx"
|
||||||
|
echo "architecture: $arch"
|
||||||
|
echo "disksize: $disksize"
|
||||||
|
echo "password: password"
|
||||||
|
echo "proceed? [yes]"
|
||||||
|
read proceed
|
||||||
|
|
||||||
|
if [ -z $proceed ]; then
|
||||||
|
proceed="yes"
|
||||||
|
fi
|
||||||
|
|
||||||
|
if [ $proceed = "yes" ]; then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
ubuntu-vm-builder kvm lucid --arch "$arch" --mem '512' --rootsize "$disksize" --swapsize '1024' --kernel-flavour 'virtual' --hostname "$hostname" --domain 'socialnerds.org' --mirror 'http://roxy.socialnerds.org/ubuntu' --components 'main,restricted,universe,multiverse' --addpkg "ssh" --name 'creator' --user "$username" --pass 'password'
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "creation finished"
|
||||||
|
exit 0
|
||||||
|
|
||||||
|
|
|
@ -0,0 +1,16 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
# reading the imagename
|
||||||
|
echo "what name should it have?"
|
||||||
|
read name
|
||||||
|
else
|
||||||
|
name=$1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# generating the actual image
|
||||||
|
qemu-img create -f qcow2 $name.qcow2 16G
|
||||||
|
|
||||||
|
exit 0
|
|
@ -0,0 +1,14 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
# generating new ids for a vm
|
||||||
|
newuuid=$(uuidgen)
|
||||||
|
newmac=$(MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; echo $MACADDR)
|
||||||
|
|
||||||
|
# printing new ids
|
||||||
|
#clear
|
||||||
|
echo
|
||||||
|
echo "UUID: $newuuid"
|
||||||
|
echo "MAC: $newmac"
|
||||||
|
echo
|
||||||
|
|
||||||
|
exit 0
|
|
@ -0,0 +1,66 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
## ##
|
||||||
|
## firstboot script ##
|
||||||
|
## kvm-tools ##
|
||||||
|
## ##
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
|
||||||
|
# **** configuration section ****
|
||||||
|
sshgroup="sshusers"
|
||||||
|
user=$(cat /etc/passwd | grep 1000)
|
||||||
|
user=${user:0:$(($(echo `expr index "$user" :`)-1))}
|
||||||
|
|
||||||
|
# **** installing fortunes-ubuntu-server ****
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y fortunes-ubuntu-server
|
||||||
|
|
||||||
|
|
||||||
|
# **** motd ****
|
||||||
|
echo 'if you want to have here something like this:
|
||||||
|
___.
|
||||||
|
\_ |__ _______ ___
|
||||||
|
| __ \ / _ \ \/ /
|
||||||
|
| \_\ ( <_> > <
|
||||||
|
|___ /\____/__/\_ \
|
||||||
|
\/ \/.socialnerds.org
|
||||||
|
|
||||||
|
socialnerds mail server
|
||||||
|
|
||||||
|
go to http://www.network-science.de/ascii/ and modify /etc/motd.tail
|
||||||
|
' > /etc/motd.tail
|
||||||
|
|
||||||
|
rm /etc/update-motd.d/10-help-text
|
||||||
|
rm /etc/update-motd.d/00-header
|
||||||
|
echo '#!/bin/bash' > /etc/update-motd.d/00-header
|
||||||
|
echo 'echo "Running Kernel: $(uname -r)"' >> /etc/update-motd.d/00-header
|
||||||
|
chmod +x /etc/update-motd.d/00-header
|
||||||
|
|
||||||
|
|
||||||
|
# **** ssh server config ****
|
||||||
|
echo "
|
||||||
|
__.-._
|
||||||
|
'-._'7'
|
||||||
|
/'.-c
|
||||||
|
| /T
|
||||||
|
_)_/LI
|
||||||
|
|
||||||
|
this machine is
|
||||||
|
protected by a
|
||||||
|
master of the force!
|
||||||
|
" > /etc/issue.net
|
||||||
|
|
||||||
|
sed -i 's/#Banner/Banner/' /etc/ssh/sshd_config
|
||||||
|
echo "" >> /etc/ssh/sshd_config
|
||||||
|
echo "# Restrictions added by firstboot.sh" >> /etc/ssh/sshd_config
|
||||||
|
echo "AllowGroups $sshgroup" >> /etc/ssh/sshd_config
|
||||||
|
echo "" >> /etc/ssh/sshd_config
|
||||||
|
|
||||||
|
addgroup $sshgroup
|
||||||
|
adduser $user $sshgroup
|
||||||
|
|
||||||
|
|
||||||
|
# **** end of script ****
|
||||||
|
exit 0
|
|
@ -0,0 +1,24 @@
|
||||||
|
# **** kvm-tools sample config file ****
|
||||||
|
# this file should be renamed to kvm-tools.conf and moved to /etc
|
||||||
|
|
||||||
|
# locations (do not use tailing slashes)
|
||||||
|
repopath="/opt/kvm-tools" # path to the local kvm-tools git repo
|
||||||
|
configpath="/etc/libvirt/qemu" # path to the xml config files
|
||||||
|
imagepath="/path/to/images" # virtual disks location (just for vm creation)
|
||||||
|
backuppath="/path/to/backup" # backup destination
|
||||||
|
|
||||||
|
# bashlib
|
||||||
|
bashlibpath="/opt/bashlib" # bashlib location
|
||||||
|
|
||||||
|
# logging
|
||||||
|
loglevel="4"
|
||||||
|
log2stdout="1"
|
||||||
|
log2syslog="1"
|
||||||
|
log2file="1"
|
||||||
|
logfile="/var/log/kvm-tools.log"
|
||||||
|
|
||||||
|
# server deployment (ubuntu)
|
||||||
|
defaultdomain="aec.at"
|
||||||
|
defaultmirror="http://updater.aec.at/ubuntu"
|
||||||
|
recpackages="ssh bash-completion acpid unattended-upgrades nload htop tshark nmap aptitude ncdu parted vim logrotate apparmor-profiles apparmor-utils"
|
||||||
|
optpackages="screen nano dnsutils locate dialog pwgen git-core iftop mtr lft zsh"
|
|
@ -0,0 +1,638 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
#######################################
|
||||||
|
## ##
|
||||||
|
## function definitions ##
|
||||||
|
## kvm-tools ##
|
||||||
|
## ##
|
||||||
|
#######################################
|
||||||
|
|
||||||
|
|
||||||
|
# **** usage message ****
|
||||||
|
# this will print the usage message
|
||||||
|
usage()
|
||||||
|
{
|
||||||
|
|
||||||
|
echo "usage: kvm-tools options <args>
|
||||||
|
|
||||||
|
OPTIONS:
|
||||||
|
-c <vmname> create guest
|
||||||
|
-e <vmname> edit guest
|
||||||
|
-a <vmname> add virtual disk to guest
|
||||||
|
-b <vmname|all> backup guest(s)
|
||||||
|
|
||||||
|
-h show this message
|
||||||
|
-v show version information
|
||||||
|
-i generate new id's
|
||||||
|
-f run environment check/fix
|
||||||
|
-u update kvm-tools (pull from git)
|
||||||
|
|
||||||
|
"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** version message ****
|
||||||
|
# this will print the version message
|
||||||
|
version()
|
||||||
|
{
|
||||||
|
|
||||||
|
echo
|
||||||
|
echo "kvm-tools"
|
||||||
|
echo
|
||||||
|
echo "vesion: $version"
|
||||||
|
echo "author: $author"
|
||||||
|
echo
|
||||||
|
|
||||||
|
log debug "version - version information printed"
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** get image path(s) ****
|
||||||
|
# with parse the image path(s) from xml
|
||||||
|
getimgpath()
|
||||||
|
{
|
||||||
|
|
||||||
|
local raw=$(virsh dumpxml $1 | grep "file=" | grep -v "/dev/" | tr -d "<'>")
|
||||||
|
for item in $raw; do
|
||||||
|
if [ $item = "source" ]; then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
echo ${item:5:$(($(echo `expr length "$item"`)-6))}
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
# **** get xml path ****
|
||||||
|
# will search for matching uuid in $configpath and defines global variable $xmlpath
|
||||||
|
getxmlpath()
|
||||||
|
{
|
||||||
|
|
||||||
|
local list=$(ls $configpath | grep xml | grep -v "~")
|
||||||
|
local uuid=$(virsh domuuid $1)
|
||||||
|
local i=0
|
||||||
|
for var in $list; do
|
||||||
|
if [ -w $configpath/$var ]; then
|
||||||
|
local raw=$(cat $configpath/$var | grep uuid)
|
||||||
|
local uuidfromfile=${raw:8:36}
|
||||||
|
if [ $uuidfromfile = $uuid ]; then
|
||||||
|
xmlpath="$configpath/$var"
|
||||||
|
let i++
|
||||||
|
fi
|
||||||
|
else
|
||||||
|
log error 'getxmlpath - some of the xml configuration files are not writeable, run "kvm-tools -f" to fix this'
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
if [ $i = "1" ]; then
|
||||||
|
:
|
||||||
|
return 0
|
||||||
|
elif [ $i = "0" ]; then
|
||||||
|
log error "getxmlpath - xml file for $1 could not be determined in $configpath"
|
||||||
|
exit 1
|
||||||
|
elif [ $i -gt "1" ]; then
|
||||||
|
log error "getxmlpath - there is more than one file with matching uuid in $configpath"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** dialog window creation ****
|
||||||
|
# creates the dialog windows
|
||||||
|
graph()
|
||||||
|
{
|
||||||
|
|
||||||
|
local bgtitle="kvm-tools $version | $1"
|
||||||
|
|
||||||
|
if [ $2 = "--inputbox" ]; then
|
||||||
|
local size="7 80"
|
||||||
|
elif [ $2 = "--yesno" ]; then
|
||||||
|
local size="20 80"
|
||||||
|
elif [ $2 = "--fselect" ]; then
|
||||||
|
local size="12 80"
|
||||||
|
else
|
||||||
|
local size="20 80 14"
|
||||||
|
fi
|
||||||
|
|
||||||
|
dialog --backtitle "$bgtitle" --no-cancel "$2" "$3" $size $4 2> /tmp/dialog
|
||||||
|
local returncode=$?
|
||||||
|
clear
|
||||||
|
dialogresult=$(cat /tmp/dialog)
|
||||||
|
rm /tmp/dialog
|
||||||
|
return $returncode
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** backup ****
|
||||||
|
# the virtual disk image and the xml config file
|
||||||
|
# will be saved to the backuppath
|
||||||
|
backup()
|
||||||
|
{
|
||||||
|
|
||||||
|
# am i root?
|
||||||
|
amiroot
|
||||||
|
|
||||||
|
# creat local variable with given machines (comma seperated list)
|
||||||
|
local list=$1
|
||||||
|
|
||||||
|
# creat local variables with all running/all shutoff machines (whitespace seperated)
|
||||||
|
local running=$(virsh list --all | grep "running" | awk '{print $2}')
|
||||||
|
local shutoff=$(virsh list --all | grep "shut off" | awk '{print $2}')
|
||||||
|
|
||||||
|
# check for given option "all"
|
||||||
|
if [ $list = "all" ]; then
|
||||||
|
# if "all" was given fill the list with all machines
|
||||||
|
list="$running $shutoff"
|
||||||
|
else
|
||||||
|
# otherwise just change the commas to whitespaces in the list
|
||||||
|
local list=${list//,/ }
|
||||||
|
fi
|
||||||
|
|
||||||
|
# getting a short timestamp like this 20110423
|
||||||
|
#local timestamp=$(gettimestamp short)
|
||||||
|
|
||||||
|
# $var will be the machine name
|
||||||
|
for var in $list; do
|
||||||
|
log info "backup - starting backup of $var"
|
||||||
|
|
||||||
|
# BACKUP XML file to backuppath
|
||||||
|
virsh dumpxml $var > $backuppath/$var.xml
|
||||||
|
log debug "backup - backing up xml file for $var"
|
||||||
|
|
||||||
|
# SUSPEND machine if running
|
||||||
|
local run=0
|
||||||
|
if [ $(echo $shutoff | grep -c $var) = "0" ]; then
|
||||||
|
log debug "backup - suspending $var"
|
||||||
|
virsh suspend $var &> /dev/null
|
||||||
|
local run=$?
|
||||||
|
fi
|
||||||
|
|
||||||
|
# check if suspend was successfull (if not.. skip)
|
||||||
|
if [ $run = 0 ]; then
|
||||||
|
|
||||||
|
# check for more than one image file
|
||||||
|
for item in $(getimgpath $var); do
|
||||||
|
|
||||||
|
# CEATE HASH for image diff
|
||||||
|
log debug "backup - creating sha1 hash of $item"
|
||||||
|
local sum=$(sha1sum $item | awk '{print $1}')
|
||||||
|
|
||||||
|
# get new filename
|
||||||
|
local newfile=${item##/*/}
|
||||||
|
#local ending=${item:$(echo `expr index "$item" .`)}
|
||||||
|
|
||||||
|
# BACKUP IMAGE file to backuppath
|
||||||
|
log debug "backup - backing up $item"
|
||||||
|
cp $item $backuppath/$newfile
|
||||||
|
|
||||||
|
# RESUME machine if paused
|
||||||
|
if [ $(echo $shutoff | grep -c $var) = "0" ]; then
|
||||||
|
log debug "backup - resuming $var"
|
||||||
|
virsh resume $var &> /dev/null
|
||||||
|
fi
|
||||||
|
|
||||||
|
# CHECK if HASH matches
|
||||||
|
log debug "backup - creating sha1 hash of $backuppath/$newfile"
|
||||||
|
if [ $sum = $(sha1sum $backuppath/$newfile | awk '{print $1}') ]; then
|
||||||
|
log debug "backup - hash for $newfile was successfully checked"
|
||||||
|
log info "backup - $var successfully backuped"
|
||||||
|
else
|
||||||
|
log error "backup - hash for $newfile does not match"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
else
|
||||||
|
log error "backup - could not suspend $var, skipping backup of $var"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** edit vm config (xml) ****
|
||||||
|
# opens vim with xml file for given guest
|
||||||
|
edit()
|
||||||
|
{
|
||||||
|
|
||||||
|
# i shouldn't be root
|
||||||
|
amiroot not
|
||||||
|
# get the xml filepath for $1
|
||||||
|
getxmlpath $1
|
||||||
|
# create sum for diff
|
||||||
|
sha1sum $xmlpath > /tmp/checksum
|
||||||
|
vi $xmlpath
|
||||||
|
sha1sum -c --status /tmp/checksum
|
||||||
|
if [ "$?" = "0" ]; then
|
||||||
|
log debug "edit - $USER did not change xml configuration for $1"
|
||||||
|
else
|
||||||
|
virsh define $xmlpath &> /dev/null
|
||||||
|
log info "edit - $USER changed xml configuration for $1"
|
||||||
|
fi
|
||||||
|
rm /tmp/checksum
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** fix stuff :) ****
|
||||||
|
# environment check, fixes stuff :)
|
||||||
|
fix()
|
||||||
|
{
|
||||||
|
|
||||||
|
# am i root?
|
||||||
|
amiroot
|
||||||
|
|
||||||
|
# install missing dependencies
|
||||||
|
log debug "fix - installing missing dependencies"
|
||||||
|
apt-get update
|
||||||
|
apt-get install -y dialog pwgen libvirt-bin python-vm-builder libxml-xpath-perl
|
||||||
|
|
||||||
|
# reseting file permissions in $configpath
|
||||||
|
chgrp -R libvirtd $configpath
|
||||||
|
chmod -R 775 $configpath
|
||||||
|
log debug "fix - reseted permissions in $configpath"
|
||||||
|
|
||||||
|
# fixing perms in $imagepath
|
||||||
|
chmod -R 775 $imagepath
|
||||||
|
chgrp -R libvirtd $imagepath
|
||||||
|
log debug "fix - reseted permission in $imagepath"
|
||||||
|
|
||||||
|
if [ -r $logfile ]; then
|
||||||
|
chmod 775 $logfile
|
||||||
|
chgrp libvirtd $logfile
|
||||||
|
log debug "fix - reseted permission for $logfile"
|
||||||
|
else
|
||||||
|
touch $logfile
|
||||||
|
chmod 775 $logfile
|
||||||
|
chgrp libvirtd $logfile
|
||||||
|
log debug "fix - created $logfile and set permissions"
|
||||||
|
fi
|
||||||
|
|
||||||
|
# adding kvm-tools to /usr/bin (symlink)
|
||||||
|
if [ -r /usr/bin/kvm-tools ]; then
|
||||||
|
:
|
||||||
|
else
|
||||||
|
ln -s $repopath/kvm-tools.sh /usr/bin/kvm-tools
|
||||||
|
log debug "fix - created a symlink for kvm-tools in /usr/bin"
|
||||||
|
fi
|
||||||
|
log info "fix - environment fix for kvm-tools successfully ran"
|
||||||
|
return 0
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
#browseqcow()
|
||||||
|
#{
|
||||||
|
# # read imagename
|
||||||
|
# if [ -z $1 ]; then
|
||||||
|
# echo 'usage: "browseqcow2 </path/to/image.qcow2>"'
|
||||||
|
# exit 1
|
||||||
|
# fi
|
||||||
|
#
|
||||||
|
# imagename=$1
|
||||||
|
#
|
||||||
|
#
|
||||||
|
# # im i root
|
||||||
|
# amiroot
|
||||||
|
#
|
||||||
|
# # run unmount
|
||||||
|
# if [[ $1 = "--unmount" || $1 = "-u" || $1 = "--umount" ]]; then
|
||||||
|
# umount /mnt/
|
||||||
|
# qemu-nbd --disconnect /dev/nbd0
|
||||||
|
# echo "image unmounted"
|
||||||
|
# echo
|
||||||
|
# exit 0
|
||||||
|
# fi
|
||||||
|
#
|
||||||
|
# # load kernel module
|
||||||
|
# modprobe nbd max_part=8
|
||||||
|
#
|
||||||
|
# # gen blockdevice
|
||||||
|
# qemu-nbd --connect=/dev/nbd0 $imagename
|
||||||
|
#
|
||||||
|
# # make the actual mount
|
||||||
|
# sleep 2
|
||||||
|
# mount /dev/nbd0p1 /mnt/
|
||||||
|
#
|
||||||
|
# echo "$imagename is mounted in /mnt"
|
||||||
|
# echo 'run "browseqcow2.sh --unmount" when ready'
|
||||||
|
# echo
|
||||||
|
#}
|
||||||
|
|
||||||
|
|
||||||
|
# **** add image ****
|
||||||
|
# attaches new created virtual disk to given guest
|
||||||
|
addimage()
|
||||||
|
{
|
||||||
|
|
||||||
|
# no need for root privileges
|
||||||
|
amiroot not
|
||||||
|
|
||||||
|
# virtual machine name (passed)
|
||||||
|
local hostname=$1
|
||||||
|
|
||||||
|
# genqcow2
|
||||||
|
genqcow $hostname
|
||||||
|
|
||||||
|
# xmlpath for $hostname
|
||||||
|
getxmlpath $hostname
|
||||||
|
|
||||||
|
local targets="vdb vdc vdd vde vdf vdg vdh vdi vdj"
|
||||||
|
for var in $targets; do
|
||||||
|
virsh dumpxml $hostname | grep $var
|
||||||
|
if [ $? = 1 ]; then
|
||||||
|
local where=$(sed -n '/\/disk/=' $xmlpath)
|
||||||
|
for item in $where; do
|
||||||
|
local nr=$item
|
||||||
|
done
|
||||||
|
sed ''$nr' a <disk type="file" device="disk"> <driver name="qemu" type="qcow2" /> <source file="'$imagepath/$imagename'" /> <target dev="'$var'" bus="virtio" /> </disk>' $xmlpath > $xmlpath"_new"
|
||||||
|
rm $xmlpath && mv $xmlpath"_new" $xmlpath
|
||||||
|
virsh define $xmlpath
|
||||||
|
log info "addimage - $var was added to $hostname"
|
||||||
|
chmod 775 $xmlpath
|
||||||
|
chgrp libvirtd $xmlpath
|
||||||
|
exit 0
|
||||||
|
else
|
||||||
|
log error "addimage - $var already exists for $hostname"
|
||||||
|
fi
|
||||||
|
done
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** does image exist? ****
|
||||||
|
# checks if images exists and gives back errorcode(0 if its there, 1 of not)
|
||||||
|
imgexisting()
|
||||||
|
{
|
||||||
|
|
||||||
|
# imgexisting needs global vars: $imagepath
|
||||||
|
# usage: imgexisting hostname imagename
|
||||||
|
|
||||||
|
# passed args
|
||||||
|
local hostname=$1
|
||||||
|
local imagename=$2
|
||||||
|
|
||||||
|
virsh dumpxml $hostname | grep $imagename
|
||||||
|
local inxml=$?
|
||||||
|
ls $imagepath | grep $imagename
|
||||||
|
local inpath=$?
|
||||||
|
if [ $inpath = 0 ]; then
|
||||||
|
log info "imgexisting - $imagename exists in $imagepath"
|
||||||
|
return 0
|
||||||
|
elif [ $inxml = 0 ]; then
|
||||||
|
log info "imgexisting - $imagename exists in the xml of $hostname"
|
||||||
|
return 0
|
||||||
|
else
|
||||||
|
log info "imgexisting - $imagename is not existing"
|
||||||
|
return 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** generate qcow2 image ****
|
||||||
|
# creates a qcow2 image in the $imagepath
|
||||||
|
genqcow()
|
||||||
|
{
|
||||||
|
|
||||||
|
local bgmessage="virtual disk creation"
|
||||||
|
local hostname=$1
|
||||||
|
|
||||||
|
# imagename
|
||||||
|
local exists=0
|
||||||
|
while [ $exists = 0 ]; do
|
||||||
|
graph "$bgmessage" --inputbox "imagename?" $hostname"_storage.qcow2"
|
||||||
|
imagename="$dialogresult" #global var
|
||||||
|
imgexisting $hostname $imagename
|
||||||
|
local exists=$?
|
||||||
|
done
|
||||||
|
|
||||||
|
# imagesize
|
||||||
|
graph "$bgmessage" --menu "imagesize?" "4 GB 8 GB 16 GB 32 GB 64 GB 128 GB"
|
||||||
|
local imagesize="$dialogresult"
|
||||||
|
|
||||||
|
# generating the actual image
|
||||||
|
qemu-img create -f qcow2 $imagepath/$imagename $imagesize"G"
|
||||||
|
log info "genqcow - image with $imagesize"GB" created: $imagepath/$imagename"
|
||||||
|
chgrp libvirtd $imagepath/$imagename
|
||||||
|
chmod 775 $imagepath/$imagename
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** generates new id's ****
|
||||||
|
# prints new ids to the logengine
|
||||||
|
newids()
|
||||||
|
{
|
||||||
|
|
||||||
|
# you shouldn't be root
|
||||||
|
amiroot not
|
||||||
|
|
||||||
|
# generating new ids for a vm
|
||||||
|
local newuuid=$(uuidgen)
|
||||||
|
local newmac=$(MACADDR="52:54:$(dd if=/dev/urandom count=1 2>/dev/null | md5sum | sed 's/^\(..\)\(..\)\(..\)\(..\).*$/\1:\2:\3:\4/')"; echo $MACADDR)
|
||||||
|
|
||||||
|
# printing new ids
|
||||||
|
log info "newids - $USER generated new id's"
|
||||||
|
echo
|
||||||
|
echo "UUID: $newuuid"
|
||||||
|
echo "MAC: $newmac"
|
||||||
|
echo
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# **** create virtual ubuntu server ****
|
||||||
|
# this will start the wizard for creating a new virtual machine
|
||||||
|
mkjeos()
|
||||||
|
{
|
||||||
|
|
||||||
|
# config section
|
||||||
|
local bgmessage="server deployment"
|
||||||
|
local hostname=$1
|
||||||
|
|
||||||
|
# am i root?
|
||||||
|
amiroot
|
||||||
|
|
||||||
|
# configuration wizard
|
||||||
|
proceed=1
|
||||||
|
while [ $proceed != "0" ]; do
|
||||||
|
|
||||||
|
# hosname
|
||||||
|
graph "$bgmessage" --inputbox "hostname?" $hostname
|
||||||
|
local hostname="$dialogresult"
|
||||||
|
|
||||||
|
# domain
|
||||||
|
graph "$bgmessage" --inputbox "domain?" $defaultdomain
|
||||||
|
local domain="$dialogresult"
|
||||||
|
|
||||||
|
# username
|
||||||
|
graph "$bgmessage" --inputbox "username?"
|
||||||
|
local username="$dialogresult"
|
||||||
|
|
||||||
|
# architecture
|
||||||
|
graph "$bgmessage" --menu "architecture?" "amd64 64bit_kernel i386 32bit_kernel"
|
||||||
|
local arch="$dialogresult"
|
||||||
|
|
||||||
|
# memory
|
||||||
|
graph "$bgmessage" --menu "memory?" "256 MB 512 MB 1024 MB 2048 MB 4096 MB 8192 MB"
|
||||||
|
local mem="$dialogresult"
|
||||||
|
|
||||||
|
# rootsize
|
||||||
|
graph "$bgmessage" --menu "rootsize?" "4 GB 8 GB 16 GB 32 GB 64 GB 128 GB"
|
||||||
|
local rootsize="$dialogresult"
|
||||||
|
|
||||||
|
# swapsize
|
||||||
|
graph "$bgmessage" --menu "swapsize?" "512 MB 1024 MB 2048 MB 4096 MB 8192 MB"
|
||||||
|
local swapsize="$dialogresult"
|
||||||
|
|
||||||
|
# ubuntu release
|
||||||
|
graph "$bgmessage" --menu "ubuntu release?" "lucid 10.04_long_term hardy 8.04_long_term maverick 10.10"
|
||||||
|
local release="$dialogresult"
|
||||||
|
|
||||||
|
# network configuration
|
||||||
|
graph "$bgmessage" --inputbox "ip address?"
|
||||||
|
local ip="$dialogresult"
|
||||||
|
graph "$bgmessage" --inputbox "netmask?"
|
||||||
|
local netmask="$dialogresult"
|
||||||
|
graph "$bgmessage" --inputbox "gateway?"
|
||||||
|
local gateway="$dialogresult"
|
||||||
|
graph "$bgmessage" --inputbox "dns server(s)?"
|
||||||
|
local dns="$dialogresult"
|
||||||
|
|
||||||
|
# mirrors
|
||||||
|
local mirrorlist="$defaultmirror default_mirror http://at.archive.ubuntu.com/ubuntu official_austria_mirror http://ftp.halifax.rwth-aachen.de/ubuntu uni_aachen"
|
||||||
|
graph "$bgmessage" --menu "mirror?" "$mirrorlist"
|
||||||
|
local mirror="$dialogresult"
|
||||||
|
|
||||||
|
# cpus
|
||||||
|
graph "$bgmessage" --menu "cpus?" "1 singlecore 2 dualcore 4 quadcore 8 insane"
|
||||||
|
local cpus="$dialogresult"
|
||||||
|
|
||||||
|
# local network bridges
|
||||||
|
local bridgelist=$(ifconfig | grep HWaddr | awk '{print $1}' | grep br)
|
||||||
|
local localbridges=""
|
||||||
|
for var in $bridgelist; do
|
||||||
|
local localbridges="$localbridges $var local_network_bridge"
|
||||||
|
done
|
||||||
|
graph "$bgmessage" --menu "network?" "$localbridges"
|
||||||
|
local bridge="$dialogresult"
|
||||||
|
|
||||||
|
# timezone
|
||||||
|
#local timezonelist="Europe/Vienna GMT+1"
|
||||||
|
#graph "$bgmessage" --menu "timezone?" "$timezonelist"
|
||||||
|
#local timezone="$dialogresult"
|
||||||
|
local timezone="Europe/Vienna"
|
||||||
|
|
||||||
|
# additional software
|
||||||
|
for var in $recpackages; do
|
||||||
|
local packagegraph="$packagegraph $var recommended on"
|
||||||
|
done
|
||||||
|
for var in $optpackages; do
|
||||||
|
local packagegraph="$packagegraph $var optional off"
|
||||||
|
done
|
||||||
|
graph "$bgmessage" --checklist "additional software?" "$packagegraph"
|
||||||
|
local packagelist="${dialogresult//\"/ }"
|
||||||
|
for var in $packagelist; do
|
||||||
|
local addpkg="$addpkg --addpkg $var"
|
||||||
|
done
|
||||||
|
|
||||||
|
# select firstboot script
|
||||||
|
#graph "$bgmessage" --fselect ~/
|
||||||
|
graph "$bgmessage" --fselect $repopath/firstboot.sh
|
||||||
|
local firstboot="$dialogresult"
|
||||||
|
|
||||||
|
|
||||||
|
#calculate rootsize
|
||||||
|
local rootsize=$((rootsize*1024))
|
||||||
|
#gen password
|
||||||
|
local password=$(pwgen -snc 10 1)
|
||||||
|
|
||||||
|
|
||||||
|
local text=$(echo "hostname: $hostname
|
||||||
|
domain: $domain
|
||||||
|
user: $username
|
||||||
|
cpus: $cpus
|
||||||
|
memory: $mem
|
||||||
|
ip: $ip
|
||||||
|
release: $release
|
||||||
|
architecture: $arch
|
||||||
|
rootsize: $rootsize
|
||||||
|
swapsize: $swapsize
|
||||||
|
network: $bridge
|
||||||
|
mirror: $mirror
|
||||||
|
firstboot: $firstboot
|
||||||
|
|
||||||
|
continue?
|
||||||
|
(CTRL+C will kill the creation process)
|
||||||
|
")
|
||||||
|
|
||||||
|
graph "$bgmessage" --yesno "$text"
|
||||||
|
local proceed=$?
|
||||||
|
|
||||||
|
done
|
||||||
|
|
||||||
|
clear
|
||||||
|
log debug "mkjeos - creation of $hostname started. this can take a while, so please be patient."
|
||||||
|
|
||||||
|
|
||||||
|
vmbuilder kvm ubuntu --verbose --mem="$mem" --cpus="$cpus" --rootsize="$rootsize" --swapsize="$swapsize" --domain="$domain" --user="$username" --pass="$password" --suite="$release" --flavour=virtual --mirror="$mirror" --security-mirror="$mirror" --timezone="$timezone" --arch="$arch" --hostname="$hostname" --libvirt="qemu:///system" --bridge="$bridge" --ip="$ip" --mask="$netmask" --gw="$gateway" --dns="$dns" $addpkg --firstboot=$firstboot --destdir="$imagepath/tmpvmbuilder" &> /tmp/builderlog
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
if [ $? = "0" ]; then
|
||||||
|
log info "mkjeos - $hostname successfully created"
|
||||||
|
|
||||||
|
# removing temporary builderlog
|
||||||
|
rm /tmp/builderlog
|
||||||
|
|
||||||
|
# moving imagefile, redefining xml (changing image path)
|
||||||
|
mv $imagepath/tmpvmbuilder/tmp* $imagepath/$hostname"_"system.qcow2
|
||||||
|
rm -r $imagepath/tmpvmbuilder
|
||||||
|
local image=$(getimgpath $hostname)
|
||||||
|
getxmlpath $hostname
|
||||||
|
sed -i 's/'${image//\//\\\/}'/'${imagepath//\//\\\/}'\/'$hostname'_system.qcow2/' $xmlpath
|
||||||
|
virsh define $xmlpath
|
||||||
|
|
||||||
|
# run env fix
|
||||||
|
fix
|
||||||
|
|
||||||
|
# start the machine?
|
||||||
|
graph "$bgmessage" --yesno "everything is done, do you want to start $hostname now?"
|
||||||
|
local startvm=$?
|
||||||
|
if [ $startvm = 0 ]; then
|
||||||
|
virsh start $hostname
|
||||||
|
fi
|
||||||
|
|
||||||
|
# log
|
||||||
|
log info "mkjeos - hostname=$hostname.$domain, user=$username, password=$password release=$release, arch=$arch, size=$rootsize, swap=$swapsize, network=$bridge, ip=$ip, mirror=$mirror"
|
||||||
|
|
||||||
|
# machine details output
|
||||||
|
clear
|
||||||
|
echo
|
||||||
|
echo "machine details"
|
||||||
|
echo
|
||||||
|
echo "hostname=$hostname.$domain"
|
||||||
|
echo "user=$username"
|
||||||
|
echo "password=$password"
|
||||||
|
echo "release=$release"
|
||||||
|
echo "arch=$arch"
|
||||||
|
echo "size=$rootsize"
|
||||||
|
echo "swap=$swapsize"
|
||||||
|
echo "network=$bridge"
|
||||||
|
echo "ip=$ip"
|
||||||
|
echo "mirror=$mirror"
|
||||||
|
echo
|
||||||
|
else
|
||||||
|
log error "mkjeos - there was an error creating virtual machine $hostname"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
# end of functions
|
|
@ -0,0 +1,122 @@
|
||||||
|
#!/bin/bash
|
||||||
|
|
||||||
|
|
||||||
|
#########################################
|
||||||
|
## ##
|
||||||
|
## kvm-tools ##
|
||||||
|
## ##
|
||||||
|
#########################################
|
||||||
|
|
||||||
|
|
||||||
|
# **** configuration section ****
|
||||||
|
# do not touch as long as you are not me
|
||||||
|
version="0.2_beta1"
|
||||||
|
author="david@socialnerds.org"
|
||||||
|
giturl="git://git.aec.at/kvm-tools.git"
|
||||||
|
functionfile="kvm-tools.func"
|
||||||
|
configfile="/etc/kvm-tools.conf"
|
||||||
|
logwhat="kvm-tools"
|
||||||
|
|
||||||
|
|
||||||
|
# **** bash trap initialisation ****
|
||||||
|
trap bashtrap INT
|
||||||
|
|
||||||
|
|
||||||
|
# **** read config file ****
|
||||||
|
if [ -r $configfile ]; then
|
||||||
|
source $configfile
|
||||||
|
else
|
||||||
|
echo "ERROR: configuration file not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# **** read function definitions ****
|
||||||
|
if [ -r $repopath/$functionfile ]; then
|
||||||
|
source $repopath/$functionfile
|
||||||
|
else
|
||||||
|
echo "ERROR: functionfile not found."
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# **** load bashlib ****
|
||||||
|
if [ -d $bashlibpath ]; then
|
||||||
|
source $bashlibpath/main
|
||||||
|
source $bashlibpath/update
|
||||||
|
source $bashlibpath/logengine
|
||||||
|
log debug "preflight - bashlib loaded"
|
||||||
|
else
|
||||||
|
echo "ERROR: bashlib not found"
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# **** option handler ****
|
||||||
|
while getopts "h,c:,e:,i,a:,b:,u,v,f" OPTION; do
|
||||||
|
case $OPTION in
|
||||||
|
h)
|
||||||
|
usage
|
||||||
|
log debug "option handler - usage message printed"
|
||||||
|
exit 0
|
||||||
|
;;
|
||||||
|
c)
|
||||||
|
log debug "option handler - server deployment process for $OPTARG started"
|
||||||
|
mkjeos "$OPTARG"
|
||||||
|
errorcode=$?
|
||||||
|
log debug "option handler - server deployment for $OPTARG finished"
|
||||||
|
exit $errorcode
|
||||||
|
;;
|
||||||
|
e)
|
||||||
|
edit "$OPTARG"
|
||||||
|
errorcode=$?
|
||||||
|
exit $errorcode
|
||||||
|
;;
|
||||||
|
i)
|
||||||
|
newids
|
||||||
|
exit $?
|
||||||
|
;;
|
||||||
|
a)
|
||||||
|
addimage "$OPTARG"
|
||||||
|
exit $?
|
||||||
|
;;
|
||||||
|
b)
|
||||||
|
log info "option handler - starting backup process"
|
||||||
|
backup "$OPTARG"
|
||||||
|
errorcode=$?
|
||||||
|
if [ $errorcode = "0" ]; then
|
||||||
|
log info "option handler - backup process successfully finished"
|
||||||
|
else
|
||||||
|
log error "option handler - an error occured during backup process"
|
||||||
|
fi
|
||||||
|
exit $errorcode
|
||||||
|
;;
|
||||||
|
u)
|
||||||
|
update
|
||||||
|
exit $?
|
||||||
|
;;
|
||||||
|
v)
|
||||||
|
version
|
||||||
|
exit $?
|
||||||
|
;;
|
||||||
|
f)
|
||||||
|
fix
|
||||||
|
exit $?
|
||||||
|
;;
|
||||||
|
?)
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
;;
|
||||||
|
esac
|
||||||
|
done
|
||||||
|
|
||||||
|
|
||||||
|
# **** print usage message if no option is given ****
|
||||||
|
if [ -z $1 ]; then
|
||||||
|
usage
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
|
|
||||||
|
# **** end of script ****
|
||||||
|
exit 0
|
Loading…
Reference in New Issue