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