#!/usr/bin/env bash script_name="$(basename $(realpath $0))" working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/${script_name%%.*}.conf" LOCK_DIR="/tmp/$(basename $0).$$.LOCK" log_file="${LOCK_DIR}/${script_name%%.*}.log" backup_date="$(date +%Y-%m-%d-%H%M)" # ------------- # - Some functions # ------------- clean_up() { # Perform program exit housekeeping rm -rf "$LOCK_DIR" exit $1 } echononl(){ if $terminal ; then echo X\\c > /tmp/shprompt$$ if [ `wc -c /tmp/shprompt$$ | awk '{print $1}'` -eq 1 ]; then echo -e -n "$*\\c" 1>&2 else echo -e -n "$*" 1>&2 fi rm /tmp/shprompt$$ fi } fatal(){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mFatal\033[m ]: $*" echo "" echo -e " \033[31m\033[1mScript was terminated\033[m!" else echo " [ Fatal ]: $*" echo "" echo " Script was terminated...." fi echo "" clean_up 1 } error (){ echo "" if $terminal ; then echo -e " [ \033[31m\033[1mError\033[m ]: $*" else echo "[ Error ]: $*" fi echo "" } warn (){ echo "" if $terminal ; then echo -e " [ \033[33m\033[1mWarning\033[m ]: $*" else echo "[ Warning ]: $*" fi echo "" } info (){ if $terminal ; then echo "" echo -e " [ \033[32m\033[1mInfo\033[m ]: $*" echo "" fi } echo_done() { if $terminal ; then echo -e "\033[75G[ \033[32mdone\033[m ]" fi } echo_failed(){ if $terminal && $LOGGING ; then echo -e "\033[75G[ \033[1;31mfailed\033[m ]" fi } echo_skipped() { if $terminal && $LOGGING ; then echo -e "\033[75G[ \033[33m\033[1mskipped\033[m ]" fi } trim() { local var="$*" var="${var#"${var%%[![:space:]]*}"}" # remove leading whitespace characters var="${var%"${var##*[![:space:]]}"}" # remove trailing whitespace characters echo -n "$var" } # ------------- # - Running in a terminal? # ------------- if [[ -t 1 ]] ; then terminal=true else terminal=false fi # ------------- # - Read Configurations from $conf_file # ------------- DEFAULT_LXC_ROOT_DIR="/var/lib/lxc" if [[ -f "$conf_file" ]]; then source "$conf_file" else warn "No Configuration file '$(basename ${conf_file})'. Using default values.." fi [[ -z "$LXC_ROOT_DIR" ]] && LXC_ROOT_DIR="$DEFAULT_LXC_ROOT_DIR" # ------------- # - Job is already running? # ------------- # - If job already runs, stop execution.. # - if mkdir "$LOCK_DIR" 2> /dev/null ; then # - Remove lockdir when the script finishes, or when it receives a signal # - trap clean_up SIGHUP SIGINT SIGTERM else datum="$(date +"%d.%m.%Y %H:%M")" msg=" [ Error ]: A previos instance of '$(basename $0)' seems already be running.\n\n Exiting now.." error "A previos instance of the script '$(basename $0)' seems already be running." exit 1 fi echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert the name new LX Containers." echo " #" echo " # Note: It's NOT the hostname, its only the name of the inew LX Container." echo " #" echo "" echononl " \033[1mName of the Container:\033[m " read LXC_NAME while [[ -z "$(trim $LXC_NAME)" ]]; do warn "Parameter is needed. Try again.." echononl "\033[1mName of the Container:\033[m " read LXC_NAME done echo "" echo "" echo -e " \033[32m---\033[m" echo -e " \033[32m\033[1mParameter Summary - Script \033[m\033[1m$script_name\033[m" echo -e " \033[32m---\033[m" echo "" echo "" echo " Base directory LX Containers.............: $LXC_ROOT_DIR" echo "" echononl "\033[1mStart creation script with this parameters [yes/no]:\033[m " read OK [[ "$(trim ${OK,,})" = "yes" ]] || fatal "Start script with different parameters." echo echononl " Replace 'lxc.rootfs' -> 'lxc.rootfs.path'.." if $(grep -qE "^\s*lxc.rootfs\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i.${backup_date} -n -p \ -e "s/^\s*lxc.rootfs\s*=\s*(.*)/lxc.rootfs.path = \1/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echononl " Replace 'lxc.mount' -> 'lxc.mount.fstab'.." if $(grep -qE "^\s*lxc.mount\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i -n -p \ -e "s/^\s*lxc.mount\s*=\s*(.*)/lxc.mount.fstab = \1/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echononl " Replace 'lxc.utsname' -> 'lxc.uts.name'.." if $(grep -qE "^\s*lxc.utsname\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i -n -p \ -e "s/^\s*lxc.utsname\s*=\s*(.*)/lxc.uts.name = \1/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echononl " Remove (comment out) 'lxc.kmsg'.." if $(grep -qE "^\s*lxc.kmsg\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i -n -p \ -e "s/^(\s*lxc.kmsg\s*=\s*.*)/#\1/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echononl " Replace 'lxc.network.' -> 'lxc.net.0.'.." if $(grep -qE "^\s*lxc.network.[^\s]*\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i -n -p \ -e "s/^\s*lxc.network\.([^\s]*)\s*=\s*(.*)/lxc.net.0.\1 = \2/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echononl " Replace 'lxc.net.[i].ipv[46]' -> 'lxc.net.[i].ipv[46].address'.." if $(grep -qE "^\s*lxc.net.[[:digit:]].ipv[[:digit:]]\s*=\s*" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i -n -p \ -e "s/^\s*lxc\.net\.(\d)\.ipv(\d)\s*=\s*(.*)/lxc.net.\1.ipv\2.address = \3/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echo echononl " Replace 'lxc.aa_' -> 'lxc.apparmor.'.." if $(grep -qE "^\s*#*\s*lxc.aa_.*=" "${LXC_ROOT_DIR}/${LXC_NAME}/config" 2> /dev/null) ; then perl -i.${backup_date} -n -p \ -e "s/^(\s*#*\s*)lxc.aa_(.*)=\s*(.*)/\1lxc.apparmor.\2 = \3/" "${LXC_ROOT_DIR}/${LXC_NAME}/config" \ > $log_file 2>&1 if [[ $? -gt 0 ]]; then echo_failed error "$(cat $log_file)" else echo_done fi else echo_skipped fi echo "" clean_up 0