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