From owner-dev-commits-src-branches@freebsd.org Wed Jul 21 16:14:09 2021 Return-Path: Delivered-To: dev-commits-src-branches@mailman.nyi.freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2610:1c1:1:606c::19:1]) by mailman.nyi.freebsd.org (Postfix) with ESMTP id C22DD666719; Wed, 21 Jul 2021 16:14:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from mxrelay.nyi.freebsd.org (mxrelay.nyi.freebsd.org [IPv6:2610:1c1:1:606c::19:3]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256 client-signature RSA-PSS (4096 bits) client-digest SHA256) (Client CN "mxrelay.nyi.freebsd.org", Issuer "R3" (verified OK)) by mx1.freebsd.org (Postfix) with ESMTPS id 4GVLHj1hCLz3Lwq; Wed, 21 Jul 2021 16:14:09 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org (gitrepo.freebsd.org [IPv6:2610:1c1:1:6068::e6a:5]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange X25519 server-signature RSA-PSS (4096 bits) server-digest SHA256) (Client did not present a certificate) by mxrelay.nyi.freebsd.org (Postfix) with ESMTPS id E8F8B20AED; Wed, 21 Jul 2021 16:14:08 +0000 (UTC) (envelope-from git@FreeBSD.org) Received: from gitrepo.freebsd.org ([127.0.1.44]) by gitrepo.freebsd.org (8.16.1/8.16.1) with ESMTP id 16LGE82G080484; Wed, 21 Jul 2021 16:14:08 GMT (envelope-from git@gitrepo.freebsd.org) Received: (from git@localhost) by gitrepo.freebsd.org (8.16.1/8.16.1/Submit) id 16LGE8qB080483; Wed, 21 Jul 2021 16:14:08 GMT (envelope-from git) Date: Wed, 21 Jul 2021 16:14:08 GMT Message-Id: <202107211614.16LGE8qB080483@gitrepo.freebsd.org> To: src-committers@FreeBSD.org, dev-commits-src-all@FreeBSD.org, dev-commits-src-branches@FreeBSD.org From: Warner Losh Subject: git: 50b2e5d608d2 - stable/13 - nanobsd: Use gpart and create code image before full disk image MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: 8bit X-Git-Committer: imp X-Git-Repository: src X-Git-Refname: refs/heads/stable/13 X-Git-Reftype: branch X-Git-Commit: 50b2e5d608d2d8876af2a8d4e5c4e60867f9ad96 Auto-Submitted: auto-generated X-BeenThere: dev-commits-src-branches@freebsd.org X-Mailman-Version: 2.1.34 Precedence: list List-Id: Commits to the stable branches of the FreeBSD src repository List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Wed, 21 Jul 2021 16:14:09 -0000 The branch stable/13 has been updated by imp: URL: https://cgit.FreeBSD.org/src/commit/?id=50b2e5d608d2d8876af2a8d4e5c4e60867f9ad96 commit 50b2e5d608d2d8876af2a8d4e5c4e60867f9ad96 Author: Arrigo Marchiori AuthorDate: 2021-07-15 22:06:51 +0000 Commit: Warner Losh CommitDate: 2021-07-21 16:13:11 +0000 nanobsd: Use gpart and create code image before full disk image The attached patch brings two main changes to the nanobsd script: 1- gpart is used instead of fdisk; 2- the code image is created first, and then used to ``assemble'' the full disk image. The patch was first proposed on the freebsd-embedded list: http://lists.freebsd.org/pipermail/freebsd-embedded/2012-June/001580.html and is currently under discussion: http://lists.freebsd.org/pipermail/freebsd-embedded/2014-January/002216.html Another effect is that the -f option ("suppress code slice extraction") now imples the -i option ("suppress disk image build"). imp@ applied Patch by hand to new legacy.sh, plus tweaked for NANO_LOG vs NANO_OBJ confusion in original. PR: 186030 Reviewed by: imp@ Differential Revision: https://reviews.freebsd.org/D31102 (cherry picked from commit 587c054bea0f652fdbd86baf5807f21c904ac2ad) --- tools/tools/nanobsd/defaults.sh | 4 +- tools/tools/nanobsd/legacy.sh | 132 ++++++++++++++++++++++++++-------------- tools/tools/nanobsd/nanobsd.sh | 17 ++++-- 3 files changed, 103 insertions(+), 50 deletions(-) diff --git a/tools/tools/nanobsd/defaults.sh b/tools/tools/nanobsd/defaults.sh index 1ebc88fecac3..7bc74c42170e 100755 --- a/tools/tools/nanobsd/defaults.sh +++ b/tools/tools/nanobsd/defaults.sh @@ -161,6 +161,8 @@ NANO_SLICE_ROOT=s1 NANO_SLICE_ALTROOT=s2 NANO_SLICE_CFG=s3 NANO_SLICE_DATA=s4 +NANO_PARTITION_ROOT=a +NANO_PARTITION_ALTROOT=a NANO_ROOT=s1a NANO_ALTROOT=s2a @@ -853,7 +855,7 @@ usage ( ) { echo "Usage: $0 [-bfhiKknqvwX] [-c config_file]" echo " -b suppress builds (both kernel and world)" echo " -c specify config file" - echo " -f suppress code slice extraction" + echo " -f suppress code slice extraction (implies -i)" echo " -h print this help summary page" echo " -i suppress disk image build" echo " -K suppress installkernel" diff --git a/tools/tools/nanobsd/legacy.sh b/tools/tools/nanobsd/legacy.sh index b59a9e795b66..0a966c8c127e 100644 --- a/tools/tools/nanobsd/legacy.sh +++ b/tools/tools/nanobsd/legacy.sh @@ -35,30 +35,18 @@ # Functions and variable definitions used by the legacy nanobsd # image building system. -create_diskimage ( ) ( - pprint 2 "build diskimage" - pprint 3 "log: ${NANO_LOG}/_.di" - - ( +calculate_partitioning ( ) ( echo $NANO_MEDIASIZE $NANO_IMAGES \ $NANO_SECTS $NANO_HEADS \ $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE | awk ' { - printf "# %s\n", $0 - # size of cylinder in sectors cs = $3 * $4 # number of full cylinders on media cyl = int ($1 / cs) - # output fdisk geometry spec, truncate cyls to 1023 - if (cyl <= 1023) - print "g c" cyl " h" $4 " s" $3 - else - print "g c" 1023 " h" $4 " s" $3 - if ($7 > 0) { # size of data partition in full cylinders dsl = int (($7 + cs - 1) / cs) @@ -69,44 +57,100 @@ create_diskimage ( ) ( # size of config partition in full cylinders csl = int (($6 + cs - 1) / cs) + # size of image partition(s) in full cylinders if ($5 == 0) { - # size of image partition(s) in full cylinders isl = int ((cyl - dsl - csl) / $2) } else { isl = int (($5 + cs - 1) / cs) } # First image partition start at second track - print "p 1 165 " $3, isl * cs - $3 + print $3, isl * cs - $3 c = isl * cs; # Second image partition (if any) also starts offset one # track to keep them identical. if ($2 > 1) { - print "p 2 165 " $3 + c, isl * cs - $3 + print $3 + c, isl * cs - $3 c += isl * cs; } # Config partition starts at cylinder boundary. - print "p 3 165 " c, csl * cs + print c, csl * cs c += csl * cs # Data partition (if any) starts at cylinder boundary. if ($7 > 0) { - print "p 4 165 " c, dsl * cs + print c, dsl * cs } else if ($7 < 0 && $1 > c) { - print "p 4 165 " c, $1 - c + print c, $1 - c } else if ($1 < c) { print "Disk space overcommitted by", \ c - $1, "sectors" > "/dev/stderr" exit 2 } - # Force slice 1 to be marked active. This is necessary - # for booting the image from a USB device to work. - print "a 1" } - ' > ${NANO_LOG}/_.fdisk + ' > ${NANO_LOG}/_.partitioning +) + +create_code_slice ( ) ( + pprint 2 "build code slice" + pprint 3 "log: ${NANO_OBJ}/_.cs" + + ( + IMG=${NANO_DISKIMGDIR}/_.disk.image + MNT=${NANO_OBJ}/_.mnt + mkdir -p ${MNT} + CODE_SIZE=`head -n 1 ${NANO_LOG}/_.partitioning | awk '{ print $2 }'` + + if [ "${NANO_MD_BACKING}" = "swap" ] ; then + MD=`mdconfig -a -t swap -s ${CODE_SIZE} -x ${NANO_SECTS} \ + -y ${NANO_HEADS}` + else + echo "Creating md backing file..." + rm -f ${IMG} + dd if=/dev/zero of=${IMG} seek=${CODE_SIZE} count=0 + MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \ + -y ${NANO_HEADS}` + fi + + trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT + + bsdlabel -w ${MD} + if [ -f ${NANO_WORLDDIR}/boot/boot ]; then + echo "Making bootable partition" + gpart bootcode -b ${NANO_WORLDDIR}/boot/boot ${MD} + else + echo "Partition will not be bootable" + fi + bsdlabel ${MD} + + # Create first image + populate_slice /dev/${MD}${NANO_PARTITION_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}" + mount /dev/${MD}a ${MNT} + echo "Generating mtree..." + ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree + ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du + nano_umount ${MNT} + + if [ "${NANO_MD_BACKING}" = "swap" ] ; then + echo "Writing out _.disk.image..." + dd conv=sparse if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k + fi + mdconfig -d -u $MD + + trap - 1 2 15 EXIT + + ) > ${NANO_OBJ}/_.cs 2>&1 +) + + +create_diskimage ( ) ( + pprint 2 "build diskimage" + pprint 3 "log: ${NANO_OBJ}/_.di" + + ( IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME} MNT=${NANO_OBJ}/_.mnt @@ -123,29 +167,34 @@ create_diskimage ( ) ( -y ${NANO_HEADS}` fi + awk ' + BEGIN { + # Create MBR partition table + print "gpart create -s mbr $1" + } + { + # Make partition + print "gpart add -t freebsd -b ", $1, " -s ", $2, " $1" + } + END { + # Force slice 1 to be marked active. This is necessary + # for booting the image from a USB device to work. + print "gpart set -a active -i 1 $1" + } + ' ${NANO_LOG}/_.partitioning > ${NANO_OBJ}/_.gpart + trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT - fdisk -i -f ${NANO_LOG}/_.fdisk ${MD} - fdisk ${MD} + sh ${NANO_OBJ}/_.gpart ${MD} + gpart show ${MD} # XXX: params # XXX: pick up cached boot* files, they may not be in image anymore. if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then - boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD} - fi - if [ -f ${NANO_WORLDDIR}/boot/boot ]; then - bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT} - else - bsdlabel -w ${MD}${NANO_SLICE_ROOT} + gpart bootcode -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOTFLAGS} ${MD} fi - bsdlabel ${MD}${NANO_SLICE_ROOT} - # Create first image - populate_slice /dev/${MD}${NANO_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}" - mount /dev/${MD}${NANO_ROOT} ${MNT} - echo "Generating mtree..." - ( cd "${MNT}" && mtree -c ) > ${NANO_LOG}/_.mtree - ( cd "${MNT}" && du -k ) > ${NANO_LOG}/_.du - nano_umount "${MNT}" + echo "Writing code image..." + dd conv=sparse if=${NANO_DISKIMGDIR}/_.disk.image of=/dev/${MD}${NANO_SLICE_ROOT} bs=64k if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then # Duplicate to second image (if present) @@ -190,11 +239,6 @@ create_diskimage ( ) ( dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k fi - if ${do_copyout_partition} ; then - echo "Writing out ${NANO_IMG1NAME}..." - dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} \ - of=${NANO_DISKIMGDIR}/${NANO_IMG1NAME} bs=64k - fi mdconfig -d -u $MD trap - 1 2 15 EXIT diff --git a/tools/tools/nanobsd/nanobsd.sh b/tools/tools/nanobsd/nanobsd.sh index 89d0bed60b7a..c5c680025fc7 100755 --- a/tools/tools/nanobsd/nanobsd.sh +++ b/tools/tools/nanobsd/nanobsd.sh @@ -40,9 +40,9 @@ do_clean=true do_kernel=true do_installkernel=true do_world=true +do_code=true do_installworld=true do_image=true -do_copyout_partition=true do_native_xtools=false do_prep_image=true @@ -94,7 +94,8 @@ do shift ;; -f) - do_copyout_partition=false + do_code=false + do_image=false shift ;; -h) @@ -215,10 +216,16 @@ if $do_prep_image ; then else pprint 2 "Skipping image prep (as instructed)" fi -if $do_image ; then - create_diskimage +if $do_code ; then + calculate_partitioning + create_code_slice + if $do_image ; then + create_diskimage + else + pprint 2 "Skipping image build (as instructed)" + fi else - pprint 2 "Skipping image build (as instructed)" + pprint 2 "Skipping code and image build (as instructed)" fi last_orders