Skip to content

Commit 1c7bab3

Browse files
authored
[core]: Enhance LXC template handling and improve error recovery (#2128)
1 parent b0cdb30 commit 1c7bab3

File tree

1 file changed

+25
-12
lines changed

1 file changed

+25
-12
lines changed

ct/create_lxc.sh

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -182,24 +182,37 @@ mapfile -t TEMPLATES < <(pveam available -section system | sed -n "s/.*\($TEMPLA
182182
[ ${#TEMPLATES[@]} -gt 0 ] || { msg_error "Unable to find a template when searching for '$TEMPLATE_SEARCH'."; exit 207; }
183183
TEMPLATE="${TEMPLATES[-1]}"
184184

185-
# Download LXC template if needed
186-
if ! pveam list $TEMPLATE_STORAGE | grep -q $TEMPLATE; then
185+
TEMPLATE_PATH="/var/lib/vz/template/cache/$TEMPLATE"
186+
# Check if template exists, if corrupt remove and redownload
187+
if ! pveam list "$TEMPLATE_STORAGE" | grep -q "$TEMPLATE"; then
188+
[[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH"
187189
msg_info "Downloading LXC Template"
188-
pveam download $TEMPLATE_STORAGE $TEMPLATE >/dev/null ||
189-
msg_error "A problem occured while downloading the LXC template."
190-
exit 208
190+
pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null ||
191+
{ msg_error "A problem occurred while downloading the LXC template."; exit 208; }
191192
msg_ok "Downloaded LXC Template"
192193
fi
193194

194-
# Combine all options
195-
DEFAULT_PCT_OPTIONS=(
196-
-arch $(dpkg --print-architecture))
195+
# Check and fix subuid/subgid
196+
grep -q "root:100000:65536" /etc/subuid || echo "root:100000:65536" >> /etc/subuid
197+
grep -q "root:100000:65536" /etc/subgid || echo "root:100000:65536" >> /etc/subgid
197198

199+
# Combine all options
198200
PCT_OPTIONS=(${PCT_OPTIONS[@]:-${DEFAULT_PCT_OPTIONS[@]}})
199-
[[ " ${PCT_OPTIONS[@]} " =~ " -rootfs " ]] || PCT_OPTIONS+=(-rootfs $CONTAINER_STORAGE:${PCT_DISK_SIZE:-8})
201+
[[ " ${PCT_OPTIONS[@]} " =~ " -rootfs " ]] || PCT_OPTIONS+=(-rootfs "$CONTAINER_STORAGE:${PCT_DISK_SIZE:-8}")
200202

201-
# Create container
203+
# Create container with template integrity check
202204
msg_info "Creating LXC Container"
203-
pct create $CTID ${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE} ${PCT_OPTIONS[@]} >/dev/null || { msg_error "A problem occured while trying to create container."; exit 200; }
205+
if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then
206+
[[ -f "$TEMPLATE_PATH" ]] && rm -f "$TEMPLATE_PATH"
207+
208+
msg_ok "Template integrity check completed"
209+
pveam download "$TEMPLATE_STORAGE" "$TEMPLATE" >/dev/null ||
210+
{ msg_error "A problem occurred while re-downloading the LXC template."; exit 208; }
211+
212+
msg_ok "Re-downloaded LXC Template"
213+
if ! pct create "$CTID" "${TEMPLATE_STORAGE}:vztmpl/${TEMPLATE}" "${PCT_OPTIONS[@]}" &>/dev/null; then
214+
msg_error "A problem occurred while trying to create container after re-downloading template."
215+
exit 200
216+
fi
217+
fi
204218
msg_ok "LXC Container ${BL}$CTID${CL} ${GN}was successfully created."
205-

0 commit comments

Comments
 (0)