Skip to content

Instantly share code, notes, and snippets.

@awsong
Created November 4, 2024 07:58
Show Gist options
  • Select an option

  • Save awsong/bf8e054105899cb30a09b2151f722935 to your computer and use it in GitHub Desktop.

Select an option

Save awsong/bf8e054105899cb30a09b2151f722935 to your computer and use it in GitHub Desktop.
initramfs NBD boot script
# NBD filesystem mounting -*- shell-script -*-
# shellcheck shell=sh
# FIXME This needs error checking
squashfs_top()
{
if [ "${squashfs_top_used}" != "yes" ]; then
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/squashfs-top"
run_scripts /scripts/squashfs-top
[ "$quiet" != "y" ] && log_end_msg
fi
squashfs_top_used=yes
}
squashfs_premount()
{
if [ "${squashfs_premount_used}" != "yes" ]; then
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/squashfs-premount"
run_scripts /scripts/squashfs-premount
[ "$quiet" != "y" ] && log_end_msg
fi
squashfs_premount_used=yes
}
squashfs_bottom()
{
if [ "${squashfs_premount_used}" = "yes" ] || [ "${squashfs_top_used}" = "yes" ]; then
[ "${quiet?}" != "y" ] && log_begin_msg "Running /scripts/squashfs-bottom"
run_scripts /scripts/squashfs-bottom
[ "$quiet" != "y" ] && log_end_msg
fi
mkdir -p ${rootmnt}/squash/ro
mkdir -p ${rootmnt}/squash/rw
mount -o move /squash/ro ${rootmnt}/squash/ro
mount -o move /squash/rw ${rootmnt}/squash/rw
squashfs_premount_used=no
squashfs_top_used=no
}
# parse squashfs bootargs and mount squashfs
squashfs_mount_root_impl()
{
configure_networking
modprobe nbd
mkdir /squash
cd /squash
mkdir ro rw
nbd-client $nbd_server /dev/nbd0 -N ${ROOT}.squashfs
nbd-client $nbd_server /dev/nbd1 -N ${ROOT}.overlay
mount /dev/nbd0 ro
mount /dev/nbd1 rw
mount -t overlay -o lowerdir=ro,upperdir=rw/upper,workdir=rw/work none ${rootmnt}
}
# NBD root mounting
squashfs_mount_root()
{
squashfs_top
# For DHCP
/sbin/modprobe af_packet
wait_for_udev 10
# Default delay is around 180s
delay=${ROOTDELAY:-180}
# loop until squashfsmount succeeds
squashfs_mount_root_impl
ret=$?
squashfs_retry_count=0
while [ ${squashfs_retry_count} -lt "${delay}" ] \
&& [ $ret -ne 0 ] ; do
[ "$quiet" != "y" ] && log_begin_msg "Retrying squashfs mount"
sleep 1
squashfs_mount_root_impl
ret=$?
squashfs_retry_count=$(( squashfs_retry_count + 1 ))
[ "$quiet" != "y" ] && log_end_msg
done
}
mountroot()
{
squashfs_mount_root
}
mount_top()
{
# Note, also called directly in case it's overridden.
squashfs_top
}
mount_premount()
{
# Note, also called directly in case it's overridden.
squashfs_premount
}
mount_bottom()
{
# Note, also called directly in case it's overridden.
squashfs_bottom
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment