xref: /freebsd/tools/boot/rootgen.sh (revision f5147e312f43a9050468de539aeafa072caa1a60)
1#!/bin/sh
2
3# $FreeBSD$
4
5#
6# Builds all the bat-shit crazy combinations we support booting from,
7# at least for amd64. It assume you have a ~sane kernel in /boot/kernel
8# and copies that into the ~150MB root images we create (we create the du
9# size of the kernel + 20MB
10#
11# Sad panda sez: this runs as root, but could be userland if someone
12# creates userland geli and zfs tools.
13#
14# This assumes an external prograam install-boot.sh which will install
15# the appropriate boot files in the appropriate locations.
16#
17# These images assume ada0 will be the root image. We should likely
18# use labels, but we don't.
19#
20# ASsumes you've already rebuilt... maybe bad? Also maybe bad: the env
21# vars should likely be conditionally set to allow better automation.
22#
23
24cpsys() {
25    src=$1
26    dst=$2
27
28    # Copy kernel + boot loader
29    (cd $src ; tar cf - .) | (cd $dst; tar xf -)
30}
31
32mk_nogeli_gpt_ufs_legacy() {
33    src=$1
34    img=$2
35
36    cat > ${src}/etc/fstab <<EOF
37/dev/ada0p2	/		ufs	rw	1	1
38EOF
39    makefs -t ffs -B little -s 200m ${img}.p2 ${src}
40    mkimg -s gpt -b ${src}/boot/pmbr \
41	  -p freebsd-boot:=${src}/boot/gptboot \
42	  -p freebsd-ufs:=${img}.p2 -o ${img}
43    rm -f ${src}/etc/fstab
44}
45
46mk_nogeli_gpt_ufs_uefi() {
47    src=$1
48    img=$2
49
50    cat > ${src}/etc/fstab <<EOF
51/dev/ada0p2	/		ufs	rw	1	1
52EOF
53# XXX need to make msdos part for this to work XXXX
54    cp ${src}/boot/boot.efifat ${img}.p1
55    makefs -t ffs -B little -s 200m ${img}.p2 ${src}
56    mkimg -s gpt -b ${src}/boot/pmbr \
57	  -p efi:=${img}.p1 \
58	  -p freebsd-ufs:=${img}.p2 -o ${img}
59    rm -f ${src}/etc/fstab
60}
61
62mk_nogeli_gpt_ufs_both() {
63    src=$1
64    img=$2
65
66    cat > ${src}/etc/fstab <<EOF
67/dev/ada0p3	/		ufs	rw	1	1
68EOF
69    # XXX need to make msdos part for this to work XXXX
70    cp ${src}/boot/boot.efifat ${img}.p1
71    makefs -t ffs -B little -s 200m ${img}.p3 ${src}
72    # p1 is boot for uefi, p2 is boot for gpt, p3 is /
73    mkimg -b ${src}/boot/pmbr -s gpt \
74	  -p efi:=${src}/boot/boot1.efifat \
75	  -p freebsd-boot:=${src}/boot/gptboot \
76	  -p freebsd-ufs:=${img}.p3 \
77	  -o ${img}
78    rm -f ${src}/etc/fstab
79}
80
81mk_nogeli_gpt_zfs_legacy() {
82    src=$1
83    img=$2
84    mntpt=$3
85    geli=$4
86    scheme=$5
87    fs=$6
88    bios=$7
89    pool=nogeli-gpt-zfs-legacy
90
91    dd if=/dev/zero of=${img} count=1 seek=$((200 * 1024 * 1024 / 512))
92    md=$(mdconfig -f ${img})
93    gpart create -s gpt ${md}
94    gpart add -t freebsd-boot -s 400k -a 4k	${md}	# <= ~540k
95    gpart add -t freebsd-zfs -l root $md
96    # install-boot will make this bootable
97    zpool create -O mountpoint=none -R ${mntpt} ${pool} ${md}p2
98    zpool set bootfs=${pool} ${pool}
99    zfs create -o mountpoint=/ ${pool}/ROOT
100    # NB: The online guides go nuts customizing /var and other mountpoints here, no need
101    cpsys ${src} ${mntpt}
102    df
103    # need to make a couple of tweaks
104    cat > ${mntpt}/boot/loader.conf <<EOF
105zfs_load=YES
106opensolaris_load=YES
107EOF
108    cp /boot/kernel/zfs.ko ${mntpt}/boot/kernel/zfs.ko
109    cp /boot/kernel/opensolaris.ko ${mntpt}/boot/kernel/opensolaris.ko
110    ls -las ${mntpt}/boot
111    # end tweaks
112    zfs umount -f ${pool}/ROOT
113    zfs set mountpoint=none ${pool}/ROOT
114    zpool set bootfs=${pool}/ROOT ${pool}
115    zpool set autoexpand=on ${pool}
116    zpool export ${pool}
117    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
118    mdconfig -d -u ${md}
119}
120
121mk_nogeli_gpt_zfs_uefi() {
122}
123
124mk_nogeli_gpt_zfs_both() {
125}
126
127mk_nogeli_mbr_ufs_legacy() {
128    src=$1
129    img=$2
130
131    cat > ${src}/etc/fstab <<EOF
132/dev/ada0s1a	/		ufs	rw	1	1
133EOF
134    makefs -t ffs -B little -s 200m ${img}.s1a ${src}
135    mkimg -s bsd -b ${src}/boot/boot -p freebsd-ufs:=${img}.s1a -o ${img}.s1
136    mkimg -a 1 ${bootmbr} -s mbr -b ${src}/boot/boot0sio -p freebsd:=${img}.s1 -o ${img}
137    rm -f ${src}/etc/fstab
138}
139
140mk_nogeli_mbr_ufs_uefi() {
141}
142
143mk_nogeli_mbr_ufs_both() {
144}
145
146mk_nogeli_mbr_zfs_legacy() {
147}
148
149mk_nogeli_mbr_zfs_uefi() {
150}
151
152mk_nogeli_mbr_zfs_both() {
153}
154
155mk_geli_gpt_ufs_legacy() {
156}
157
158mk_geli_gpt_ufs_uefi() {
159}
160
161mk_geli_gpt_ufs_both() {
162}
163
164mk_geli_gpt_zfs_legacy() {
165}
166
167mk_geli_gpt_zfs_uefi() {
168}
169
170mk_geli_gpt_zfs_both() {
171}
172
173mk_geli_mbr_ufs_legacy() {
174}
175
176mk_geli_mbr_ufs_uefi() {
177}
178
179mk_geli_mbr_ufs_both() {
180}
181
182mk_geli_mbr_zfs_legacy() {
183}
184
185mk_geli_mbr_zfs_uefi() {
186}
187
188mk_geli_mbr_zfs_both() {
189}
190
191# iso
192# pxeldr
193# u-boot
194# powerpc
195
196mk_sparc64_nogeli_vtoc8_ufs_ofw() {
197    src=$1
198    img=$2
199    mntpt=$3
200    geli=$4
201    scheme=$5
202    fs=$6
203    bios=$7
204
205    cat > ${src}/etc/fstab <<EOF
206/dev/ada0a	/		ufs	rw	1	1
207EOF
208    makefs -t ffs -B big -s 200m ${img} ${src}
209    md=$(mdconfig -f ${img})
210    # For non-native builds, ensure that geom_part(4) supports VTOC8.
211    kldload geom_part_vtoc8.ko
212    gpart create -s VTOC8 ${md}
213    gpart add -t freebsd-ufs ${md}
214    ${SRCTOP}/tools/boot/install-boot.sh -g ${geli} -s ${scheme} -f ${fs} -b ${bios} -d ${src} ${md}
215    mdconfig -d -u ${md}
216    rm -f ${src}/etc/fstab
217}
218
219qser="-serial telnet::4444,server -nographic"
220
221# https://wiki.freebsd.org/QemuRecipes
222# aarch64
223qemu_aarch64_uefi()
224{
225    img=$1
226    sh=$2
227
228    echo "qemu-system-aarch64 -m 4096M -cpu cortex-a57 -M virt  \
229        -bios QEMU_EFI.fd ${qser} \
230        -drive if=none,file=${img},id=hd0 \
231        -device virtio-blk-device,drive=hd0" > $sh
232# https://wiki.freebsd.org/arm64/QEMU also has
233#       -device virtio-net-device,netdev=net0
234#       -netdev user,id=net0
235}
236
237# Amd64 qemu
238qemu_amd64_legacy()
239{
240    img=$1
241    sh=$2
242
243    echo "qemu-system-x86_64 --drive file=${img},format=raw ${qser}" > $sh
244}
245
246qemu_amd64_uefi()
247{
248    img=$1
249    sh=$2
250
251    echo "qemu-system-x86_64 -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" > $sh
252}
253
254qemu_amd64_both()
255{
256    img=$1
257    sh=$2
258
259    echo "qemu-system-x86_64 --drive file=${img},format=raw ${qser}" > $sh
260    echo "qemu-system-x86_64 -bios ~/bios/OVMF-X64.fd --drive file=${img},format=raw ${qser}" > $sh
261}
262
263# arm
264# nothing listed?
265
266# i386
267qemu_i386_legacy()
268{
269    img=$1
270    sh=$2
271
272    echo "qemu-system-i386 --drive file=${img},format=raw ${qser}" > $sh
273}
274
275# Not yet supported
276qemu_i386_uefi()
277{
278    img=$1
279    sh=$2
280
281    echo "qemu-system-i386 -bios ~/bios/OVMF-X32.fd --drive file=${img},format=raw ${qser}" > $sh
282}
283
284# Needs UEFI to be supported
285qemu_i386_both()
286{
287    img=$1
288    sh=$2
289
290    echo "qemu-system-i386 --drive file=${img},format=raw ${qser}" > $sh
291    echo "qemu-system-i386 -bios ~/bios/OVMF-X32.fd --drive file=${img},format=raw ${qser}" > $sh
292}
293
294# mips
295# qemu-system-mips -kernel /path/to/rootfs/boot/kernel/kernel -nographic -hda /path/to/disk.img -m 2048
296
297# Powerpc -- doesn't work but maybe it would enough for testing -- needs details
298# powerpc64
299# qemu-system-ppc64 -drive file=/path/to/disk.img,format=raw
300
301# sparc64
302# qemu-system-sparc64 -drive file=/path/to/disk.img,format=raw
303
304# Misc variables
305SRCTOP=$(make -v SRCTOP)
306cd ${SRCTOP}/stand
307OBJDIR=$(make -v .OBJDIR)
308IMGDIR=${OBJDIR}/boot-images
309mkdir -p ${IMGDIR}
310MNTPT=$(mktemp -d /tmp/stand-test.XXXXXX)
311
312# Setup the installed tree...
313DESTDIR=${OBJDIR}/boot-tree
314rm -rf ${DESTDIR}
315mkdir -p ${DESTDIR}/boot/defaults
316mkdir -p ${DESTDIR}/boot/kernel
317# XXX boot1 exists only on sparc64
318cp /boot/boot1 ${DESTDIR}/boot
319cp /boot/loader ${DESTDIR}/boot
320cp /boot/kernel/kernel ${DESTDIR}/boot/kernel
321echo -h -D -S115200 > ${DESTDIR}/boot.config
322# XXX
323cp /boot/device.hints ${DESTDIR}/boot/device.hints
324# Assume we're already built
325make install DESTDIR=${DESTDIR} MK_MAN=no MK_INSTALL_AS_USER=yes
326# Copy init, /bin/sh, minimal libraries and testing /etc/rc
327mkdir -p ${DESTDIR}/sbin ${DESTDIR}/bin \
328      ${DESTDIR}/lib ${DESTDIR}/libexec \
329      ${DESTDIR}/etc ${DESTDIR}/dev
330for f in /sbin/halt /sbin/init /bin/sh $(ldd /bin/sh | awk 'NF == 4 { print $3; }') /libexec/ld-elf.so.1; do
331    cp $f ${DESTDIR}/$f
332done
333cat > ${DESTDIR}/etc/rc <<EOF
334#!/bin/sh
335
336echo "RC COMMAND RUNNING -- SUCCESS!!!!!"
337halt -p
338EOF
339
340# OK. Let the games begin
341
342for arch in amd64; do
343    for geli in nogeli geli; do
344	for scheme in gpt mbr; do
345	    for fs in ufs zfs; do
346		for bios in legacy uefi both; do
347		    # Create sparse file and mount newly created filesystem(s) on it
348		    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
349		    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
350		    echo "vvvvvvvvvvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvvvvvvvvvv"
351		    rm -f ${img}*
352		    eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
353		    eval qemu_${arch}_${bios} ${img} ${sh}
354		    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
355		    echo "^^^^^^^^^^^^^^^^^^^^^^   Creating $img  ^^^^^^^^^^^^^^^^^^^^^^^"
356		done
357	    done
358	done
359    done
360done
361
362rmdir ${MNTPT}
363
364exit 0
365
366# Notes for the future
367
368for arch in i386; do
369    for geli in nogeli geli; do
370	for scheme in gpt mbr; do
371	    for fs in ufs zfs; do
372		for bios in legacy; do
373		    # Create sparse file and mount newly created filesystem(s) on it
374		    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
375		    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
376		    echo "vvvvvvvvvvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvvvvvvvvvv"
377		    rm -f ${img}*
378		    eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
379		    eval qemu_${arch}_${bios} ${img} ${sh}
380		    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
381		    echo "^^^^^^^^^^^^^^^^^^^^^^   Creating $img  ^^^^^^^^^^^^^^^^^^^^^^^"
382		done
383	    done
384	done
385    done
386done
387
388for arch in arm aarch64; do
389    for scheme in gpt mbr; do
390	fs=ufs
391	for bios in uboot efi; do
392	    # Create sparse file and mount newly created filesystem(s) on it
393	    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
394	    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
395	    echo "vvvvvvvvvvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvvvvvvvvvv"
396	    rm -f ${img}*
397	    eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
398	    eval qemu_${arch}_${bios} ${img} ${sh}
399	    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
400	    echo "^^^^^^^^^^^^^^^^^^^^^^   Creating $img  ^^^^^^^^^^^^^^^^^^^^^^^"
401	done
402    done
403done
404
405for arch in powerpc powerpc64; do
406    for scheme in ppc-wtf; do
407	fs=ufs
408	for bios in ofw uboot chrp; do
409	    # Create sparse file and mount newly created filesystem(s) on it
410	    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
411	    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
412	    echo "vvvvvvvvvvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvvvvvvvvvv"
413	    rm -f ${img}*
414	    eval mk_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
415	    eval qemu_${arch}_${bios} ${img} ${sh}
416	    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
417	    echo "^^^^^^^^^^^^^^^^^^^^^^   Creating $img  ^^^^^^^^^^^^^^^^^^^^^^^"
418	done
419    done
420done
421
422for arch in sparc64; do
423    for geli in nogeli; do
424	for scheme in vtoc8; do
425	    for fs in ufs; do
426		for bios in ofw; do
427		    # Create sparse file and mount newly created filesystem(s) on it
428		    img=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.img
429		    sh=${IMGDIR}/${arch}-${geli}-${scheme}-${fs}-${bios}.sh
430		    echo "vvvvvvvvvvvvvvvvvvvvvv   Creating $img  vvvvvvvvvvvvvvvvvvvvvvv"
431		    rm -f ${img}*
432		    eval mk_${arch}_${geli}_${scheme}_${fs}_${bios} ${DESTDIR} ${img} ${MNTPT} ${geli} ${scheme} ${fs} ${bios}
433		    eval qemu_${arch}_${bios} ${img} ${sh}
434		    [ -n "${SUDO_USER}" ] && chown ${SUDO_USER} ${img}*
435		    echo "^^^^^^^^^^^^^^^^^^^^^^   Creating $img  ^^^^^^^^^^^^^^^^^^^^^^^"
436		done
437	    done
438	done
439    done
440done
441