From 4beab46951b636bac682dd9e1b2a9002f24856c8 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 27 Apr 2021 16:50:53 +0200 Subject: [PATCH] create-lx-container.sh: Add support for 'Ubuntu' guest system. --- create-lx-container.sh | 1232 ++++++++++++++++++++++++++-------------- 1 file changed, 799 insertions(+), 433 deletions(-) diff --git a/create-lx-container.sh b/create-lx-container.sh index b119cec..51b1cb6 100755 --- a/create-lx-container.sh +++ b/create-lx-container.sh @@ -364,6 +364,7 @@ while ! $(is_valid_ipv4 $IPV4_ADDRESS_1); do done _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" @@ -412,114 +413,161 @@ IPV4_PREFIX_1="$(netmask2cidr $IPV4_NETMASK_1)" IPV6_PREFIX_1=$DEFAULT_IPV6_PREFIX -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 [[ "$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 + 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" + _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 - DEFAULT_MAC_ADDRESS_2="${MAC_ADDRESS_1:0:14}:$_new_last_octet" - echo "" - echo -e "\033[32m--\033[m" - echo "" +else # if [[ "$LXC_DIST" = "debian" ]]; then - 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 + echo "" + echo -e "\033[32m--\033[m" + echo "" - 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 " # Insert Nameserver for the first IPv4 Address." + echo " #" + echo " # Type to accept the default one" + echo "" + echononl "\033[1mNameserver (IPv4 Address) [$DEFAULT_IPV4_NAME_SERVER_1]:\033[m " + read IPV4_NAME_SERVER_1 - echo "" - echo -e "\033[32m--\033[m" - echo "" + while ! $(is_valid_ipv4 $IPV4_NAME_SERVER_1) ; do + if [[ -z "$(trim $IPV4_NAME_SERVER_1)" ]]; then + IPV4_NAME_SERVER_1="$DEFAULT_IPV4_NAME_SERVER_1" + break + else + warn "'$IPV4_NAME_SERVER_1' is not a valid IPv4 address. Try again.." + fi + echononl "\033[1mNameserver (IPv4 Address) [$DEFAULT_IPV4_NAME_SERVER_1]:\033[m " + read IPV4_NAME_SERVER_1 + done - echo " # Insert second IPv4 Address for the new LX Containers." - echo " #" - echo "" - echononl "\033[1mSecond IPv4 Address:\033[m " - read IPV4_ADDRESS_2 + 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 ! $(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 + 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 + IPV6_ADDRESS_1="" + fi - 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 first 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" @@ -749,8 +797,28 @@ msg="Create Lx Container '$LXC_NAME' .." length_msg=${#msg} echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" -lxc-create -n $LXC_NAME -t $LXC_DIST -P "$(dirname "$LXC_CONTAINER_DIR")" -- \ - --release $LXC_RELEASE --arch amd64 > /dev/null 2> $err_msg +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." @@ -831,41 +899,43 @@ if [[ "$(dirname "$LXC_CONTAINER_DIR")" != "$LXC_ROOT_DIR" ]]; then fi -# - 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" +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 + 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)" + 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 @@ -905,14 +975,17 @@ else error "$(cat $err_msg)" fi -# - 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 + +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 @@ -940,25 +1013,25 @@ 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 + 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 + 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 + fi - if [[ $? -ne 0 ]]; then - _failed=true - fi + if [[ $? -ne 0 ]]; then + _failed=true + fi - if [[ -n "$IPV4_ADDRESS_2" ]]; then - cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg + 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 @@ -969,26 +1042,24 @@ lxc.network.hwaddr = $MAC_ADDRESS_2 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 + 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 + 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 - if [[ $? -ne 0 ]]; then - _failed=true - fi - fi -else - - - - cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg + cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg lxc.rootfs.path = ${LXC_CONTAINER_DIR}/rootfs # Common configuration @@ -1015,25 +1086,25 @@ 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 + 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 + 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 + fi - if [[ $? -ne 0 ]]; then - _failed=true - fi + if [[ $? -ne 0 ]]; then + _failed=true + fi - if [[ -n "$IPV4_ADDRESS_2" ]]; then - cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg + 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 @@ -1044,27 +1115,27 @@ lxc.net.1.hwaddr = $MAC_ADDRESS_2 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 + 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 + 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 - 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 + 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 @@ -1081,11 +1152,11 @@ lxc.start.order = 100 # - lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1000000000 )) EOF - if [[ $? -ne 0 ]]; then - _failed=true - fi -else - cat << EOF >> ${LXC_CONTAINER_DIR}/config 2>> $err_msg + 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 @@ -1147,38 +1218,199 @@ lxc.start.order = 100 # - lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1000000000 )) 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" ]]; + + msg="Adjust Container configuration file '${LXC_CONTAINER_DIR}/config' .." + length_msg=${#msg} + _failed=false + + echo -en "\033[1G \033[1;30m[ \033[5m\033[1m...\033[m ] \033[32m$msg\033[m" + + sed -i '/\s*lxc.net.0.type\s*=/d' ${LXC_CONTAINER_DIR}/config > /dev/null 2>$err_msg 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}" + cat <> ${LXC_CONTAINER_DIR}/config 2>>$err_msg +### 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 +EOF + 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 +EOF + if [[ $? -ne 0 ]]; then + _failed=true + 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 )) +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 + + +# 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 )) +EOF + if [[ $? -ne 0 ]]; then + _failed=true + fi + 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 @@ -1192,53 +1424,54 @@ 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" + # - 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 + cp ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces \ + ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces.ORIG -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 + 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" + # - 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 + _failed=false + if [[ -z "$IPV4_ADDRESS_2" ]] ; then + cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces #----------------------------- # lo: loopback @@ -1255,12 +1488,12 @@ iface eth0 inet manual iface eth0 inet6 manual EOF - if [[ $? -ne 0 ]]; then - _failed=true - fi + if [[ $? -ne 0 ]]; then + _failed=true + fi -else - cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces 2> $err_msg + else + cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/network/interfaces 2> $err_msg #----------------------------- # lo: loopback @@ -1288,38 +1521,133 @@ 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 [[ $? -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}" + 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 + +else # if [[ "$LXC_DIST" = "debian" ]]; + + 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 + + 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 + + + # - 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 + cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml +network: + version: 2 + renderer: networkd + ethernets: + eth0: + dhcp4: no + addresses: + - ${IPV4_ADDRESS_1}/24 + gateway4: ${IPV4_GATEWAY_1} + nameservers: + addresses: + - ${IPV4_NAME_SERVER_1} + search: + - ${SEARCH_DOMAIN} + +EOF + if [[ $? -ne 0 ]]; then + _failed=true + fi + + 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 @@ -1372,7 +1700,10 @@ fi # - Create new 'sources.list'-file. # - msg="Create file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' .." -cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2> $err_msg + +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 @@ -1390,6 +1721,22 @@ deb-src http://ftp.de.debian.org/debian/ $LXC_RELEASE-backports main contrib non EOF +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://de.archive.ubuntu.com/ubuntu/ $LXC_RELEASE-backports main restricted universe multiverse +deb http://archive.canonical.com/ubuntu $LXC_RELEASE partner + +EOF + +fi + + if [[ $? -eq 0 ]] ; then msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' created." length_msg_ok=${#msg_ok} @@ -1608,18 +1955,60 @@ else fi +if [[ "$LXC_DIST" = "debian" ]]; 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 + # - 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" + 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" + + mv "${LXC_CONTAINER_DIR}/rootfs/etc/motd" "${LXC_CONTAINER_DIR}/rootfs/etc/motd.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then - msg_ok="Debian package '$_pkg' successfully installed." + 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 @@ -1632,7 +2021,7 @@ if ! $(aptitude search "$_pkg" | grep " $_pkg " | grep -e "^i" > /dev/null 2>&1) fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" else - msg_failed="Installation of debiab package '$_pkg' failed!" + 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 @@ -1646,136 +2035,17 @@ if ! $(aptitude search "$_pkg" | grep " $_pkg " | grep -e "^i" > /dev/null 2>&1) 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 '/etc/motd' + # - -# - 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" + msg="Create a new file '${LXC_CONTAINER_DIR}/rootfs/etc/motd' .." + echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m" -mv "${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}" -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 - -# - 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 - - - -# - 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" - -mv "${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}" -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 - -# - 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 + figlet $HOSTNAME > "${LXC_CONTAINER_DIR}/rootfs/etc/motd" if [[ $? -eq 0 ]] ; then - msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config' created." + 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 @@ -1788,7 +2058,7 @@ if [[ -f "${working_dir}/supported-files/sshd_config" ]]; then 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'!" + 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 @@ -1802,9 +2072,92 @@ if [[ -f "${working_dir}/supported-files/sshd_config" ]]; then 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 + +fi # if [[ "$LXC_DIST" = "debian" ]] + + +if [[ "$LXC_DIST" = "debian" ]]; 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" + + mv "${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}" + 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 + + # - 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 + +fi # if [[ "$LXC_DIST" = "debian" ]] # - Backup Containers file '/root/.bashrc' @@ -2313,10 +2666,23 @@ 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 add_new_user.sh chris add_new_user.sh sysadm add_new_user.sh back\033[m" echo "" + + +echo "" clean_up 0