]> CyberLeo.Net >> Repos - FreeBSD/FreeBSD.git/blob - tools/tools/nanobsd/legacy.sh
ipfw(8): Fix a few mandoc related issues
[FreeBSD/FreeBSD.git] / tools / tools / nanobsd / legacy.sh
1 #!/bin/sh
2 #
3 # Copyright (c) 2005 Poul-Henning Kamp All rights reserved.
4 # Copyright (c) 2016 M. Warner Losh <imp@FreeBSD.org>
5 #
6 #
7 # Redistribution and use in source and binary forms, with or without
8 # modification, are permitted provided that the following conditions
9 # are met:
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.
15 #
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
26 # SUCH DAMAGE.
27 #
28 # $FreeBSD$
29 #
30
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
34
35 # Functions and variable definitions used by the legacy nanobsd
36 # image building system.
37
38 create_diskimage ( ) (
39         pprint 2 "build diskimage"
40         pprint 3 "log: ${NANO_LOG}/_.di"
41
42         (
43         echo $NANO_MEDIASIZE $NANO_IMAGES \
44                 $NANO_SECTS $NANO_HEADS \
45                 $NANO_CODESIZE $NANO_CONFSIZE $NANO_DATASIZE |
46         awk '
47         {
48                 printf "# %s\n", $0
49
50                 # size of cylinder in sectors
51                 cs = $3 * $4
52
53                 # number of full cylinders on media
54                 cyl = int ($1 / cs)
55
56                 # output fdisk geometry spec, truncate cyls to 1023
57                 if (cyl <= 1023)
58                         print "g c" cyl " h" $4 " s" $3
59                 else
60                         print "g c" 1023 " h" $4 " s" $3
61
62                 if ($7 > 0) {
63                         # size of data partition in full cylinders
64                         dsl = int (($7 + cs - 1) / cs)
65                 } else {
66                         dsl = 0;
67                 }
68
69                 # size of config partition in full cylinders
70                 csl = int (($6 + cs - 1) / cs)
71
72                 if ($5 == 0) {
73                         # size of image partition(s) in full cylinders
74                         isl = int ((cyl - dsl - csl) / $2)
75                 } else {
76                         isl = int (($5 + cs - 1) / cs)
77                 }
78
79                 # First image partition start at second track
80                 print "p 1 165 " $3, isl * cs - $3
81                 c = isl * cs;
82
83                 # Second image partition (if any) also starts offset one
84                 # track to keep them identical.
85                 if ($2 > 1) {
86                         print "p 2 165 " $3 + c, isl * cs - $3
87                         c += isl * cs;
88                 }
89
90                 # Config partition starts at cylinder boundary.
91                 print "p 3 165 " c, csl * cs
92                 c += csl * cs
93
94                 # Data partition (if any) starts at cylinder boundary.
95                 if ($7 > 0) {
96                         print "p 4 165 " c, dsl * cs
97                 } else if ($7 < 0 && $1 > c) {
98                         print "p 4 165 " c, $1 - c
99                 } else if ($1 < c) {
100                         print "Disk space overcommitted by", \
101                             c - $1, "sectors" > "/dev/stderr"
102                         exit 2
103                 }
104
105                 # Force slice 1 to be marked active. This is necessary
106                 # for booting the image from a USB device to work.
107                 print "a 1"
108         }
109         ' > ${NANO_LOG}/_.fdisk
110
111         IMG=${NANO_DISKIMGDIR}/${NANO_IMGNAME}
112         MNT=${NANO_OBJ}/_.mnt
113         mkdir -p ${MNT}
114
115         if [ "${NANO_MD_BACKING}" = "swap" ] ; then
116                 MD=`mdconfig -a -t swap -s ${NANO_MEDIASIZE} -x ${NANO_SECTS} \
117                         -y ${NANO_HEADS}`
118         else
119                 echo "Creating md backing file..."
120                 rm -f ${IMG}
121                 dd if=/dev/zero of=${IMG} seek=${NANO_MEDIASIZE} count=0
122                 MD=`mdconfig -a -t vnode -f ${IMG} -x ${NANO_SECTS} \
123                         -y ${NANO_HEADS}`
124         fi
125
126         trap "echo 'Running exit trap code' ; df -i ${MNT} ; nano_umount ${MNT} || true ; mdconfig -d -u $MD" 1 2 15 EXIT
127
128         fdisk -i -f ${NANO_LOG}/_.fdisk ${MD}
129         fdisk ${MD}
130         # XXX: params
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}
134         fi
135         if [ -f ${NANO_WORLDDIR}/boot/boot ]; then
136                 bsdlabel -w -B -b ${NANO_WORLDDIR}/boot/boot ${MD}${NANO_SLICE_ROOT}
137         else
138                 bsdlabel -w ${MD}${NANO_SLICE_ROOT}
139         fi
140         bsdlabel ${MD}${NANO_SLICE_ROOT}
141
142         # Create first image
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
148         nano_umount "${MNT}"
149
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
156                 do
157                         sed -i "" "s=${NANO_DRIVE}${NANO_SLICE_ROOT}=${NANO_DRIVE}${NANO_SLICE_ALTROOT}=g" $f
158                 done
159                 nano_umount ${MNT}
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}
164                 fi
165         fi
166
167         # Create Config slice
168         populate_cfg_slice /dev/${MD}${NANO_SLICE_CFG} "${NANO_CFGDIR}" ${MNT} "${NANO_SLICE_CFG}"
169
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."
174                 exit 2
175         fi
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}"
178         fi
179
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
184                 else
185                         echo "Writing out ${NANO_IMGNAME}..."
186                         dd if=/dev/${MD} of=${IMG} bs=64k
187                 fi
188
189                 echo "Writing out ${NANO_IMGNAME}..."
190                 dd conv=sparse if=/dev/${MD} of=${IMG} bs=64k
191         fi
192
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
197         fi
198         mdconfig -d -u $MD
199
200         trap - 1 2 15 EXIT
201
202         ) > ${NANO_LOG}/_.di 2>&1
203 )