-
Notifications
You must be signed in to change notification settings - Fork 49
/
install.sh
352 lines (300 loc) · 9.82 KB
/
install.sh
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
#!/bin/bash
# Define color codes
declare -A colors=(
["PURPLE"]='\033[0;35m'
["RED"]='\033[0;31m'
["GREEN"]='\033[0;32m'
["YELLOW"]='\033[0;33m'
["NC"]='\033[0m'
)
# Configuration variables
readonly ROOTFS_DIR="/home/container"
readonly BASE_URL="https://images.linuxcontainers.org/images"
# Add to PATH
export PATH=$PATH:~/.local/usr/bin
# Define all available distributions
declare distributions=(
[1]="Debian"
[2]="Ubuntu"
[3]="Void Linux:true"
[4]="Alpine Linux"
[5]="CentOS"
[6]="Rocky Linux"
[7]="Fedora"
[8]="AlmaLinux"
[9]="Slackware Linux"
[10]="Kali Linux"
[11]="openSUSE"
[12]="Gentoo Linux:true"
[13]="Arch Linux"
[14]="Devuan Linux"
[15]="Chimera Linux:custom"
[16]="Oracle Linux"
[17]="Amazon Linux"
[18]="Plamo Linux"
[19]="Linux Mint"
[20]="Alt Linux"
[21]="openEuler"
)
# Get the length of the distributions array
num_distros=${#distributions[@]}
# Error handling function
error_exit() {
printf "${colors[RED]}Error: $1${colors[NC]}\n" >&2
exit 1
}
# Logger function
log() {
local level=$1
local message=$2
local color=${colors[$3]}
if [ -z "$color" ]; then
color=${colors[NC]}
fi
printf "${color}[$level]${colors[NC]} $message\n"
}
# Detect the machine architecture.
ARCH=$(uname -m)
# Detect architecture
detect_architecture() {
case "$ARCH" in
x86_64)
echo "amd64"
;;
aarch64)
echo "arm64"
;;
riscv64)
echo "riscv64"
;;
*)
error_exit "Unsupported CPU architecture: $ARCH"
;;
esac
}
# Verify network connectivity
check_network() {
if ! curl -s --head "$BASE_URL" >/dev/null; then
error_exit "Unable to connect to $BASE_URL. Please check your internet connection."
fi
}
# Function to cleanup temporary files
cleanup() {
log "INFO" "Cleaning up temporary files..." "YELLOW"
rm -f "$ROOTFS_DIR/rootfs.tar.xz"
rm -rf /tmp/sbin
}
# Function to install a specific distro
install() {
local distro_name="$1"
local pretty_name="$2"
local is_custom="${3:-false}"
log "INFO" "Preparing to install $pretty_name..." "GREEN"
local url_path
local image_names
if [[ "$is_custom" == "true" ]]; then
url_path="$BASE_URL/$distro_name/current/$ARCH_ALT/"
else
url_path="$BASE_URL/$distro_name/"
fi
# Fetch available versions with error handling
image_names=$(curl -s "$url_path" | grep 'href="' | grep -o '"[^/"]*/"' | tr -d '"/' | grep -v '^\.\.$') ||
error_exit "Failed to fetch available versions for $pretty_name"
# Display available versions
local -a versions=($image_names)
for i in "${!versions[@]}"; do
printf "* [%d] %s (%s)\n" $((i + 1)) "$pretty_name" "${versions[i]}"
done
# Version selection with validation
local version
while true; do
printf "${colors[YELLOW]}Enter the desired version (1-${#versions[@]}): ${colors[NC]}\n"
read -r version
if [[ "$version" =~ ^[0-9]+$ ]] && ((version >= 1 && version <= ${#versions[@]})); then
break
fi
log "ERROR" "Invalid selection. Please try again." "RED"
done
local selected_version=${versions[$((version - 1))]}
log "INFO" "Selected version: $selected_version" "GREEN"
# Download and extract rootfs
download_and_extract_rootfs "$distro_name" "$selected_version" "$is_custom"
}
# Function to install custom distribution from URL
install_custom() {
local pretty_name="$1"
local url="$2"
log "INFO" "Installing $pretty_name..." "GREEN"
mkdir -p "$ROOTFS_DIR"
local file_name
file_name=$(basename "${url}")
if ! curl -Ls "${url}" -o "$ROOTFS_DIR/$file_name"; then
error_exit "Failed to download $pretty_name rootfs"
fi
if ! tar -xf "$ROOTFS_DIR/$file_name" -C "$ROOTFS_DIR"; then
error_exit "Failed to extract $pretty_name rootfs"
fi
mkdir -p "$ROOTFS_DIR/home/container/"
# Cleanup downloaded archive
rm -f "$ROOTFS_DIR/$file_name"
}
# Function to get Chimera Linux URL
get_chimera_linux() {
local base_url="https://repo.chimera-linux.org/live/latest/"
local latest_file
latest_file=$(curl -s "$base_url" | grep -o "chimera-linux-$ARCH-ROOTFS-[0-9]\{8\}-bootstrap\.tar\.gz" | sort -V | tail -n 1) ||
error_exit "Failed to fetch Chimera Linux version"
if [ -n "$latest_file" ]; then
local date
date=$(echo "$latest_file" | grep -o '[0-9]\{8\}')
echo "${base_url}chimera-linux-$ARCH-ROOTFS-$date-bootstrap.tar.gz"
else
error_exit "No suitable Chimera Linux version found"
fi
}
# Function to download and extract rootfs
download_and_extract_rootfs() {
local distro_name="$1"
local version="$2"
local is_custom="$3"
local arch_url
local url
if [[ "$is_custom" == "true" ]]; then
arch_url="${BASE_URL}/${distro_name}/current/"
url="${BASE_URL}/${distro_name}/current/${ARCH_ALT}/${version}/"
else
arch_url="${BASE_URL}/${distro_name}/${version}/"
url="${BASE_URL}/${distro_name}/${version}/${ARCH_ALT}/default/"
fi
# Check if the distro support $ARCH_ALT
if ! curl -s "$arch_url" | grep -q "$ARCH_ALT"; then
error_exit "This distro doesn't support $ARCH_ALT. Exiting...."
cleanup
exit 1
fi
# Get latest version
local latest_version
latest_version=$(curl -s "$url" | grep 'href="' | grep -o '"[^/"]*/"' | tr -d '"' | sort -r | head -n 1) ||
error_exit "Failed to determine latest version"
log "INFO" "Downloading rootfs..." "GREEN"
mkdir -p "$ROOTFS_DIR"
if ! curl -Ls "${url}${latest_version}/rootfs.tar.xz" -o "$ROOTFS_DIR/rootfs.tar.xz"; then
error_exit "Failed to download rootfs"
fi
log "INFO" "Extracting rootfs..." "GREEN"
if ! tar -xf "$ROOTFS_DIR/rootfs.tar.xz" -C "$ROOTFS_DIR"; then
error_exit "Failed to extract rootfs"
fi
mkdir -p "$ROOTFS_DIR/home/container/"
}
# Function to handle post-install configuration for specific distros
post_install_config() {
local distro="$1"
case "$distro" in
"archlinux")
log "INFO" "Configuring Arch Linux specific settings..." "GREEN"
sed -i '/^#RootDir/s/^#//' "$ROOTFS_DIR/etc/pacman.conf"
sed -i 's|/var/lib/pacman/|/var/lib/pacman|' "$ROOTFS_DIR/etc/pacman.conf"
sed -i '/^#DBPath/s/^#//' "$ROOTFS_DIR/etc/pacman.conf"
;;
esac
}
# Main menu display
display_menu() {
printf "\033c"
printf "${colors[GREEN]}┏━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┓${colors[NC]}\n"
printf "${colors[GREEN]}┃ ┃${colors[NC]}\n"
printf "${colors[GREEN]}┃ ${colors[PURPLE]} Pterodactyl VPS EGG ${colors[GREEN]} ┃${colors[NC]}\n"
printf "${colors[GREEN]}┃ ┃${colors[NC]}\n"
printf "${colors[GREEN]}┃ ${colors[RED]}© 2021 - $(date +%Y) ${colors[PURPLE]}@ysdragon${colors[GREEN]} ┃${colors[NC]}\n"
printf "${colors[GREEN]}┃ ┃${colors[NC]}\n"
printf "${colors[GREEN]}┗━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━┛${colors[NC]}\n"
printf "\n${colors[YELLOW]}Please choose your favorite distro:${colors[NC]}\n\n"
# Display all distributions
for i in "${!distributions[@]}"; do
printf "* [%d] %s\n" "$i" "${distributions[$i]%%:*}"
done
printf "\n${colors[YELLOW]}Enter the desired distro (1-${num_distros}): ${colors[NC]}\n"
}
# Initial setup
ARCH_ALT=$(detect_architecture)
check_network
# Display menu and get selection
display_menu
# Handle user selection and installation
read -rp "" selection
case "$selection" in
1)
install "debian" "Debian"
;;
2)
install "ubuntu" "Ubuntu"
;;
3)
install "voidlinux" "Void Linux" "true"
;;
4)
install "alpine" "Alpine Linux"
;;
5)
install "centos" "CentOS"
;;
6)
install "rockylinux" "Rocky Linux"
;;
7)
install "fedora" "Fedora"
;;
8)
install "almalinux" "Alma Linux"
;;
9)
install "slackware" "Slackware"
;;
10)
install "kali" "Kali Linux"
;;
11)
install "opensuse" "openSUSE"
;;
12)
install "gentoo" "Gentoo Linux" "true"
;;
13)
install "archlinux" "Arch Linux"
post_install_config "archlinux"
;;
14)
install "devuan" "Devuan Linux"
;;
15)
chimera_url=$(get_chimera_linux)
install_custom "Chimera Linux" "$chimera_url"
;;
16)
install "oracle" "Oracle Linux"
;;
17)
install "amazonlinux" "Amazon Linux"
;;
18)
install "plamo" "Plamo Linux"
;;
19)
install "mint" "Linux Mint"
;;
20)
install "alt" "Alt Linux"
;;
21)
install "openeuler" "openEuler"
;;
*)
error_exit "Invalid selection (1-${num_distros})"
;;
esac
# Copy run.sh script to ROOTFS_DIR and make it executable
cp /run.sh "$ROOTFS_DIR/run.sh"
chmod +x "$ROOTFS_DIR/run.sh"
# Trap for cleanup on script exit
trap cleanup EXIT