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