2 ###############################################################################
3 # IPFire.org - An Open Source Firewall Solution #
4 # Copyright (C) - IPFire Development Team <info@ipfire.org> #
5 ###############################################################################
9 SCRIPT_PATH
="$(dirname "$
(readlink
-f "$0")")"
19 EXIT_COMMAND_NOT_FOUND
=127
36 echo "[${level}] ${message}"
43 log DEBUG
"Running command: ${cmd}"
53 log DEBUG
"Returned with code '${ret}'"
61 umount
${IMAGE_MOUNT_DIR}
63 # Remove partition from the kernel table.
66 # Remove loopback device
72 if [ -d "${WORKING_DIR}" ]; then
73 rm -dfR "${WORKING_DIR}"
77 generate_image_filename
() {
82 echo "${distro}-${version}-${arch}"
89 # Check that pakfire-server binary is available
90 # Check that pakfire binary is available
92 # Check that repo files are installed. (pakfire need to know which repos exist)
93 local repo_dir
="/etc/pakfire/repos"
95 if [ ! -d "${repo_dir}" ]; then
96 log ERROR
"Could not find respository directory ${repo_dir}"
100 return ${return_value}
104 local compression
=${1}
105 local image_file
=${2}
108 log debug
"Compressing ${image_file} with ${compression}"
110 case "${compression}" in
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"
117 cmd xz
"-${level}" "${image_file}"
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"
126 cmd
zip "-${level}" "${image_file}.zip" "${image_file}"
127 # Remove the file which we compressed+
128 rm -f "${image_file}"
134 reset_root_password
() {
137 cp -avf ${root_dir}/etc
/passwd
${root_dir}/etc
/passwd.orig
139 # Drop root password.
140 sed -e "s/^\(root:\)[^:]*:/\1:/" ${root_dir}/etc
/passwd.orig
> ${root_dir}/etc
/passwd
142 # Remove passwd backup file.
143 rm -rvf ${root_dir}/etc
/passwd.orig
147 # Dir where the repos should be located
158 # Clone git repositories.
159 for repo
in ${repos}; do
165 # This function is used to publish the produced images
169 # The image we created usually a img. file
170 local image_base_file
=${2}
172 local image_name_final
="$(generate_image_filename "${DISTRO}" "${VERSION}" "${ARCH}")"
175 local compression_type
176 local compression_level
=1
178 if [[ ${IMAGE_RELEASE} -eq ${TRUE} ]]; then
182 # Do these steps for every image format we like to publish
183 for image_type
in ${IMAGE_TYPES_PUBLISH}; do
184 # Get compressioon type
185 compression_type
="$(get_compression_type ${image_type})"
186 # Convert images to the type specified in IMAGE_TYPES_PUBLISH
187 convert_image
"${image_type}" "${image_base_file}" "${image_name_final}"
190 compress_image
"${compression_type}" "${image_name_final}.${image_type}" ${compression_level}
192 # Move images to this path
193 mv -f "${image_name_final}.${image_type}.${compression_type}" ${path}
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}"
208 if [[ ${type} = "img" ]]; then
209 # We do not need to convert the image here but we need to rename
210 mv -f ${from} ${to}.${type}
214 if [[ ${type} = "qcow2" ]]; then
215 local command="qemu-img convert -c -O ${type} ${from} ${to}.${type}"
217 local command="qemu-img convert -O ${type} ${from} ${to}.${type}"
223 get_compression_type
() {
224 local image_type
=${1}
226 case "${image_type}" in
228 # These types can be used only under Unix so we use xz as compression
232 # These types can be also under Windows so we use zip as compression
239 check_for_free_space
() {
242 local space_in_path
=0
244 space_in_path
=$
(df
-h -B MB
--output=avail
${path} |
tail -n 1)
245 space_in_path
=${space_in_path%MB}
246 log debug
${space_in_path}
249 if [ ${space_in_path} -lt ${space} ]; then
250 log error
"Not enough free space available under ${path}"
251 log error
"Free space is ${space_in_path}MB but we need at least ${space}MB"
257 while [ $# -gt 0 ]; do
260 IMAGE_RELEASE
=${TRUE}
264 error
"Invalid argument: ${1}"
265 return ${EXIT_CONF_ERROR}
278 VERSION
="$(date +"%Y
%m
%d
")"
279 WORKING_DIR
=$
(mktemp
-d /tmp
/ipfire3_image.XXXXXXXX
)
281 log DEBUG
"Working dir is ${WORKING_DIR}"
287 IMAGE_BASE_FILE
="$(mktemp -u ${WORKING_DIR}/image_base_file.XXXXXXX.img)"
289 IMAGE_MOUNT_DIR
="$(mktemp -d ${WORKING_DIR}/image.XXXXXXX)"
291 IMAGE_TYPES_PUBLISH
="qcow2 vmdk vdi img"
292 IMAGE_DIR_PUBLISH
="/home/jschlag/public/ipfire3-images"
294 # The used filesystem.
297 # Additional packages which should be installed.
298 PACKAGES
="xfsprogs kernel openssh-server wget htop tmux"
300 # Use git network stack. ( When using the git network stack,
301 # development tools and git automatically will be installed.)
302 USE_GIT_NETWORK_STACK
="True"
304 # List of packages which are required to build the network stack.
305 NETWORK_BUILD_DEPS
="asciidoc autoconf automake docbook-xsl libnl3-devel libxslt systemd-devel"
307 # Install development tools.
308 INSTALL_DEV_PACKAGES
="True"
310 # List of development tools which should be installed.
311 DEVELOPMENT_PACKAGES
="make gcc libtool git"
314 # Git repositories which also should be checked, out.
318 # Stuff for the local repo
321 # Use local repository.
322 USE_LOCAL_REPO
="True"
324 # Source path for the local repo packages.
325 LOCAL_REPO_SOURCE_PATH
="/var/lib/pakfire/local/"
327 # Path were the local repo is created
328 LOCAL_REPO_DIR
="$(mktemp -d ${WORKING_DIR}/ipfire-repo.XXXXXXX)"
330 # Config file for the local repo
331 LOCAL_REPO_FILE
="/etc/pakfire/repos/local.repo"
335 # Scripts starts here
341 # Check that pakfire is working
344 if ! check_for_free_space
10000 "${WORKING_DIR}"; then
348 # Check that the image does not exist yet
349 if [ -f ${IMAGE_BASE_FILE} ]; then
350 log ERROR
"Image file does already exists"
354 # Check that the local repo file does not exists yet.
355 # We do not want to override custom user configurations.
356 if [ -f "${LOCAL_REPO_FILE}" ]; then
357 log ERROR
"Config file ${LOCAL_REPO_FILE} for the local repo does already exists"
361 # cd into working directory
362 cd ${WORKING_DIR} ||
exit ${EXIT_ERROR}
365 ## Create the disk image.
367 dd if=/dev
/zero of
=${IMAGE_BASE_FILE} seek
=${IMAGE_SIZE}M count
=1k bs
=1
369 # Setup the loopback device.
370 outlo
=`losetup -f --show ${IMAGE_BASE_FILE}`
372 log INFO
"Create partions and filesystem"
374 # Create and msdos compatible table on the image.
375 parted
${outlo} mklabel msdos
377 # Add a new partition to the image.
378 parted
${outlo} mkpart primary
${FILESYSTEM} 2048k
100% -a minimal
380 # Make the primary partition bootable.
381 parted
${outlo} set 1 boot on
383 # Notify the kernel about the new partition.
387 ## Create the filesystem.
389 mkfs.
${FILESYSTEM} ${outlo}p1
392 ## Mount the filesystem.
395 log INFO
"Mount partion in ${IMAGE_MOUNT_DIR}"
397 # Afterwards mount the image.
398 mount
-t ${FILESYSTEM} ${outlo}p1 ${IMAGE_MOUNT_DIR}
401 ## Install IPFire 3.x.
404 # Add grub on x86_64 to the package list.
405 if [ "${ARCH}" == "x86_64" ] ||
[ "${ARCH}" == "i686" ]; then
406 PACKAGES
="${PACKAGES} grub"
408 # Store, that grub is present.
412 # Check if the git network stack should be installed.
413 if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
414 GIT_REPOS
="${GIT_REPOS} git://git.ipfire.org/network.git"
416 # Add build dependencies of network package.
417 PACKAGES
="${PACKAGES} ${NETWORK_BUILD_DEPS}"
420 # Add develoment packes to the package list, if required.
421 if [ "${INSTALL_DEV_PACKAGES}" == "True" ] ||
[ ! -z "${GIT_REPOS}" ]; then
422 PACKAGES
="${PACKAGES} ${DEVELOPMENT_PACKAGES}"
425 log INFO
"Create local respository"
428 # Check if the local repo should be used.
429 if [ "${USE_LOCAL_REPO}" == "True" ]; then
430 # Create local repository.
431 mkdir
-pv "${LOCAL_REPO_DIR}"
434 if ! pakfire-server repo create
${LOCAL_REPO_DIR} ${LOCAL_REPO_SOURCE_PATH}; then
435 log ERROR
"Failed to create a local respository"
440 # Create temporary pakfire repo file.
441 echo "[repo:local]" >> "${LOCAL_REPO_FILE}"
442 echo "description = Local repository." >> "${LOCAL_REPO_FILE}"
443 echo "enabled = 0" >> "${LOCAL_REPO_FILE}"
444 echo "baseurl = ${LOCAL_REPO_DIR}" >> "${LOCAL_REPO_FILE}"
446 ENABLE_LOCAL
="--enable-repo=local"
449 # Install IPFire 3.x in the created image.
450 yes | pakfire
--root=${IMAGE_MOUNT_DIR} ${ENABLE_LOCAL} install @Base ${PACKAGES}
453 # Enable serial console
457 #echo "GRUB_TERMINAL=\"serial console\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
458 #echo "GRUB_SERIAL_COMMAND=\"serial --unit=0 --speed=115200\"" >> "${IMAGE_MOUNT_DIR}/etc/default/grub"
460 #Hack to install a /etc/default/grub file
462 cmd
cp -f "${SCRIPT_PATH}/grub" "${IMAGE_MOUNT_DIR}/etc/default"
468 # Gather the uuid of the partition.
469 FS_UUID
=$
(blkid
-o value
-s UUID
${outlo}p1
)
472 echo "UUID=${FS_UUID} / ${FILESYSTEM} defaults 0 0" > "${IMAGE_MOUNT_DIR}/etc/fstab"
474 cat "${IMAGE_MOUNT_DIR}/etc/fstab"
477 ## Remove the password for user root.
480 reset_root_password
"${IMAGE_MOUNT_DIR}"
483 ## Setup git repositories.
486 clone_git_repos
"${IMAGE_MOUNT_DIR}/build" ${GIT_REPOS}
489 ## Prepare chrooting into the image.
492 # Check if the network stack should be build.
493 if [ "${USE_GIT_NETWORK_STACK}" == "True" ]; then
494 BUILD_NETWORK_CMDS
="cd network/ && ./autogen.sh && ./configure && make && make install"
497 ENABLE_GETTY
="/bin/systemctl enable getty@.service"
499 # Check if the arch uses grub
500 if [ "${HAVE_GRUB}" == "True" ]; then
501 GENERATE_GRUB_CONF
="grub-install --boot-directory=${IMAGE_MOUNT_DIR}/boot/ --modules="${FILESYSTEM} part_msdos" ${outlo} && \
502 grub-mkconfig -o /boot/grub/grub.cfg"
506 # Use systemd-nspawn to spawn a chroot environment and execute
507 # commands inside it.
509 # The first command enables the terminal on TTY1.
510 # The second command generates the configuration file for grub2.
513 systemd-nspawn
-D ${IMAGE_MOUNT_DIR} --bind /dev
--capability=CAP_SYS_ADMIN
,CAP_SYS_RAWIO
--bind /proc
--bind /sys
<< END
514 echo "Execute commands inside chroot"
516 ${GENERATE_GRUB_CONF}
518 ${BUILD_NETWORK_CMDS}
519 echo "All commands executed"
524 # Insert the UUID because grub-mkconfig often fails to
525 # detect that correctly
527 sed -i "${IMAGE_MOUNT_DIR}/boot/grub/grub.cfg" \
528 -e "s/root=[A-Za-z0-9\/=-]*/root=UUID=${FS_UUID}/g"
530 cat "${IMAGE_MOUNT_DIR}/boot/grub/grub.cfg"
532 cat "${IMAGE_MOUNT_DIR}/etc/fstab"
542 # Check filesystem for damage.
543 fsck.
${FILESYSTEM} ${outlo}p1
547 publish
"${IMAGE_DIR_PUBLISH}" "${IMAGE_BASE_FILE}"