#!/usr/bin/env bash working_dir="$(dirname $(realpath $0))" conf_file="${working_dir}/conf/create-lx-container.conf" LOCK_DIR="/tmp/create-lx-container.LOCK" err_msg="$LOCK_DIR/error.log" # ------------- # - 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" } is_valid_ipv4() { local -a octets=( ${1//\./ } ) local RETURNVALUE=0 # return an error if the IP doesn't have exactly 4 octets [[ ${#octets[@]} -ne 4 ]] && return 1 for octet in ${octets[@]} do if [[ ${octet} =~ ^[0-9]{1,3}$ ]] then # shift number by 8 bits, anything larger than 255 will be > 0 ((RETURNVALUE += octet>>8 )) else # octet wasn't numeric, return error return 1 fi done return ${RETURNVALUE} } is_valid_mac_address() { [[ "$1" =~ ^([0-9a-fA-F]{2}:){5}[0-9a-fA-F]{2}$ ]] && return 0 || return 1 } netmask2cidr() { case $1 in 0x*) local hex=${1#0x*} quad= while [ -n "${hex}" ]; do local lastbut2=${hex#??*} quad=${quad}${quad:+.}0x${hex%${lastbut2}*} hex=${lastbut2} done set -- ${quad} ;; esac local i= len= local IFS=. for i in $1; do while [ ${i} != "0" ]; do len=$((${len} + ${i} % 2)) i=$((${i} >> 1)) done done echo "${len}" } detect_os_1 () { if $(which lsb_release > /dev/null 2>&1) ; then os_dist="$(lsb_release -i | awk '{print tolower($3)}')" os_version="$(lsb_release -r | awk '{print tolower($2)}')" os_codename="$(lsb_release -c | awk '{print tolower($2)}')" if [[ "$os_dist" = "debian" ]]; then if $(echo "$os_version" | grep -q '\.') ; then os_version=$(echo "$os_version" | cut --delimiter='.' -f1) fi fi elif [[ -e "/etc/os-release" ]]; then . /etc/os-release os_dist=$ID os_version=${VERSION_ID} fi # remove whitespace from os_dist and os_version os_dist="${os_dist// /}" os_version="${os_version// /}" } # ------------- # - 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" DEFAULT_LXC_DIST="debian" DEFAULT_MAC_ADDRESS_1="$(echo -n 00 ; dd bs=1 count=5 if=/dev/random 2>/dev/null | hexdump -v -e '/1 ":%02X"')" DEFAULT_IPV4_NETMASK_1="255.255.255.0" DEFAULT_IPV6_PREFIX=64 DEFAULT_CODENAME="$(lsb_release -c | awk '{print tolower($2)}')" if [[ -f "$conf_file" ]]; then source "$conf_file" else warn "No Configuration file '$(basename ${conf_file})'. Using default values.." fi [[ -z "$LXC_ROOT_DIR" ]] || DEFAULT_LXC_ROOT_DIR="$LXC_ROOT_DIR" [[ -z "$LXC_DIST" ]] || DEFAULT_LXC_DIST="$LXC_DIST" # ------------- # - 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 clear #echo -e " \033[32m---\033[m" #echo -e " \033[32m\033[1mInsert parameter for the new LX Container\033[m" #echo -e " \033[32m---\033[m" #echo # #echo " # Insert base directory for LX Containers." #echo " #" #echo " # Type to accept the default '$DEFAULT_LXC_ROOT_DIR'." #echo "" #echononl "\033[1mRoot directory for LX Containers [$DEFAULT_LXC_ROOT_DIR]:\033[m " #read LXC_ROOT_DIR #if [[ -z "$(trim $LXC_ROOT_DIR)" ]] ; then # LXC_ROOT_DIR="$DEFAULT_LXC_ROOT_DIR" #fi # - We won't change the default. Realise different container directories # - by using symlinks. # - LXC_ROOT_DIR="$DEFAULT_LXC_ROOT_DIR" 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 -e "\033[32m--\033[m" echo "" DEFAULT_LXC_CONTAINER_DIR="${DEFAULT_LXC_ROOT_DIR}/$LXC_NAME" echo -e " # Insert root directory for the new LX Container \033[1m$LXC_NAME\\033[m." echo " #" echo " # Type to accept the default '$DEFAULT_LXC_CONTAINER_DIR'." echo "" echononl "\033[1mRoot directory for LX Containers [$DEFAULT_LXC_CONTAINER_DIR]:\033[m " read LXC_CONTAINER_DIR if [[ -z "$(trim $LXC_CONTAINER_DIR)" ]] ; then LXC_CONTAINER_DIR="$DEFAULT_LXC_CONTAINER_DIR" fi echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert linux distribution for the new LX Containers." echo " #" echo " # Type to accept the default '$DEFAULT_LXC_DIST'." echo "" echononl "\033[1mLinux Distribution [$DEFAULT_LXC_DIST]:\033[m " read LXC_DIST if [[ -z "$(trim $LXC_DIST)" ]] ; then LXC_DIST="$DEFAULT_LXC_DIST" else LXC_DIST="${LXC_DIST,,}" fi echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert the Code name of the Linux Distribution." echo " #" echo " # Type to accept the default '$DEFAULT_CODENAME'." echo " #" echo " # In case of Ubuntu type in the Short Code Name:" echo " # Noble Numbat -> Noble" echo " #" echo "" echononl "\033[1mCode name [${DEFAULT_CODENAME}]:\033[m " read LXC_RELEASE if [[ -z "$(trim $LXC_RELEASE)" ]] ; then LXC_RELEASE="$DEFAULT_CODENAME" else LXC_RELEASE="${LXC_RELEASE,,}" fi #while [[ -z "$(trim $LXC_RELEASE)" ]]; do # warn "Parameter is needed. Try again.." # echononl "\033[1mCode name:\033[m " # read LXC_RELEASE #done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert MAC Address for the first network device of the new LX Containers." echo " #" echo " # Type to accept the randomised created one '$DEFAULT_MAC_ADDRESS_1'." echo "" echononl "\033[1mMAC Address of the firts network device [$DEFAULT_MAC_ADDRESS_1]:\033[m " read MAC_ADDRESS_1 while ! $(is_valid_mac_address $MAC_ADDRESS_1) ; do if [[ -z "$(trim $MAC_ADDRESS_1)" ]] ; then MAC_ADDRESS_1="$DEFAULT_MAC_ADDRESS_1" break fi warn "'$MAC_ADDRESS_1' isn't a valid MAC Address." echononl "\033[1mMAC Address of the firts network device [$DEFAULT_MAC_ADDRESS_1]:\033[m " read MAC_ADDRESS_1 done echo "" echo -e "\033[32m--\033[m" echo "" NETWORK_CONFIGURATION_DHCP=false echo " # Insert first IPv4 Address for the new LX Containers." echo " #" if [[ "$LXC_DIST" = "ubuntu" ]] ; then echo -e " # Type \033[33mNone\033[m or \033[33mDHCP\033[m if no ipv4-address should be assigned" echo " #" fi echo "" echononl "\033[1mFirst IPv4 Address:\033[m " read IPV4_ADDRESS_1 if [[ "$LXC_DIST" = "ubuntu" ]] && ([[ "${IPV4_ADDRESS_1,,}" = 'none' ]] \ || [[ "${IPV4_ADDRESS_1,,}" = 'dhcp' ]]) ; then NETWORK_CONFIGURATION_DHCP=true else while ! $(is_valid_ipv4 $IPV4_ADDRESS_1); do if [[ -z "$(trim $IPV4_ADDRESS_1)" ]]; then warn "Parameter is needed. Try again.." else warn "'$IPV4_ADDRESS_1' is not a valid IPv4 Address. Try again.." fi echononl "\033[1mFirst IPv4 Address:\033[m " read IPV4_ADDRESS_1 if [[ "$LXC_DIST" = "ubuntu" ]] && ([[ "${IPV4_ADDRESS_1,,}" = 'none' ]] \ || [[ "${IPV4_ADDRESS_1,,}" = 'dhcp' ]]) ; then NETWORK_CONFIGURATION_DHCP=true break fi done fi if ! $NETWORK_CONFIGURATION_DHCP ; then _octets=( ${IPV4_ADDRESS_1//\./ } ) DEFAULT_IPV4_GATEWAY_1="${_octets[0]}.${_octets[1]}.${_octets[2]}.1" DEFAULT_IPV4_NAME_SERVER_1="${_octets[0]}.${_octets[1]}.${_octets[2]}.1" echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert IPv4 Address for the gateway." echo " #" echo " # Type to accept the default '$DEFAULT_IPV4_GATEWAY_1'." echo "" echononl "\033[1mIPv4 (first) Gateway Address [$DEFAULT_IPV4_GATEWAY_1]:\033[m " read IPV4_GATEWAY_1 while ! $(is_valid_ipv4 $IPV4_GATEWAY_1) ; do if [[ -z "$(trim $IPV4_GATEWAY_1)" ]]; then IPV4_GATEWAY_1="$DEFAULT_IPV4_GATEWAY_1" break else warn "'$IPV4_GATEWAY_1' is not a valid IPv4 Address. Try again.." fi echononl "\033[1mIPv4 (first) Gateway Address [$DEFAULT_IPV4_GATEWAY_1]:\033[m " read IPV4_GATEWAY_1 done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert Netmask for the first IPv4 Address." echo " #" echo " # Type to accept the default '$DEFAULT_IPV4_NETMASK_1'." echo "" echononl "\033[1mNetmask (first) IPv4 Address [$DEFAULT_IPV4_NETMASK_1]:\033[m " read IPV4_NETMASK_1 while ! $(is_valid_ipv4 $IPV4_NETMASK_1) ; do if [[ -z "$(trim $IPV4_NETMASK_1)" ]]; then IPV4_NETMASK_1="$DEFAULT_IPV4_NETMASK_1" break else warn "'$IPV4_NETMASK_1' is not a valid netmask. Try again.." fi echononl "\033[1mNetmask (first) IPv4 Address [$DEFAULT_IPV4_NETMASK_1]:\033[m " read IPV4_NETMASK_1 done IPV4_PREFIX_1="$(netmask2cidr $IPV4_NETMASK_1)" IPV6_PREFIX_1=$DEFAULT_IPV6_PREFIX if [[ "$LXC_DIST" = "debian" ]]; then echo "" echo -e "\033[32m--\033[m" echo "" echononl "\033[1mDo you want to apply a second IPv4 Address?\033[m [yes/no]: " read OK [[ "$(trim ${OK,,})" = "yes" ]] && _second_ipv4=true || _second_ipv4=false if $_second_ipv4 ; then _last_octet=${MAC_ADDRESS_1: -2} _new_last_octet="$(printf "%X\n" $((0x$_last_octet + 1)))" if [[ ${#_new_last_octet} -eq 1 ]]; then _new_last_octet="0$_new_last_octet" elif [[ ${#_new_last_octet} -eq 3 ]]; then _new_last_octet="00" fi DEFAULT_MAC_ADDRESS_2="${MAC_ADDRESS_1:0:14}:$_new_last_octet" echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert MAC Address for the second network device of the new LX Containers." echo " #" echo " # Type to accept the randomised created one '$DEFAULT_MAC_ADDRESS_2'." echo "" echononl "\033[1mMAC Address of the second network device [$DEFAULT_MAC_ADDRESS_2]:\033[m " read MAC_ADDRESS_2 while ! $(is_valid_mac_address $MAC_ADDRESS_2) ; do if [[ -z "$(trim $MAC_ADDRESS_2)" ]] ; then MAC_ADDRESS_2="$DEFAULT_MAC_ADDRESS_2" break fi warn "'$MAC_ADDRESS_2' isn't a valid MAC Address." echononl "\033[1mMAC Address of the second network device [$DEFAULT_MAC_ADDRESS_2]:\033[m " read MAC_ADDRESS_2 done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert second IPv4 Address for the new LX Containers." echo " #" echo "" echononl "\033[1mSecond IPv4 Address:\033[m " read IPV4_ADDRESS_2 while ! $(is_valid_ipv4 $IPV4_ADDRESS_2); do if [[ -z "$(trim $IPV4_ADDRESS_2)" ]]; then warn "Parameter is needed. Try again.." else warn "'$IPV4_ADDRESS_2' is not a valid IPv4 Address. Try again.." fi echononl "\033[1mSecond IPv4 Address:\033[m " read IPV4_ADDRESS_2 done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert IPv4 Address for the gateway (second)." echo " #" echo " # Normally, the second gateway is the same as the first one. Change if you" echo " # know what you are doing." echo " #" echo " # Type to accept the default one" echo "" echononl "\033[1mIPv4 (second) Gateway Address [$IPV4_GATEWAY_1]:\033[m " read IPV4_GATEWAY_2 while ! $(is_valid_ipv4 $IPV4_GATEWAY_2) ; do if [[ -z "$(trim $IPV4_GATEWAY_2)" ]]; then IPV4_GATEWAY_2="$IPV4_GATEWAY_1" break else warn "'$IPV4_GATEWAY_2' is not a valid IPv4 Address. Try again.." fi echononl "\033[1mIPv4 (second) Gateway Address:\033[m " read IPV4_GATEWAY_2 done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert Netmask for the second IPv4 Address." echo " #" echo " # Type to accept the default one" echo "" echononl "\033[1mNetmask (second) IPv4 Address [$IPV4_NETMASK_1]:\033[m " read IPV4_NETMASK_2 while ! $(is_valid_ipv4 $IPV4_NETMASK_2) ; do if [[ -z "$(trim $IPV4_NETMASK_2)" ]]; then IPV4_NETMASK_2="$IPV4_NETMASK_1" break else warn "'$IPV4_NETMASK_2' is not a valid netmask. Try again.." fi echononl "\033[1mNetmask (second) IPv4 Address:\033[m " read IPV4_NETMASK_2 done IPV4_PREFIX_2="$(netmask2cidr $IPV4_NETMASK_2)" IPV6_PREFIX_2=$DEFAULT_IPV6_PREFIX fi fi # if [[ "$LXC_DIST" = "debian" ]]; then echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert first IPv6 Address for the new LX Containers." echo " #" echo -e " # Type \033[33mNone\033[m if no ipv6-address should be assigned" echo " #" echo "" echononl "\033[1mFirst IPv6 Address:\033[m " read IPV6_ADDRESS_1 while [[ -z "$(trim $IPV6_ADDRESS_1)" ]]; do warn "Parameter is needed. Try again.." echononl "\033[1mFirst IPv6 Address:\033[m " read IPV6_ADDRESS_1 done if [[ "${IPV6_ADDRESS_1,,}" = 'none' ]] ; then IPV6_ADDRESS_1="" fi if [[ -n "$IPV6_ADDRESS_1" ]] ; then echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert IPv6 Address for the gateway." echo " #" echo "" echononl "\033[1mIPv6 (first) Gateway Address:\033[m " read IPV6_GATEWAY_1 while [[ -z "$(trim $IPV6_GATEWAY_1)" ]]; do warn "Parameter is needed. Try again.." echononl "\033[1mFirst IPv6 i(first) Gateway Address:\033[m " read IPV6_GATEWAY_1 done fi echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert (list of) Nameserver(s)." echo " #" echo " # Note:" echo " # if the LX container supports IPv6 Adresses also IPv6 nameserver(s) are possible." echo " #" echo " #" echo " # Example:" echo " # 185.12.64.1 2a01:4ff:ff00::add:2 185.12.64.2 2a01:4ff:ff00::add:1" echo " #" echo " # Give a blank separated list of ip addresses" echo " #" echo " #" echo " #" echo " # Type to accept the default one" echo "" echononl "\033[1mNameserver (IPv4 Address) [$DEFAULT_IPV4_NAME_SERVER_1]:\033[m " read NAME_SERVER_IPS if [[ -z "$(trim $NAME_SERVER_IPS)" ]]; then NAME_SERVER_IPS="$DEFAULT_IPV4_NAME_SERVER_1" fi declare -a nameserver_ip_arr=() for _ip in ${NAME_SERVER_IPS} ; do nameserver_ip_arr+=("$_ip") done echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert Search Domain for the new LX Containers." echo " #" echo -e " # Type \033[33mNone\033[m if no search domain should be assigned" echo " #" echo "" echononl "\033[1mSearch Domain:\033[m " read SEARCH_DOMAIN while [[ -z "$(trim $SEARCH_DOMAIN)" ]]; do warn "Parameter is needed. Try again.." echononl "\033[1mSEARCH_DOMAIN:\033[m " read SEARCH_DOMAIN done if [[ "${SEARCH_DOMAIN,,}" = 'none' ]] ; then SEARCH_DOMAIN="" fi fi # if ! $NETWORK_CONFIGURATION_DHCP ; then echo "" echo -e "\033[32m--\033[m" echo "" echo " # Insert full quallified hostname for the new LX Container." echo " #" echo "" echononl "\033[1mFull qualified hostname:\033[m " read FQ_HOSTNAME while [[ -z "$(trim $FQ_HOSTNAME)" ]]; do warn "Parameter is needed. Try again" echononl "\033[1mFull qualified hostname::\033[m " read FQ_HOSTNAME done HOSTNAME="$(echo "$FQ_HOSTNAME" | cut -f1 -d'.')" # - Determin LXC Version # - LXC_VERSION="$(lxc-info --version)" declare -i LXC_MAJOR_VERSION=$(echo $LXC_VERSION | cut -d '.' -f1) echo "" echo "" echo -e " \033[32m---\033[m" echo -e " \033[32m\033[1mParameter Summary - create new LX Container \033[m\033[1m$LXC_NAME\033[m" echo -e " \033[32m---\033[m" echo "" echo " LXC Version..............................: $LXC_VERSION" echo " LXC Major Version........................: $LXC_MAJOR_VERSION" echo "" echo " Base directory LX Containers.............: $LXC_ROOT_DIR" echo " Root directory new LX Container..........: $LXC_CONTAINER_DIR" echo "" echo " Name of the new LX Container.............: $LXC_NAME" echo " Linux distribution.......................: $LXC_DIST" echo " Code name (or release number)............: $LXC_RELEASE" echo "" echo " MAC Address (first) network device.......: $MAC_ADDRESS_1" echo "" if $NETWORK_CONFIGURATION_DHCP ; then echo -e " Network Configuration....................: \033[33mvia DHCP\033[m" else echo " IPv4 Address (first).....................: $IPV4_ADDRESS_1" echo " Netmask first IPv4 Address...............: $IPV4_NETMASK_1" echo " IPv4 Gateway Address (first).............: $IPV4_GATEWAY_1" declare -i _index=0 for _ip in "${nameserver_ip_arr[@]}" ; do if [[ ${_index} -lt 1 ]] ; then echo " Nameserver(s)............................: ${_ip}" else echo " ${_ip}" fi (( _index++ )) done if [[ -n "${SEARCH_DOMAIN}" ]] ; then echo " Search Domain............................: ${SEARCH_DOMAIN}" fi echo " CIDR (IPv4 Prefix) of netmask............: $IPV4_PREFIX_1" echo "" if [[ -n "$IPV6_ADDRESS_1" ]] ; then echo " IPv6 Address (first).....................: $IPV6_ADDRESS_1" echo " IPv6 Gateway Address (first).............: $IPV6_GATEWAY_1" else echo -e " IPv6 Address (first).....................: - \033[33mNot set\033[m - " fi if $_second_ipv4 ; then echo "" echo " MAC Address (second) network device......: $MAC_ADDRESS_2" echo " IPv4 Address (second)....................: $IPV4_ADDRESS_2" echo " IPv4 Gateway Address (second)............: $IPV4_GATEWAY_2" echo " Netmask second IPv4 Address..............: $IPV4_NETMASK_2" echo " CIDR (IPv4 Prefix) of netmask............: $IPV4_PREFIX_2" fi fi echo "" echo " Hostname.................................: $HOSTNAME" echo " Full qualified hostname..................: $FQ_HOSTNAME" 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 "" # --- # - Create the base container # --- echo "" echo "" echo -e " \033[37m\033[1mPreinstallation task....\033[m" echo "" if [[ "$(dirname "$LXC_CONTAINER_DIR")" != "$LXC_ROOT_DIR" ]]; then msg="Create directory '$(dirname "$LXC_CONTAINER_DIR")'.." _dir="$(dirname "$LXC_CONTAINER_DIR")" if [[ ! -d "$_dir" ]] ; then mkdir "$_dir" > /dev/null 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="Directory '${_dir}' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of directory '$_dir' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" fatal "$(cat $err_msg)" fi else msg_skipped="Directory '$_dir' already exists." length_msg_skipped=${#msg_skipped} blank_signs="" if [[ $length_msg -gt $length_msg_skipped ]]; then number_blank_sign=$(expr $length_msg - $length_msg_skipped) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[33m\033[1mskip\033[m ] ${msg_skipped}${blank_signs}" if [[ -d "$LXC_CONTAINER_DIR" ]] ; then if [[ "$(ls -A $LXC_CONTAINER_DIR)" ]]; then fatal "Container directory "$LXC_CONTAINER_DIR" exists but is not empty.." else msg="Remove empty directory '${LXC_CONTAINER_DIR}'.." rmdir "${LXC_CONTAINER_DIR}" > /dev/null 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="(Empty) directory '${LXC_CONTAINER_DIR}' removed.." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Removing directory '${LXC_CONTAINER_DIR}' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" fatal "$(cat $err_msg)" fi fi # if [[ "$(ls -A $LXC_CONTAINER_DIR)" ]]; then fi # if [[ -d "$LXC_CONTAINER_DIR" ]] fi # if [[ ! -d "$_dir" ]] ; then fi # if [[ "$(dirname "$LXC_CONTAINER_DIR")" != "$LXC_ROOT_DIR" ]]; then # - Prevent script from installing into an existing Container dir. # - if [[ -d "${LXC_CONTAINER_DIR}" ]] ; then fatal "Container directory '$LXC_CONTAINER_DIR' already exists." fi echo "" echo "" echo -e " \033[37m\033[1mCreate the base container..\033[m" echo "" msg="Create Lx Container '$LXC_NAME' .." length_msg=${#msg} echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ "$LXC_DIST" = "ubuntu" ]] ; then lxc-create -n $LXC_NAME -t download -P "$(dirname "$LXC_CONTAINER_DIR")" -- \ -d $LXC_DIST --release $LXC_RELEASE --arch amd64 > /dev/null 2> $err_msg elif [[ "$LXC_DIST" = "debian" ]]; then lxc-create -n $LXC_NAME -t $LXC_DIST -P "$(dirname "$LXC_CONTAINER_DIR")" -- \ --release $LXC_RELEASE --arch amd64 > /dev/null 2> $err_msg else msg_failed="Creation of Lx Container '$LXC_NAME' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" echo "" fatal "Linux distribution \033[1m${LXC_DIST}\033[m is NOT supported!" fi if [[ $? -eq 0 ]] ; then msg_ok="Lx Container 'LXC_NAME' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of Lx Container '$LXC_NAME' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" echo "" if [[ "$LXC_DIST" = "ubuntu" ]] ; then echo -e " Command was: lxc-create -n $LXC_NAME -t download -P $(dirname \"$LXC_CONTAINER_DIR\")\" -- \\ --no-validate -d $LXC_DIST --release $LXC_RELEASE --arch amd64" elif [[ "$LXC_DIST" = "debian" ]]; then echo -e " Command was: lxc-create -n ${LXC_NAME} -t ${LXC_DIST} -P $(dirname \"${LXC_CONTAINER_DIR}\")\" -- \\ --release ${XC_RELEASE}--arch amd64" fi echo "" fatal "$(cat $err_msg)" fi # --- # - Configure the new LX Container # --- echo "" echo "" echo -e " \033[37m\033[1mConfigure the new LX Container..\033[m" echo "" if [[ "$(dirname "$LXC_CONTAINER_DIR")" != "$LXC_ROOT_DIR" ]]; then msg="Create a symlink '${LXC_ROOT_DIR}/${LXC_NAME}' --> '${LXC_CONTAINER_DIR}' .." length_msg=${#msg} echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" ln -s ${LXC_CONTAINER_DIR} ${LXC_ROOT_DIR}/${LXC_NAME} > /dev/null 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="Symlink '${LXC_ROOT_DIR}/${LXC_NAME}' --> '${LXC_CONTAINER_DIR}' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of symlink '${LXC_ROOT_DIR}/${LXC_NAME}' --> '${LXC_CONTAINER_DIR}' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" fatal "$(cat $err_msg)" fi fi if [[ "$LXC_DIST" = "debian" ]]; then # - Create an empty fstab '${LXC_CONTAINER_DIR}/fstab' # - msg="Create an empty file '${LXC_CONTAINER_DIR}/fstab' .." length_msg=${#msg} echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" touch ${LXC_CONTAINER_DIR}/fstab > /dev/null 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="Empty file '${LXC_CONTAINER_DIR}' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of (empty) file '${LXC_CONTAINER_DIR}/fstab' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi fi # - Backup original configuration file # - msg="Backup original Container configuration file '${LXC_CONTAINER_DIR}/config' .." length_msg=${#msg} echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" cp ${LXC_CONTAINER_DIR}/config ${LXC_CONTAINER_DIR}/config.ORIG if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/config.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/config'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi if [[ "$LXC_DIST" = "debian" ]]; then # - Create the Container configuration file # - msg="Create Container Configuration '${LXC_CONTAINER_DIR}/config (LXC Version $LXC_VERSION)' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" _failed=false if [[ $LXC_MAJOR_VERSION -lt 3 ]]; then cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg lxc.rootfs = ${LXC_CONTAINER_DIR}/rootfs # Common configuration lxc.include = /usr/share/lxc/config/${LXC_DIST}.common.conf # Container specific configuration lxc.mount = ${LXC_CONTAINER_DIR}/fstab lxc.utsname = $LXC_NAME lxc.arch = amd64 lxc.autodev = 1 lxc.kmsg = 0 # Network configuration ### Device 1 lxc.network.type = veth lxc.network.flags = up lxc.network.link = br0 lxc.network.name = eth0 lxc.network.hwaddr = $MAC_ADDRESS_1 # IPv4 lxc.network.ipv4 = ${IPV4_ADDRESS_1}/$IPV4_PREFIX_1 lxc.network.ipv4.gateway = ${IPV4_GATEWAY_1} # IPv6 EOF if [[ -n "${IPV6_ADDRESS_1}" ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.network.ipv6 = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 lxc.network.ipv6.gateway = $IPV6_GATEWAY_1 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.network.ipv6 = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 #lxc.network.ipv6.gateway = $IPV6_GATEWAY_1 EOF fi if [[ $? -ne 0 ]]; then _failed=true fi if [[ -n "$IPV4_ADDRESS_2" ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg ### Device 2 lxc.network.type = veth lxc.network.flags = up lxc.network.link = br0 lxc.network.name = eth1 lxc.network.hwaddr = $MAC_ADDRESS_2 ## IPv4 lxc.network.ipv4 = ${IPV4_ADDRESS_2}/$IPV4_PREFIX_2 ## IPv6 EOF if [[ -n "$IPV6_ADDRESS_2" ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.network.ipv6 = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.network.ipv6 = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 EOF fi if [[ $? -ne 0 ]]; then _failed=true fi fi else cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg lxc.rootfs.path = dir:${LXC_CONTAINER_DIR}/rootfs # Common configuration lxc.include = /usr/share/lxc/config/${LXC_DIST}.common.conf # Container specific configuration lxc.mount.fstab = ${LXC_CONTAINER_DIR}/fstab lxc.uts.name = $LXC_NAME lxc.arch = amd64 lxc.autodev = 1 # Network configuration ### Device 1 lxc.net.0.type = veth lxc.net.0.flags = up lxc.net.0.link = br0 lxc.net.0.name = eth0 lxc.net.0.hwaddr = $MAC_ADDRESS_1 # IPv4 lxc.net.0.ipv4.address = ${IPV4_ADDRESS_1}/$IPV4_PREFIX_1 lxc.net.0.ipv4.gateway = ${IPV4_GATEWAY_1} # IPv6 EOF if [[ -n "${IPV6_ADDRESS_1}" ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.net.0.ipv6.address = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_1 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.net.0.ipv6.address = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 #lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_1 EOF fi if [[ $? -ne 0 ]]; then _failed=true fi if [[ -n "$IPV4_ADDRESS_2" ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg ### Device 2 lxc.net.1.type = veth lxc.net.1.flags = up lxc.net.1.link = br0 lxc.net.1.name = eth1 lxc.net.1.hwaddr = $MAC_ADDRESS_2 ## IPv4 lxc.net.1.ipv4.address = ${IPV4_ADDRESS_2}/$IPV4_PREFIX_2 ## IPv6 EOF if [[ -n "$IPV6_ADDRESS_2" ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.net.1.ipv6.address = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.net.1.ipv6.address = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 EOF fi if [[ $? -ne 0 ]]; then _failed=true fi fi fi if [[ $LXC_MAJOR_VERSION -lt 3 ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg # Mount iso file inside LX Container #lxc.aa_profile = unconfined #lxc.cgroup.devices.allow = b 7:* rwm #lxc.cgroup.devices.allow = c 10:237 rwm # Autostart lxc.start.auto = 1 lxc.start.delay = 5 lxc.start.order = 100 # Limt memory to 1GB less than full Memory of the server # - lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1000000000 )) # Exposing a directory on the host machine to an LXC container # # - lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 # #lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 EOF if [[ $? -ne 0 ]]; then _failed=true fi else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg # Mount iso file inside LX Container #lxc.apparmor.profile = unconfined #lxc.cgroup.devices.allow = b 7:* rwm #lxc.cgroup.devices.allow = c 10:237 rwm # Specify the apparmor profile under which the container should be run. # # To specify that the container should be unconfined, use # lxc.apparmor.profile = unconfined # # If the apparmor profile should remain unchanged (i.e. if you are nesting # containers and are already confined), then use # lxc.apparmor.profile = unchanged # # If you instruct LXC to generate the apparmor profile, then use # lxc.apparmor.profile = generated # # # MariaDB (and maybe others) does not start # ========================================= # # see also: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=920643 # # AppArmor support in Debian has increased, thus preventing some systemd # isolation features to work in LXC 3.0.X. Debian has backported some # patches from LXC 3.1 that, along with some configurations in a # container, will allow systemd isolation features to work. # # This may entirely be a an apparmor/systemd issue and nothing to do # with mariadb. # # A workaround is to remove the the following lines from # systemd service file: # ProtectSystem=full # PrivateDevices=true # ProtectHome=true # # Another workarround is to diasble apparmor here: # lxc.apparmor.profile = unconfined # # Note: # The following commands solved this problem too, but have other # problems (e.g. errors installing/updating debian packages). # So DO NOT USE: # lxc.apparmor.profile = generated # lxc.apparmor.allow_nesting = 1 # lxc.apparmor.profile = unconfined # support fuse (filesystem in userspace) # # fuse-overlayfs package must be installed both on host and # inside container # lxc.mount.entry = /dev/fuse dev/fuse none bind,create=file 0 0 # Autostart lxc.start.auto = 1 lxc.start.delay = 5 lxc.start.order = 100 # Limt memory to 1GB less than full Memory of the server # - lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1000000000 )) # Exposing a directory on the host machine to an LXC container # # - lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 # #lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if $_failed ; then msg_failed="Creation of '${LXC_CONTAINER_DIR}/config' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="Container Configuration '${LXC_CONTAINER_DIR}/config' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi else # if [[ "$LXC_DIST" = "debian" ]]; # - Create the Container configuration file # - msg="Create Container Configuration '${LXC_CONTAINER_DIR}/config (LXC Version $LXC_VERSION)' .." echo -en "\033[1G \033[1;30m[ \033[5m\033[1m...\033[m ] \033[32m$msg\033[m" length_msg=${#msg} _failed=false cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg # Template used to create this container: /usr/share/lxc/templates/lxc-download # Parameters passed to the template: -d ubuntu --release noble --arch amd64 # For additional config options, please look at lxc.container.conf(5) # Uncomment the following line to support nesting containers: #lxc.include = /usr/share/lxc/config/nesting.conf # (Be aware this has security implications) # Distribution configuration lxc.include = /usr/share/lxc/config/common.conf #lxc.include = /usr/share/lxc/config/${LXC_DIST}.common.conf lxc.arch = linux64 # Container specific configuration #lxc.apparmor.profile = generated #lxc.apparmor.allow_nesting = 1 lxc.rootfs.path = dir:${LXC_CONTAINER_DIR}/rootfs lxc.uts.name = $LXC_NAME #lxc.mount.fstab = ${LXC_CONTAINER_DIR}/fstab # Network configuration ### Device 1 lxc.net.0.type = veth lxc.net.0.flags = up lxc.net.0.link = br0 lxc.net.0.name = eth0 lxc.net.0.hwaddr = $MAC_ADDRESS_1 # IPv4 lxc.net.0.ipv4.address = ${IPV4_ADDRESS_1}/$IPV4_PREFIX_1 lxc.net.0.ipv4.gateway = ${IPV4_GATEWAY_1} # IPv6 EOF if [[ $? -ne 0 ]]; then _failed=true fi if [[ -n "${IPV6_ADDRESS_1}" ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.net.0.ipv6.address = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_1 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.net.0.ipv6.address = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1 #lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_1 EOF fi if [[ $? -ne 0 ]]; then _failed=true fi if [[ -n "$IPV4_ADDRESS_2" ]]; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg ### Device 2 lxc.net.1.type = veth lxc.net.1.flags = up lxc.net.1.link = br0 lxc.net.1.name = eth1 lxc.net.1.hwaddr = $MAC_ADDRESS_2 # IPv4 lxc.net.1.ipv4.address = ${IPV4_ADDRESS_2}/$IPV4_PREFIX_2 lxc.net.1.ipv4.gateway = ${IPV4_GATEWAY_2} # IPv6 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if [[ -n "${IPV6_ADDRESS_2}" ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg lxc.net.0.ipv6.address = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_2 EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg #lxc.net.0.ipv6.address = #lxc.net.0.ipv6.gateway = EOF fi if [[ $? -ne 0 ]]; then _failed=true fi cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg # Mount iso file inside LX Container #lxc.apparmor.profile = unconfined #lxc.cgroup.devices.allow = b 7:* rwm #lxc.cgroup.devices.allow = c 10:237 rwm # Specify the apparmor profile under which the container should be run. # # To specify that the container should be unconfined, use # lxc.apparmor.profile = unconfined # # If the apparmor profile should remain unchanged (i.e. if you are nesting # containers and are already confined), then use # lxc.apparmor.profile = unchanged # # If you instruct LXC to generate the apparmor profile, then use # lxc.apparmor.profile = generated # # # MariaDB (and maybe others) does not start # ========================================= # # see also: https://bugs.debian.org/cgi-bin/bugreport.cgi?bug=920643 # # AppArmor support in Debian has increased, thus preventing some systemd # isolation features to work in LXC 3.0.X. Debian has backported some # patches from LXC 3.1 that, along with some configurations in a # container, will allow systemd isolation features to work. # # This may entirely be a an apparmor/systemd issue and nothing to do # with mariadb. # # A workaround is to remove the the following lines from # systemd service file: # ProtectSystem=full # PrivateDevices=true # ProtectHome=true # # Another workarround is to diasble apparmor here: # lxc.apparmor.profile = unconfined # # Note: # The following commands solved this problem too, but have other # problems (e.g. errors installing/updating debian packages). # So DO NOT USE: # lxc.apparmor.profile = generated # lxc.apparmor.allow_nesting = 1 # lxc.apparmor.profile = unconfined # support fuse (filesystem in userspace) # # fuse-overlayfs package must be installed both on host and # inside container # lxc.mount.entry = /dev/fuse dev/fuse none bind,create=file 0 0 # Autostart lxc.start.auto = 1 lxc.start.delay = 5 lxc.start.order = 100 # Limt memory to 1GB less than full Memory of the server # - lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1000000000 )) # Exposing a directory on the host machine to an LXC container # # - lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 # #lxc.mount.entry = /path/to/folder/on/host /path/to/mount/point none bind 0 0 EOF if [[ $? -ne 0 ]]; then _failed=true fi if $_failed ; then msg_failed="Adjusting '${LXC_CONTAINER_DIR}/config' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="Container Configuration '${LXC_CONTAINER_DIR}/config' adjusted." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi fi # --- # - Adjust network configuration # --- echo "" echo "" echo -e " \033[37m\033[1mAdjust network configuration inside the new LX Container ..\033[m" echo "" if [[ "$LXC_DIST" = "debian" ]]; then # - Backup file '/etc/network/interfaces' inside the Container # - msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" cp ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces \ ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces.ORIG 2> ${err_msg} if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create Containers network configuration # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" _failed=false if [[ -z "$IPV4_ADDRESS_2" ]] ; then cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces #----------------------------- # lo: loopback #----------------------------- auto lo iface lo inet loopback #----------------------------- # eth0 #----------------------------- auto eth0 iface eth0 inet manual iface eth0 inet6 manual EOF if [[ $? -ne 0 ]]; then _failed=true fi else cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces 2> $err_msg #----------------------------- # lo: loopback #----------------------------- auto lo iface lo inet loopback #----------------------------- # eth0 #----------------------------- auto eth0 iface eth0 inet manual up ip route add default via ${IPV4_GATEWAY_1} dev eth0 table 100 up ip rule add from ${IPV4_ADDRESS_1} table 100 prio 100 #----------------------------- # eth1 #----------------------------- auto eth1 iface eth1 inet manual up ip route add default via ${IPV4_GATEWAY_2} dev eth1 table 200 up ip rule add from ${IPV4_ADDRESS_2} table 200 prio 120 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if $_failed ; then msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi # - Backup Containers file '/etc/resolv.conf' # - echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" ]] ; then cp "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" \ "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" # - Create new containers file '/etc/resolv.conf' # - _failed=false msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" : > "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" for _ip in "${nameserver_ip_arr[@]}" ; do echo "nameserver ${_ip}" >> "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" if [[ $? -ne 0 ]]; then _failed=true fi done if [[ -n "${SEARCH_DOMAIN}" ]] ; then echo "search ${SEARCH_DOMAIN}" >> "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" if [[ $? -ne 0 ]]; then _failed=true fi fi if ${_failed} ; then msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else msg_skipped="File '${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf not present." length_msg_skipped=${#msg_skipped} blank_signs="" if [[ $length_msg -gt $length_msg_skipped ]]; then number_blank_sign=$(expr $length_msg - $length_msg_skipped) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] ${msg_skipped}${blank_signs}" fi elif [[ "$LXC_DIST" = "ubuntu" ]]; then if [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml" ]] ; then msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml" echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" cp ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml \ ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml.ORIG 2> ${err_msg} if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi elif [ -e /etc/netplan/*.yaml ] ; then for _file in $(ls ${LXC_CONTAINER_DIR}/root/netplan/*.yml) ; do msg="Backup file '${_file}'" echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" mv "${_file}" "${_file}.ORIG" 2> ${err_msg} if [[ $? -eq 0 ]] ; then msg_ok="File '${_file}.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${_file}'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi done fi # - Create Containers network configuration # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml' .." echo -en "\033[1G \033[1;30m[ \033[5m\033[1m...\033[m ] \033[32m$msg\033[m" _failed=false if $NETWORK_CONFIGURATION_DHCP ; then cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml network: version: 2 ethernets: eth0: dhcp4: true dhcp-identifier: mac EOF if [[ $? -ne 0 ]]; then _failed=true fi else if [[ -n ${IPV6_ADDRESS_1} ]] ; then cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no dhcp6: no addresses: - ${IPV4_ADDRESS_1}/${IPV4_PREFIX_1} - ${IPV6_ADDRESS_1}/${IPV6_PREFIX_1} gateway4: ${IPV4_GATEWAY_1} gateway6: ${IPV6_GATEWAY_1} EOF if [[ $? -ne 0 ]]; then _failed=true fi else cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml network: version: 2 renderer: networkd ethernets: eth0: dhcp4: no addresses: - ${IPV4_ADDRESS_1}/${IPV4_PREFIX_1} gateway4: ${IPV4_GATEWAY_1} EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if [[ ${#nameserver_ip_arr[@]} -gt 0 ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml nameservers: addresses: EOF for _nameserver in ${nameserver_ip_arr[@]} ; do cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml - ${_nameserver} EOF if [[ $? -ne 0 ]]; then _failed=true fi done fi if [[ ${#search_ip_arr[@]} -gt 0 ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml search: EOF for _search_domain in ${search_ip_arr[@]} ; do cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml - ${SEARCH_DOMAIN} EOF if [[ $? -ne 0 ]]; then _failed=true fi done fi fi # if $NETWORK_CONFIGURATION_DHCP if $_failed ; then msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi fi # --- # - Some post-installation tasks on the new LX Container # --- echo "" echo "" echo -e " \033[37m\033[1mSome post-installation tasks inside the new LX Container '${LXC_NAME}' ..\033[m" echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' .." mv "${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list" "${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list.ORIG" > $err_msg 2>&1 # - Backup 'sources.list'-file. # - if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list.ORIG' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Backup of file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create new 'sources.list'-file. # - msg="Create file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' .." if [[ "$LXC_DIST" = "debian" ]]; then cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2> $err_msg deb http://ftp.de.debian.org/debian/ $LXC_RELEASE main non-free contrib deb-src http://ftp.de.debian.org/debian/ $LXC_RELEASE main non-free contrib EOF if [[ "$LXC_RELEASE" = 'buster' ]] \ || [[ "$LXC_RELEASE" = 'stretch' ]] \ || [[ "$LXC_RELEASE" = 'jessie' ]] \ || [[ "$LXC_RELEASE" = 'wheezy' ]] ; then cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2> $err_msg deb http://security.debian.org/ $LXC_RELEASE/updates main contrib non-free deb-src http://security.debian.org/ $LXC_RELEASE/updates main contrib non-free EOF else cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2>> $err_msg deb http://security.debian.org/debian-security ${LXC_RELEASE}-security main contrib non-free deb-src http://security.debian.org/debian-security ${LXC_RELEASE}-security main contrib non-free EOF cat << EOF >> ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2>> $err_msg # $LXC_RELEASE-updates, previously known as 'volatile' deb http://ftp.de.debian.org/debian/ $LXC_RELEASE-updates main contrib non-free deb-src http://ftp.de.debian.org/debian/ $LXC_RELEASE-updates main contrib non-free # $LXC_RELEASE-backports, previously on backports.debian.org deb http://ftp.de.debian.org/debian/ $LXC_RELEASE-backports main contrib non-free deb-src http://ftp.de.debian.org/debian/ $LXC_RELEASE-backports main contrib non-free EOF fi else cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2>> $err_msg # Generated by distrobuilder deb http://archive.ubuntu.com/ubuntu ${LXC_RELEASE} main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu ${LXC_RELEASE}-updates main restricted universe multiverse deb http://security.ubuntu.com/ubuntu ${LXC_RELEASE}-security main restricted universe multiverse deb http://archive.ubuntu.com/ubuntu ${LXC_RELEASE}-backports main restricted universe multiverse EOF fi if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Backup symlink '/bin/sh'. Move to '/bin/sh.ORIG'. # - echo "" msg="Move Symlink '/bin/sh' to '/bin/sh.ORIG' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" mv "${LXC_CONTAINER_DIR}/rootfs/bin/sh" "${LXC_CONTAINER_DIR}/rootfs/bin/sh.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="Symlink '${LXC_CONTAINER_DIR}/rootfs/bin/sh.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Saving Symlink '${LXC_CONTAINER_DIR}/rootfs/etc/bin/sh' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create new symlink '/bin/sh' # - msg="Create new Symlink '/bin/sh' pointing to 'bash' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" ln -s bash ${LXC_CONTAINER_DIR}/rootfs/bin/sh > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="New Symlink '${LXC_CONTAINER_DIR}/rootfs/bin/sh' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Creation of new Symlink '${LXC_CONTAINER_DIR}/rootfs/etc/bin/sh' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Add entry for hostname in /etc/hosts # - echo "" msg="Add entry for hostname '$FQ_HOSTNAME' to file '${LXC_CONTAINER_DIR}/rootfs/etc/hosts' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" perl -i.ORIG -n -p -e "s/^(\s*127\.0\.0\.1.*)/\1\n127.0.1.1 $FQ_HOSTNAME $HOSTNAME\n/" ${LXC_CONTAINER_DIR}/rootfs/etc/hosts > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="Entry '127.0.1.1 $FQ_HOSTNAME $HOSTNAME' added to '${LXC_CONTAINER_DIR}/rootfs/etc/hosts'." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Adding entry '127.0.1.1 $FQ_HOSTNAME $HOSTNAME' to '${LXC_CONTAINER_DIR}/rootfs/etc/hosts' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Backup Containers file '/etc/hostname' # - echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/hostname' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" mv "${LXC_CONTAINER_DIR}/rootfs/etc/hostname" "${LXC_CONTAINER_DIR}/rootfs/etc/hostname.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/hostname.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/hostname'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Write new Containers file /etc/hostname # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/hostname' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" echo "$HOSTNAME" > "${LXC_CONTAINER_DIR}/rootfs/etc/hostname" 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/hostname' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/hostname'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi if [[ "$LXC_DIST" = "debian" ]] || [[ "${LXC_DIST}" = "ubuntu" ]] ; then # - Install 'figlet' from debian package system # - echo "" _pkg="figlet" msg="Install '$_pkg' from debian package system .." if ! $(aptitude search "$_pkg" | grep " $_pkg " | grep -e "^i" > /dev/null 2>&1) ; then DEBIAN_FRONTEND=noninteractive apt-get -y install $_pkg > /dev/null 2> "$err_msg" if [[ $? -eq 0 ]] ; then msg_ok="Debian package '$_pkg' successfully installed." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Installation of debiab package '$_pkg' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi # - Backup Containers file '/etc/motd' # - echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/motd' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/motd" ]] ; then cp "${LXC_CONTAINER_DIR}/rootfs/etc/motd" "${LXC_CONTAINER_DIR}/rootfs/etc/motd.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/motd.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" # - Create new containers file '/etc/motd' # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/motd' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" figlet $HOSTNAME > "${LXC_CONTAINER_DIR}/rootfs/etc/motd" if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/motd' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/motd'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/motd'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else msg_skipped="File '${LXC_CONTAINER_DIR}/rootfs/etc/motd not present." length_msg_skipped=${#msg_skipped} blank_signs="" if [[ $length_msg -gt $length_msg_skipped ]]; then number_blank_sign=$(expr $length_msg - $length_msg_skipped) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] ${msg_skipped}${blank_signs}" fi fi # if [[ "$LXC_DIST" = "debian" ]] if [[ "$LXC_DIST" = "debian" ]] || [[ "${LXC_DIST}" = "ubuntu" ]]; then # - Backup Containers file '/etc/ssh/sshd_config' # - echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/sshd_config" ]] ; then cp "${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config" \ "${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" # - Create new containers file '/etc/ssh/sshd_config' # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${working_dir}/supported-files/sshd_config" ]]; then cp -a "${working_dir}/supported-files/sshd_config" \ "${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config" 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else msg_skipped="File '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config not present." length_msg_skipped=${#msg_skipped} blank_signs="" if [[ $length_msg -gt $length_msg_skipped ]]; then number_blank_sign=$(expr $length_msg - $length_msg_skipped) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] ${msg_skipped}${blank_signs}" fi fi # if [[ "$LXC_DIST" = "debian" ]] # - Backup Containers file '/root/.bashrc' # - echo "" msg="Backup file '${LXC_CONTAINER_DIR}/rootfs//root/.bashrc' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" mv "${LXC_CONTAINER_DIR}/rootfs/root/.bashrc" "${LXC_CONTAINER_DIR}/rootfs/root/.bashrc.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/root/.bashrc.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/root/.bashrc'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create new containers file '/root/.bashrc' # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/root/.bashrc' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${working_dir}/supported-files/templates_root/.bashrc" ]]; then cp -a "${working_dir}/supported-files/templates_root/.bashrc" \ "${LXC_CONTAINER_DIR}/rootfs/root/.bashrc" 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/root/.bashrc' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/root/.bashrc'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi # - Backup Containers file '/root/.profile' # - msg="Backup file '${LXC_CONTAINER_DIR}/rootfs//root/.profile' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" mv "${LXC_CONTAINER_DIR}/rootfs/root/.profile" "${LXC_CONTAINER_DIR}/rootfs/root/.profile.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="File '${LXC_CONTAINER_DIR}/rootfs/root/.profile.ORIG' saved." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Could not backup file '${LXC_CONTAINER_DIR}/rootfs/root/.profile'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create new containers file '/root/.profile' # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/root/.profile' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${working_dir}/supported-files/templates_root/.profile" ]]; then cp -a "${working_dir}/supported-files/templates_root/.profile" \ "${LXC_CONTAINER_DIR}/rootfs/root/.profile" 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/root/.profile' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/root/.profile'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi # - Create new containers file '/root/.vimrc' # - msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/root/.vimrc' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${working_dir}/supported-files/templates_root/.vimrc" ]]; then cp -a "${working_dir}/supported-files/templates_root/.vimrc" \ "${LXC_CONTAINER_DIR}/rootfs/root/.vimrc" 2>$err_msg if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/root/.vimrc' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/root/.vimrc'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi # - Create new container directory '/root/bin' # - echo "" msg="Create a new directory '${LXC_CONTAINER_DIR}/rootfs/root/bin' .." mkdir "${LXC_CONTAINER_DIR}/rootfs/root/bin" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="New directory '${LXC_CONTAINER_DIR}/rootfs/root/bin' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to create directory '${LXC_CONTAINER_DIR}/rootfs/root/bin'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Add git repository 'admin-stuff' # - msg="Clone git repository 'admin-stuff' to '${LXC_CONTAINER_DIR}/rootfs/root/bin/admin-stuff'" git clone https://git.oopen.de/script/admin-stuff ${LXC_CONTAINER_DIR}/rootfs/root/bin/admin-stuff 2> /dev/null if [[ $? -eq 0 ]] ; then msg_ok="Git repository '${LXC_CONTAINER_DIR}/rootfs/root/bin/admin-stuff' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to clone git repository 'admin-stuff'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi # - Create new containers directory '/root/.ssh' # - echo "" msg="Create a new directory '${LXC_CONTAINER_DIR}/rootfs/root/.ssh' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" if [[ -f "${working_dir}/supported-files/templates_root/.ssh/authorized_keys2" ]] || [[ -f "${working_dir}/supported-files/templates_root/.ssh/authorized_keys" ]]; then if [[ -d "${LXC_CONTAINER_DIR}/rootfs/root/.ssh" ]] ; then echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" else mkdir "${LXC_CONTAINER_DIR}/rootfs/root/.ssh" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="New directory '${LXC_CONTAINER_DIR}/rootfs/root/.ssh' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" msg="Copy supported files to '${LXC_CONTAINER_DIR}/rootfs/root/.ssh/' .." cp -a "${working_dir}/supported-files/templates_root/.ssh" \ "${LXC_CONTAINER_DIR}/rootfs/root/" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then msg_ok="Supported files copied to new directory '${LXC_CONTAINER_DIR}/rootfs/root/.ssh'." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Failed to copy supported files to '${LXC_CONTAINER_DIR}/rootfs/root/.ssh'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi else msg_failed="Failed to create directory '${LXC_CONTAINER_DIR}/rootfs/root/.ssh'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi fi else echo -e "\033[1G [ \033[30m\033[1mskip\033[m ] $msg" fi # --- # - Start the new LX Container # --- echo "" echo "" echo -e " \033[37m\033[1mStart the new LX Container '${LXC_NAME}' ..\033[m" echo "" msg="Start Container '${LXC_NAME}' as a background process .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" lxc-start -d -n $LXC_NAME > /dev/null 2> $err_msg if [[ $? -eq 0 ]] ; then msg_ok="LX Container '${LXC_NAME}' started successfully." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else msg_failed="Starting Container '${LXC_NAME}' failed!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" fi echo "" # - Write summary for LX Container # - msg="Write summary for LX Container '${LXC_NAME}' to file '${working_dir}/conf/${LXC_NAME}.conf' .." echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" _failed=false cat << EOF > ${working_dir}/conf/${LXC_NAME}.conf 2> $err_msg LXC_ROOT_DIR=$LXC_ROOT_DIR LXC_CONTAINER_DIR=$LXC_CONTAINER_DIR LXC_NAME=$LXC_NAME LXC_DIST=$LXC_DIST LXC_RELEASE=$LXC_RELEASE MAC_ADDRESS_1=$MAC_ADDRESS_1 IPV4_ADDRESS_1=$IPV4_ADDRESS_1 IPV4_NETMASK_1=$IPV4_NETMASK_1 IPV4_GATEWAY_1=$IPV4_GATEWAY_1 IPV4_PREFIX_1=$IPV4_PREFIX_1 EOF if [[ $? -ne 0 ]]; then _failed=true fi if [[ "$IPV6_ADDRESS_1" ]]; then cat << EOF >> ${working_dir}/conf/${LXC_NAME}.conf 2>> $err_msg IPV6_ADDRESS_1=$IPV6_ADDRESS_1/$IPV6_PREFIX_1 IPV6_GATEWAY_1=$IPV6_GATEWAY_1 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if $_second_ipv4 ; then cat << EOF >> ${working_dir}/conf/${LXC_NAME}.conf 2>> $err_msg MAC_ADDRESS_2=$MAC_ADDRESS_2 IPV4_ADDRESS_2=$IPV4_ADDRESS_2 IPV4_NETMASK_2=$IPV4_NETMASK_2 IPV4_GATEWAY_2=$IPV4_GATEWAY_2 IPV4_PREFIX_2=$IPV4_PREFIX_2 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi if [[ "$IPV6_ADDRESS_2" ]]; then cat << EOF >> ${working_dir}/conf/${LXC_NAME}.conf 2>> $err_msg IPV6_ADDRESS_2=$IPV6_ADDRESS_2/$IPV6_PREFIX_2 EOF if [[ $? -ne 0 ]]; then _failed=true fi fi cat << EOF >> ${working_dir}/conf/${LXC_NAME}.conf 2>> $err_msg HOSTNAME=$HOSTNAME FQ_HOSTNAME=$FQ_HOSTNAME EOF if [[ $? -ne 0 ]]; then _failed=true fi if $_failed ; then msg_failed="Failed to create summary file '${working_dir}/conf/${LXC_NAME}.conf'!" length_msg_failed=${#msg_failed} blank_signs="" if [[ $length_msg -gt $length_msg_failed ]]; then number_blank_sign=$(expr $length_msg - $length_msg_failed) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" error "$(cat $err_msg)" else msg_ok="Summary file '${working_dir}/conf/${LXC_NAME}.conf' created." length_msg_ok=${#msg_ok} blank_signs="" if [[ $length_msg -gt $length_msg_ok ]]; then number_blank_sign=$(expr $length_msg - $length_msg_ok) index_i=0 while [[ $index_i -lt $number_blank_sign ]] ; do blank_signs="$blank_signs " (( index_i++ )) done fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi echo "" info " Direct access to the new container with command: \033[1mlxc-attach -n $LXC_NAME\033[m" if [[ "$LXC_DIST" = "ubuntu" ]]; then info " This is a really minimal Ubuntu installation. Inside the Container, first install and configure locales, also openssh is not yet installed: \033[1mapt update apt dist-upgrade apt install locales dpkg-reconfigure locales apt install openssh-server\033[m" fi info " Inside the new Contaimer, run the following commands to create/adjust users: \033[1madd_new_user.sh root apt update apt install sudo vim git mc add_new_user.sh chris add_new_user.sh sysadm add_new_user.sh back\033[m" echo "" echo "" clean_up 0