-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathrun.sh
More file actions
executable file
·250 lines (198 loc) · 6.9 KB
/
run.sh
File metadata and controls
executable file
·250 lines (198 loc) · 6.9 KB
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
#!/bin/bash
#Defaults
DISTRO_HOSTNAME=milkvduo
ROOTPW=milkv
POSTBUILD="Distro/post_build.sh"
# Check if whiptail is installed
if ! command -v whiptail &> /dev/null; then
echo "whiptail is not installed."
# Check if apt is available
if command -v apt &> /dev/null; then
echo "Attempting to install whiptail using apt..."
sudo apt update && sudo apt install -y whiptail
# Verify if whiptail installed successfully
if command -v whiptail &> /dev/null; then
echo "whiptail installed successfully."
else
echo "Failed to install whiptail."
exit 1
fi
else
echo "apt is not available on this system. Cannot install whiptail automatically."
exit 1
fi
fi
PBDEBUG=false
# Check if any arguments
for arg in "$@"; do
if [ "$arg" = "bash" ]; then
BASHARG="bash"
fi
if [ "$arg" = "debug" ]; then
PBDEBUG=true
fi
done
# Check if docker command exists
if ! command -v docker &> /dev/null; then
echo "Error: docker command not found." >&2
exit 1
fi
IMAGE_PREFIX="duosdk"
if docker ps --format '{{.Image}}' | grep -q "^${IMAGE_PREFIX}"; then
echo "Error: docker image in use." >&2
exit 1
fi
DOBUILD="true"
function start() {
set -e
[ "$DOBUILD" = "true" ] && CACHE="--no-cache"
mkdir -p images
docker build . -t ${IMAGE} --network=host ${CACHE} --build-arg CACHE_BUST=$(date +%s) \
--build-arg DISTRO_HOSTNAME=${DISTRO_HOSTNAME} \
--build-arg ROOTPW=${ROOTPW} \
--build-arg BUSYBOX_CONF=${BUSYBOX_CONF} \
--build-arg POSTBUILD=${POSTBUILD} \
--target ${IMAGE}
docker run --rm -it --net=host --privileged \
-v ./images:/duo-buildroot-sdk/out \
-e DISTRO_HOSTNAME="${DISTRO_HOSTNAME}" -e ROOTPW="${ROOTPW}" \
-e PBDEBUG="${PBDEBUG}" \
--name "duosdk-builder" \
${IMAGE} $BASHARG $DEVCHOICE
}
check_docker_image() {
local creation_date=""
local image_info=""
# Check if the image exists locally
if docker images --format '{{.Repository}}' | grep -q "^${IMAGE}$"; then
# Get the creation date in YYYY-MM-DD HH:MM:SS format
creation_date=$(docker inspect --format '{{.Created}}' "${IMAGE}" |
cut -d'T' -f1)
image_info="${IMAGE} ${creation_date}"
# Present menu using whiptail
choice=$(whiptail --title "Docker Image Selection" \
--menu "Choose an option" 15 60 2 \
"1" "Use existing SDK: ${image_info}" \
"2" "Force Rebuild" \
3>&1 1>&2 2>&3)
[ "$choice" = "1" ] && DOBUILD="false"
fi
}
function select_distro() {
# Array to store matching files
declare -a tar_files=()
declare -a conf_dirs=()
# Search current directory and subdirectories with absolute paths
while IFS= read -r -d '' file; do
# Convert to absolute path using realpath
abs_file=$(realpath "$file")
tar_files+=("$abs_file")
done < <(find "$(pwd)" -type f -name "*-$ARCH*-*.tar.gz" -print0)
# Check and search ../CrocNetDistro if it exists
if [ -d "../CrocNetDistro" ]; then
while IFS= read -r -d '' file; do
# Convert to absolute path using realpath
abs_file=$(realpath "$file")
tar_files+=("$abs_file")
done < <(find "../CrocNetDistro" -name "rootfs" -type d -prune -o -type f -name "*-$ARCH*-*.tar.gz" -print0)
fi
# --- Search for directories containing busybox.conf in current directory ---
echo "Searching for directories containing busybox.conf in $(pwd)..."
for dir in */; do
# Check if it's actually a directory and contains the file
# Make sure dir doesn't literally match "*/" if no dirs are found
if [ -d "$dir" ] && [ -f "${dir}busybox.conf" ]; then
# Store the relative path (e.g., ./mydir/ or mydir/)
conf_dirs+=("$dir")
fi
done
# Prepare menu options (showing only filenames)
# menu_options=("Busybox BASH" "")
for file in "${tar_files[@]}"; do
filename=$(basename "$file")
menu_options+=("$filename" "")
done
# Add found configuration directories (showing directory names)
for dir in "${conf_dirs[@]}"; do
# Remove trailing slash for cleaner display if present
dir_name=$(basename "$dir")
menu_options+=("$dir_name" "")
done
# Show whiptail menu and get selection
selected=$(whiptail --title "Select Distribution" --menu "Choose build:" 15 60 7 "${menu_options[@]}" 3>&1 1>&2 2>&3)
# If user cancelled
if [ $? -ne 0 ]; then
exit 0
fi
# If Default Busybox was selected, return without setting ROOTFS_TAR
if [[ ! "$selected" = *.gz ]]; then
POSTBUILD="$selected/post_build.sh"
BUSYBOX_CONF="$selected/busybox.conf"
return 0
fi
# Find the full path of the selected file
for file in "${tar_files[@]}"; do
if [ "$(basename "$file")" = "$selected" ]; then
# Ensure ROOTFS_TAR is an absolute path
ROOTFS_TAR=$(realpath "$file")
return 0
fi
done
echo "Failed to find $selected"
exit 1
}
CHOICE=$(whiptail --title "Architecture Selection" --menu "Choose an architecture:" 15 60 2 \
"1" "ARM64" \
"2" "RISC-V" \
3>&1 1>&2 2>&3)
# Check the exit status of whiptail (0 means OK, 1 means Cancel)
if [ $? -eq 0 ]; then
# If-else block based on the user's selection
if [ "$CHOICE" = "1" ]; then
ARCH="arm64"
elif [ "$CHOICE" = "2" ]; then
ARCH="riscv64"
else
exit 0
fi
fi
[ -z "$ARCH" ] && exit 0
[ -z "$ROOTFS_TAR" ] && select_distro
if [ -n "$ROOTFS_TAR" ]; then
# Check if ROOTFS_TAR is set and points to an existing file
if [ ! -e "$ROOTFS_TAR" ]; then
echo "Error: ROOTFS_TAR is not set or does not exist"
exit 1
fi
rm -f ./rootfs.tar.gz
cp "$ROOTFS_TAR" ./rootfs.tar.gz
IMAGE=duosdk-${ARCH}
else
IMAGE=duosdk-busybox-${ARCH}
fi
check_docker_image
# Prompt the user, pre-filling with the current value
NEW_HOSTNAME=$(whiptail --title "Hostname Setup" \
--inputbox "Enter the desired hostname:" 10 60 \
"$DISTRO_HOSTNAME" \
3>&1 1>&2 2>&3) # Capture stderr to stdout
# Check exit status ($?): 0 means OK, non-zero means Cancel/Esc
exitstatus=$?
if [ $exitstatus = 0 ]; then
DISTRO_HOSTNAME="$NEW_HOSTNAME" # Update variable only if OK was pressed
echo "Hostname set to: $DISTRO_HOSTNAME"
else
exit 1
fi
NEW_ROOTPW=$(whiptail --title "Root Password Setup" \
--inputbox "Enter the root password:" 10 60 \
"$ROOTPW" \
3>&1 1>&2 2>&3) # Capture stderr to stdout
exitstatus=$?
if [ $exitstatus = 0 ]; then
ROOTPW="$NEW_ROOTPW"
echo "Root password has been set/updated."
else
exit 1
fi
start