Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Various fixes and improvements #204

Merged
merged 15 commits into from
Sep 17, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -167,7 +167,7 @@ List of Distributions using CustomPiOS
--------------------------------------

* `OctoPi <https://octopi.octoprint.org/>`_ - The ready-to-go Raspberry Pi image with OctoPrint
* `FarmPi <https://farmpi.kevenaar.name/>`_ - An Ubuntu ARM 64bit Raspbery Pi image running `OctoFarm <https://octofarm.net/>`_
* `FarmPi <https://farmpi.kevenaar.name/>`_ - An Ubuntu ARM 64bit Raspberry Pi image running `OctoFarm <https://octofarm.net/>`_
* `FullPageOS <https://github.com/guysoft/FullPageOS>`_ - A Raspberry Pi distro to display a full page browser on boot
* `Zynthian <http://zynthian.org/>`_ - Open Synth Platform
* `ElectricSheepPi <https://github.com/guysoft/ElectricSheepPi>`_ - A Raspberry Pi distribution to run Electric Sheep digital art
Expand All @@ -183,6 +183,6 @@ List of Distributions using CustomPiOS
* `FluiddPi <https://github.com/cadriel/fluiddpi>`_ - Distro that packages `Fluidd <https://github.com/cadriel/fluidd>`_, `Moonraker <https://github.com/Arksine/moonraker>`_, and `Klipper <https://github.com/KevinOConnor/klipper>`_ into the ultimate 3D printer firmware package.
* `My Naturewatch Camera <https://github.com/interactionresearchstudio/NaturewatchCameraServer>`_ - A Python / OpenCV camera server to stream Pi camera content to a remote client through a website.
* `PiFireOS <https://github.com/calonmerc/PiFireOS>`_ - Distro for pellet grill/smoker control, running `PiFire <https://nebhead.github.io/PiFire>`_.
* `MonsterPi <https://docs.fdm-monster.net/guides/monsterpi>`_ - An Ubuntu ARM 64bit Raspbery Pi image running `FDM Monster <https://fdm-monster.net/>`_. This 3D Print server will help you connect 200+ OctoPrints together while providing a strong, professional workflow.
* `MonsterPi <https://docs.fdm-monster.net/guides/monsterpi>`_ - An Ubuntu ARM 64bit Raspberry Pi image running `FDM Monster <https://fdm-monster.net/>`_. This 3D Print server will help you connect 200+ OctoPrints together while providing a strong, professional workflow.

Code contribution would be appreciated!
40 changes: 38 additions & 2 deletions src/build_custom_os
Original file line number Diff line number Diff line change
@@ -1,8 +1,44 @@
#!/usr/bin/env bash
set -e
set -x


usage() {
cat <<EOF
usage: $0 [OPTIONS] [<variant>]

builds the current distro

OPTIONS
-l <logfile> write build-log to given file (instead of ${LOG:-build.log}
-h print this help and exit

EOF

}

while getopts "hl:" opt; do
case "$opt" in
h)
usage
exit 0
;;
l)
export LOG="$OPTARG"
;;
?)
usage
exit 2
;;
esac
done
shift $(($OPTIND - 1))

echo "Distro path: ${DIST_PATH}"
echo "CustomPiOS path: ${CUSTOM_PI_OS_PATH}"
echo "================================================================"
DIR="$( cd "$( dirname "${BASH_SOURCE[0]}" )" && pwd )"
${DIR}/build $1


set -x

${DIR}/build "$1"
46 changes: 23 additions & 23 deletions src/common.sh
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
#!/usr/bin/env bash

function die () {
echo >&2 "$@"
echo_red "$@" >&2
exit 1
}

Expand Down Expand Up @@ -153,15 +153,15 @@ function detach_all_loopback(){
# Cleans up mounted loopback devices from the image name
# NOTE: it might need a better way to grep for the image name, its might clash with other builds
for img in $(losetup | grep $1 | awk '{ print $1 }' ); do
if [[ -f $img ]]; then
if [ -f "${img}" ] || [ -b "${img}" ]; then
losetup -d $img
fi
done
}

function test_for_image(){
if [ ! -f "$1" ]; then
echo "Warning, can't see image file: $image"
echo_red "Warning, can't see image file: $image"
fi
}

