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