From: František Dvořák Date: Thu, 1 Oct 2015 16:32:42 +0000 (+0200) Subject: LXC! X-Git-Tag: v1.0.0~21 X-Git-Url: http://scientific.zcu.cz/git/?a=commitdiff_plain;h=3d7355962b29a50504fcd659123a81445d8afdbd;p=virtualization.git LXC! --- diff --git a/lxc.sh b/lxc.sh new file mode 100755 index 0000000..fbbed84 --- /dev/null +++ b/lxc.sh @@ -0,0 +1,200 @@ +#!/bin/sh -e + +if [ -z $1 ]; then + echo "Usage:" + echo + echo "$0 cfg_hostname.sh [image | setup | xml | define | start]" + echo "$0 cfg_hostname.sh [undefine | delete]" + exit 1 +fi +. ./$1 || exit 1 + +IMAGE_POOL=/var/lib/lxc + +IMAGE_FILE=${IMAGE_POOL}/${FACTER_hostname} +IMAGE_SOURCE=/var/lib/lxc/debian7-x86_64.tar.gz +VIRSH_ARGS="-c lxc://" + +if [ -z $FACTER_fqdn ]; then + echo "ERROR: no facter fqdn settings found" + exit 1 +fi +if [ -z $FACTER_hostname ]; then + echo "ERROR: no facter hostname settings found" + exit 1 +fi + + +image() { + if [ -e ${IMAGE_FILE} ]; then + echo "ERROR: image ${IMAGE_FILE} already exists" + exit 1 + fi + + mkdir ${IMAGE_FILE} + (cd ${IMAGE_FILE}; tar xzf ${IMAGE_SOURCE}) +} + + +setup() { + cd ${IMAGE_FILE} || exit 1 + + cp -vp /etc/{resolv.conf,apt/sources.list,krb5.conf} ./etc/ + echo "pts/0" >> etc/securetty + + echo "${FACTER_hostname}" > etc/hostname + + cat << __EOF__ > etc/network/interfaces +# This file describes the network interfaces available on your system +# and how to activate them. For more information, see interfaces(5). + +# The loopback network interface +auto lo +iface lo inet loopback + +__EOF__ + if [ -n "${FACTER_ipaddress}" ]; then + if [ "${FACTER_ipaddress}" = 'dhcp' ]; then + cat << __EOF__ >> etc/network/interfaces +auto eth0 +iface eth0 inet dhcp + +__EOF__ + else + cat << __EOF__ >> etc/network/interfaces +auto eth0 +iface eth0 inet static + address $FACTER_ipaddress + netmask $FACTER_netmask + gateway $FACTER_gw + +__EOF__ + fi + fi + if [ -n "${FACTER_ipaddress6}" ]; then + cat << __EOF__ >> etc/network/interfaces +iface eth0 inet6 static + address ${FACTER_ipaddress6} + netmask ${FACTER_netmask6} + gateway ${FACTER_gw6} + +__EOF__ + fi + + if [ ! -d root/.ssh ]; then + mkdir -p root/.ssh + fi + if [ ! -f root/.ssh/authorized_keys -a -f /root/.ssh/authorized_keys ]; then + cp -v /root/.ssh/authorized_keys root/.ssh + fi + + sed -i etc/inittab -e 's/^#T\(0\)/T\1/' + sed -i etc/inittab -e 's/^\([2-5]:\)/#\1/' + + cat >> root/.k5login << __EOF__ +dexter@ADMIN.META +mulac@ADMIN.META +salvet@ADMIN.META +valtri@ADMIN.META +xparak@ADMIN.META +__EOF__ + + cd +} + + +xml() { + i=1 + for d in ${DISKS}; do + XML_DISK="${XML_DISK} + + + + " + i=$((i+1)) + done + if [ -n "${XENBR}" ]; then + XML_NET=" + + + + + + +" + else + XML_NET=" + + + + + + +" + fi + cat << __EOF__ > /tmp/machine.xml + + ${FACTER_hostname} + ${SIZE_MEM} + ${SIZE_MEM} + ${SIZE_CPU} + + exe + /sbin/init + + + + + + + + /usr/lib/libvirt/libvirt_lxc + + + + + ${XML_DISK} +${XML_NET} + + + + + + + +__EOF__ +} + +shift +actions="$@" + +if [ -z "${actions}" ]; then + actions='image setup xml define start' +fi + +for action in ${actions}; do case ${action} in +image) image ;; +setup) setup ;; +xml) xml ;; +define) + virsh ${VIRSH_ARGS} define /tmp/machine.xml + ;; +start) + virsh ${VIRSH_ARGS} start ${FACTER_hostname} + echo "INFO: ${FACTER_hostname} started" + ;; +stop) + virsh ${VIRSH_ARGS} destroy ${FACTER_hostname} + echo "INFO: ${FACTER_hostname} destroyed" + ;; +undefine) + virsh ${VIRSH_ARGS} undefine ${FACTER_hostname} + ;; +delete) + virsh ${VIRSH_ARGS} undefine ${FACTER_hostname} || : + rm -fr ${IMAGE_POOL}/${FACTER_hostname} + ;; +esac +done + +echo "INFO: $0 done"