3 # Copyright (c) 2005 Poul-Henning Kamp.
4 # Copyright (c) 2016 M. Warner Losh.
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 [ -z "$NANO_SECTS" ] || NANO_SECTS=63
33 [ -z "$NANO_HEADS" ] || NANO_HEADS=16
35 # Functions and variable definitions used by the legacy nanobsd
36 # image building system.
38 create_diskimage ( ) (
39 pprint 2 "build diskimage"
40 pprint 3 "log: ${NANO_LOG}/_.di"
43 echo $NANO_MEDIASIZE $NANO_IMAGES \
44 $NANO_SECTS $NANO_HEADS \
45 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
50 # size of cylinder in sectors
53 # number of full cylinders on media
56 # output fdisk geometry spec, truncate cyls to 1023
58 print "g c" cyl " h" $4 " s" $3
60 print "g c" 1023 " h" $4 " s" $3
63 # size of data partition in full cylinders
64 dsl = int (($7 + cs - 1) / cs)
69 # size of config partition in full cylinders
70 csl = int (($6 + cs - 1) / cs)
73 # size of image partition(s) in full cylinders
74 isl = int ((cyl - dsl - csl) / $2)
76 isl = int (($5 + cs - 1) / cs)
79 # First image partition start at second track
80 print "p 1 165 " $3, isl * cs - $3
83 # Second image partition (if any) also starts offset one
84 # track to keep them identical.
86 print "p 2 165 " $3 + c, isl * cs - $3
90 # Config partition starts at cylinder boundary.
91 print "p 3 165 " c, csl * cs
94 # Data partition (if any) starts at cylinder boundary.
96 print "p 4 165 " c, dsl * cs
97 } else if ($7 < 0 && $1 > c) {
98 print "p 4 165 " c, $1 - c
100 print "Disk space overcommitted by", \
101 c - $1, "sectors" > "/dev/stderr"
105 # Force slice 1 to be marked active. This is necessary
106 # for booting the image from a USB device to work.
109 ' > ${NANO_LOG}/_.fdisk
111 IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
112 MNT=${NANO_OBJ}/_.mnt
115 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
116 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
119 echo "Creating md backing file..."
121 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
122 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
126 trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
128 fdisk -i -f ${NANO_LOG}/_.fdisk ${MD}
131 # XXX: pick up cached boot* files, they may not be in image anymore.
132 if [ -f ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ]; then
133 boot0cfg -B -b ${NANO_WORLDDIR}/${NANO_BOOTLOADER} ${NANO_BOOT0CFG} ${MD}
135 if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
136 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT}
138 bsdlabel -w ${MD}${NANO_SLICE_ROOT}
140 bsdlabel ${MD}${NANO_SLICE_ROOT}
143 populate_slice /dev/${MD}${NANO_ROOT} ${NANO_WORLDDIR} ${MNT} "${NANO_ROOT}"
144 mount /dev/${MD}${NANO_ROOT} ${MNT}
145 echo "Generating mtree..."
146 ( cd "${MNT}" && mtree -c ) > ${NANO_LOG}/_.mtree
147 ( cd "${MNT}" && du -k ) > ${NANO_LOG}/_.du
150 if [ $NANO_IMAGES -gt 1 -a $NANO_INIT_IMG2 -gt 0 ] ; then
151 # Duplicate to second image (if present)
152 echo "Duplicating to second image..."
153 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} of=/dev/${MD}${NANO_SLICE_ALTROOT} bs=64k
154 mount /dev/${MD}${NANO_ALTROOT} ${MNT}
155 for f in ${MNT}/etc/fstab ${MNT}/conf/base/etc/fstab
157 sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
160 # Override the label from the first partition so we
161 # don't confuse glabel with duplicates.
162 if [ -n "${NANO_LABEL}" ]; then
163 tunefs -L ${NANO_LABEL}"${NANO_ALTROOT}" /dev/${MD}${NANO_ALTROOT}
167 # Create Config slice
168 populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
170 # Create Data slice, if any.
171 if [ -n "$NANO_SLICE_DATA" -a "$NANO_SLICE_CFG" = "$NANO_SLICE_DATA" -a \
172 "$NANO_DATASIZE" -ne 0 ]; then
173 pprint 2 "NANO_SLICE_DATA is the same as NANO_SLICE_CFG, fix."
176 if [ $NANO_DATASIZE -ne 0 -a -n "$NANO_SLICE_DATA" ] ; then
177 populate_data_slice /dev/${MD}${NANO_SLICE_DATA} "${NANO_DATADIR}" ${MNT} "${NANO_SLICE_DATA}"
180 if [ "${NANO_MD_BACKING}" = "swap" ] ; then
181 if [ ${NANO_IMAGE_MBRONLY} ]; then
182 echo "Writing out _.disk.mbr..."
183 dd if=/dev/${MD} of=${NANO_DISKIMGDIR}/_.disk.mbr bs=512 count=1
185 echo "Writing out ${NANO_IMGNAME}..."
186 dd if=/dev/${MD} of=${IMG} bs=64k
189 echo "Writing out ${NANO_IMGNAME}..."
190 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
193 if ${do_copyout_partition} ; then
194 echo "Writing out ${NANO_IMG1NAME}..."
195 dd conv=sparse if=/dev/${MD}${NANO_SLICE_ROOT} \
196 of=${NANO_DISKIMGDIR}/${NANO_IMG1NAME} bs=64k
202 ) > ${NANO_LOG}/_.di 2>&1