xref: /freebsd/tools/boot/rootgen.sh (revision 732a02b4e77866604a120a275c082bb6221bd2ff)
1#!/bin/sh
2
3# $FreeBSD$
4
5passphrase=passphrase
6iterations=50000
7
8# The smallest FAT32 filesystem is 33292 KB
9espsize=33292
10
11#
12# Builds all the bat-shit crazy combinations we support booting from,
13# at least for amd64. It assume you have a ~sane kernel in /boot/kernel
14# and copies that into the ~150MB root images we create (we create the du
15# size of the kernel + 20MB
16#
17# Sad panda sez: this runs as root, but could be userland if someone
18# creates userland geli and zfs tools.
19#
20# This assumes an external program install-boot.sh which will install
21# the appropriate boot files in the appropriate locations.
22#
23# These images assume ada0 will be the root image. We should likely
24# use labels, but we don't.
25#
26# Assumes you've already rebuilt... maybe bad? Also maybe bad: the env
27# vars should likely be conditionally set to allow better automation.
28#
29
30. $(dirname $0)/install-boot.sh
31
32cpsys() {
33    src=$1
34    dst=$2
35
36    # Copy kernel + boot loader
37    (cd $src ; tar cf - .) | (cd $dst; tar xf -)
38}
39
40mk_nogeli_gpt_ufs_legacy() {
41    src=$1
42    img=$2
43
44    cat > ${src}/etc/fstab <<EOF
45/dev/ada0p2	/		ufs	rw	1	1
46EOF
47    makefs -t ffs -B little -s 200m ${img}.p2 ${src}
48    mkimg -s gpt -b ${src}/boot/pmbr \
49	  -p freebsd-boot:=${src}/boot/gptboot \
50	  -p freebsd-ufs:=${img}.p2 -o ${img}
51    rm -f ${src}/etc/fstab
52}
53
54mk_nogeli_gpt_ufs_uefi() {
55    src=$1
56    img=$2
57
58    cat > ${src}/etc/fstab <<EOF
59/dev/ada0p2	/		ufs	rw	1	1
60EOF
61    make_esp_file ${img}.p1 ${espsize} ${src}/boot/loader.efi
62    makefs -t ffs -B little -s 200m ${img}.p2 ${src}
63    mkimg -s gpt \
64	  -p efi:=${img}.p1 \
65	  -p freebsd-ufs:=${img}.p2 -o ${img}
66    rm -f ${src}/etc/fstab
67}
68
69mk_nogeli_gpt_ufs_both() {
70    src=$1
71    img=$2
72
73    cat > ${src}/etc/fstab <<EOF
74/dev/ada0p3	/		ufs	rw	1	1
75EOF
76    make_esp_file ${img}.p1 ${espsize} ${src}/boot/loader.efi
77    makefs -t ffs -B little -s 200m ${img}.p3 ${src}
78    # p1 is boot for uefi, p2 is boot for gpt, p3 is /
79    mkimg -b ${src}/boot/pmbr -s gpt \
80	  -p efi:=${img}.p1 \
81	  -p freebsd-boot:=${src}/boot/gptboot \
82	  -p freebsd-ufs:=${img}.p3 \
83	  -o ${img}
84    rm -f ${src}/etc/fstab
85}
86
87mk_nogeli_gpt_zfs_legacy() {
88    src=$1
89    img=$2
90    mntpt=$3
91    geli=$4
92    scheme=$5
93    fs=$6
94    bios=$7
95    pool=nogeli-gpt-zfs-legacy
96
97    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
98    md=$(mdconfig -f ${img})
99    gpart create -s gpt ${md}
100    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
101    gpart add -t freebsd-zfs -l root $md
102    # install-boot will make this bootable
103    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p2
104    zpool set bootfs=${pool} ${pool}
105    zfs create -po mountpoint=/ ${pool}/ROOT/default
106    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
107    cpsys ${src} ${mntpt}
108    # need to make a couple of tweaks
109    cat >> ${mntpt}/boot/loader.conf <<EOF
110zfs_load=YES
111opensolaris_load=YES
112EOF
113    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
114    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
115    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
116    # end tweaks
117    zfs umount -f ${pool}/ROOT/default
118    zfs set mountpoint=none ${pool}/ROOT/default
119    zpool set bootfs=${pool}/ROOT/default ${pool}
120    zpool set autoexpand=on ${pool}
121    zpool export ${pool}
122    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
123    mdconfig -d -u ${md}
124}
125
126mk_nogeli_gpt_zfs_uefi() {
127    src=$1
128    img=$2
129    mntpt=$3
130    geli=$4
131    scheme=$5
132    fs=$6
133    bios=$7
134    pool=nogeli-gpt-zfs-uefi
135
136    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
137    md=$(mdconfig -f ${img})
138    gpart create -s gpt ${md}
139    gpart add -t efi -s ${espsize}k -a 4k ${md}
140    gpart add -t freebsd-zfs -l root $md
141    # install-boot will make this bootable
142    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p2
143    zpool set bootfs=${pool} ${pool}
144    zfs create -po mountpoint=/ ${pool}/ROOT/default
145    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
146    cpsys ${src} ${mntpt}
147    # need to make a couple of tweaks
148    cat >> ${mntpt}/boot/loader.conf <<EOF
149zfs_load=YES
150opensolaris_load=YES
151EOF
152    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
153    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
154    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
155    # end tweaks
156    zfs umount -f ${pool}/ROOT/default
157    zfs set mountpoint=none ${pool}/ROOT/default
158    zpool set bootfs=${pool}/ROOT/default ${pool}
159    zpool set autoexpand=on ${pool}
160    zpool export ${pool}
161    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
162    mdconfig -d -u ${md}
163}
164
165mk_nogeli_gpt_zfs_both() {
166    src=$1
167    img=$2
168    mntpt=$3
169    geli=$4
170    scheme=$5
171    fs=$6
172    bios=$7
173    pool=nogeli-gpt-zfs-both
174
175    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
176    md=$(mdconfig -f ${img})
177    gpart create -s gpt ${md}
178    gpart add -t efi -s ${espsize}k -a 4k ${md}
179    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
180    gpart add -t freebsd-zfs -l root $md
181    # install-boot will make this bootable
182    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p3
183    zpool set bootfs=${pool} ${pool}
184    zfs create -po mountpoint=/ ${pool}/ROOT/default
185    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
186    cpsys ${src} ${mntpt}
187    # need to make a couple of tweaks
188    cat >> ${mntpt}/boot/loader.conf <<EOF
189zfs_load=YES
190opensolaris_load=YES
191EOF
192    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
193    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
194    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
195    # end tweaks
196    zfs umount -f ${pool}/ROOT/default
197    zfs set mountpoint=none ${pool}/ROOT/default
198    zpool set bootfs=${pool}/ROOT/default ${pool}
199    zpool set autoexpand=on ${pool}
200    zpool export ${pool}
201    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
202    mdconfig -d -u ${md}
203}
204
205mk_nogeli_mbr_ufs_legacy() {
206    src=$1
207    img=$2
208
209    cat > ${src}/etc/fstab <<EOF
210/dev/ada0s1a	/		ufs	rw	1	1
211EOF
212    makefs -t ffs -B little -s 200m ${img}.s1a ${src}
213    mkimg -s bsd -b ${src}/boot/boot -p freebsd-ufs:=${img}.s1a -o ${img}.s1
214    mkimg -a 1 -s mbr -b ${src}/boot/boot0sio -p freebsd:=${img}.s1 -o ${img}
215    rm -f ${src}/etc/fstab
216}
217
218mk_nogeli_mbr_ufs_uefi() {
219    src=$1
220    img=$2
221
222    cat > ${src}/etc/fstab <<EOF
223/dev/ada0s2a	/		ufs	rw	1	1
224EOF
225    make_esp_file ${img}.s1 ${espsize} ${src}/boot/loader.efi
226    makefs -t ffs -B little -s 200m ${img}.s2a ${src}
227    mkimg -s bsd -p freebsd-ufs:=${img}.s2a -o ${img}.s2
228    mkimg -a 1 -s mbr -p efi:=${img}.s1 -p freebsd:=${img}.s2 -o ${img}
229    rm -f ${src}/etc/fstab
230}
231
232mk_nogeli_mbr_ufs_both() {
233    src=$1
234    img=$2
235
236    cat > ${src}/etc/fstab <<EOF
237/dev/ada0s2a	/		ufs	rw	1	1
238EOF
239    make_esp_file ${img}.s1 ${espsize} ${src}/boot/loader.efi
240    makefs -t ffs -B little -s 200m ${img}.s2a ${src}
241    mkimg -s bsd -b ${src}/boot/boot -p freebsd-ufs:=${img}.s2a -o ${img}.s2
242    mkimg -a 2 -s mbr -b ${src}/boot/mbr -p efi:=${img}.s1 -p freebsd:=${img}.s2 -o ${img}
243    rm -f ${src}/etc/fstab
244}
245
246mk_nogeli_mbr_zfs_legacy() {
247    src=$1
248    img=$2
249    mntpt=$3
250    geli=$4
251    scheme=$5
252    fs=$6
253    bios=$7
254    pool=nogeli-mbr-zfs-legacy
255
256    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
257    md=$(mdconfig -f ${img})
258    gpart create -s mbr ${md}
259    gpart add -t freebsd ${md}
260    gpart set -a active -i 1 ${md}
261    gpart create -s bsd ${md}s1
262    gpart add -t freebsd-zfs ${md}s1
263    # install-boot will make this bootable
264    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}s1a
265    zpool set bootfs=${pool} ${pool}
266    zfs create -po mountpoint=/ ${pool}/ROOT/default
267    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
268    cpsys ${src} ${mntpt}
269    # need to make a couple of tweaks
270    cat >> ${mntpt}/boot/loader.conf <<EOF
271zfs_load=YES
272opensolaris_load=YES
273EOF
274    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
275    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
276    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
277    # end tweaks
278    zfs umount -f ${pool}/ROOT/default
279    zfs set mountpoint=none ${pool}/ROOT/default
280    zpool set bootfs=${pool}/ROOT/default ${pool}
281    zpool set autoexpand=on ${pool}
282    zpool export ${pool}
283    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
284    mdconfig -d -u ${md}
285}
286
287mk_nogeli_mbr_zfs_uefi() {
288    src=$1
289    img=$2
290    mntpt=$3
291    geli=$4
292    scheme=$5
293    fs=$6
294    bios=$7
295    pool=nogeli-mbr-zfs-uefi
296
297    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
298    md=$(mdconfig -f ${img})
299    gpart create -s mbr ${md}
300    gpart add -t efi -s ${espsize}k ${md}
301    gpart add -t freebsd ${md}
302    gpart set -a active -i 2 ${md}
303    gpart create -s bsd ${md}s2
304    gpart add -t freebsd-zfs ${md}s2
305    # install-boot will make this bootable
306    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}s2a
307    zpool set bootfs=${pool} ${pool}
308    zfs create -po mountpoint=/ ${pool}/ROOT/default
309    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
310    cpsys ${src} ${mntpt}
311    # need to make a couple of tweaks
312    cat >> ${mntpt}/boot/loader.conf <<EOF
313zfs_load=YES
314opensolaris_load=YES
315EOF
316    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
317    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
318    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
319    # end tweaks
320    zfs umount -f ${pool}/ROOT/default
321    zfs set mountpoint=none ${pool}/ROOT/default
322    zpool set bootfs=${pool}/ROOT/default ${pool}
323    zpool set autoexpand=on ${pool}
324    zpool export ${pool}
325    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
326    mdconfig -d -u ${md}
327}
328
329mk_nogeli_mbr_zfs_both() {
330    src=$1
331    img=$2
332    mntpt=$3
333    geli=$4
334    scheme=$5
335    fs=$6
336    bios=$7
337    pool=nogeli-mbr-zfs-both
338
339    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
340    md=$(mdconfig -f ${img})
341    gpart create -s mbr ${md}
342    gpart add -t efi -s  ${espsize}k ${md}
343    gpart add -t freebsd ${md}
344    gpart set -a active -i 2 ${md}
345    gpart create -s bsd ${md}s2
346    gpart add -t freebsd-zfs ${md}s2
347    # install-boot will make this bootable
348    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}s2a
349    zpool set bootfs=${pool} ${pool}
350    zfs create -po mountpoint=/ ${pool}/ROOT/default
351    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
352    cpsys ${src} ${mntpt}
353    # need to make a couple of tweaks
354    cat >> ${mntpt}/boot/loader.conf <<EOF
355zfs_load=YES
356opensolaris_load=YES
357EOF
358    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
359    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
360    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
361    # end tweaks
362    zfs umount -f ${pool}/ROOT/default
363    zfs set mountpoint=none ${pool}/ROOT/default
364    zpool set bootfs=${pool}/ROOT/default ${pool}
365    zpool set autoexpand=on ${pool}
366    zpool export ${pool}
367    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
368    mdconfig -d -u ${md}
369}
370
371mk_geli_gpt_ufs_legacy() {
372    src=$1
373    img=$2
374    mntpt=$3
375    geli=$4
376    scheme=$5
377    fs=$6
378    bios=$7
379
380    dd if=/dev/zero of=${img} count=1 seek=$(( 200 * 1024 * 1024 / 512 ))
381    md=$(mdconfig -f ${img})
382    gpart create -s gpt ${md}
383    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
384    gpart add -t freebsd-ufs -l root $md
385    # install-boot will make this bootable
386    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p2
387    echo ${passphrase} | geli attach -j - ${md}p2
388    newfs /dev/${md}p2.eli
389    mount /dev/${md}p2.eli ${mntpt}
390    cpsys ${src} ${mntpt}
391    # need to make a couple of tweaks
392    cat > ${mntpt}/boot/loader.conf <<EOF
393geom_eli_load=YES
394EOF
395    cat > ${mntpt}/etc/fstab <<EOF
396/dev/ada0p2.eli	/		ufs	rw	1	1
397EOF
398
399    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
400    # end tweaks
401    umount -f ${mntpt}
402    geli detach ${md}p2
403    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
404    mdconfig -d -u ${md}
405}
406
407mk_geli_gpt_ufs_uefi() {
408    src=$1
409    img=$2
410    mntpt=$3
411    geli=$4
412    scheme=$5
413    fs=$6
414    bios=$7
415
416    dd if=/dev/zero of=${img} count=1 seek=$(( 200 * 1024 * 1024 / 512 ))
417    md=$(mdconfig -f ${img})
418    gpart create -s gpt ${md}
419    gpart add -t efi -s ${espsize}k -a 4k ${md}
420    gpart add -t freebsd-ufs -l root $md
421    # install-boot will make this bootable
422    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p2
423    echo ${passphrase} | geli attach -j - ${md}p2
424    newfs /dev/${md}p2.eli
425    mount /dev/${md}p2.eli ${mntpt}
426    cpsys ${src} ${mntpt}
427    # need to make a couple of tweaks
428    cat > ${mntpt}/boot/loader.conf <<EOF
429geom_eli_load=YES
430EOF
431    cat > ${mntpt}/etc/fstab <<EOF
432/dev/ada0p2.eli	/		ufs	rw	1	1
433EOF
434
435    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
436    # end tweaks
437    umount -f ${mntpt}
438    geli detach ${md}p2
439    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
440    mdconfig -d -u ${md}
441}
442
443mk_geli_gpt_ufs_both() {
444    src=$1
445    img=$2
446    mntpt=$3
447    geli=$4
448    scheme=$5
449    fs=$6
450    bios=$7
451
452    dd if=/dev/zero of=${img} count=1 seek=$(( 200 * 1024 * 1024 / 512 ))
453    md=$(mdconfig -f ${img})
454    gpart create -s gpt ${md}
455    gpart add -t efi -s ${espsize}k -a 4k ${md}
456    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
457    gpart add -t freebsd-ufs -l root $md
458    # install-boot will make this bootable
459    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p3
460    echo ${passphrase} | geli attach -j - ${md}p3
461    newfs /dev/${md}p3.eli
462    mount /dev/${md}p3.eli ${mntpt}
463    cpsys ${src} ${mntpt}
464    # need to make a couple of tweaks
465    cat > ${mntpt}/boot/loader.conf <<EOF
466geom_eli_load=YES
467EOF
468    cat > ${mntpt}/etc/fstab <<EOF
469/dev/ada0p3.eli	/		ufs	rw	1	1
470EOF
471
472    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
473    # end tweaks
474    umount -f ${mntpt}
475    geli detach ${md}p3
476    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
477    mdconfig -d -u ${md}
478}
479
480mk_geli_gpt_zfs_legacy() {
481    src=$1
482    img=$2
483    mntpt=$3
484    geli=$4
485    scheme=$5
486    fs=$6
487    bios=$7
488    pool=geli-gpt-zfs-legacy
489
490    # Note that in this flavor we create an empty p2 ufs partition, and put
491    # the bootable zfs stuff on p3, just to test the ability of the zfs probe
492    # probe routines to find a pool on a partition other than the first one.
493
494    dd if=/dev/zero of=${img} count=1 seek=$(( 300 * 1024 * 1024 / 512 ))
495    md=$(mdconfig -f ${img})
496    gpart create -s gpt ${md}
497    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
498    gpart add -t freebsd-ufs -s 100m ${md}
499    gpart add -t freebsd-zfs -l root $md
500    # install-boot will make this bootable
501    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p3
502    echo ${passphrase} | geli attach -j - ${md}p3
503    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p3.eli
504    zpool set bootfs=${pool} ${pool}
505    zfs create -po mountpoint=/ ${pool}/ROOT/default
506    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
507    cpsys ${src} ${mntpt}
508    # need to make a couple of tweaks
509    cat >> ${mntpt}/boot/loader.conf <<EOF
510zfs_load=YES
511opensolaris_load=YES
512geom_eli_load=YES
513EOF
514    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
515    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
516    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
517    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
518    # end tweaks
519    zfs umount -f ${pool}/ROOT/default
520    zfs set mountpoint=none ${pool}/ROOT/default
521    zpool set bootfs=${pool}/ROOT/default ${pool}
522    zpool set autoexpand=on ${pool}
523    zpool export ${pool}
524    geli detach ${md}p3
525    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
526    mdconfig -d -u ${md}
527}
528
529mk_geli_gpt_zfs_uefi() {
530    src=$1
531    img=$2
532    mntpt=$3
533    geli=$4
534    scheme=$5
535    fs=$6
536    bios=$7
537    pool=geli-gpt-zfs-uefi
538
539    # Note that in this flavor we create an empty p2 ufs partition, and put
540    # the bootable zfs stuff on p3, just to test the ability of the zfs probe
541    # probe routines to find a pool on a partition other than the first one.
542
543    dd if=/dev/zero of=${img} count=1 seek=$(( 300 * 1024 * 1024 / 512 ))
544    md=$(mdconfig -f ${img})
545    gpart create -s gpt ${md}
546    gpart add -t efi -s ${espsize}k -a 4k ${md}
547    gpart add -t freebsd-ufs -s 100m ${md}
548    gpart add -t freebsd-zfs -l root $md
549    # install-boot will make this bootable
550    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p3
551    echo ${passphrase} | geli attach -j - ${md}p3
552    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p3.eli
553    zpool set bootfs=${pool} ${pool}
554    zfs create -po mountpoint=/ ${pool}/ROOT/default
555    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
556    cpsys ${src} ${mntpt}
557    # need to make a couple of tweaks
558    cat >> ${mntpt}/boot/loader.conf <<EOF
559zfs_load=YES
560opensolaris_load=YES
561geom_eli_load=YES
562EOF
563    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
564    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
565    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
566    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
567    # end tweaks
568    zfs umount -f ${pool}/ROOT/default
569    zfs set mountpoint=none ${pool}/ROOT/default
570    zpool set bootfs=${pool}/ROOT/default ${pool}
571    zpool set autoexpand=on ${pool}
572    zpool export ${pool}
573    geli detach ${md}p3
574    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
575    mdconfig -d -u ${md}
576}
577
578mk_geli_gpt_zfs_both() {
579    src=$1
580    img=$2
581    mntpt=$3
582    geli=$4
583    scheme=$5
584    fs=$6
585    bios=$7
586    pool=geli-gpt-zfs-both
587
588    dd if=/dev/zero of=${img} count=1 seek=$(( 200 * 1024 * 1024 / 512 ))
589    md=$(mdconfig -f ${img})
590    gpart create -s gpt ${md}
591    gpart add -t efi -s ${espsize}k -a 4k ${md}
592    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
593    gpart add -t freebsd-zfs -l root $md
594    # install-boot will make this bootable
595    echo ${passphrase} | geli init -bg -e AES-XTS -i ${iterations} -J - -l 256 -s 4096 ${md}p3
596    echo ${passphrase} | geli attach -j - ${md}p3
597    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p3.eli
598    zpool set bootfs=${pool} ${pool}
599    zfs create -po mountpoint=/ ${pool}/ROOT/default
600    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
601    cpsys ${src} ${mntpt}
602    # need to make a couple of tweaks
603    cat > ${mntpt}/boot/loader.conf <<EOF
604zfs_load=YES
605opensolaris_load=YES
606geom_eli_load=YES
607EOF
608    cp /boot/kernel/acl_nfs4.ko ${mntpt}/boot/kernel/acl_nfs4.ko
609    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
610    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
611    cp /boot/kernel/geom_eli.ko ${mntpt}/boot/kernel/geom_eli.ko
612    # end tweaks
613    zfs umount -f ${pool}/ROOT/default
614    zfs set mountpoint=none ${pool}/ROOT/default
615    zpool set bootfs=${pool}/ROOT/default ${pool}
616    zpool set autoexpand=on ${pool}
617    zpool export ${pool}
618    geli detach ${md}p3
619    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
620    mdconfig -d -u ${md}
621}
622
623# GELI+MBR is not a valid configuration
624mk_geli_mbr_ufs_legacy() {
625}
626
627mk_geli_mbr_ufs_uefi() {
628}
629
630mk_geli_mbr_ufs_both() {
631}
632
633mk_geli_mbr_zfs_legacy() {
634}
635
636mk_geli_mbr_zfs_uefi() {
637}
638
639mk_geli_mbr_zfs_both() {
640}
641
642# iso
643# pxeldr
644# u-boot
645# powerpc
646
647mk_sparc64_nogeli_vtoc8_ufs_ofw() {
648    src=$1
649    img=$2
650    mntpt=$3
651    geli=$4
652    scheme=$5
653    fs=$6
654    bios=$7
655
656    cat > ${src}/etc/fstab <<EOF
657/dev/ada0a	/		ufs	rw	1	1
658EOF
659    makefs -t ffs -B big -s 200m ${img} ${src}
660    md=$(mdconfig -f ${img})
661    # For non-native builds, ensure that geom_part(4) supports VTOC8.
662    kldload geom_part_vtoc8.ko
663    gpart create -s VTOC8 ${md}
664    gpart add -t freebsd-ufs ${md}
665    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
666    mdconfig -d -u ${md}
667    rm -f ${src}/etc/fstab
668}
669
670qser="-serial telnet::4444,server -nographic"
671
672# https://wiki.freebsd.org/QemuRecipes
673# aarch64
674qemu_aarch64_uefi()
675{
676    img=$1
677    sh=$2
678
679    echo "qemu-system-aarch64 -m 4096M -cpu cortex-a57 -M virt  \
680        -bios QEMU_EFI.fd ${qser} \
681        -drive if=none,file=${img},id=hd0 \
682        -device virtio-blk-device,drive=hd0" > $sh
683    chmod 755 $sh
684# https://wiki.freebsd.org/arm64/QEMU also has
685#       -device virtio-net-device,netdev=net0
686#       -netdev user,id=net0
687}
688
689# Amd64 qemu
690qemu_amd64_legacy()
691{
692    img=$1
693    sh=$2
694
695    echo "qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser}" > $sh
696    chmod 755 $sh
697}
698
699qemu_amd64_uefi()
700{
701    img=$1
702    sh=$2
703
704    echo "qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" > $sh
705    chmod 755 $sh
706}
707
708qemu_amd64_both()
709{
710    img=$1
711    sh=$2
712
713    echo "qemu-system-x86_64 -m 256m --drive file=${img},format=raw ${qser}" > $sh
714    echo "qemu-system-x86_64 -m 256m -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" >> $sh
715    chmod 755 $sh
716}
717
718# arm
719# nothing listed?
720
721# i386
722qemu_i386_legacy()
723{
724    img=$1
725    sh=$2
726
727    echo "qemu-system-i386 --drive file=${img},format=raw ${qser}" > $sh
728    chmod 755 $sh
729}
730
731# Not yet supported
732qemu_i386_uefi()
733{
734    img=$1
735    sh=$2
736
737    echo "qemu-system-i386 -bios ~/bios/OVMF-X32.fd --drive file=${img},format=raw ${qser}" > $sh
738    chmod 755 $sh
739}
740
741# Needs UEFI to be supported
742qemu_i386_both()
743{
744    img=$1
745    sh=$2
746
747    echo "qemu-system-i386 --drive file=${img},format=raw ${qser}" > $sh
748    echo "qemu-system-i386 -bios ~/bios/OVMF-X32.fd --drive file=${img},format=raw ${qser}" >> $sh
749    chmod 755 $sh
750}
751
752make_one_image()
753{
754    local arch=${1?}
755    local geli=${2?}
756    local scheme=${3?}
757    local fs=${4?}
758    local bios=${5?}
759
760    # Create sparse file and mount newly created filesystem(s) on it
761    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
762    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
763    echo "vvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvv"
764    rm -f ${img}*
765    eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
766    eval qemu_${arch}_${bios} ${img} ${sh}
767    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
768    echo "^^^^^^^^^^^^^^   Created $img   ^^^^^^^^^^^^^^^"
769}
770
771# mips
772# qemu-system-mips -kernel /path/to/rootfs/boot/kernel/kernel -nographic -hda /path/to/disk.img -m 2048
773
774# Powerpc -- doesn't work but maybe it would enough for testing -- needs details
775# powerpc64
776# qemu-system-ppc64 -drive file=/path/to/disk.img,format=raw
777
778# sparc64
779# qemu-system-sparc64 -drive file=/path/to/disk.img,format=raw
780
781# Misc variables
782SRCTOP=$(make -v SRCTOP)
783cd ${SRCTOP}/stand
784OBJDIR=$(make -v .OBJDIR)
785IMGDIR=${OBJDIR}/boot-images
786mkdir -p ${IMGDIR}
787MNTPT=$(mktemp -d /tmp/stand-test.XXXXXX)
788
789# Setup the installed tree...
790DESTDIR=${OBJDIR}/boot-tree
791rm -rf ${DESTDIR}
792mkdir -p ${DESTDIR}/boot/defaults
793mkdir -p ${DESTDIR}/boot/kernel
794cp /boot/kernel/kernel ${DESTDIR}/boot/kernel
795echo -h -D -S115200 > ${DESTDIR}/boot.config
796cat > ${DESTDIR}/boot/loader.conf <<EOF
797console=comconsole
798comconsole_speed=115200
799boot_serial=yes
800boot_multicons=yes
801EOF
802# XXX
803cp /boot/device.hints ${DESTDIR}/boot/device.hints
804# Assume we're already built
805make install DESTDIR=${DESTDIR} MK_MAN=no MK_INSTALL_AS_USER=yes WITHOUT_DEBUG_FILES=yes
806if [ $? -ne 0 ]; then
807        echo "make install failed"
808        exit 1
809fi
810# Copy init, /bin/sh, minimal libraries and testing /etc/rc
811mkdir -p ${DESTDIR}/sbin ${DESTDIR}/bin \
812      ${DESTDIR}/lib ${DESTDIR}/libexec \
813      ${DESTDIR}/etc ${DESTDIR}/dev
814for f in /sbin/halt /sbin/init /bin/sh /sbin/sysctl $(ldd /bin/sh | awk 'NF == 4 { print $3; }') /libexec/ld-elf.so.1; do
815    cp $f ${DESTDIR}/$f
816done
817cat > ${DESTDIR}/etc/rc <<EOF
818#!/bin/sh
819
820sysctl machdep.bootmethod
821echo "RC COMMAND RUNNING -- SUCCESS!!!!!"
822halt -p
823EOF
824
825# If we were given exactly 5 args, go make that one image.
826
827if [ $# -eq 5 ]; then
828    make_one_image $*
829    exit
830fi
831
832# OK. Let the games begin
833
834for arch in amd64; do
835    for geli in nogeli geli; do
836	for scheme in gpt mbr; do
837	    for fs in ufs zfs; do
838		for bios in legacy uefi both; do
839		    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
840		done
841	    done
842	done
843    done
844done
845
846rmdir ${MNTPT}
847
848exit 0
849
850# Notes for the future
851
852for arch in i386; do
853    for geli in nogeli geli; do
854	for scheme in gpt mbr; do
855	    for fs in ufs zfs; do
856		for bios in legacy; do
857		    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
858		done
859	    done
860	done
861    done
862done
863
864for arch in arm aarch64; do
865    for scheme in gpt mbr; do
866	fs=ufs
867	for bios in uboot efi; do
868	    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
869	done
870    done
871done
872
873for arch in powerpc powerpc64; do
874    for scheme in ppc-wtf; do
875	fs=ufs
876	for bios in ofw uboot chrp; do
877	    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
878	done
879    done
880done
881
882for arch in sparc64; do
883    for geli in nogeli; do
884	for scheme in vtoc8; do
885	    for fs in ufs; do
886		for bios in ofw; do
887		    make_one_image ${arch} ${geli} ${scheme} ${fs} ${bios}
888		done
889	    done
890	done
891    done
892done
893