Skip to content

Commit 315949b

Browse files
MickLeskhavardthom
andauthored
Massive Update: build.func | install.func | create_lxc.sh (Part 1) (#643)
* Big Update: Build.func Install.Func Create_LXC * Change Author Co-authored-by: Håvard Gjøby Thom <[email protected]> * Fix indentation / naming / echos * Fix some Parts * Add alpine-install.func * update alpine to 3.20 * fix spaces * Update build.func * Merge Create_LXC from DEV * Merge from DEV * Merge from DEV --------- Co-authored-by: Håvard Gjøby Thom <[email protected]>
1 parent bcc6342 commit 315949b

File tree

5 files changed

+435
-173
lines changed

5 files changed

+435
-173
lines changed

ct/alpine.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ var_disk="0.1"
2424
var_cpu="1"
2525
var_ram="512"
2626
var_os="alpine"
27-
var_version="3.19"
27+
var_version="3.20"
2828
variables
2929
color
3030
catch_errors

ct/create_lxc.sh

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,33 @@
22

33
# Copyright (c) 2021-2024 tteck
44
# Author: tteck (tteckster)
5+
# Co-Author: MickLesk
56
# License: MIT
67
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
78

89
# This sets verbose mode if the global variable is set to "yes"
910
# if [ "$VERBOSE" == "yes" ]; then set -x; fi
1011

1112
# This function sets color variables for formatting output in the terminal
13+
# Colors
1214
YW=$(echo "\033[33m")
15+
YWB=$(echo "\033[93m")
1316
BL=$(echo "\033[36m")
1417
RD=$(echo "\033[01;31m")
1518
GN=$(echo "\033[1;92m")
19+
20+
# Formatting
1621
CL=$(echo "\033[m")
17-
CM="${GN}${CL}"
18-
CROSS="${RD}${CL}"
22+
UL=$(echo "\033[4m")
23+
BOLD=$(echo "\033[1m")
1924
BFR="\\r\\033[K"
2025
HOLD=" "
26+
TAB=" "
27+
28+
# Icons
29+
CM="${TAB}✔️${TAB}${CL}"
30+
CROSS="${TAB}✖️${TAB}${CL}"
31+
INFO="${TAB}💡${TAB}${CL}"
2132

2233
# This sets error handling options and defines the error_handler function to handle errors
2334
set -Eeuo pipefail
@@ -36,19 +47,24 @@ function error_handler() {
3647

3748
# This function displays a spinner.
3849
function spinner() {
39-
local chars="/-\|"
40-
local spin_i=0
41-
printf "\e[?25l"
42-
while true; do
43-
printf "\r \e[36m%s\e[0m" "${chars:spin_i++%${#chars}:1}"
44-
sleep 0.1
45-
done
50+
local frames=('' '' '' '' '' '' '' '' '' '')
51+
local spin_i=0
52+
local interval=0.1
53+
printf "\e[?25l"
54+
55+
local color="${YWB}"
56+
57+
while true; do
58+
printf "\r ${color}%s${CL}" "${frames[spin_i]}"
59+
spin_i=$(( (spin_i + 1) % ${#frames[@]} ))
60+
sleep "$interval"
61+
done
4662
}
4763

4864
# This function displays an informational message with a yellow color.
4965
function msg_info() {
5066
local msg="$1"
51-
echo -ne " ${HOLD} ${YW}${msg} "
67+
echo -ne "${TAB}${YW}${HOLD}${msg}${HOLD}"
5268
spinner &
5369
SPINNER_PID=$!
5470
}
@@ -58,15 +74,15 @@ function msg_ok() {
5874
if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
5975
printf "\e[?25h"
6076
local msg="$1"
61-
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
77+
echo -e "${BFR}${CM}${GN}${msg}${CL}"
6278
}
6379

6480
# This function displays a error message with a red color.
6581
function msg_error() {
6682
if [ -n "$SPINNER_PID" ] && ps -p $SPINNER_PID > /dev/null; then kill $SPINNER_PID > /dev/null; fi
6783
printf "\e[?25h"
6884
local msg="$1"
69-
echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
85+
echo -e "${BFR}${CROSS}${RD}${msg}${CL}"
7086
}
7187

7288
# This checks for the presence of valid Container Storage and Template Storage locations
@@ -105,7 +121,7 @@ function select_storage() {
105121
local TAG=$(echo $line | awk '{print $1}')
106122
local TYPE=$(echo $line | awk '{printf "%-10s", $2}')
107123
local FREE=$(echo $line | numfmt --field 4-6 --from-unit=K --to=iec --format %.2f | awk '{printf( "%9sB", $6)}')
108-
local ITEM=" Type: $TYPE Free: $FREE "
124+
local ITEM="Type: $TYPE Free: $FREE "
109125
local OFFSET=2
110126
if [[ $((${#ITEM} + $OFFSET)) -gt ${MSG_MAX_LENGTH:-} ]]; then
111127
local MSG_MAX_LENGTH=$((${#ITEM} + $OFFSET))
@@ -123,11 +139,14 @@ function select_storage() {
123139
"Which storage pool you would like to use for the ${CONTENT_LABEL,,}?\nTo make a selection, use the Spacebar.\n" \
124140
16 $(($MSG_MAX_LENGTH + 23)) 6 \
125141
"${MENU[@]}" 3>&1 1>&2 2>&3) || exit "Menu aborted."
142+
if [ $? -ne 0 ]; then
143+
echo -e "${CROSS}${RD} Menu aborted by user.${CL}"
144+
exit 0
145+
fi
126146
done
127-
printf $STORAGE
147+
printf "%s" "$STORAGE"
128148
fi
129149
}
130-
131150
# Test if required variables are set
132151
[[ "${CTID:-}" ]] || exit "You need to set 'CTID' variable."
133152
[[ "${PCT_OSTYPE:-}" ]] || exit "You need to set 'PCT_OSTYPE' variable."

misc/alpine-install.func

Lines changed: 73 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,42 @@
1+
# Copyright (c) 2021-2024 tteck
2+
# Author: tteck (tteckster)
3+
# Co-Author: MickLesk
4+
# License: MIT
5+
# https://github.com/community-scripts/ProxmoxVE/raw/main/LICENSE
6+
7+
# This function sets color variables for formatting output in the terminal
18
color() {
9+
# Colors
210
YW=$(echo "\033[33m")
11+
YWB=$(echo "\033[93m")
312
BL=$(echo "\033[36m")
413
RD=$(echo "\033[01;31m")
5-
BGN=$(echo "\033[4;92m")
614
GN=$(echo "\033[1;92m")
7-
DGN=$(echo "\033[32m")
15+
16+
# Formatting
817
CL=$(echo "\033[m")
18+
BFR="\\r\\033[K"
19+
BOLD=$(echo "\033[1m")
20+
TAB=" "
21+
22+
# System
923
RETRY_NUM=10
1024
RETRY_EVERY=3
1125
i=$RETRY_NUM
12-
CM="${GN}✓${CL}"
13-
CROSS="${RD}✗${CL}"
14-
BFR="\\r\\033[K"
15-
HOLD="-"
26+
27+
# Icons
28+
CM="${TAB}✔️${TAB}${CL}"
29+
CROSS="${TAB}✖️${TAB}${CL}"
30+
INFO="${TAB}💡${TAB}${CL}"
31+
NETWORK="${TAB}📡${TAB}${CL}"
32+
OS="${TAB}🖥️${TAB}${CL}"
33+
OSVERSION="${TAB}🌟${TAB}${CL}"
34+
HOSTNAME="${TAB}🏠${TAB}${CL}"
35+
GATEWAY="${TAB}🌐${TAB}${CL}"
36+
DEFAULT="${TAB}⚙️${TAB}${CL}"
1637
}
1738

39+
# This function enables IPv6 if it's not disabled and sets verbose mode if the global variable is set to "yes"
1840
verb_ip6() {
1941
if [ "$VERBOSE" = "yes" ]; then
2042
STD=""
@@ -27,11 +49,13 @@ verb_ip6() {
2749
fi
2850
}
2951

52+
# This function catches errors and handles them with the error handler function
3053
catch_errors() {
3154
set -Eeuo pipefail
3255
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
3356
}
3457

58+
# This function handles errors
3559
error_handler() {
3660
local exit_code="$?"
3761
local line_number="$1"
@@ -40,21 +64,25 @@ error_handler() {
4064
echo -e "\n$error_message\n"
4165
}
4266

67+
# This function displays an informational message with a yellow color.
4368
msg_info() {
4469
local msg="$1"
45-
echo -ne " ${HOLD} ${YW}${msg}..."
70+
echo -ne " ${TAB}${YW}${msg}"
4671
}
4772

73+
# This function displays a success message with a green color.
4874
msg_ok() {
4975
local msg="$1"
50-
echo -e "${BFR} ${CM} ${GN}${msg}${CL}"
76+
echo -e "${BFR}${CM}${GN}${msg}${CL}"
5177
}
5278

79+
# This function displays a error message with a red color.
5380
msg_error() {
5481
local msg="$1"
55-
echo -e "${BFR} ${CROSS} ${RD}${msg}${CL}"
82+
echo -e "${BFR}${CROSS}${RD}${msg}${CL}"
5683
}
5784

85+
# This function sets up the Container OS by generating the locale, setting the timezone, and checking the network connection
5886
setting_up_container() {
5987
msg_info "Setting up Container OS"
6088
while [ $i -gt 0 ]; do
@@ -68,23 +96,26 @@ setting_up_container() {
6896

6997
if [ "$(ip addr show | grep 'inet ' | grep -v '127.0.0.1' | awk '{print $2}' | cut -d'/' -f1)" = "" ]; then
7098
echo 1>&2 -e "\n${CROSS}${RD} No Network After $RETRY_NUM Tries${CL}"
71-
echo -e " 🖧 Check Network Settings"
99+
echo -e "${NETWORK}Check Network Settings"
72100
exit 1
73101
fi
74102
msg_ok "Set up Container OS"
75103
msg_ok "Network Connected: ${BL}$(ip addr show | grep 'inet ' | awk '{print $2}' | cut -d'/' -f1 | tail -n1)${CL}"
76104
}
77105

106+
# This function checks the network connection by pinging a known IP address and prompts the user to continue if the internet is not connected
78107
network_check() {
79108
set +e
80109
trap - ERR
81-
if ping -c 1 -W 1 1.1.1.1 &>/dev/null; then msg_ok "Internet Connected"; else
110+
if ping -c 1 -W 1 1.1.1.1 &>/dev/null || ping -c 1 -W 1 8.8.8.8 &>/dev/null || ping -c 1 -W 1 9.9.9.9 &>/dev/null; then
111+
msg_ok "Internet Connected";
112+
else
82113
msg_error "Internet NOT Connected"
83114
read -r -p "Would you like to continue anyway? <y/N> " prompt
84115
if [[ "${prompt,,}" =~ ^(y|yes)$ ]]; then
85-
echo -e " ⚠️ ${RD}Expect Issues Without Internet${CL}"
116+
echo -e "${INFO}${RD}Expect Issues Without Internet${CL}"
86117
else
87-
echo -e " 🖧 Check Network Settings"
118+
echo -e "${NETWORK}Check Network Settings"
88119
exit 1
89120
fi
90121
fi
@@ -94,27 +125,54 @@ network_check() {
94125
trap 'error_handler $LINENO "$BASH_COMMAND"' ERR
95126
}
96127

128+
# This function updates the Container OS by running apt-get update and upgrade
97129
update_os() {
98130
msg_info "Updating Container OS"
99131
$STD apk update
100132
$STD apk upgrade
101133
msg_ok "Updated Container OS"
102134
}
103135

136+
# This function modifies the message of the day (motd) and SSH settings
104137
motd_ssh() {
138+
# Set terminal to 256-color mode
105139
echo "export TERM='xterm-256color'" >>/root/.bashrc
106-
echo -e "$APPLICATION LXC provided by https://Helper-Scripts.com/\n" >/etc/motd
140+
IP=$(ip -4 addr show eth0 | awk '/inet / {print $2}' | cut -d/ -f1 | head -n 1)
141+
# Get OS information
142+
if [ -f "/etc/os-release" ]; then
143+
OS_NAME=$(grep ^NAME /etc/os-release | cut -d= -f2 | tr -d '"')
144+
OS_VERSION=$(grep ^VERSION_ID /etc/os-release | cut -d= -f2 | tr -d '"')
145+
else
146+
OS_NAME="Alpine Linux"
147+
OS_VERSION="Unknown"
148+
fi
149+
# Set MOTD with application info and system details
150+
MOTD_FILE="/etc/motd"
151+
if [ -f "$MOTD_FILE" ]; then
152+
echo -e "\n${BOLD}${APPLICATION} LXC Container${CL}" > "$MOTD_FILE"
153+
echo -e "${TAB}${GATEWAY}${YW} Provided by: ${GN}community-scripts ORG ${YW}| Project: ${GN}ProxmoxVE ${YW}| GitHub: ${GN}https://github.com/community-scripts/ProxmoxVE${CL}\n" >> "$MOTD_FILE"
154+
echo -e "${TAB}${OS}${YW} OS: ${GN}${OS_NAME} ${OS_VERSION}${CL}" >> "$MOTD_FILE"
155+
echo -e "${TAB}${HOSTNAME}${YW} Hostname: ${GN}$(hostname)${CL}" >> "$MOTD_FILE"
156+
echo -e "${TAB}${INFO}${YW} IP Address: ${GN}${IP}${CL}" >> "$MOTD_FILE"
157+
else
158+
echo -e "${RD}[WARNING] MOTD file does not exist!${CL}" >&2
159+
fi
160+
# Configure SSH if enabled
107161
if [[ "${SSH_ROOT}" == "yes" ]]; then
162+
# Enable sshd service
108163
$STD rc-update add sshd
164+
# Allow root login via SSH
109165
sed -i "s/#PermitRootLogin prohibit-password/PermitRootLogin yes/g" /etc/ssh/sshd_config
166+
# Start the sshd service
110167
$STD /etc/init.d/sshd start
111168
fi
112169
}
113170

171+
# This function customizes the container and enables passwordless login for the root user
114172
customize() {
115173
if [[ "$PASSWORD" == "" ]]; then
116174
msg_info "Customizing Container"
117175
bash -c "passwd -d root" >/dev/null 2>&1
118176
msg_ok "Customized Container"
119177
fi
120-
}
178+
}

0 commit comments

Comments
 (0)