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
30 # Media geometry, only relevant if bios doesn't understand LBA.
31 [ -n "$NANO_SECTS" ] || NANO_SECTS=63
32 [ -n "$NANO_HEADS" ] || NANO_HEADS=16
34 # Functions and variable definitions used by the legacy nanobsd
35 # image building system.
37 calculate_partitioning ( ) (
38 echo $NANO_MEDIASIZE $NANO_IMAGES \
39 $NANO_SECTS $NANO_HEADS \
40 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
43 # size of cylinder in sectors
46 # number of full cylinders on media
50 # size of data partition in full cylinders
51 dsl = int (($7 + cs - 1) / cs)
56 # size of config partition in full cylinders
57 csl = int (($6 + cs - 1) / cs)
59 # size of image partition(s) in full cylinders
61 isl = int ((cyl - dsl - csl) / $2)
63 isl = int (($5 + cs - 1) / cs)
66 # First image partition start at second track
67 print $3, isl * cs - $3
70 # Second image partition (if any) also starts offset one
71 # track to keep them identical.
73 print $3 + c, isl * cs - $3
77 # Config partition starts at cylinder boundary.
81 # Data partition (if any) starts at cylinder boundary.
84 } else if ($7 < 0 && $1 > c) {
87 print "Disk space overcommitted by", \
88 c - $1, "sectors" > "/dev/stderr"
93 ' > ${NANO_LOG}/_.partitioning
96 create_code_slice ( ) (
97 pprint 2 "build code slice"
98 pprint 3 "log: ${NANO_OBJ}/_.cs"
101 IMG=${NANO_DISKIMGDIR}/_.disk.image
102 MNT=${NANO_OBJ}/_.mnt
104 CODE_SIZE=`head -n 1 ${NANO_LOG}/_.partitioning | awk '{ print $2 }'`
106 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
107 MD=`mdconfig -a -t swap -s ${CODE_SIZE} -x ${NANO_SECTS} \
110 echo "Creating md backing file..."
112 dd if=/dev/zero of=${IMG} seek=${CODE_SIZE} count=0
113 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
117 trap "echo 'Running exit trap code' ; df -i ${MNT} ; umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
120 if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
121 echo "Making bootable partition"
122 gpart bootcode -b ${NANO_WORLDDIR}/boot/boot ${MD}
124 echo "Partition will not be bootable"
129 populate_slice /dev/${MD}${NANO_PARTITION_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}"
130 mount /dev/${MD}a ${MNT}
131 echo "Generating mtree..."
132 ( cd ${MNT} && mtree -c ) > ${NANO_OBJ}/_.mtree
133 ( cd ${MNT} && du -k ) > ${NANO_OBJ}/_.du
136 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
137 echo "Writing out _.disk.image..."
138 dd conv=sparse if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.image bs=64k
144 ) > ${NANO_OBJ}/_.cs 2>&1
148 create_diskimage ( ) (
149 pprint 2 "build diskimage"
150 pprint 3 "log: ${NANO_OBJ}/_.di"
154 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
155 MNT=${NANO_OBJ}/_.mnt
158 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
159 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
162 echo "Creating md backing file..."
164 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
165 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
171 # Create MBR partition table
172 print "gpart create -s mbr $1"
176 print "gpart add -t freebsd -b ", $1, " -s ", $2, " $1"
179 # Force slice 1 to be marked active. This is necessary
180 # for booting the image from a USB device to work.
181 print "gpart set -a active -i 1 $1"
183 ' ${NANO_LOG}/_.partitioning > ${NANO_OBJ}/_.gpart
185 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
187 sh ${NANO_OBJ}/_.gpart ${MD}
190 # XXX: pick up cached boot* files, they may not be in image anymore.
191 if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
192 gpart bootcode -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOTFLAGS} ${MD}
195 echo "Writing code image..."
196 dd conv=sparse if=${NANO_DISKIMGDIR}/_.disk.image of=/dev/${MD}${NANO_SLICE_ROOT} bs=64k
198 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
199 # Duplicate to second image (if present)
200 echo "Duplicating to second image..."
201 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
202 mount /dev/${MD}${NANO_ALTROOT} ${MNT}
203 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
205 sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
208 # Override the label from the first partition so we
209 # don't confuse glabel with duplicates.
210 if [ -n "${NANO_LABEL}" ]; then
211 tunefs -L ${NANO_LABEL}"${NANO_ALTROOT}" /dev/${MD}${NANO_ALTROOT}
215 # Create Config slice
216 populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
218 # Create Data slice, if any.
219 if [ -n "$NANO_SLICE_DATA" -a "$NANO_SLICE_CFG" = "$NANO_SLICE_DATA" -a \
220 "$NANO_DATASIZE" -ne 0 ]; then
221 pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
224 if [ $NANO_DATASIZE -ne 0 -a -n "$NANO_SLICE_DATA" ] ; then
225 populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
228 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
229 if [ ${NANO_IMAGE_MBRONLY} ]; then
230 echo "Writing out _.disk.mbr..."
231 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
233 echo "Writing out ${NANO_IMGNAME}..."
234 dd if=/dev/${MD} of=${IMG} bs=64k
237 echo "Writing out ${NANO_IMGNAME}..."
238 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
245 ) > ${NANO_LOG}/_.di 2>&1