From 0fb729fa2f5ba8624dd57c76f134894b16566028 Mon Sep 17 00:00:00 2001 From: Christoph Date: Tue, 21 Jan 2025 12:36:58 +0100 Subject: [PATCH] create-lx-container.sh: among other things, fix support for ubuntu distribution. --- create-lx-container.sh | 828 ++++++++++++++++++++++++++++------------- 1 file changed, 571 insertions(+), 257 deletions(-) diff --git a/create-lx-container.sh b/create-lx-container.sh index bde8ec3..9702238 100755 --- a/create-lx-container.sh +++ b/create-lx-container.sh @@ -311,6 +311,10 @@ 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 @@ -508,7 +512,7 @@ if ! $NETWORK_CONFIGURATION_DHCP ; then 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 " # Type to accept the default one" echo "" echononl "\033[1mIPv4 (second) Gateway Address [$IPV4_GATEWAY_1]:\033[m " read IPV4_GATEWAY_2 @@ -530,7 +534,7 @@ if ! $NETWORK_CONFIGURATION_DHCP ; then echo " # Insert Netmask for the second IPv4 Address." echo " #" - echo " # Type to accept the default one" + echo " # Type to accept the default one" echo "" echononl "\033[1mNetmask (second) IPv4 Address [$IPV4_NETMASK_1]:\033[m " read IPV4_NETMASK_2 @@ -549,50 +553,6 @@ if ! $NETWORK_CONFIGURATION_DHCP ; then IPV6_PREFIX_2=$DEFAULT_IPV6_PREFIX fi - else # if [[ "$LXC_DIST" = "debian" ]]; then - - echo "" - echo -e "\033[32m--\033[m" - echo "" - - 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 - - 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 "" - 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 - IPV6_ADDRESS_1="" - fi - fi # if [[ "$LXC_DIST" = "debian" ]]; then echo "" @@ -633,6 +593,58 @@ if ! $NETWORK_CONFIGURATION_DHCP ; then 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 @@ -681,8 +693,20 @@ if $NETWORK_CONFIGURATION_DHCP ; then echo -e " Network Configuration....................: \033[33mvia DHCP\033[m" else echo " IPv4 Address (first).....................: $IPV4_ADDRESS_1" - echo " IPv4 Gateway Address (first).............: $IPV4_GATEWAY_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 @@ -830,8 +854,10 @@ 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")" -- \ - --no-validate -d $LXC_DIST --release $LXC_RELEASE --arch amd64 > /dev/null 2> $err_msg + -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 @@ -1255,7 +1281,7 @@ EOF # # Note: # The following commands solved this problem too, but have other -# problems (e.g. errors installing/updating debian packages). +# problems (e.g. errors installing/updating debian packages). # So DO NOT USE: # lxc.apparmor.profile = generated # lxc.apparmor.allow_nesting = 1 @@ -1320,31 +1346,75 @@ EOF fi echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi -else # if [[ "$LXC_DIST" = "debian" ]]; +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" - 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 - cat <> ${LXC_CONTAINER_DIR}/config 2>>$err_msg + 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 @@ -1354,44 +1424,36 @@ 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 [[ $LXC_MAJOR_VERSION -lt 3 ]]; then + if [[ -n "${IPV6_ADDRESS_2}" ]] ; 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 +lxc.net.0.ipv6.address = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2 +lxc.net.0.ipv6.gateway = $IPV6_GATEWAY_2 EOF - if [[ $? -ne 0 ]]; then - _failed=true - fi - 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 @@ -1436,12 +1498,12 @@ EOF # # Note: # The following commands solved this problem too, but have other -# problems (e.g. errors installing/updating debian packages). +# 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 +lxc.apparmor.profile = unconfined # support fuse (filesystem in userspace) @@ -1468,9 +1530,8 @@ lxc.cgroup.memory.limit_in_bytes = $(( $(free -b | grep -oP '\d+' | head -n 1)-1 # #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 [[ $? -ne 0 ]]; then + _failed=true fi if $_failed ; then @@ -1523,7 +1584,7 @@ if [[ "$LXC_DIST" = "debian" ]]; then 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 + ${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." @@ -1646,44 +1707,207 @@ EOF echo -e "\033[1G [ \033[32m\033[1mok\033[m ] ${msg_ok}${blank_signs}" fi -elif [[ "$LXC_DIST" = "ubuntu" ]]; then - msg="Backup file '${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml" + + + # - 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" - cp ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml \ - ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml.ORIG + if [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/resolv.conf" ]] ; then - 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++ )) + 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 - 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} + + 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_failed ]]; then - number_blank_sign=$(expr $length_msg - $length_msg_failed) + 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[31m\033[1mfailed\033[m ] ${msg_failed}${blank_signs}" - error "$(cat $err_msg)" + 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' .." @@ -1700,10 +1924,33 @@ network: dhcp4: true dhcp-identifier: mac EOF + if [[ $? -ne 0 ]]; then + _failed=true + fi else - cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/netplan/10-lxc.yaml + 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 @@ -1711,22 +1958,45 @@ network: eth0: dhcp4: no addresses: - - ${IPV4_ADDRESS_1}/24 + - ${IPV4_ADDRESS_1}/${IPV4_PREFIX_1} gateway4: ${IPV4_GATEWAY_1} - nameservers: - addresses: - - ${IPV4_NAME_SERVER_1} - search: - - ${SEARCH_DOMAIN} - 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 [[ $? -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} @@ -1835,7 +2105,7 @@ deb-src http://security.debian.org/debian-security ${LXC_RELEASE}-security main 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 @@ -1851,17 +2121,16 @@ 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} 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 +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." @@ -2081,7 +2350,7 @@ else fi -if [[ "$LXC_DIST" = "debian" ]]; then +if [[ "$LXC_DIST" = "debian" ]] || [[ "${LXC_DIST}" = "ubuntu" ]] ; then # - Install 'figlet' from debian package system # - @@ -2131,128 +2400,12 @@ if [[ "$LXC_DIST" = "debian" ]]; then 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 [[ -f "${LXC_CONTAINER_DIR}/rootfs/etc/motd" ]] ; then - 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 - -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 + cp "${LXC_CONTAINER_DIR}/rootfs/etc/motd" "${LXC_CONTAINER_DIR}/rootfs/etc/motd.ORIG" > $err_msg 2>&1 if [[ $? -eq 0 ]] ; then - msg_ok="New file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config' created." + 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 @@ -2264,8 +2417,47 @@ if [[ "$LXC_DIST" = "debian" ]]; then 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="Failed to create file '${LXC_CONTAINER_DIR}/rootfs/etc/ssh/sshd_config'!" + 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 @@ -2279,9 +2471,130 @@ if [[ "$LXC_DIST" = "debian" ]]; 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 + + 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" ]] @@ -2797,11 +3110,11 @@ fi echo "" -info " Direct access to the new container with command: +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 + 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 @@ -2811,6 +3124,7 @@ if [[ "$LXC_DIST" = "ubuntu" ]]; then 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