]> git.ipfire.org Git - people/jschlag/ipfire-3.x-image.git/blob - generate_image.sh
Refactor cleanup.
[people/jschlag/ipfire-3.x-image.git] / generate_image.sh
1 #!/bin/bash
2 ###############################################################################
3 # IPFire.org - An Open Source Firewall Solution #
4 # Copyright (C) - IPFire Development Team <info@ipfire.org> #
5 ###############################################################################
6
7 # Constants
8
9 # Proper error codes
10 EXIT_OK=0
11 EXIT_ERROR=1
12 EXIT_CONF_ERROR=2
13 EXIT_NOT_SUPPORTED=3
14 EXIT_NOT_HANDLED=4
15 EXIT_COMMAND_NOT_FOUND=127
16 EXIT_ERROR_ASSERT=128
17
18 EXIT_TRUE=0
19 EXIT_FALSE=1
20 EXIT_UNKNOWN=2
21
22 # Functions
23
24 log() {
25 local level=${1}
26 local message=${2}
27 echo "[${level}] ${message}"
28 }
29
30 cleanup() {
31 # Unmount image
32 umount ${IMAGE_MOUNT_DIR}
33
34 # Remove partition from the kernel table.
35 partx -d ${outlo}p1
36
37 # Remove loopback device
38 losetup -d ${outlo}
39
40 # Drop working dir
41 if [ -d "${WORKING_DIR}" ]; then
42 rm -dfR "${WORKING_DIR}"
43 fi
44 }
45
46 generate_image_filename() {
47 local distro=${1}
48 local version=${2}
49 local arch=${3}
50
51 echo "${distro}-${version}-${arch}"
52 }
53
54 check_for_pakfire() {
55 local return_value=0
56
57 # TODO
58 # Check that pakfire-server binary is available
59 # Check that pakfire binary is available
60
61 # Check that repo files are installed. (pakfire need to know which repos exist)
62 local repo_dir="/etc/pakfire/repos"
63
64 if [ ! -d "${repo_dir}" ]; then
65 log ERROR "Could not find respository directory ${repo_dir}"
66 return_value=1
67 fi
68
69 return ${return_value}
70 }
71
72 compress_image() {
73 local compression=${1}
74 local image_file=${2}
75 local level=${3}
76
77 log debug "Compressing ${image_file} with ${compression}"
78
79 case "${compression}" in
80 "xz")
81 # Check that the file does not exist yet
82 if [ -f "${image_file}.xz" ]; then
83 log ERROR "Failed to compress the image. The file already exists"
84 return ${EXIT_ERROR}
85 fi
86 xz "-${level}" "${image_file}"
87 ;;
88 "zip")
89 # Check that the file does not exist yet
90 if [ -f "${image_file}.zip" ]; then
91 log ERROR "Failed to compress the image. The file already exists"
92 return ${EXIT_ERROR}
93
94 fi
95 zip "-${level}" "${image_file}.zip" "${image_file}"
96 # Remove the file which we compressed+
97 rm -f "${image_file}"
98 ;;
99
100 esac
101 }
102
103 reset_root_password() {
104 local root_dir=${1}
105 # Backup passwd file
106 cp -avf ${root_dir}/etc/passwd ${root_dir}/etc/passwd.orig
107
108 # Drop root password.
109 sed -e "s/^\(root:\)[^:]*:/\1:/" ${root_dir}/etc/passwd.orig > ${root_dir}/etc/passwd
110
111 # Remove passwd backup file.
112 rm -rvf ${root_dir}/etc/passwd.orig
113 }
114
115 clone_git_repos() {
116 # Dir where the repos should be located
117 local dir=${1}
118 shift
119 local repos="$@"
120
121 local repo
122
123 mkdir -pv ${dir}
124
125 (
126 cd ${dir}
127 # Clone git repositories.
128 for repo in ${repos}; do
129 git clone ${repo}
130 done
131 )
132 }
133
134 # This function is used to publish the produced images
135
136 publish() {
137 local path=${1}
138 # The image we created usually a img. file
139 local image_base_file=${2}
140
141 local image_name_final="$(generate_image_filename "${DISTRO}" "${VERSION}" "${ARCH}")"
142
143 local image_type
144 local compression_type
145
146 # Do these steps for every image format we like to publish
147 for image_type in ${IMAGE_TYPES_PUBLISH}; do
148 # Get compressioon type
149 compression_type="$(get_compression_type ${image_type})"
150 # Convert images to the type specified in IMAGE_TYPES_PUBLISH
151 convert_image "${image_type}" "${image_base_file}" "${image_name_final}"
152
153 # compress image.
154 compress_image "${compression_type}" "${image_name_final}.${image_type}" "1"
155
156 # Move images to this path
157 mv -f "${image_name_final}.${image_type}.${compression_type}" ${path}
158
159 # point the latest links to these images
160 ln -s -f "${path}/${image_name_final}.${image_type}.${compression_type}" \
161 "${path}/$(generate_image_filename "${DISTRO}" "latest" "${ARCH}").${image_type}.${compression_type}"
162
163 done
164
165 }
166
167 convert_image() {
168 local type=${1}
169 local from=${2}
170 local to=${3}
171
172 if [[ ${type} = "img" ]]; then
173 # We do not need to convert the image here but we need to rename
174 mv -f ${from} ${to}.${type}
175 fi
176
177 if [[ ${type} = "qcow2" ]]; then
178 local cmd="qemu-img convert -c -O ${type} ${from} ${to}.${type}"
179 else
180 local cmd="qemu-img convert -O ${type} ${from} ${to}.${type}"
181 fi
182
183 log debug "${cmd}"
184
185 # Execute the command to convert the image
186 ${cmd}
187 }
188
189 get_compression_type() {
190 local image_type=${1}
191
192 case "${image_type}" in
193 "qcow2" | "img")
194 # These types can be used only under Unix so we use xz as compression
195 echo "xz"
196 ;;
197 "vmdk" | "vdi")
198 # These types can be also under Windows so we use zip as compression
199 echo "zip"
200 ;;
201 esac
202
203 }
204
205 #
206 # General settings
207 #
208
209 ARCH="x86_64"
210 DISTRO="ipfire3"
211 VERSION="$(date +"%Y%m%d")"
212 WORKING_DIR=$(mktemp -d /tmp/ipfire3_image.XXXXXXXX)
213
214 log DEBUG "Working dir is ${WORKING_DIR}"
215
216 #
217 # Image
218 #
219
220 IMAGE_TYPE="img"
221 IMAGE_BASE_FILE="$(mktemp -u ${WORKING_DIR}/image_base_file.XXXXXXX.img)"
222 IMAGE_SIZE="8196"
223 IMAGE_MOUNT_DIR="$(mktemp -d ${WORKING_DIR}/image.XXXXXXX)"
224
225 IMAGE_TYPES_PUBLISH="qcow2 vmdk vdi img"
226
227 # The used filesystem.
228 FILESYSTEM="xfs"
229
230 # Additional packages which should be installed.
231 PACKAGES="xfsprogs kernel openssh-server wget htop tmux"
232
233 # Use git network stack. ( When using the git network stack,
234 # development tools and git automatically will be installed.)
235 USE_GIT_NETWORK_STACK="True"
236
237 # List of packages which are required to build the network stack.
238 NETWORK_BUILD_DEPS="autoconf automake docbook-xsl libnl3-devel libxslt systemd-devel"
239
240 # Install development tools.
241 INSTALL_DEV_PACKAGES="False"
242
243 # List of development tools which should be installed.
244 DEVELOPMENT_PACKAGES="make gcc libtool git"
245
246
247 # Git repositories which also should be checked, out.
248 GIT_REPOS=""
249
250 #
251 # Stuff for the local repo
252 #
253
254 # Use local repository.
255 USE_LOCAL_REPO="True"
256
257 # Source path for the local repo packages.
258 LOCAL_REPO_SOURCE_PATH="/var/lib/pakfire/local/"
259
260 # Path were the local repo is created
261 LOCAL_REPO_DIR="$(mktemp -d ${WORKING_DIR}/ipfire-repo.XXXXXXX)"
262
263 # Config file for the local repo
264 LOCAL_REPO_FILE="/etc/pakfire/repos/local.repo"
265
266
267 # Check that pakfire is working
268 check_for_pakfire
269
270
271 # Check that the image does not exist yet
272 if [ -f ${IMAGE_BASE_FILE} ]; then
273 log ERROR "Image file does already exists"
274 exit 1
275 fi
276
277 # Check that the local repo file does not exists yet.
278 # We do not want to override custom user configurations.
279 if [ -f "${LOCAL_REPO_FILE}" ]; then
280 log ERROR "Config file ${LOCAL_REPO_FILE} for the local repo does already exists"
281 exit 1
282 fi
283
284 # cd into working directory
285 cd ${WORKING_DIR} || exit ${EXIT_ERROR}
286
287 #
288 ## Create the disk image.
289 #
290 dd if=/dev/zero of=${IMAGE_BASE_FILE} seek=${IMAGE_SIZE}MB count=1k bs=1
291
292 # Setup the loopback device.
293 outlo=`losetup -f --show ${IMAGE_BASE_FILE}`
294
295 log INFO "Create partions and filesystem"
296
297 # Create and msdos compatible table on the image.
298 parted ${outlo} mklabel msdos
299
300 # Add a new partition to the image.
301 parted ${outlo} mkpart primary ${FILESYSTEM} 2048k 100% -a minimal
302
303 # Make the primary partition bootable.
304 parted ${outlo} set 1 boot on
305
306 # Notify the kernel about the new partition.
307 partx -a ${outlo}
308
309 #
310 ## Create the filesystem.
311 #
312 mkfs.${FILESYSTEM} ${outlo}p1
313
314 #
315 ## Mount the filesystem.
316 #
317
318 log INFO "Mount partion in ${IMAGE_MOUNT_DIR}"
319
320 # Create a temporary mountpoint.
321 mkdir -p ${IMAGE_MOUNT_DIR}
322
323 # Afterwards mount the image.
324 mount -t ${FILESYSTEM} ${outlo}p1 ${IMAGE_MOUNT_DIR}
325
326 #
327 ## Install IPFire 3.x.
328 #
329
330 # Add grub on x86_64 to the package list.
331 if [ "${ARCH}" == "x86_64" ] || [ "${ARCH}" == "i686" ]; then
332 PACKAGES="${PACKAGES} grub"
333
334 # Store, that grub is present.
335 HAVE_GRUB="True"
336 fi
337
338 # Check if the git network stack should be installed.
339 if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
340 GIT_REPOS="${GIT_REPOS} git://git.ipfire.org/network.git"
341
342 # Add build dependencies of network package.
343 PACKAGES="${PACKAGES} ${NETWORK_BUILD_DEPS}"
344 fi
345
346 # Add develoment packes to the package list, if required.
347 if [ "${INSTALL_DEV_PACKAGES}" == "True" ] || [ ! -z "${GIT_REPOS}" ]; then
348 PACKAGES="${PACKAGES} ${DEVELOPMENT_PACKAGES}"
349 fi
350
351 log INFO "Create local respository"
352
353
354 # Check if the local repo should be used.
355 if [ "${USE_LOCAL_REPO}" == "True" ]; then
356 # Create local repository.
357 mkdir -pv "${LOCAL_REPO_DIR}"
358
359 # Master repository.
360 if ! pakfire-server repo create ${LOCAL_REPO_DIR} ${LOCAL_REPO_SOURCE_PATH}; then
361 log ERROR "Failed to create a local respository"
362 cleanup
363 exit 1
364 fi
365
366 # Create temporary pakfire repo file.
367 echo "[repo:local]" >> "${LOCAL_REPO_FILE}"
368 echo "description = Local repository." >> "${LOCAL_REPO_FILE}"
369 echo "enabled = 0" >> "${LOCAL_REPO_FILE}"
370 echo "baseurl = ${LOCAL_REPO_DIR}" >> "${LOCAL_REPO_FILE}"
371
372 ENABLE_LOCAL="--enable-repo=local"
373 fi
374
375 # Install IPFire 3.x in the created image.
376 yes | pakfire --root=${IMAGE_MOUNT_DIR} ${ENABLE_LOCAL} install @Base ${PACKAGES}
377
378 #
379 # Enable serial console
380 #
381
382
383 #echo "GRUB_TERMINAL=\"serial console\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
384 #echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=115200\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
385
386 #Hack to install a /etc/default/grub file
387
388 cp -f grub "${IMAGE_MOUNT_DIR}/etc/default"
389
390 #
391 ## Install grub2 if neccessary
392 #
393 if [ "${HAVE_GRUB}" == "True" ]; then
394 grub2-install --boot-directory=${IMAGE_MOUNT_DIR}/boot/ --modules="${FILESYSTEM} part_msdos" ${outlo}
395 fi
396
397 #
398 ## Generate fstab
399 #
400
401 # Gather the uuid of the partition.
402 FS_UUID=$(blkid -o value -s UUID ${outlo}p1)
403
404 # Write fstab.
405 echo "UUID=${FS_UUID} / ${FILESYSTEM} defaults 0 0" > ${IMAGE_MOUNT_DIR}/etc/fstab
406
407 #
408 ## Remove the password for user root.
409 #
410
411 reset_root_password "${IMAGE_MOUNT_DIR}"
412
413 #
414 ## Setup git repositories.
415 #
416
417 clone_git_repos "${IMAGE_MOUNT_DIR}/build" ${GIT_REPOS}
418
419 #
420 ## Prepare chrooting into the image.
421 #
422
423 # Check if the network stack should be build.
424 if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
425 BUILD_NETWORK_CMDS="cd network/ && ./autogen.sh && ./configure && make && make install"
426 fi
427
428 ENABLE_GETTY="/bin/systemctl enable getty@.service"
429
430 # Check if the arch uses grub
431 if [ "${HAVE_GRUB}" == "True" ]; then
432 GENERATE_GRUB_CONF="grub-mkconfig -o /boot/grub2/grub.cfg"
433 fi
434
435 # Use systemd-nspawn to spawn a chroot environment and execute
436 # commands inside it.
437 #
438 # The first command enables the terminal on TTY1.
439 # The second command generates the configuration file for grub2.
440
441
442 systemd-nspawn -D ${IMAGE_MOUNT_DIR} --bind /dev --capability=CAP_SYS_ADMIN,CAP_SYS_RAWIO --bind /proc --bind /sys << END
443 echo "Execute commands inside chroot"
444 ${ENABLE_GETTY}
445 ${GENERATE_GRUB_CONF}
446 cd /git-repos/ && ls
447 ${BUILD_NETWORK_CMDS}
448 echo "All commands executed"
449 END
450
451
452
453 # Insert the UUID because grub-mkconfig often fails to
454 # detect that correctly
455
456 sed -i "${IMAGE_MOUNT_DIR}/boot/grub2/grub.cfg" \
457 -e "s/root=[A-Za-z0-9\/=-]*/root=UUID=${FS_UUID}/g"
458
459 cat "${IMAGE_MOUNT_DIR}/boot/grub2/grub.cfg"
460 #
461 ## Tidy up.
462 #
463
464 # Wait a second.
465 sleep 5
466
467 # Check filesystem for damage.
468 fsck.${FILESYSTEM} ${outlo}p1
469
470 # Cleanup
471 cleanup
472
473 publish "/home/jschlag/public/ipfire3-images" "${IMAGE_BASE_FILE}"
474
475 #compress_image "xz" "${IMAGE_BASE_FILE}"