Skip to content

Instantly share code, notes, and snippets.

@Kralian
Created July 31, 2020 13:52
Show Gist options
  • Save Kralian/88819c847d94a3ccb19d18d81926a959 to your computer and use it in GitHub Desktop.
Save Kralian/88819c847d94a3ccb19d18d81926a959 to your computer and use it in GitHub Desktop.
Small rc.d script for freebsd to shift / onto md_config created device and 'reboot -r' to get the kernel to restart on that drive
#!/bin/sh
#
# $FreeBSD reboot2md$
#
# BEFORE: root
# REQUIRE: fsck
# KEYWORD: nojail
# example for rc.conf
#
# reboot2md_enable="YES" # enable script to run
# reboot2md_mdparam="-s 4g" # create a 4gb memory disk
# reboot2md_fixfstab="YES" # manipulate fstab
# reboot2md_oldroot="diskroot" # set '/' mount to '/diskroot' instead of the default
. /etc/rc.subr
name="reboot2md"
desc="Move root to md device and reboot2md from there"
rcvar="reboot2md_enable reboot2md_oldroot reboot2md_size reboot2md_fixfstab"
start_cmd="reboot2md_start"
stop_cmd=sync
: ${reboot2md_semaphore:="etc/.REBOOTED2MD"}
: ${reboot2md_oldroot:="oldroot"}
: ${reboot2md_mdparam:="-s 4g"}
: ${reboot2md_fixfstab:="YES"}
reboot2md_start()
{
if [ ! -r /${reboot2md_semaphore} ]; then
echo -n "Rerooting: md"
md=$(/sbin/mdconfig ${reboot2md_mdparam})
echo -n " is $md"
echo -n ", fsbuild"
/sbin/newfs /dev/$md 2>/dev/null >/dev/null
/sbin/mount /dev/$md /mnt 2>/dev/null >/dev/null
echo ", file copy"
mkdir -p /mnt/tmp
chmod 1777 /mnt/tmp
env TMPDIR=/mnt/tmp /bin/pax -Xrw -pe / /mnt 2>/dev/null >/dev/null
if checkyesno reboot2md_fixfstab; then
mkdir -p /mnt/${reboot2md_oldroot}
echo ", fstab fixups"
/bin/cat /etc/fstab|/rescue/sed -e "s/^\([^[:space:]]*\)[[:space:]]*[[:space:]]\(\/\)[[:space:]][[:space:]]*\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)[[:space:]]*\([^[:space:]]*\)/\1 \2${reboot2md_oldroot} \3 noauto,\4 \5 \6/g">/mnt/etc/fstab
echo "/dev/${md} / ufs rw 0 0">>/mnt/etc/fstab
fi
echo ", semaphore"
touch /mnt/${reboot2md_semaphore}
echo -n ", reboot2md"
if [ -r /mnt/${reboot2md_semaphore} -a -r /mnt/boot/kernel/kernel ]; then
echo " now"
kenv vfs.root.mountfrom=ufs:/dev/$md
reboot -r
else
echo " aborted"
fi
fi
}
load_rc_config $name
run_rc_command "$1"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment