Skip to content

Commit

Permalink
implemented ADDPART command
Browse files Browse the repository at this point in the history
ADDPART appends a partition in the SIZE with a partition type and file system
  • Loading branch information
jonashoechst committed Jan 7, 2025
1 parent b608d15 commit 765abce
Show file tree
Hide file tree
Showing 4 changed files with 69 additions and 1 deletion.
3 changes: 2 additions & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,14 @@ FROM debian:bookworm-slim

LABEL description="Reconfigure Raspberry Pi images with an easy, Docker-like configuration file"
LABEL maintainer="[email protected]"
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 \
Expand Down
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 <MODE> SOURCE DEST`
`INSTALL` installs a given file or directory into the destination in the image.
Expand Down
4 changes: 4 additions & 0 deletions stages/00-commands.sh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,10 @@ PUMP() {
:
}

ADDPART() {
:
}

# Stage 3x
INSTALL() {
:
Expand Down
60 changes: 60 additions & 0 deletions stages/20-prepare.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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}"
}

0 comments on commit 765abce

Please sign in to comment.