diff --git a/cts/lxc_autogen.sh.in b/cts/lxc_autogen.sh.in index 7cee31ada0..50e0cd5fdd 100755 --- a/cts/lxc_autogen.sh.in +++ b/cts/lxc_autogen.sh.in @@ -1,284 +1,288 @@ #!/bin/bash containers="2" download=0 # different than default libvirt network in case this is run nested in a KVM instance addr="192.168.123.1" restore=0 restore_pcmk=0 generate=0 cib=0 add_clone=0 working_dir="@CRM_CONFIG_CTS@/lxc" curdir=$(pwd) function helptext() { echo "lxc_autogen.sh - A tool for generating libvirt lxc containers for testing purposes." echo "" echo "Usage: lxc-autogen [options]" echo "" echo "Options:" echo "-g, --generate Generate libvirt lxc environment in the directory this script is run from." echo "-r, --restore-libvirt Restore the default network, and libvirt config to before this script ran." echo "-p, --restore-cib Remove cib entries this script generated." echo "" echo "-a, --add-cib Add remote-node entries for each lxc instance into the cib" echo "-m, --add-clone Add clone resource shared between remote-nodes" echo "-d, --download-agent Download and install the latest VirtualDomain agent." echo "-c, --containers Specify the number of containers to generate, defaults to $containers. Used with -g" echo "-n, --network What network to override default libvirt network to. Example: -n 192.168.123.1. Used with -g" echo "" exit $1 } while true ; do case "$1" in --help|-h|-\?) helptext 0;; -c|--containers) containers="$2"; shift; shift;; -d|--download-agent) download=1; shift;; -n|--network) addr="$2"; shift; shift;; -r|--restore-libvirt) restore=1; shift;; -p|--restore-cib) restore_pcmk=1; shift;; -g|--generate) generate=1; shift;; -a|--add-cib) cib=1; shift;; -m|--add-clone) add_clone=1; shift;; "") break;; *) helptext 1;; esac done #strip last digits off addr tmp="s/\.$(echo "$addr" | tr '.' ' ' | awk '{print $4}')$//g" addr=$(echo $addr | sed -e ${tmp}) set_network() { rm -f cur_network.xml cat << END >> cur_network.xml default 41ebdb84-7134-1111-a136-91f0f1119225 END ls restore_default.xml > /dev/null 2>&1 if [ $? -ne 0 ]; then virsh net-dumpxml default > restore_default.xml fi virsh net-destroy default virsh net-undefine default virsh net-define cur_network.xml virsh net-start default } generate() { set_network #generate pacemaker remote key ls /etc/pacemaker/authkey > /dev/null 2>&1 if [ $? != 0 ]; then mkdir -p /etc/pacemaker dd if=/dev/urandom of=/etc/pacemaker/authkey bs=4096 count=1 fi # Generate libvirt domains in xml for (( c=1; c <= $containers; c++ )) do rm -rf lxc$c-filesystem mkdir -p lxc$c-filesystem/var/run/ mkdir -p lxc$c-filesystem/usr/var/run rm -f lxc$c.xml cat << END >> lxc$c.xml lxc$c 102400 exe $working_dir/lxc$c-filesystem/launch-helper END rm -f container$c.cib cat << END >> container$c.cib END rm -f lxc-ms$c.cib cat << END >> lxc-ms.cib END rm -f lxc$c-filesystem/launch-helper cat << END >> lxc$c-filesystem/launch-helper #!/bin/bash ifconfig eth0 $addr.10$c route add 0.0.0.0 gw $addr.1 eth0 /usr/sbin/pacemaker_remoted END chmod 711 lxc$c-filesystem/launch-helper cat << END >> /etc/hosts $addr.10$c lxc$c END done } apply_cib_clone() { cibadmin -Q > cur.cib export CIB_file=cur.cib cibadmin -o resources -C -x lxc-ms.cib for tmp in $(ls lxc*.xml); do tmp=$(echo $tmp | sed -e 's/\.xml//g') echo "" > tmp_constraint cibadmin -o constraints -C -x tmp_constraint + echo "" > tmp_constraint + cibadmin -o constraints -C -x tmp_constraint rm -f tmp_constraint done unset CIB_file cibadmin --replace --xml-file cur.cib rm -f cur.cib } apply_cib_entries() { node=$(crm_node -n) cibadmin -Q > cur.cib export CIB_file=cur.cib for tmp in $(ls container*.cib); do cibadmin -o resources -C -x $tmp tmp=$(echo $tmp | sed -e 's/\.cib//g') crm_resource -M -r $tmp -H $node done unset CIB_file cibadmin --replace --xml-file cur.cib rm -f cur.cib } restore_cib() { node=$(crm_node -n) cibadmin -Q > cur.cib export CIB_file=cur.cib for tmp in $(ls lxc*.xml); do tmp=$(echo $tmp | sed -e 's/\.xml//g') echo "" > tmp_constraint cibadmin -o constraints -D -x tmp_constraint + echo "" > tmp_constraint + cibadmin -o constraints -D -x tmp_constraint rm -f tmp_constraint done cibadmin -o resources -D -x lxc-ms.cib for tmp in $(ls container*.cib); do tmp=$(echo $tmp | sed -e 's/\.cib//g') crm_resource -U -r $tmp -H $node crm_resource -D -r $tmp -t primitive done unset CIB_file cibadmin --replace --xml-file cur.cib rm -f cur.cib } restore_libvirt() { for tmp in $(ls lxc*.xml); do tmp=$(echo $tmp | sed -e 's/\.xml//g') virsh -c lxc:/// destroy $tmp > /dev/null 2>&1 virsh -c lxc:/// undefine $tmp > /dev/null 2>&1 sed -i.bak "/...\....\....\..* ${tmp}/d" /etc/hosts echo "$tmp destroyed" done ls restore_default.xml > /dev/null 2>&1 if [ $? -eq 0 ]; then virsh net-destroy default > /dev/null 2>&1 virsh net-undefine default > /dev/null 2>&1 virsh net-define restore_default.xml virsh net-start default if [ $? -eq 0 ]; then echo "default network restored" fi fi rm -f restore_default.xml > /dev/null 2>&1 } mkdir -p $working_dir cd $working_dir if [ $download -eq 1 ]; then wget https://raw.github.com/ClusterLabs/resource-agents/master/heartbeat/VirtualDomain chmod 755 VirtualDomain mv -f VirtualDomain /usr/lib/ocf/resource.d/heartbeat/VirtualDomain fi if [ $restore_pcmk -eq 1 ]; then restore_cib fi if [ $restore -eq 1 ]; then restore_libvirt fi if [ $generate -eq 1 ]; then generate fi if [ $cib -eq 1 ]; then apply_cib_entries fi if [ $add_clone -eq 1 ]; then apply_cib_clone fi cd $curdir