-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnspawn-builder
More file actions
executable file
·237 lines (213 loc) · 6.39 KB
/
nspawn-builder
File metadata and controls
executable file
·237 lines (213 loc) · 6.39 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
#!/usr/bin/env bash
#
# nspawn-builder - nspawn-builder is a wrapper around mkosi that helps
# to generate images dynamically.
#
# Copyright (c) 2021 by Eduard Tolosa <edu4rdshl@protonmail.com>
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http: #www.gnu.org/licenses/
#
#======================================================================
# Author: Eduard Tolosa
# Email: edu4rdshl@protonmail.com
# Github: www.github.com/edu4rdshl
VERSION="0.3"
STATIC_NAME="Nspawn Builder (https://nspawn.org)"
# Mkosi
MKOSI_CMD=$(command -v mkosi)
MKOSI_IMAGE_TYPES=()
MKOSI_ARGS=" --force"
# Enable/Disable specific format builds, leave empty to build in the specified format
DISABLE_TAR=""
DISABLE_RAW=""
# Create mkosi.cache/ directory if no exists, leave empty if you don't want it to be created
CREATE_MKOSI_CACHE_DIR="yes"
# Set the cache dir for mkosi to use. By default, it's /var/cache/mkosi.
MKOSI_CACHE_BASE_DIR="/var/cache/mkosi"
set -e
ctrl_c() {
echo "Keyboard Interrupt detected, leaving."
exit
}
trap ctrl_c 2
version() {
echo "$STATIC_NAME $VERSION"
}
check_mkosi() {
if [[ -z "$MKOSI_CMD" ]]; then
echo "mkosi was not found in PATH, please install it https://github.com/systemd/mkosi"
exit 1
fi
}
check_and_create_dir() {
local output_dir=""
output_dir=$(grep "^Output=" "$1")
output_dir=${output_dir#*=}
output_dir=${output_dir%/*}/
if [ ! -d "$output_dir" ]; then
mkdir -p "$output_dir"
fi
}
helpout() {
echo "Menu usage for $STATIC_NAME $VERSION"
echo
echo "${0##*/} {COMMAND} [PARAMETER]"
echo
echo "Wrapper around mkosi for creating images in https://nspawn.org"
echo "If you find an issue, report it to: https://github.com/nspawn/nspawn"
echo
echo "Commands:"
echo -e " -n/--name \tBuild an image with the following parameters: <scripts-name>"
echo -e " --notar \tDo not build the TAR image."
echo -e " --noraw \tDo not build the RAW image."
echo -e " -h/--help \tPrints this help message"
echo -e " -v/--version \tPrints version info"
echo
echo "Parameters:"
echo -e " <scripts-name> \tBase name of the scripts to use."
}
if [[ $# -eq 0 ]]; then
helpout
exit
fi
check_root() {
if [[ $EUID -ne 0 ]]; then
echo "This script requires root privileges to work, leaving."
exit 1
fi
}
build_image() {
check_mkosi
check_root
scripts_name="$1"
tar_default="${scripts_name}-tar.default"
raw_default="${scripts_name}-raw.default"
mkosi_extra="${scripts_name}.extra"
mkosi_skeleton="${scripts_name}.skeleton"
mkosi_build="${scripts_name}.build"
mkosi_prepare="${scripts_name}.prepare"
mkosi_postinst="${scripts_name}.postinst"
mkosi_finalize="${scripts_name}.finalize"
mkosi_mksquashfs_tool="${scripts_name}.mksquashfs-tool"
mkosi_nspawn="${scripts_name}.nspawn"
mkosi_cache="${scripts_name}.cache"
mkosi_buildir="${scripts_name}.builddir"
# mkosi_rootpw="${scripts_name}.rootpw"
# mkosi_passphrase="${scripts_name}.passphrase"
mkosi_secure_boot_crt="${scripts_name}.secure-boot.crt"
mkosi_output="${scripts_name}.output"
# Verify that Output= directory exists, and, if not then create it.
# Also create images array
if [ -f "$tar_default" ] && [[ -z $DISABLE_TAR ]]; then
echo "Verifying that output directory for tar image exists..."
check_and_create_dir "$tar_default"
MKOSI_IMAGE_TYPES+=(" --default $tar_default")
fi
if [ -f "$raw_default" ] && [[ -z $DISABLE_RAW ]]; then
echo "Verifying that output directory for raw image exists..."
check_and_create_dir "$raw_default"
MKOSI_IMAGE_TYPES+=(" --default $raw_default")
fi
if [ ${#MKOSI_IMAGE_TYPES[@]} -eq 0 ]; then
echo "No $tar_default or $raw_default are available for the scripts name $scripts_name, leaving."
exit 1
fi
# Create mkosi.cache/ dir if flag is set
if [ ! -d "$mkosi_cache" ] && [[ -n $CREATE_MKOSI_CACHE_DIR ]]; then
if [[ -n $MKOSI_CACHE_BASE_DIR ]]; then
mkosi_cache="$MKOSI_CACHE_BASE_DIR/$mkosi_cache"
fi
mkdir -p "$mkosi_cache"
fi
# Workaround for Centos 7
if [ "$scripts_name" == "centos7" ]; then
MKOSI_ARGS+=" --without-unified-kernel-images"
fi
# Static options
if [ -d "$mkosi_extra" ]; then
MKOSI_ARGS+=" --extra-tree=$mkosi_extra"
fi
if [ -d "$mkosi_skeleton" ]; then
MKOSI_ARGS+=" --skeleton-tree=$mkosi_skeleton"
fi
if [ -f "$mkosi_build" ]; then
MKOSI_ARGS+=" --build-script=$mkosi_build"
fi
if [ -f "$mkosi_prepare" ]; then
MKOSI_ARGS+=" --prepare-script=$mkosi_prepare"
fi
if [ -f "$mkosi_postinst" ]; then
MKOSI_ARGS+=" --postinst-script=$mkosi_postinst"
fi
if [ -f "$mkosi_finalize" ]; then
MKOSI_ARGS+=" --finalize-script=$mkosi_finalize"
fi
if [ -f "$mkosi_mksquashfs_tool" ]; then
MKOSI_ARGS+=" --mksquashfs=$mkosi_mksquashfs_tool"
fi
if [ -f "$mkosi_nspawn" ]; then
MKOSI_ARGS+=" --settings=$mkosi_nspawn"
fi
if [ -d "$mkosi_cache" ]; then
MKOSI_ARGS+=" --cache=$mkosi_cache"
fi
if [ -d "$mkosi_buildir" ]; then
MKOSI_ARGS+=" --build-dir=$mkosi_buildir"
fi
if [ -f "$mkosi_secure_boot_crt" ]; then
MKOSI_ARGS+=" --secure-boot-certificate=$mkosi_secure_boot_crt"
fi
if [ -d "$mkosi_output" ]; then
MKOSI_ARGS+=" --output-dir=$mkosi_output"
fi
# Build each image
echo -e "\n\t Building mkosi images, it may take a while... \n"
for image in "${MKOSI_IMAGE_TYPES[@]}"; do
echo -e "\n Running $MKOSI_CMD $image $MKOSI_ARGS \n"
$MKOSI_CMD $image $MKOSI_ARGS
done
}
POSITIONAL=()
while [[ $# -gt 0 ]]; do
key="$1"
case $key in
-v | --version)
version
exit
;;
-h | --help)
helpout
exit
;;
--notar )
DISABLE_TAR="yes"
shift
;;
--noraw )
DISABLE_RAW="yes"
shift
;;
-n | --name)
build_image "$2"
shift
shift
;;
*)
POSITIONAL+=("$1")
echo "Ivalid argument(s): $POSITIONAL"
shift
;;
esac
done
set -- "${POSITIONAL[@]}"