make-memstick.sh revision 264935
1#!/bin/sh
2#
3# This script generates a "memstick image" for UEFI-capable systems.
4#
5# Prerequisites:
6#  - 'make release'
7#  - KERNCONF *must* be VT (or vt_efifb(4) compiled into the kernel)
8#
9# Note:  This only works for amd64, because i386 lacks the EFI boot bits.
10#
11# Usage: make-memstick.sh <directory tree> <image filename>
12#
13# $FreeBSD: head/release/amd64/make-uefi-memstick.sh 264935 2014-04-25 21:11:00Z gjb $
14#
15
16PATH=/bin:/usr/bin:/sbin:/usr/sbin
17export PATH
18
19if [ $# -ne 2 ]; then
20	echo "make-memstick.sh /path/to/directory /path/to/image/file"
21	exit 1
22fi
23
24if [ ! -d ${1} ]; then
25	echo "${1} must be a directory"
26	exit 1
27fi
28
29if [ -e ${2} ]; then
30	echo "won't overwrite ${2}"
31	exit 1
32fi
33
34dirsize=$(du -shLm ${1} | awk '{print $1}')
35dirsize=$(( $(( $(( ${dirsize} + 256 )) * 1024 * 1024 )) ))
36truncate -s ${dirsize} ${2}
37
38unit=$(mdconfig -a -t vnode -f ${2})
39gpart create -s mbr /dev/${unit}
40gpart add -t '!0xEF' -s 32M /dev/${unit}
41gpart add -t freebsd /dev/${unit}
42gpart set -a active -i 2 /dev/${unit}
43gpart bootcode -b ${1}/boot/boot0 /dev/${unit}
44gpart create -s bsd -n 20 /dev/${unit}s2
45gpart add -t freebsd-ufs /dev/${unit}s2
46gpart bootcode -b ${1}/boot/boot /dev/${unit}s2
47newfs_msdos /dev/${unit}s1
48newfs -L rootfs /dev/${unit}s2a
49mkdir -p ${1}/mnt
50mount -t msdosfs /dev/${unit}s1 ${1}/mnt
51mkdir -p ${1}/mnt/efi/boot
52cp -p ${1}/boot/boot1.efi ${1}/mnt/efi/boot/BOOTx64.efi
53
54while ! umount ${1}/mnt; do
55	sleep 1
56done
57
58mkdir -p mnt
59mount /dev/${unit}s2a mnt
60tar -cf - -C ${1} . | tar -xf - -C mnt
61echo "/dev/ufs/rootfs / ufs ro,noatime 1 1" > mnt/etc/fstab
62
63while ! umount mnt; do
64	sleep 1
65done
66
67# Default boot selection to MBR so systems that do not support UEFI
68# do not fail to boot without human interaction.
69boot0cfg -s 2 /dev/${unit}
70
71mdconfig -d -u ${unit}
72rmdir mnt
73
74