diff --git a/Dockerfile b/Dockerfile index 141c0a7..25e1ead 100644 --- a/Dockerfile +++ b/Dockerfile @@ -2,13 +2,14 @@ FROM debian:bookworm-slim LABEL description="Reconfigure Raspberry Pi images with an easy, Docker-like configuration file" LABEL maintainer="hoechst@mathematik.uni-marburg.de" -LABEL version="0.6.1" +LABEL version="0.7.0" RUN bash RUN apt-get update && \ apt-get install -y \ binfmt-support \ + exfatprogs \ fdisk \ file \ git \ diff --git a/README.md b/README.md index 639da82..9c0075f 100644 --- a/README.md +++ b/README.md @@ -198,6 +198,9 @@ This is an alternative to `FROM` and `TO`. ##### `PUMP SIZE` `PUMP` increases the image's size about the given amount (suffixes K, M, G are allowed). +##### `ADDPART SIZE PTYPE FS` +`PUMP` appends a partition of the size (suffixes K, M, G are allowed) using a partion type and file system (ext4, exfat, ...). + #### 3. Chroot Stage ##### `INSTALL SOURCE DEST` `INSTALL` installs a given file or directory into the destination in the image. diff --git a/stages/00-commands.sh b/stages/00-commands.sh index f2c6f33..0c9d89f 100644 --- a/stages/00-commands.sh +++ b/stages/00-commands.sh @@ -44,6 +44,10 @@ PUMP() { : } +ADDPART() { + : +} + # Stage 3x INSTALL() { : diff --git a/stages/20-prepare.sh b/stages/20-prepare.sh index c28d79e..f355a40 100644 --- a/stages/20-prepare.sh +++ b/stages/20-prepare.sh @@ -42,3 +42,63 @@ PUMP() { umount_image "${loop}" } + +# ADDPART adds a partition of a given size, partition type and file system +# +# Usage: ADDPART SIZE PTYPE FS +ADDPART() { + if [ $# -ne 3 ]; then + echo -e "\033[0;31m### Error: usage ADDPART SIZE PTYPE FS\033[0m" + return 1 + fi + + echo -e "\033[0;32m### ADDPART ${1} ${2} ${3}\033[0m" + + if [[ -b "${DEST_IMG}" ]]; then + echo -e "\033[0;31m### Error: Block device ${DEST_IMG} cannot be altered.\033[0m" + return 1 + fi + + BS="1M" + + # units does not print to stderr, thus test call before using output + echo -n "addpart conversion to ${BS} * " + units -t "${1}B" "${BS}B" + + COUNT=$(units -t ${1}B ${BS}B) + + # Ceil the number if a decimal is given. + if [[ "${COUNT}" == *.* ]]; then + COUNT=$(( $(echo "${COUNT}" | cut -d. -f1) + 1 )) + fi + + echo "addpart ceil: ${BS} * ${COUNT}" + + # Error on unset PTYPE + if [[ -z ${2+x} ]]; then + echo -e "\033[0;31m### Error: Partition type unspecified, possible options:\033[0m" + sfdisk -T + return 1 + fi + + echo "checking mkfs.${3}" + + if ! command -v mkfs.${3}; then + echo -e "\033[0;31m### Error: file system ${3} is not available.\033[0m" + return 1 + fi + + dd if=/dev/zero bs="${BS}" count="${COUNT}" >> "${DEST_IMG}" + + local data_part_start + data_part_start=$(( $(sfdisk -l "${DEST_IMG}" -o END -n | tail -n1) + 1 )) + + echo "$data_part_start,+,${2}" | sfdisk -a "${DEST_IMG}" + + local loop + loop=$(mount_image "${DEST_IMG}") + + mkfs.${3} "/dev/mapper/${loop}p$((IMG_ROOT + 1))" + + umount_image "${loop}" +}