3 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
7 [ "${*}" ] && echo "$(basename "${0}"): ${*}" && echo ""
8 echo "Generate a card image using the specified boot and root images,"
9 echo " and the optional conf directory to fill the free space."
12 echo " -b <image> Boot image filename (default boot.img)"
13 echo " -r <image> Root image filename (default root.img)"
14 echo " -c <dir> Config directory to embed into card image"
15 echo " -o <image> Card image filename to write (default card.img)"
16 echo " -s <sects> Intended card size, in 512-byte sectors"
17 echo " -a <image> Bootcode file to use (default /boot/boot)"
23 [ ! -f "${1}" ] && return 0
25 eval $(stat -s "${1}")
26 echo "$(( ( ${st_size} + ( ${st_size} % 512 ) ) / 512 ))"
31 # Params: 1: cardsects 2: boot.img 3: root.img
35 [ -z "${card_size}" -o -z "${boot_file}" -o -z "${root_file}" ] && pebkac "RTFS, bitch!"
39 boot_ofst="$(( ${omfg_ofst} + ${omfg_size} ))"
40 boot_size="$(img_sects "${boot_file}")"
41 root_ofst="$(( ${boot_ofst} + ${boot_size} ))"
42 root_size="$(img_sects "${root_file}")"
43 conf_ofst="$(( ${root_ofst} + ${root_size} ))"
44 conf_size="$(( ${card_size} - ( ${omfg_size} + ${boot_size} + ${root_size} ) ))"
45 last_ofst="$(( ${conf_ofst} + ${conf_size} ))"
47 echo "a: ${boot_size} ${boot_ofst} 4.2BSD"
48 echo "b: ${omfg_size} ${omfg_ofst} boot"
49 echo "c: ${card_size} 0 unused"
50 echo "d: ${root_size} ${root_ofst} ISO9660"
51 echo "e: ${conf_size} ${conf_ofst} 4.2BSD"
57 # Since almost all of the boot process refers absolutely to /boot/... and
58 # cannot work when /boot is on its own filesystem, the bootcode in /boot/boot
59 # must be moved to make way for a 'boot -> .' symlink so that it works.
60 # Unfortunately, this breaks bsdlabel, which expects the bootcode to be in
61 # /boot/boot. bsdlabel will happily store whatever is in /boot/boot into the
62 # track, including the directory itself. >o.O<
63 [ ! -f "${bootcode}" -a -f "/boot/boot.blk" ] && bootcode="/boot/boot.blk"
71 # Parse command line options
72 while getopts "b:r:c:o:s:a:h" opt
75 b) boot_img="${OPTARG}" ;;
76 r) root_img="${OPTARG}" ;;
77 c) conf_dir="${OPTARG}" ;;
78 o) card_img="${OPTARG}" ;;
79 s) card_size="${OPTARG}" ;;
80 a) bootcode="${OPTARG}" ;;
82 [?]) pebkac "Unrecognized option ${opt}" ;;
85 shift $(( $OPTIND - 1 ))
87 [ -f "${boot_img}" ] || err "${boot_img}: no such file or directory"
88 [ -f "${root_img}" ] || err "${root_img}: no such file or directory"
89 [ -d "${conf_dir}" ] || err "${conf_dir}: not a directory"
90 [ "${card_size}" -gt 0 ] || err "${card_size}: not a number "
91 [ -f "${bootcode}" ] || err "${bootcode}: not a regular file"
93 log Card size: ${card_size} sectors
95 dd if=/dev/zero of="${card_img}" bs=512 count=0 seek="${card_size}" 2>/dev/null
96 md=$(mdconfig -a -t vnode -f "${card_img}")
97 mnt="$(mktemp -d /tmp/conf.XXXXXXXX)"
98 trap "umount '${mnt}'; rm -Rf '${mnt}'; mdconfig -d -u '${md}'" exit hup int term kill
100 compute_label "${card_size}" "${boot_img}" "${root_img}" | bsdlabel -R -B -b "${bootcode}" "${md}" /dev/stdin
104 log Writing boot image from "${boot_img}"
105 dd if="${boot_img}" of="/dev/${md}a" bs=131072 2>/dev/null
107 log Writing root image from "${root_img}"
108 dd if="${root_img}" of="/dev/${md}d" bs=131072 2>/dev/null
110 log Writing conf image
111 newfs -U -L conf "/dev/${md}e"
113 log Populating conf from "${conf_dir}"
114 mount "/dev/${md}e" "${mnt}"
115 ( cd "${conf_dir}"; find . | cpio -p "${mnt}" )