git: 731704f5ea2f - main - bsdinstall: Fix installation script splitting
- Go to: [ bottom of page ] [ top of archives ] [ this month ]
Date: Tue, 09 Jan 2024 18:17:52 UTC
The branch main has been updated by grembo: URL: https://cgit.FreeBSD.org/src/commit/?id=731704f5ea2f6f9d7e3c4b5ed2ad1a3cba703f42 commit 731704f5ea2f6f9d7e3c4b5ed2ad1a3cba703f42 Author: Michael Gmelin <grembo@FreeBSD.org> AuthorDate: 2024-01-06 16:55:31 +0000 Commit: Michael Gmelin <grembo@FreeBSD.org> CommitDate: 2024-01-09 18:14:17 +0000 bsdinstall: Fix installation script splitting This allows writing setup scripts that contain lines starting with "#!", e.g., a shebang when creating a shell script using cat: #!/bin/sh echo "Populate rc.local" cat >/etc/rc.local<<EOF #!/bin/sh echo booted | logger -s -t 'example' EOF Prevent accidentally running a setup script left behind by a previous invocation of bsdinstall. Reviewed by: imp, jrtc27 Differential Revision: https://reviews.freebsd.org/D43350 --- usr.sbin/bsdinstall/scripts/jail | 18 ++++++++++++------ usr.sbin/bsdinstall/scripts/script | 14 ++++++++++---- 2 files changed, 22 insertions(+), 10 deletions(-) diff --git a/usr.sbin/bsdinstall/scripts/jail b/usr.sbin/bsdinstall/scripts/jail index de6dee04b891..9acea20a34d8 100755 --- a/usr.sbin/bsdinstall/scripts/jail +++ b/usr.sbin/bsdinstall/scripts/jail @@ -61,11 +61,16 @@ export BSDINSTALL_CHROOT=$1 rm -rf $BSDINSTALL_TMPETC mkdir $BSDINSTALL_TMPETC mkdir -p $1 || error "mkdir failed for $1" +rm -f $TMPDIR/bsdinstall-installscript-setup -if [ -n "$SCRIPT" ] -then - split -a 2 -p '^#!.*' "$SCRIPT" $TMPDIR/bsdinstall-installscript- - . $TMPDIR/bsdinstall-installscript-aa +if [ -n "$SCRIPT" ]; then + # split script into preamble and setup script at first shebang + awk 'BEGIN {pathb=ARGV[2]; ARGV[2]=""} /^#!/{b=1} { + if (b) print >pathb; else print}' \ + "$SCRIPT" $TMPDIR/bsdinstall-installscript-setup \ + >$TMPDIR/bsdinstall-installscript-preamble + + . $TMPDIR/bsdinstall-installscript-preamble fi test ! -d $BSDINSTALL_DISTDIR && mkdir -p $BSDINSTALL_DISTDIR @@ -146,8 +151,9 @@ cp /etc/localtime $1/etc cp /var/db/zoneinfo $1/var/db # Run post-install script -if [ -f $TMPDIR/bsdinstall-installscript-ab ]; then - cp $TMPDIR/bsdinstall-installscript-ab $BSDINSTALL_CHROOT/tmp/installscript +if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then + cp $TMPDIR/bsdinstall-installscript-setup \ + $BSDINSTALL_CHROOT/tmp/installscript chmod a+x $BSDINSTALL_CHROOT/tmp/installscript mount -t devfs devfs "$BSDINSTALL_CHROOT/dev" chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1 diff --git a/usr.sbin/bsdinstall/scripts/script b/usr.sbin/bsdinstall/scripts/script index ae1c6b3011fa..00ded5f8e24d 100755 --- a/usr.sbin/bsdinstall/scripts/script +++ b/usr.sbin/bsdinstall/scripts/script @@ -88,10 +88,15 @@ shift f_dprintf "Began Installation at %s" "$( date )" rm -rf $BSDINSTALL_TMPETC mkdir $BSDINSTALL_TMPETC +rm -f $TMPDIR/bsdinstall-installscript-setup -split -a 2 -p '^#!.*' "$SCRIPT" $TMPDIR/bsdinstall-installscript- +# split script into preamble and setup script at first shebang +awk 'BEGIN {pathb=ARGV[2]; ARGV[2]=""} /^#!/{b=1} { + if (b) print >pathb; else print}' \ + "$SCRIPT" $TMPDIR/bsdinstall-installscript-setup \ + >$TMPDIR/bsdinstall-installscript-preamble -. $TMPDIR/bsdinstall-installscript-aa +. $TMPDIR/bsdinstall-installscript-preamble : ${DISTRIBUTIONS="kernel.txz base.txz"}; export DISTRIBUTIONS export BSDINSTALL_DISTDIR @@ -158,8 +163,9 @@ if [ ! -f $BSDINSTALL_CHROOT/etc/resolv.conf -a -f /etc/resolv.conf ]; then fi # Run post-install script -if [ -f $TMPDIR/bsdinstall-installscript-ab ]; then - cp $TMPDIR/bsdinstall-installscript-ab $BSDINSTALL_CHROOT/tmp/installscript +if [ -f $TMPDIR/bsdinstall-installscript-setup ]; then + cp $TMPDIR/bsdinstall-installscript-setup \ + $BSDINSTALL_CHROOT/tmp/installscript chmod a+x $BSDINSTALL_CHROOT/tmp/installscript chroot $BSDINSTALL_CHROOT /tmp/installscript $@ 2>&1 rm $BSDINSTALL_CHROOT/tmp/installscript