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 <bytes> Intended card size, in 512-byte sectors"
22 [ ! -f "${1}" ] && return 0
24 eval $(stat -s "${1}")
25 echo "$(( ( ${st_size} + ( ${st_size} % 512 ) ) / 512 ))"
30 # Params: 1: cardsects 2: boot.img 3: root.img
34 [ -z "${card_size}" -o -z "${boot_file}" -o -z "${root_file}" ] && pebkac "RTFS, bitch!"
38 boot_ofst="$(( ${omfg_ofst} + ${omfg_size} ))"
39 boot_size="$(img_sects "${boot_file}")"
40 root_ofst="$(( ${boot_ofst} + ${boot_size} ))"
41 root_size="$(img_sects "${root_file}")"
42 conf_ofst="$(( ${root_ofst} + ${root_size} ))"
43 conf_size="$(( ${card_size} - ( ${omfg_size} + ${boot_size} + ${root_size} ) ))"
44 last_ofst="$(( ${conf_ofst} + ${conf_size} ))"
46 echo "a: ${boot_size} ${boot_ofst} 4.2BSD"
47 echo "c: ${card_size} 0 unused"
48 echo "d: ${root_size} ${root_ofst} 4.2BSD"
49 echo "e: ${conf_size} ${conf_ofst} 4.2BSD"
59 # Parse command line options
60 while getopts "b:r:c:o:s:h" opt
63 b) boot_img="${OPTARG}" ;;
64 r) root_img="${OPTARG}" ;;
65 c) conf_dir="${OPTARG}" ;;
66 o) card_img="${OPTARG}" ;;
67 s) card_size="${OPTARG}" ;;
69 [?]) pebkac "Unrecognized option ${opt}" ;;
72 shift $(( $OPTIND - 1 ))
74 [ -f "${boot_img}" ] || err "${boot_img}: no such file or directory"
75 [ -f "${root_img}" ] || err "${root_img}: no such file or directory"
76 [ -d "${conf_dir}" ] || err "${conf_dir}: not a directory"
77 [ "${card_size}" -gt 0 ] || err "${card_size}: not a number "
79 log Card size: ${card_size} sectors
81 dd if=/dev/zero of="${card_img}" bs=512 count=0 seek="${card_size}" 2>/dev/null
82 md=$(mdconfig -a -t vnode -f "${card_img}")
83 mnt="$(mktemp -d /tmp/conf.XXXXXXXX)"
84 trap "umount '${mnt}'; rm -Rf '${mnt}'; mdconfig -d -u '${md}'" exit hup int term kill
86 compute_label "${card_size}" "${boot_img}" "${root_img}" | bsdlabel -R -B "${md}" /dev/stdin
90 log Writing boot image from "${boot_img}"
91 dd if="${boot_img}" of="/dev/${md}a" bs=131072 2>/dev/null
93 log Writing root image from "${root_img}"
94 dd if="${root_img}" of="/dev/${md}d" bs=131072 2>/dev/null
96 log Writing conf image
97 newfs -U -L conf "/dev/${md}e"
99 log Populating conf from "${conf_dir}"
100 mount "/dev/${md}e" "${mnt}"
101 ( cd "${conf_dir}"; find . | cpio -p "${mnt}" )