3 # Copyright (c) 2005 Poul-Henning Kamp All rights reserved.
4 # Copyright (c) 2016 M. Warner Losh <imp@FreeBSD.org>
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
10 # 1. Redistributions of source code must retain the above copyright
11 # notice, this list of conditions and the following disclaimer.
12 # 2. Redistributions in binary form must reproduce the above copyright
13 # notice, this list of conditions and the following disclaimer in the
14 # documentation and/or other materials provided with the distribution.
16 # THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
17 # ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
18 # IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
19 # ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
20 # FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
21 # DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
22 # OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
23 # HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
24 # LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
25 # OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 # Media geometry, only relevant if bios doesn't understand LBA.
32 [ -n "$NANO_SECTS" ] || NANO_SECTS=63
33 [ -n "$NANO_HEADS" ] || NANO_HEADS=16
35 # Functions and variable definitions used by the legacy nanobsd
36 # image building system.
38 calculate_partitioning ( ) (
39 echo $NANO_MEDIASIZE $NANO_IMAGES \
40 $NANO_SECTS $NANO_HEADS \
41 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
44 # size of cylinder in sectors
47 # number of full cylinders on media
51 # size of data partition in full cylinders
52 dsl = int (($7 + cs - 1) / cs)
57 # size of config partition in full cylinders
58 csl = int (($6 + cs - 1) / cs)
60 # size of image partition(s) in full cylinders
62 isl = int ((cyl - dsl - csl) / $2)
64 isl = int (($5 + cs - 1) / cs)
67 # First image partition start at second track
68 print $3, isl * cs - $3
71 # Second image partition (if any) also starts offset one
72 # track to keep them identical.
74 print $3 + c, isl * cs - $3
78 # Config partition starts at cylinder boundary.
82 # Data partition (if any) starts at cylinder boundary.
85 } else if ($7 < 0 && $1 > c) {
88 print "Disk space overcommitted by", \
89 c - $1, "sectors" > "/dev/stderr"
94 ' > ${NANO_LOG}/_.partitioning
97 create_code_slice ( ) (
98 pprint 2 "build code slice"
99 pprint 3 "log: ${NANO_OBJ}/_.cs"
102 IMG=${NANO_DISKIMGDIR}/_.disk.image
103 MNT=${NANO_OBJ}/_.mnt
105 CODE_SIZE=`head -n 1 ${NANO_LOG}/_.partitioning | awk '{ print $2 }'`
107 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
108 MD=`mdconfig -a -t swap -s ${CODE_SIZE} -x ${NANO_SECTS} \
111 echo "Creating md backing file..."
113 dd if=/dev/zero of=${IMG} seek=${CODE_SIZE} count=0
114 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
118 trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
121 if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
122 echo "Making bootable partition"
123 gpart bootcode -b ${NANO_WORLDDIR}/boot/boot ${MD}
125 echo "Partition will not be bootable"
130 populate_slice /dev/${MD}${NANO_PARTITION_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}"
131 mount /dev/${MD}a ${MNT}
132 echo "Generating mtree..."
133 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
134 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
137 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
138 echo "Writing out _.disk.image..."
139 dd conv=sparse if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
145 ) > ${NANO_OBJ}/_.cs 2>&1
149 create_diskimage ( ) (
150 pprint 2 "build diskimage"
151 pprint 3 "log: ${NANO_OBJ}/_.di"
155 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
156 MNT=${NANO_OBJ}/_.mnt
159 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
160 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
163 echo "Creating md backing file..."
165 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
166 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
172 # Create MBR partition table
173 print "gpart create -s mbr $1"
177 print "gpart add -t freebsd -b ", $1, " -s ", $2, " $1"
180 # Force slice 1 to be marked active. This is necessary
181 # for booting the image from a USB device to work.
182 print "gpart set -a active -i 1 $1"
184 ' ${NANO_LOG}/_.partitioning > ${NANO_OBJ}/_.gpart
186 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
188 sh ${NANO_OBJ}/_.gpart ${MD}
191 # XXX: pick up cached boot* files, they may not be in image anymore.
192 if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
193 gpart bootcode -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOTFLAGS} ${MD}
196 echo "Writing code image..."
197 dd conv=sparse if=${NANO_DISKIMGDIR}/_.disk.image of=/dev/${MD}${NANO_SLICE_ROOT} bs=64k
199 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
200 # Duplicate to second image (if present)
201 echo "Duplicating to second image..."
202 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
203 mount /dev/${MD}${NANO_ALTROOT} ${MNT}
204 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
206 sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
209 # Override the label from the first partition so we
210 # don't confuse glabel with duplicates.
211 if [ -n "${NANO_LABEL}" ]; then
212 tunefs -L ${NANO_LABEL}"${NANO_ALTROOT}" /dev/${MD}${NANO_ALTROOT}
216 # Create Config slice
217 populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
219 # Create Data slice, if any.
220 if [ -n "$NANO_SLICE_DATA" -a "$NANO_SLICE_CFG" = "$NANO_SLICE_DATA" -a \
221 "$NANO_DATASIZE" -ne 0 ]; then
222 pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
225 if [ $NANO_DATASIZE -ne 0 -a -n "$NANO_SLICE_DATA" ] ; then
226 populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
229 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
230 if [ ${NANO_IMAGE_MBRONLY} ]; then
231 echo "Writing out _.disk.mbr..."
232 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
234 echo "Writing out ${NANO_IMGNAME}..."
235 dd if=/dev/${MD} of=${IMG} bs=64k
238 echo "Writing out ${NANO_IMGNAME}..."
239 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
246 ) > ${NANO_LOG}/_.di 2>&1