Creating code slice before disk image in nanobsd

Nick Hibma nick at van-laarhoven.org
Tue Jan 28 08:48:32 UTC 2014


>>>> I'll take a look at things... It isn't obvious at first blush this is
>>>> the right thing to do, but I need to think about it a bit...
>>>> 
>>>> Warner
>>> 
>>> Perhaps an aside, but I'm pretty sure Warren (cc'd) has said that gpart
>>> can do anything that fdisk AND bsdlabel can do?
>> 
>> Yes.  Although there is an exception: gpart silently forces alignment to 
>> CHS values on MBR slices, and fdisk does not.  So to get a 4K-aligned 
>> slice requires fdisk.  gpart can align BSD partitions inside a slice to 
>> 4K, but that is a workaround.  (I believe gpart should default to CHS 
>> but use -a alignment or -b values when given for MBR slices, regardless 
>> of the MBR spec.  This becomes increasingly important as 4K drives and 
>> SSDs become more common.)
> 
> I didn't take alignment into account; I just adapted the calculations
> to match the parameters required by gpart.
> 
>>> If that's so, and the
>>> object here is to deprecate fdisk, why not also replace these lines:
>>> 
>>> +
>>> +	bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}
>>> +	bsdlabel ${MD}
>>> +
>>> 
>>> with their gpart equivalents?
>>> 
>>> (and no, I don't know what those should be in this context)
>> 
>> To create the initial MBR, probably already done before the code above:
>>   gpart create -s mbr ${MD}
> 
> This is done by the patch.
> 
> [...]
>> To create a slice and set it active:
>>   gpart add -s freebsd ${MD}    [1]
> [...]
>> [1] when creating the slice, -a4k or -b1m can be specified but will be
>>     silently rounded to CHS values
> 
> The -b parameter is also used, therefore your note [1] applies.
> 
>>   gpart set -a active -i1 ${MD}
> 
> This is done by the patch.
> 
>> To create the BSDlabel inside the slice and write the bootcode to it:
>>   gpart create -s bsd ${MD}s1
>>   gpart bootcode -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
>> Then BSD partitions can be added with, for example:
>>   gpart add -t freebsd-ufs -s2g ${MD}s1  [2]
> [...]
>> [2] when creating BSD partitions, -a4k can be used and they will be
>>     created with an offset inside the slice to end up aligned
> 
> These were kept as the original:
> 
> bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}s1
> 
>> For reference, see 
>> http://www.wonkity.com/~wblock/docs/html/disksetup.html#_the_old_standard_mbr

For reference, I’ve replaced the disk image build with a simpler disk image function, and a separate script to generate a full disk image (used only for new installations. The disk image script is a simplification of the existing one in nanobsd.sh.

The second, separate script does the full initialisation of a disk depending on what it finds. It also does automatic filling of the disk with a /data partition (so we can use 2 150MB code partitions, cfg partition and the rest for storage on any medium). As you can see we still use the boot0cfg commands here. It can build an MBR style partitioning, but also a BSD style partitioning (USB drives) (left out for brevity).

(Note: Our scripts can not only generate MBR style partitioning, but also BSD style partitioning holding 2 code slices. For that to work you will need to do some fancy twiddling of the partition switching after update).

Maybe this is of use for reference in this discussions.

Nick



echo "### Creating MBR style layout for HD/CF style image on $CFCARD" 1>&4
# AWIMGroot1    ada0s1a
# AWIMGroot2    ada0s2a
# AWIMGcfg      ada0s3
# AWIMGdata     ada0s4
gpart create -s MBR $CFCARD

# We need to increase the size of the root partitions by a small amount to
# make room for the BSD label at the start
gpart add -t freebsd -s $(($IMAGE_CODESIZE+1))M $CFCARD
gpart create -s BSD ${CFCARD}s1
gpart bootcode -b $NANO_WORLDDIR/boot/boot ${CFCARD}s1
gpart add -i 1 -t freebsd-ufs -s ${IMAGE_CODESIZE}M ${CFCARD}s1
root1=${CFCARD}s1a

if [ $NANO_IMAGES -gt 1 ]; then
        gpart add -i 2 -t freebsd -s $(($IMAGE_CODESIZE+1))M $CFCARD
        gpart create -s BSD ${CFCARD}s2
        gpart bootcode -b $NANO_WORLDDIR/boot/boot ${CFCARD}s2
        gpart add -t freebsd-ufs -s ${IMAGE_CODESIZE}M ${CFCARD}s2
        root2=${CFCARD}s2a
fi

gpart add -i 3 -t freebsd -s ${IMAGE_CFGSIZE}M $CFCARD
cfgpart=${CFCARD}s3
if [ -z "$IMAGE_DATASIZE" ] || [ "$IMAGE_DATASIZE" -gt 0 ]; then
        gpart add -i 4 -t freebsd ${IMAGE_DATASIZE:+-s ${IMAGE_DATASIZE}M} $CFCARD
        datapart=${CFCARD}s4
fi

# Mark the first partition bootable
gpart bootcode -b $NANO_WORLDDIR/$NANO_BOOTLOADER $CFCARD
gpart set -a active -i 1 $CFCARD
# Configure boot0
boot0cfg $NANO_BOOT0CFG $CFCARD

echo "### Populating root 1 partition" 1>&4
dd if=$IMG of=/dev/$root1 bs=8k skip=1 seek=1

if [ -n "$root2" ]; then
        echo "### Leaving empty root 2 partition" 1>&4
        echo "### Populating config partition" 1>&4
fi

newfs -L ${NANO_LABEL}cfg $cfgpart
populate_fs /dev/$cfgpart ./Cfg ${IMAGE_SUBNAME:+./$IMAGE_SUBNAME/Cfg} $cfgdir

if [ -n "$datapart" ]; then
        echo "### Populating data partition" 1>&4
        newfs -L ${NANO_LABEL}data $datapart
        populate_fs /dev/$datapart ${IMAGE_DEFAULTS}/Data ./Data ${IMAGE_SUBNAME:+./$IMAGE_SUBNAME/Data}
fi


-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 243 bytes
Desc: Message signed with OpenPGP using GPGMail
URL: <http://lists.freebsd.org/pipermail/freebsd-embedded/attachments/20140128/eab11e82/attachment-0001.sig>


More information about the freebsd-embedded mailing list