LXC/create-lx-container.sh
2018-04-11 04:13:19 +02:00

1674 lines
48 KiB
Bash
Executable File

#!/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}"
}
# -------------
# - 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_IPV6_PREFIX=64
if [[ -f "$conf_file" ]]; then
source "$conf_file"
fatal "Configuration file '$(basename ${conf_file})' not found!"
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 root directory for LX Containers."
echo " #"
echo " # Type <return> 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
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 ""
echo " # Insert linux distribution for the new LX Containers."
echo " #"
echo " # Type <return> 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"
fi
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo " # Insert the Code name of the Linux Distribution."
echo " #"
echo " # Example for debian: 'stretch'"
echo ""
echononl "\033[1mCode name:\033[m "
read LXC_RELEASE
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 <return> 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 ""
echo " # Insert first IPv4 Address for the new LX Containers."
echo " #"
echo ""
echononl "\033[1mFirst IPv4 Address:\033[m "
read IPV4_ADDRESS_1
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
done
echo ""
echo -e "\033[32m--\033[m"
echo ""
echo " # Insert IPv4 Address for the gateway."
echo " #"
echo ""
echononl "\033[1mIPv4 (first) Gateway Address:\033[m "
read IPV4_GATEWAY_1
while ! $(is_valid_ipv4 $IPV4_GATEWAY_1) ; do
if [[ -z "$(trim $IPV4_GATEWAY_1)" ]]; then
warn "Parameter is needed. Try again"
else
warn "'$IPV4_GATEWAY_1' is not a valid IPv4 Address. Try again.."
fi
echononl "\033[1mIPv4 (first) Gateway Address:\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 ""
echononl "\033[1mNetmask (first) IPv4 Address:\033[m "
read IPV4_NETMASK_1
while ! $(is_valid_ipv4 $IPV4_NETMASK_1) ; do
if [[ -z "$(trim $IPV4_NETMASK_1)" ]]; then
warn "Parameter is needed. Try again"
else
warn "'$IPV4_NETMASK_1' is not a valid netmask. Try again.."
fi
echononl "\033[1mNetmask (first) IPv4 Address:\033[m "
read IPV4_NETMASK_1
done
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? [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 <return> 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 <return> 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 <return> 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
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 $IPV4_NETMASK_1)" ]]; 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'.')"
echo ""
echo ""
echo -e " \033[32m---\033[m"
echo -e " \033[32m\033[1mParameter Summary\033[m"
echo -e " \033[32m---\033[m"
echo ""
echo " Root directory LX Container..........: $LXC_ROOT_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 " IPv4 Address (first).................: $IPV4_ADDRESS_1"
echo " IPv4 Gateway Address (first).........: $IPV4_GATEWAY_1"
echo " Netmask first IPv4 Address...........: $IPV4_NETMASK_1"
echo " CIDR (IPv4 Prefix) of netmask........: $IPV4_PREFIX_1"
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
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[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"
lxc-create -n $LXC_NAME -t $LXC_DIST -- --release $LXC_RELEASE --arch amd64 > /dev/null 2> $err_msg
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}"
error "$(cat $err_msg)"
fi
# ---
# - Configure the new LX Container
# ---
echo ""
echo ""
echo -e " \033[37m\033[1mConfigure the new LX Container..\033[m"
echo ""
# - Create an empty fstab '${LXC_ROOT_DIR}/${LXC_NAME}/fstab'
# -
msg="Create an empty file '${LXC_ROOT_DIR}/${LXC_NAME}/fstab' .."
length_msg=${#msg}
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
touch ${LXC_ROOT_DIR}/${LXC_NAME}/fstab > /dev/null 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="Empty file '${LXC_ROOT_DIR}/${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 (empty) file '${LXC_ROOT_DIR}/${LXC_NAME}/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
# - Backup original configuration file
# -
msg="Backup original Container configuration file '${LXC_ROOT_DIR}/${LXC_NAME}/config' .."
length_msg=${#msg}
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
cp ${LXC_ROOT_DIR}/${LXC_NAME}/config ${LXC_ROOT_DIR}/${LXC_NAME}/config.ORIG
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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 the Container configuration file
# -
msg="Create Container Configuration '${LXC_ROOT_DIR}/${LXC_NAME}/config' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
_failed=false
cat << EOF > ${LXC_ROOT_DIR}/${LXC_NAME}/config 2> $err_msg
lxc.rootfs = /var/lib/lxc/${LXC_NAME}/rootfs
# Common configuration
lxc.include = /usr/share/lxc/config/${LXC_DIST}.common.conf
# Container specific configuration
lxc.mount = /var/lib/lxc/${LXC_NAME}/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
#lxc.network.ipv6 = ${IPV6_ADDRESS_1}/$IPV6_PREFIX_1
#lxc.network.ipv6.gateway = $IPV6_GATEWAY_1
EOF
if [[ $? -ne 0 ]]; then
_failed=true
fi
if [[ -n "$IPV4_ADDRESS_2" ]]; then
cat << EOF >> ${LXC_ROOT_DIR}/${LXC_NAME}/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
#lxc.network.ipv6 = ${IPV6_ADDRESS_2}/$IPV6_PREFIX_2
EOF
if [[ $? -ne 0 ]]; then
_failed=true
fi
fi
cat << EOF >> ${LXC_ROOT_DIR}/${LXC_NAME}/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
EOF
if [[ $? -ne 0 ]]; then
_failed=true
fi
if $_failed ; then
msg_failed="Creation of '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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
# ---
# - Adjust network configuration
# ---
echo ""
echo ""
echo -e " \033[37m\033[1mAdjust network configuration inside the new LX Container ..\033[m"
echo ""
# - Backup file '/etc/network/interfaces' inside the Container
# -
msg="Backup file '${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/network/interfaces' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
cp ${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/network/interfaces \
${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/network/interfaces.ORIG
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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
# ---
# - 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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/apt/sources.list' .."
mv "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/apt/sources.list" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/apt/sources.list.ORIG" > $err_msg 2>&1
# - Backup 'sources.list'-file.
# -
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/apt/sources.list' .."
cat << EOF > ${LXC_ROOT_DIR}/${LXC_NAME}/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
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
# $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
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/bin/sh" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/bin/sh.ORIG" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="Symlink '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/bin/sh > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="New Symlink '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hosts > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="Entry '127.0.1.1 $FQ_HOSTNAME $HOSTNAME' added to '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hostname' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
mv "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hostname" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hostname.ORIG" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hostname' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
echo "$HOSTNAME" > "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/hostname" 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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
# - Backup Containers file '/etc/ssh/sshd_config'
# -
echo ""
msg="Backup file '${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/ssh/sshd_config' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
mv "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/ssh/sshd_config" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/etc/ssh/sshd_config.ORIG" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/etc/ssh/sshd_config" 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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[31m\033[1mskipped ] $msg"
fi
# - Backup Containers file '/root/.bashrc'
# -
echo ""
msg="Backup file '${LXC_ROOT_DIR}/${LXC_NAME}/rootfs//root/.bashrc' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
mv "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/.bashrc" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/.bashrc.ORIG" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/root/.bashrc" 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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[31m\033[1mskipped ] $msg"
fi
# - Backup Containers file '/root/.profile'
# -
msg="Backup file '${LXC_ROOT_DIR}/${LXC_NAME}/rootfs//root/.profile' .."
echo -en "\033[1G \033[1;30m[ ... ] \033[32m$msg\033[m"
mv "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/.profile" "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/.profile.ORIG" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="File '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/root/.profile" 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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[31m\033[1mskipped ] $msg"
fi
# - Create new containers file '/root/.vimrc'
# -
msg="Create a new file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/root/.vimrc" 2>$err_msg
if [[ $? -eq 0 ]] ; then
msg_ok="New file '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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[31m\033[1mskipped ] $msg"
fi
# - Create new containers directory '/root/.ssh'
# -
echo ""
msg="Create a new directory '${LXC_ROOT_DIR}/${LXC_NAME}/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
mkdir "${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/.ssh" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="New directory '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/rootfs/root/.ssh/' .."
cp -a "${working_dir}/supported-files/templates_root/.ssh" \
"${LXC_ROOT_DIR}/${LXC_NAME}/rootfs/root/" > $err_msg 2>&1
if [[ $? -eq 0 ]] ; then
msg_ok="Supported files copied to new directory '${LXC_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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_ROOT_DIR}/${LXC_NAME}/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
echo -e "\033[1G [ \033[31m\033[1mskipped ] $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_NAME=$LXC_NAME
LXC_DIST=$LXC_DIST
LXC_RELEASE=$LXC_REALEASE
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 $_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
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 "Directly access to the new container with command: \033[1;32mlxc-attach -n $LXC_NAME\033[m"
echo ""
clean_up 0