-
-
Save rofl0r/7446914 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
### Binaries | |
SCREEN=$(which screen) | |
LXCSTART=$(which lxc-start) | |
LXCSTOP=$(which lxc-stop) | |
### Configuration to get lxc directory | |
LXCCONF="/etc/default/lxc" | |
ERR=0 | |
### catch some common errors | |
if [ -z $SCREEN ] || [ -z $LXCSTART ] || [ -z $LXCSTOP ] ; then | |
echo "ERROR: Could not find screen or lxc-start" | |
exit 1 | |
fi | |
if [ -r $LXCCONF ]; then | |
source $LXCCONF | |
else | |
echo "ERROR: Could not find config $LXCCONF" | |
exit 2 | |
fi | |
### Functions | |
# starting function | |
function vm_start () { | |
for vm in $* ; do | |
if [ -d $LXC_DIRECTORY/$vm ]; then | |
if screen -ls | grep $vm &> /dev/null ; then | |
echo "WARN: $vm already running" | |
else | |
screen -d -m -S $vm $LXCSTART -n $vm -f $LXC_DIRECTORY/$vm/config | |
fi | |
else | |
echo "ERROR: $vm doesn't look like a valid container on your system" | |
((ERR++)) | |
fi | |
done | |
} | |
# stopping function | |
function vm_stop () { | |
for vm in $* ; do | |
if [ -d $LXC_DIRECTORY/$vm ]; then | |
if screen -ls | grep $vm &> /dev/null ; then | |
$LXCSTOP -n $vm | |
echo "WARN: $vm killed" | |
fi | |
else | |
echo "ERROR: $vm doesn't look like a valid container on your system" | |
((ERR++)) | |
fi | |
done | |
} | |
# Print infos about all screens on the system and lxc status | |
function vm_info () { | |
echo "Screens:" | |
screen -ls | |
echo "LXC Status:" | |
for vm in $* ; do | |
echo -n "$vm: " | |
lxc-info -n $vm | head -1 | awk '{print $NF }' | |
done | |
} | |
# Print network configs and stuff | |
function vm_netstat () { | |
for vm in $* ; do | |
echo "Netstat for $vm:" | |
grep "address" $LXC_DIRECTORY/$vm/rootfs/etc/network/interfaces 2>/dev/null | awk '{print "IP: "$2}' | |
lxc-netstat -n $vm | grep -v "^tcp6" | grep -v "^unix" | |
echo | |
done | |
} | |
function vm_clone () { | |
if [ -z "$1" -o -z "$2" ] || [ ! -d "$LXC_DIRECTORY/$1" ]; then | |
echo "ERROR: non-correct usage - see help" | |
echo "ERROR: $LXC_DIRECTORY/$2 may exist or $LXC_DIRECTORY/$1 doesn't." | |
exit 3 | |
fi | |
# Copy configuration | |
echo "INFO: Copying container..." | |
cp -a $LXC_DIRECTORY/$1 $LXC_DIRECTORY/$2 | |
echo "INFO: Changing lxc config..." | |
sed -i "s/$1/$2/g" $LXC_DIRECTORY/$2/config | |
# ATTENTION! | |
# This requries a convention of vm$IP-name-of-vm | |
echo "INFO: Getting last octet from vmXX..." | |
OCTET=$(echo $2 | sed 's#vm\(..\).*#\1#') | |
echo "INFO: Making network config with 10.10.0.$OCTET..." | |
sed -i "s/address.*/address 10.10.0.$OCTET/" $LXC_DIRECTORY/$2/rootfs/etc/network/interfaces | |
} | |
function vm_help () { | |
cat << EOF | |
Usage: | |
$0 <commands> [container] | |
Commands: | |
# Starting | |
* start <container> - Start a container | |
* startall - Start all your containers (be careful with that!) | |
# Stopping | |
* stop <container> - Stop a container | |
* stopall - Stop all containers | |
# Cloning | |
* clone <container> <new container> - Relative to LXC home! | |
# Information | |
* info - Show status for containers and screens | |
* ls - Same as info | |
* net - Show all netstats for all your containers | |
* help - Print this message | |
EOF | |
} | |
### Runtime function switch case | |
case $1 in | |
startall ) vm_start $(lxc-ls | sort | uniq);; | |
start ) shift ; vm_start $* ;; | |
stopall ) vm_stop $(lxc-ls | sort | uniq);; | |
stop ) shift ; vm_stop $* ;; | |
clone ) vm_clone "$2" "$3";; | |
info|ls ) vm_info $(lxc-ls | sort | uniq);; | |
net ) vm_netstat $(lxc-ls | sort | uniq);; | |
help ) vm_help ;; | |
* ) vm_help ;; | |
esac | |
exit $ERR |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment