create-lx-container.sh: Add support for 'Ubuntu' guest system.

This commit is contained in:
Christoph 2021-04-27 16:50:53 +02:00
parent 1ad01fb5fe
commit 4beab46951

View File

@ -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,6 +413,7 @@ IPV4_PREFIX_1="$(netmask2cidr $IPV4_NETMASK_1)"
IPV6_PREFIX_1=$DEFAULT_IPV6_PREFIX
if [[ "$LXC_DIST" = "debian" ]]; then
echo ""
echo -e "\033[32m--\033[m"
echo ""
@ -500,7 +502,7 @@ if $_second_ipv4 ; then
echo -e "\033[32m--\033[m"
echo ""
echo " # Insert Netmask for the first IPv4 Address."
echo " # Insert Netmask for the second IPv4 Address."
echo " #"
echo " # Type <return> to accept the default one"
echo ""
@ -521,6 +523,52 @@ if $_second_ipv4 ; 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 <return> 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 ""
echo -e "\033[32m--\033[m"
echo ""
@ -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"
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,6 +899,7 @@ if [[ "$(dirname "$LXC_CONTAINER_DIR")" != "$LXC_ROOT_DIR" ]]; then
fi
if [[ "$LXC_DIST" = "debian" ]]; then
# - Create an empty fstab '${LXC_CONTAINER_DIR}/fstab'
# -
msg="Create an empty file '${LXC_CONTAINER_DIR}/fstab' .."
@ -867,6 +936,7 @@ else
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,6 +975,9 @@ else
error "$(cat $err_msg)"
fi
if [[ "$LXC_DIST" = "debian" ]]; then
# - Create the Container configuration file
# -
msg="Create Container Configuration '${LXC_CONTAINER_DIR}/config (LXC Version $LXC_VERSION)' .."
@ -986,8 +1059,6 @@ EOF
fi
else
cat << EOF > ${LXC_CONTAINER_DIR}/config 2> $err_msg
lxc.rootfs.path = ${LXC_CONTAINER_DIR}/rootfs
@ -1180,6 +1251,167 @@ else
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
cat <<EOF >> ${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,6 +1424,7 @@ 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
# -
@ -1322,6 +1555,101 @@ else
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,6 +1700,9 @@ fi
# - Create new 'sources.list'-file.
# -
msg="Create file '${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list' .."
if [[ "$LXC_DIST" = "debian" ]]; then
cat << EOF > ${LXC_CONTAINER_DIR}/rootfs/etc/apt/sources.list 2> $err_msg
deb http://ftp.de.debian.org/debian/ $LXC_RELEASE main non-free contrib
@ -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,6 +1955,7 @@ else
fi
if [[ "$LXC_DIST" = "debian" ]]; then
# - Install 'figlet' from debian package system
# -
@ -1725,8 +2073,11 @@ else
error "$(cat $err_msg)"
fi
fi # if [[ "$LXC_DIST" = "debian" ]]
if [[ "$LXC_DIST" = "debian" ]]; then
# - Backup Containers file '/etc/ssh/sshd_config'
# -
echo ""
@ -1806,6 +2157,8 @@ 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