Expand Down Expand Up @@ -189,16 +189,16 @@ function mount_image() {
boot_offset=$(($(jq ".partitiontable.partitions[] | select(.node == \"$image_path$boot_partition\").start" <<< ${fdisk_output}) * 512))
root_offset=$(($(jq ".partitiontable.partitions[] | select(.node == \"$image_path$root_partition\").start" <<< ${fdisk_output}) * 512))

echo "Mounting image $image_path on $mount_path, offset for boot partition is $boot_offset, offset for root partition is $root_offset"
echo_green "Mounting image $image_path on $mount_path, offset for boot partition is $boot_offset, offset for root partition is $root_offset"

# mount root and boot partition

detach_all_loopback $image_path
echo "Mounting root parition"
echo_green "Mounting root partition"
sudo losetup -f
sudo mount -o loop,offset=$root_offset $image_path $mount_path/
if [[ "$boot_partition" != "$root_partition" ]]; then
echo "Mounting boot partition"
echo_green "Mounting boot partition"
sudo losetup -f
sudo mount -o loop,offset=$boot_offset,sizelimit=$( expr $root_offset - $boot_offset ) "${image_path}" "${mount_path}"/"${boot_mount_path}"
fi
Expand All @@ -222,7 +222,7 @@ function unmount_image() {
then
for pid in $(sudo lsof -t $mount_path)
do
echo "Killing process $(ps -p $pid -o comm=) with pid $pid..."
echo_green "Killing process $(ps -p $pid -o comm=) with pid $pid..."
sudo kill -9 $pid
done
fi
Expand All @@ -240,7 +240,7 @@ function unmount_image() {
# Also we sort in reverse to get the deepest mounts first.
for m in $(sudo mount | grep $mount_path | awk -F " on " '{print $2}' | awk '{print $1}' | sort -r)
do
echo "Unmounting $m..."
echo_green "Unmounting $m..."
sudo umount $m
done
}
Expand Down Expand Up @@ -277,7 +277,7 @@ function enlarge_ext() {
partition=$2
size=$3

echo "Adding $size MB to partition $partition of $image"
echo_green "Adding $size MB to partition $partition of $image"
start=$(sfdisk --json "${image}" | jq ".partitiontable.partitions[] | select(.node == \"$image$partition\").start")
offset=$(($start*512))
dd if=/dev/zero bs=1M count=$size >> $image
Expand Down Expand Up @@ -321,12 +321,12 @@ FDISK
rmdir "$TDIR"
fi
else
echo "Could not determine the filesystem of the volume, output is: $(file -Ls $LODEV)"
echo_red "Could not determine the filesystem of the volume, output is: $(file -Ls $LODEV)"
fi
losetup -d $LODEV

trap - EXIT
echo "Resized parition $partition of $image to +$size MB"
echo_green "Resized partition $partition of $image to +$size MB"
}

function shrink_ext() {
Expand All @@ -337,7 +337,7 @@ function shrink_ext() {
partition=$2
size=$3

echo "Resizing file system to $size MB..."
echo_green "Resizing file system to $size MB..."
start=$(sfdisk --json "${image}" | jq ".partitiontable.partitions[] | select(.node == \"$image$partition\").start")
offset=$(($start*512))

Expand All @@ -351,14 +351,14 @@ function shrink_ext() {
e2ftarget_bytes=$(($size * 1024 * 1024))
e2ftarget_blocks=$(($e2ftarget_bytes / 512 + 1))

echo "Resizing file system to $e2ftarget_blocks blocks..."
echo_green "Resizing file system to $e2ftarget_blocks blocks..."
resize2fs $LODEV ${e2ftarget_blocks}s
losetup -d $LODEV
trap - EXIT

new_end=$(($start + $e2ftarget_blocks))

echo "Resizing partition to end at $start + $e2ftarget_blocks = $new_end blocks..."
echo_green "Resizing partition to end at $start + $e2ftarget_blocks = $new_end blocks..."
fdisk $image <<FDISK
p
d
Expand All @@ -373,11 +373,11 @@ w
FDISK

new_size=$((($new_end + 1) * 512))
echo "Truncating image to $new_size bytes..."
echo_green "Truncating image to $new_size bytes..."
truncate --size=$new_size $image
fdisk -l $image

echo "Resizing filesystem ..."
echo_green "Resizing filesystem ..."
detach_all_loopback $image
test_for_image $image
LODEV=$(losetup -f --show -o $offset $image)
Expand All @@ -394,7 +394,7 @@ function minimize_ext() {
partition=$2
buffer=$3

echo "Resizing partition $partition on $image to minimal size + $buffer MB"
echo_green "Resizing partition $partition on $image to minimal size + $buffer MB"
fdisk_output=$(sfdisk --json "${image_path}" )

start=$(jq ".partitiontable.partitions[] | select(.node == \"$image_path$partition\").start" <<< ${fdisk_output})
Expand Down Expand Up @@ -426,19 +426,19 @@ function minimize_ext() {
size_offset_mb=$(($e2fsize_mb - $e2ftarget_mb))


echo "Actual size is $e2fsize_mb MB ($e2fsize_blocks blocks), Minimum size is $e2fminsize_mb MB ($e2fminsize file system blocks, $e2fminsize_blocks blocks)"
echo "Resizing to $e2ftarget_mb MB ($e2ftarget_blocks blocks)"
echo_green "Actual size is $e2fsize_mb MB ($e2fsize_blocks blocks), Minimum size is $e2fminsize_mb MB ($e2fminsize file system blocks, $e2fminsize_blocks blocks)"
echo_green "Resizing to $e2ftarget_mb MB ($e2ftarget_blocks blocks)"

if [ $size_offset_mb -gt 0 ]; then
echo "Partition size is bigger then the desired size, shrinking"
echo_green "Partition size is bigger then the desired size, shrinking"
shrink_ext $image $partition $(($e2ftarget_mb - 1)) # -1 to compensat rounding mistakes
elif [ $size_offset_mb -lt 0 ]; then
echo "Partition size is lower then the desired size, enlarging"
echo_green "Partition size is lower then the desired size, enlarging"
enlarge_ext $image $partition $((-$size_offset_mb + 1)) # +1 to compensat rounding mistakes
fi

elif ( file -Ls $LODEV | grep -qi btrfs ); then
echo "WARNING: minimize_ext not implemented for btrfs"
echo_red "WARNING: minimize_ext not implemented for btrfs"
btrfs check --repair $LODEV
fi

Expand Down Expand Up @@ -517,7 +517,7 @@ function systemctl_if_exists() {
if hash systemctl 2>/dev/null; then
systemctl "$@"
else
echo "no systemctl, not running"
echo_red "no systemctl, not running"
fi
}

Expand Down
68 changes: 33 additions & 35 deletions src/config
Original file line number Diff line number Diff line change
@@ -1,79 +1,77 @@
CONFIG_DIR=$(dirname $(realpath -s $BASH_SOURCE))
CONFIG_DIR=$(dirname $(realpath -s "${BASH_SOURCE}"))

export BUILD_VARIANT=default
BUILD_FLAVOR=default
WORKSPACE_POSTFIX=

if [ "$#" -gt 0 ]; then
export BUILD_VARIANT=$1
fi
export BUILD_VARIANT=""
BUILD_VARIANT="$1"
: ${BUILD_VARIANT:=default}

export BUILD_FLAVOR=""
# Disable flavor system
# if [ "$#" -gt 1 ]; then
# BUILD_FLAVOR=$2
# fi
#BUILD_FLAVOR="$1"
: ${BUILD_FLAVOR:=default}

echo -e "--> Building VARIANT $BUILD_VARIANT, FLAVOR $BUILD_FLAVOR"

# Import the local config if we have one

if [ -f $CONFIG_DIR/config.local ]
if [ -f "${CONFIG_DIR}/config.local" ]
then
echo "Sourcing config.local..."
source $CONFIG_DIR/config.local
source "${CONFIG_DIR}/config.local"
fi

source ${DIST_PATH}/config

if [ $BUILD_VARIANT != 'default' ]; then
WORKSPACE_POSTFIX=-$BUILD_VARIANT
if [ "${BUILD_VARIANT}" != 'default' ]; then
WORKSPACE_POSTFIX="-${BUILD_VARIANT}"

if [ -d "${DIST_PATH}/variants/$BUILD_VARIANT" ]; then
export VARIANT_BASE="${DIST_PATH}/variants/$BUILD_VARIANT"
elif [ -d "${CUSTOM_PI_OS_PATH}/variants/$BUILD_VARIANT" ]; then
export VARIANT_BASE="${CUSTOM_PI_OS_PATH}/variants/$BUILD_VARIANT"
if [ -d "${DIST_PATH}/variants/${BUILD_VARIANT}" ]; then
export VARIANT_BASE="${DIST_PATH}/variants/${BUILD_VARIANT}"
elif [ -d "${CUSTOM_PI_OS_PATH}/variants/${BUILD_VARIANT}" ]; then
export VARIANT_BASE="${CUSTOM_PI_OS_PATH}/variants/${BUILD_VARIANT}"
else
die "Could not find Variant $BUILD_VARIANT"
die "Could not find Variant ${BUILD_VARIANT}"
fi

if [ $BUILD_FLAVOR == '' ] || [ $BUILD_FLAVOR == 'default' ]
if [ "${BUILD_FLAVOR}" = '' ] || [ "${BUILD_FLAVOR}" = 'default' ]
then
VARIANT_CONFIG=$VARIANT_BASE/config
VARIANT_CONFIG=${VARIANT_BASE}/config
FLAVOR_CONFIG=
else
VARIANT_CONFIG=$VARIANT_BASE/config
FLAVOR_CONFIG=$VARIANT_BASE/config.$BUILD_FLAVOR
VARIANT_CONFIG=${VARIANT_BASE}/config
FLAVOR_CONFIG=${VARIANT_BASE}/config.${BUILD_FLAVOR}
fi

if [ -n "$FLAVOR_CONFIG" ] && [ ! -f $FLAVOR_CONFIG ]
if [ -n "${FLAVOR_CONFIG}" ] && [ ! -f "${FLAVOR_CONFIG}" ]
then
die "Could not find config file $FLAVOR_CONFIG"
die "Could not find config file ${FLAVOR_CONFIG}"
fi
fi

echo Import the variant config if we have one
if [ -n "$VARIANT_CONFIG" ] && [ -f $VARIANT_CONFIG ]
if [ -n "${VARIANT_CONFIG}" ] && [ -f "${VARIANT_CONFIG}" ]
then
echo "Sourcing variant config $VARIANT_CONFIG..."
echo "Sourcing variant config ${VARIANT_CONFIG}..."
set -a
source $VARIANT_CONFIG
source "${VARIANT_CONFIG}"
set +a
fi

# Import the flavor config if we have one

if [ -n "$FLAVOR_CONFIG" ] && [ -f $FLAVOR_CONFIG ]
if [ -n "${FLAVOR_CONFIG}" ] && [ -f "${FLAVOR_CONFIG}" ]
then
echo "Sourcing flavor config $FLAVOR_CONFIG..."
source $FLAVOR_CONFIG
echo "Sourcing flavor config ${FLAVOR_CONFIG}..."
source "${FLAVOR_CONFIG}"
fi



if [ -f ${DIST_PATH}/config.local ]
if [ -f "${DIST_PATH}/config.local" ]
then
echo "Sourcing distro config.local..."
source ${DIST_PATH}/config.local
source "${DIST_PATH}/config.local"
fi

# Get only a list
Expand All @@ -89,9 +87,9 @@ do
export MODULE_PATH="${CUSTOM_PI_OS_PATH}/modules/${module}"
fi

echo loading $module config at ${MODULE_PATH}/config
if [ -f ${MODULE_PATH}/config ]; then
source ${MODULE_PATH}/config
echo "loading $module config at ${MODULE_PATH}/config"
if [ -f "${MODULE_PATH}/config" ]; then
source "${MODULE_PATH}/config"
else
echo "WARNING: module ${module} has no config file"
fi
Expand Down
15 changes: 14 additions & 1 deletion src/custompios
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ export LC_ALL=C

source ${CUSTOM_PI_OS_PATH}/common.sh

echo_green -e "\nBUILD STARTED @ $(date)!\n"

function execute_chroot_script() {

# In docker, these extra commands are required to enable this black-magic
Expand Down Expand Up @@ -92,6 +94,17 @@ function execute_chroot_script() {
fi
}

# check prerequisites
if [ -n "$BASE_IMAGE_ENLARGEROOT" ] || [ -n "$BASE_IMAGE_RESIZEROOT" ]; then
# resizing the root partition requires 'sfdisk' in our path
which sfdisk >/dev/null 2>/dev/null || \
die "'sfdisk' not found in PATH; did you mean to run the script as root?"
fi


# start!


mkdir -p $BASE_WORKSPACE
mkdir -p $BASE_MOUNT_PATH

Expand Down Expand Up @@ -200,4 +213,4 @@ pushd $BASE_WORKSPACE
fi
popd

echo_green -e "\nBUILD SUCCEEDED!\n"
echo_green -e "\nBUILD SUCCEEDED @ $(date)!\n"
2 changes: 2 additions & 0 deletions src/dist_generators/dist_example/src/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
/workspace
/config.local
3 changes: 3 additions & 0 deletions src/dist_generators/dist_example/src/image/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
*
!README
!.gitignore
Loading