first commit
This commit is contained in:
105
external/packages/raspi/scripts/common
vendored
Normal file
105
external/packages/raspi/scripts/common
vendored
Normal file
@@ -0,0 +1,105 @@
|
||||
log (){
|
||||
date +"[%T] $*" | tee -a "${LOG_FILE}"
|
||||
}
|
||||
export -f log
|
||||
|
||||
bootstrap(){
|
||||
local BOOTSTRAP_CMD=debootstrap
|
||||
local BOOTSTRAP_ARGS=()
|
||||
|
||||
export http_proxy=${APT_PROXY}
|
||||
|
||||
BOOTSTRAP_ARGS+=(--arch arm64)
|
||||
BOOTSTRAP_ARGS+=(--include gnupg)
|
||||
BOOTSTRAP_ARGS+=(--components "main,contrib,non-free")
|
||||
#BOOTSTRAP_ARGS+=(--keyring "${STAGE_DIR}/files/raspberrypi.gpg")
|
||||
BOOTSTRAP_ARGS+=(--exclude=info)
|
||||
BOOTSTRAP_ARGS+=(--include=ca-certificates)
|
||||
BOOTSTRAP_ARGS+=("$@")
|
||||
printf -v BOOTSTRAP_STR '%q ' "${BOOTSTRAP_ARGS[@]}"
|
||||
|
||||
capsh $CAPSH_ARG -- -c "'${BOOTSTRAP_CMD}' $BOOTSTRAP_STR" || true
|
||||
|
||||
if [ -d "$2/debootstrap" ] && ! rmdir "$2/debootstrap"; then
|
||||
cp "$2/debootstrap/debootstrap.log" "${STAGE_WORK_DIR}"
|
||||
log "bootstrap failed: please check ${STAGE_WORK_DIR}/debootstrap.log"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
export -f bootstrap
|
||||
|
||||
copy_previous(){
|
||||
if [ ! -d "${PREV_ROOTFS_DIR}" ]; then
|
||||
echo "Previous stage rootfs not found"
|
||||
false
|
||||
fi
|
||||
mkdir -p "${ROOTFS_DIR}"
|
||||
rsync -aHAXx --exclude var/cache/apt/archives "${PREV_ROOTFS_DIR}/" "${ROOTFS_DIR}/"
|
||||
}
|
||||
export -f copy_previous
|
||||
|
||||
unmount(){
|
||||
if [ -z "$1" ]; then
|
||||
DIR=$PWD
|
||||
else
|
||||
DIR=$1
|
||||
fi
|
||||
|
||||
while mount | grep -q "$DIR"; do
|
||||
local LOCS
|
||||
LOCS=$(mount | grep "$DIR" | cut -f 3 -d ' ' | sort -r)
|
||||
for loc in $LOCS; do
|
||||
umount "$loc"
|
||||
done
|
||||
done
|
||||
}
|
||||
export -f unmount
|
||||
|
||||
unmount_image(){
|
||||
sync
|
||||
sleep 1
|
||||
LOOP_DEVICE=$(losetup --list | grep "$1" | cut -f1 -d' ')
|
||||
if [ -n "$LOOP_DEVICE" ]; then
|
||||
for part in "$LOOP_DEVICE"p*; do
|
||||
if DIR=$(findmnt -n -o target -S "$part"); then
|
||||
unmount "$DIR"
|
||||
fi
|
||||
done
|
||||
losetup -d "$LOOP_DEVICE"
|
||||
fi
|
||||
}
|
||||
export -f unmount_image
|
||||
|
||||
on_chroot() {
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/proc)"; then
|
||||
mount -t proc proc "${ROOTFS_DIR}/proc"
|
||||
fi
|
||||
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev)"; then
|
||||
mount --bind /dev "${ROOTFS_DIR}/dev"
|
||||
fi
|
||||
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/dev/pts)"; then
|
||||
mount --bind /dev/pts "${ROOTFS_DIR}/dev/pts"
|
||||
fi
|
||||
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/sys)"; then
|
||||
mount --bind /sys "${ROOTFS_DIR}/sys"
|
||||
fi
|
||||
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/run)"; then
|
||||
mount -t tmpfs tmpfs "${ROOTFS_DIR}/run"
|
||||
fi
|
||||
|
||||
if ! mount | grep -q "$(realpath "${ROOTFS_DIR}"/tmp)"; then
|
||||
mount -t tmpfs tmpfs "${ROOTFS_DIR}/tmp"
|
||||
fi
|
||||
|
||||
capsh $CAPSH_ARG "--chroot=${ROOTFS_DIR}/" -- -e "$@"
|
||||
}
|
||||
export -f on_chroot
|
||||
|
||||
update_issue() {
|
||||
echo -e "Raspberry Pi reference ${IMG_DATE}\nGenerated using ${PI_GEN}, ${PI_GEN_REPO}, ${GIT_HASH}, ${1}" > "${ROOTFS_DIR}/etc/rpi-issue"
|
||||
}
|
||||
export -f update_issue
|
||||
53
external/packages/raspi/scripts/dependencies_check
vendored
Normal file
53
external/packages/raspi/scripts/dependencies_check
vendored
Normal file
@@ -0,0 +1,53 @@
|
||||
# dependencies_check
|
||||
# $@ Dependency files to check
|
||||
#
|
||||
# Each dependency is in the form of a tool to test for, optionally followed by
|
||||
# a : and the name of a package if the package on a Debian-ish system is not
|
||||
# named for the tool (i.e., qemu-user-static).
|
||||
dependencies_check()
|
||||
{
|
||||
local depfile deps missing
|
||||
|
||||
for depfile in "$@"; do
|
||||
if [[ -e "$depfile" ]]; then
|
||||
deps="$(sed -f "${SCRIPT_DIR}/remove-comments.sed" < "${BASE_DIR}/depends")"
|
||||
|
||||
fi
|
||||
for dep in $deps; do
|
||||
if ! hash "${dep%:*}" 2>/dev/null; then
|
||||
missing="${missing:+$missing }${dep#*:}"
|
||||
fi
|
||||
done
|
||||
done
|
||||
|
||||
if [[ "$missing" ]]; then
|
||||
echo "Required dependencies not installed"
|
||||
echo
|
||||
echo "This can be resolved on Debian/Raspbian systems by installing:"
|
||||
echo "$missing"
|
||||
false
|
||||
fi
|
||||
|
||||
# If we're building on a native arm platform, we don't need to check for
|
||||
# binfmt_misc or require it to be loaded.
|
||||
|
||||
binfmt_misc_required=1
|
||||
|
||||
case $(uname -m) in
|
||||
aarch64)
|
||||
binfmt_misc_required=0
|
||||
;;
|
||||
arm*)
|
||||
binfmt_misc_required=0
|
||||
;;
|
||||
esac
|
||||
|
||||
if [[ "${binfmt_misc_required}" == "1" ]]; then
|
||||
if ! grep -q "/proc/sys/fs/binfmt_misc" /proc/mounts; then
|
||||
echo "Module binfmt_misc not loaded in host"
|
||||
echo "Please run:"
|
||||
echo " sudo modprobe binfmt_misc"
|
||||
exit 1
|
||||
fi
|
||||
fi
|
||||
}
|
||||
256
external/packages/raspi/scripts/qcow2_handling
vendored
Normal file
256
external/packages/raspi/scripts/qcow2_handling
vendored
Normal file
@@ -0,0 +1,256 @@
|
||||
#!/bin/bash
|
||||
|
||||
# QCOW2 Routines
|
||||
|
||||
export CURRENT_IMAGE
|
||||
export CURRENT_MOUNTPOINT
|
||||
|
||||
export NBD_DEV
|
||||
export MAP_BOOT_DEV
|
||||
export MAP_ROOT_DEV
|
||||
|
||||
# set in build.sh
|
||||
# should be fairly enough for the beginning
|
||||
# overwrite here by uncommenting following lines
|
||||
# BASE_QCOW2_SIZE=12G
|
||||
|
||||
# find and initialize free block device nodes
|
||||
init_nbd() {
|
||||
modprobe nbd max_part=16
|
||||
if [ -z "${NBD_DEV}" ]; then
|
||||
for x in /sys/class/block/nbd* ; do
|
||||
S=`cat $x/size`
|
||||
if [ "$S" == "0" ] ; then
|
||||
NBD_DEV=/dev/$(basename $x)
|
||||
MAP_BOOT_DEV=/dev/mapper/$(basename $x)p1
|
||||
MAP_ROOT_DEV=/dev/mapper/$(basename $x)p2
|
||||
break
|
||||
fi
|
||||
done
|
||||
fi
|
||||
}
|
||||
export -f init_nbd
|
||||
|
||||
# connect image to block device
|
||||
connect_blkdev() {
|
||||
init_nbd
|
||||
qemu-nbd --discard=unmap -c $NBD_DEV "$1"
|
||||
sync
|
||||
kpartx -as $NBD_DEV
|
||||
sync
|
||||
CURRENT_IMAGE="$1"
|
||||
}
|
||||
export -f connect_blkdev
|
||||
|
||||
# disconnect image from block device
|
||||
disconnect_blkdev() {
|
||||
kpartx -d $NBD_DEV
|
||||
qemu-nbd -d $NBD_DEV
|
||||
NBD_DEV=
|
||||
MAP_BOOT_DEV=
|
||||
MAP_ROOT_DEV=
|
||||
CURRENT_IMAGE=
|
||||
}
|
||||
export -f disconnect_blkdev
|
||||
|
||||
# mount qcow2 image: mount_image <image file> <mountpoint>
|
||||
mount_qimage() {
|
||||
connect_blkdev "$1"
|
||||
mount -v -t ext4 $MAP_ROOT_DEV "$2"
|
||||
mkdir -p "${ROOTFS_DIR}/boot"
|
||||
mount -v -t vfat $MAP_BOOT_DEV "$2/boot"
|
||||
CURRENT_MOUNTPOINT="$2"
|
||||
}
|
||||
export -f mount_qimage
|
||||
|
||||
# umount qcow2 image: umount_image <current mountpoint>
|
||||
umount_qimage() {
|
||||
sync
|
||||
#umount "$1/boot"
|
||||
while mount | grep -q "$1"; do
|
||||
local LOCS
|
||||
LOCS=$(mount | grep "$1" | cut -f 3 -d ' ' | sort -r)
|
||||
for loc in $LOCS; do
|
||||
echo "$loc"
|
||||
while mountpoint -q "$loc" && ! umount "$loc"; do
|
||||
sleep 0.1
|
||||
done
|
||||
done
|
||||
done
|
||||
CURRENT_MOUNTPOINT=
|
||||
disconnect_blkdev
|
||||
}
|
||||
export -f umount_qimage
|
||||
|
||||
# create base image / backing image / mount image
|
||||
load_qimage() {
|
||||
if [ -z "${CURRENT_MOUNTPOINT}" ]; then
|
||||
if [ ! -d "${ROOTFS_DIR}" ]; then
|
||||
mkdir -p "${ROOTFS_DIR}";
|
||||
fi
|
||||
|
||||
if [ "${CLEAN}" = "1" ] && [ -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then
|
||||
rm -f "${WORK_DIR}/image-${STAGE}.qcow2";
|
||||
fi
|
||||
|
||||
if [ ! -f "${WORK_DIR}/image-${STAGE}.qcow2" ]; then
|
||||
pushd ${WORK_DIR} > /dev/null
|
||||
init_nbd
|
||||
if [ -z "${PREV_STAGE}" ]; then
|
||||
echo "Creating base image: image-${STAGE}.qcow2"
|
||||
# -o preallocation=falloc
|
||||
qemu-img create -f qcow2 image-${STAGE}.qcow2 $BASE_QCOW2_SIZE
|
||||
sync
|
||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
||||
sync
|
||||
sfdisk $NBD_DEV << EOF
|
||||
4MiB,250MiB,c,*
|
||||
254MiB,,83;
|
||||
EOF
|
||||
sync
|
||||
kpartx -as $NBD_DEV
|
||||
mkdosfs -n boot -F 32 -s 4 -v $MAP_BOOT_DEV
|
||||
mkfs.ext4 -L rootfs -O "^huge_file,^64bit" $MAP_ROOT_DEV
|
||||
sync
|
||||
else
|
||||
if [ ! -f "${WORK_DIR}/image-${PREV_STAGE}.qcow2" ]; then
|
||||
exit 1;
|
||||
fi
|
||||
echo "Creating backing image: image-${STAGE}.qcow2 <- ${WORK_DIR}/image-${PREV_STAGE}.qcow2"
|
||||
qemu-img create -f qcow2 \
|
||||
-o backing_file=${WORK_DIR}/image-${PREV_STAGE}.qcow2 \
|
||||
${WORK_DIR}/image-${STAGE}.qcow2
|
||||
sync
|
||||
qemu-nbd --discard=unmap -c $NBD_DEV image-${STAGE}.qcow2
|
||||
sync
|
||||
kpartx -as $NBD_DEV
|
||||
fi
|
||||
|
||||
mount -v -t ext4 $MAP_ROOT_DEV "${ROOTFS_DIR}"
|
||||
mkdir -p "${ROOTFS_DIR}/boot"
|
||||
mount -v -t vfat $MAP_BOOT_DEV "${ROOTFS_DIR}/boot"
|
||||
CURRENT_IMAGE=${WORK_DIR}/image-${STAGE}.qcow2
|
||||
CURRENT_MOUNTPOINT=${ROOTFS_DIR}
|
||||
popd > /dev/null
|
||||
else
|
||||
mount_qimage "${WORK_DIR}/image-${STAGE}.qcow2" "${ROOTFS_DIR}"
|
||||
fi
|
||||
echo "Current image in use: ${CURRENT_IMAGE} (MP: ${CURRENT_MOUNTPOINT})"
|
||||
fi
|
||||
}
|
||||
export -f load_qimage
|
||||
|
||||
# umount current image and refresh mount point env var
|
||||
unload_qimage() {
|
||||
if [ ! -z "${CURRENT_MOUNTPOINT}" ]; then
|
||||
fstrim -v "${CURRENT_MOUNTPOINT}" || true
|
||||
umount_qimage "${CURRENT_MOUNTPOINT}"
|
||||
fi
|
||||
}
|
||||
export -f unload_qimage
|
||||
|
||||
# based on: https://github.com/SirLagz/RaspberryPi-ImgAutoSizer
|
||||
# helper function for make_bootable_image, do not call directly
|
||||
function resize_qcow2() {
|
||||
if [ -z "$CALL_FROM_MBI" ]; then
|
||||
echo "resize_qcow2: cannot be called directly, use make_bootable_image instead"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# ROOT_MARGIN=$((800*1024*1024))
|
||||
ROOT_MARGIN=$((1*1024*1024))
|
||||
PARTED_OUT=`parted -s -m "$NBD_DEV" unit B print`
|
||||
PART_NO=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print $1 } '`
|
||||
PART_START=`echo "$PARTED_OUT" | grep ext4 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
||||
|
||||
e2fsck -y -f $MAP_ROOT_DEV || true
|
||||
|
||||
DATA_SIZE=`resize2fs -P $MAP_ROOT_DEV | awk -F': ' ' { print $2 } '`
|
||||
BLOCK_SIZE=$(dumpe2fs -h $MAP_ROOT_DEV | grep 'Block size' | awk -F': ' ' { print $2 }')
|
||||
BLOCK_SIZE=${BLOCK_SIZE// /}
|
||||
|
||||
let DATA_SIZE=$DATA_SIZE+$ROOT_MARGIN/$BLOCK_SIZE
|
||||
resize2fs -p $MAP_ROOT_DEV $DATA_SIZE
|
||||
sleep 1
|
||||
|
||||
let PART_NEW_SIZE=$DATA_SIZE*$BLOCK_SIZE
|
||||
let PART_NEW_END=$PART_START+$PART_NEW_SIZE
|
||||
ACT1=`parted -s "$NBD_DEV" rm 2`
|
||||
ACT2=`parted -s "$NBD_DEV" unit B mkpart primary $PART_START $PART_NEW_END`
|
||||
NEW_IMG_SIZE=`parted -s -m "$NBD_DEV" unit B print free | tail -1 | awk -F: ' { print substr($2,1,length($2)-1) } '`
|
||||
}
|
||||
export -f resize_qcow2
|
||||
|
||||
# create raw img from qcow2: make_bootable_image <in.qcow2> <out.img>
|
||||
function make_bootable_image() {
|
||||
|
||||
EXPORT_QCOW2="$1"
|
||||
EXPORT_IMAGE="$2"
|
||||
|
||||
echo "Connect block device to source qcow2"
|
||||
connect_blkdev "${EXPORT_QCOW2}"
|
||||
|
||||
echo "Resize fs and partition"
|
||||
CALL_FROM_MBI=1
|
||||
resize_qcow2
|
||||
sync
|
||||
CALL_FROM_MBI=
|
||||
|
||||
echo "Disconnect block device"
|
||||
disconnect_blkdev
|
||||
|
||||
if [ -z "$NEW_IMG_SIZE" ]; then
|
||||
echo "NEW_IMG_SIZE could not be calculated, cannot process image. Exit."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Shrinking qcow2 image"
|
||||
qemu-img resize --shrink "${EXPORT_QCOW2}" $NEW_IMG_SIZE
|
||||
sync
|
||||
|
||||
echo "Convert qcow2 to raw image"
|
||||
qemu-img convert -f qcow2 -O raw "${EXPORT_QCOW2}" "${EXPORT_IMAGE}"
|
||||
sync
|
||||
|
||||
echo "Get PARTUUIDs from image"
|
||||
IMGID="$(blkid -o value -s PTUUID "${EXPORT_IMAGE}")"
|
||||
|
||||
BOOT_PARTUUID="${IMGID}-01"
|
||||
echo "Boot: $BOOT_PARTUUID"
|
||||
ROOT_PARTUUID="${IMGID}-02"
|
||||
echo "Root: $ROOT_PARTUUID"
|
||||
|
||||
echo "Mount image"
|
||||
MOUNTROOT=${WORK_DIR}/tmpimage
|
||||
mkdir -p $MOUNTROOT
|
||||
|
||||
MOUNTPT=$MOUNTROOT
|
||||
PARTITION=2
|
||||
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
||||
|
||||
MOUNTPT=$MOUNTROOT/boot
|
||||
PARTITION=1
|
||||
mount "${EXPORT_IMAGE}" "$MOUNTPT" -o loop,offset=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*start=[ ]*//' | sed 's/,.*//'` * 512 ],sizelimit=$[ `/sbin/sfdisk -d "${EXPORT_IMAGE}" | grep "start=" | head -n $PARTITION | tail -n1 | sed 's/.*size=[ ]*//' | sed 's/,.*//'` * 512 ] || exit 1
|
||||
|
||||
if [ ! -d "${MOUNTROOT}/root" ]; then
|
||||
echo "Image damaged or not mounted. Exit."
|
||||
exit 1
|
||||
fi
|
||||
|
||||
echo "Setup PARTUUIDs"
|
||||
if [ ! -z "$BOOT_PARTUUID" ] && [ ! -z "$ROOT_PARTUUID" ]; then
|
||||
echo "Set UUIDs to make it bootable"
|
||||
sed -i "s/BOOTDEV/PARTUUID=${BOOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/etc/fstab"
|
||||
sed -i "s/ROOTDEV/PARTUUID=${ROOT_PARTUUID}/" "${MOUNTROOT}/boot/cmdline.txt"
|
||||
fi
|
||||
|
||||
echo "Umount image"
|
||||
sync
|
||||
umount "${MOUNTROOT}/boot" || exit 1
|
||||
umount "${MOUNTROOT}" || exit 1
|
||||
|
||||
echo "Remove qcow2 export image"
|
||||
rm -f "${EXPORT_QCOW2}"
|
||||
}
|
||||
export -f make_bootable_image
|
||||
11
external/packages/raspi/scripts/remove-comments.sed
vendored
Normal file
11
external/packages/raspi/scripts/remove-comments.sed
vendored
Normal file
@@ -0,0 +1,11 @@
|
||||
# Deletes comments and collapses whitespace in ##-packages files
|
||||
|
||||
# Append (N)ext line to buffer
|
||||
# if (!)not ($)buffer is EOF, (b)ranch to (:)label loop
|
||||
:loop
|
||||
N
|
||||
$ !b loop
|
||||
|
||||
# Buffer is "line1\nline2\n...lineN", del comments and collapse whitespace
|
||||
s/#[^\n]*//g
|
||||
s/[[:space:]]\{1,\}/ /g
|
||||
Reference in New Issue
Block a user