-
Notifications
You must be signed in to change notification settings - Fork 0
/
prep-image
executable file
·111 lines (95 loc) · 3.89 KB
/
prep-image
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
#!/usr/bin/env bash
#
# Generates customized cloud images (highly opinionated). Supports the following releases:
# - Debian 11 (bullseye)
# - Debian 12 (bookworm)
#
# Performs the following operations on the image:
# - Sets the default locale to en_US.UTF8 and generates locales.
# Requires libguestfs-tools
readonly DEBIAN_BASE_URL="https://cloud.debian.org/images/cloud"
set -o errexit
set -o pipefail
set -o nounset
function die() {
echo "$*" 1>&2
exit -1
}
IMAGE_FILENAME=""
IMAGE_URL=""
CUSTOMIZED_FILENAME=""
DEFAULT_LOCALE="en_US.UTF-8"
DHCP_INTERFACE="${DHCP_INTERFACE:-ens18}"
#
# Extra commands to be run on the image.
#
declare -a EXTRA_COMMANDS
EXTRA_COMMANDS=()
# Set default locale to en_US.UTF-8 and generate locales
EXTRA_COMMANDS+=('echo "locales locales/default_environment_locale select '${DEFAULT_LOCALE}'" | sudo debconf-set-selections')
EXTRA_COMMANDS+=('echo "locales locales/locales_to_be_generated multiselect '${DEFAULT_LOCALE}' UTF-8" | debconf-set-selections')
EXTRA_COMMANDS+=('sed -i "s/^# '${DEFAULT_LOCALE}' UTF-8/'${DEFAULT_LOCALE}' UTF-8/" /etc/locale.gen')
EXTRA_COMMANDS+=('dpkg-reconfigure --frontend noninteractive locales')
# Limit dhcp on initial boot
EXTRA_COMMANDS+=('sed -i "s/^source-directory \/run\/network\/interfaces.d/# source-directory \/run\/network\/interfaces.d/" /etc/network/interfaces')
EXTRA_COMMANDS+=('echo "source /run/network/interfaces.d/'${DHCP_INTERFACE}'" >> /etc/network/interfaces')
printf "\n ** Cloud image preparation **\n\n"
###############################################################################
# Select a distro #
###############################################################################
PS3=$'\n'"Select a distro: "
select release in "Debian Bullseye" "Debian Bookworm" "Quit"; do
case $release in
"Debian Bullseye")
IMAGE_FILENAME="debian-11-generic-amd64.qcow2"
IMAGE_URL="${DEBIAN_BASE_URL}/bullseye/latest/${IMAGE_FILENAME}"
break
;;
"Debian Bookworm")
IMAGE_FILENAME="debian-12-generic-amd64.qcow2"
IMAGE_URL="${DEBIAN_BASE_URL}/bookworm/latest/${IMAGE_FILENAME}"
break
;;
"Quit")
exit -1
;;
*)
echo "Invalid selection"
;;
esac
done
printf "\nBuilding a custom cloud image for $release.\n\n"
CUSTOMIZED_FILENAME="$(basename ${IMAGE_FILENAME} .qcow2).customized.qcow2"
###############################################################################
# Download the base image if it does not exist
###############################################################################
if [[ ! -f "$IMAGE_FILENAME" ]]; then
printf "Image $IMAGE_FILENAME not available locally.\nDownloading from ${IMAGE_URL}...\n\n"
curl -L -o "$IMAGE_FILENAME" "$IMAGE_URL"
else
printf "Found existing image ${IMAGE_FILENAME}. Skipping download.\n"
fi
###############################################################################
# Prepare for customizations
###############################################################################
[[ -f "$CUSTOMIZED_FILENAME" ]] && rm "$CUSTOMIZED_FILENAME"
cp "$IMAGE_FILENAME" "$CUSTOMIZED_FILENAME"
###############################################################################
# Build the virt-customize args
###############################################################################
declare -a CUSTOMIZE_ARGS
CUSTOMIZE_ARGS=("-a" "$CUSTOMIZED_FILENAME")
CUSTOMIZE_ARGS+=("--install" "qemu-guest-agent")
CUSTOMIZE_ARGS+=("--install" "xfsprogs")
for cmd in "${EXTRA_COMMANDS[@]}"; do
CUSTOMIZE_ARGS+=("--run-command" "${cmd}")
done
CUSTOMIZE_ARGS+=("--truncate" "/etc/machine-id")
###############################################################################
# Execute!
###############################################################################
printf "\nPreparing custom image ${CUSTOMIZED_FILENAME}...\n\n"
(
set -x; virt-customize "${CUSTOMIZE_ARGS[@]}"
)
printf "\nCustomized image is available: $CUSTOMIZED_FILENAME\n"