]> CyberLeo.Net >> Repos - CDN/Mosi.git/blob - script/gencard
Run saveconfig twice on shutdown, as early as possible and as late as possible; early...
[CDN/Mosi.git] / script / gencard
1 #!/bin/sh
2
3 _root="$(dirname "${0}")"; . "${_root}/lib/env.sh"
4 want root log
5
6 pebkac(){
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."
10   echo ""
11   echo "Options:"
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"
17   echo " -h          Hi!"
18   exit 1
19 }
20
21 img_sects(){
22   [ ! -f "${1}" ] && return 0
23   (
24     eval $(stat -s "${1}")
25     echo "$(( ( ${st_size} + ( ${st_size} % 512 ) ) / 512 ))"
26   )
27 }
28
29 compute_label(){
30   # Params: 1: cardsects 2: boot.img 3: root.img
31   card_size="${1}"
32   boot_file="${2}"
33   root_file="${3}"
34   [ -z "${card_size}" -o -z "${boot_file}" -o -z "${root_file}" ] && pebkac "RTFS, bitch!"
35   
36   omfg_ofst=0
37   omfg_size=16
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} ))"
45   
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"
50 }
51
52 # Defaults
53 boot_img="boot.img"
54 root_img="root.img"
55 conf_dir="conf"
56 card_img="card.img"
57 card_size=1000944
58
59 # Parse command line options
60 while getopts "b:r:c:o:s:h" opt
61 do
62   case "${opt}" in
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}" ;;
68     h) pebkac ;;
69     [?]) pebkac "Unrecognized option ${opt}" ;;
70   esac
71 done
72 shift $(( $OPTIND - 1 ))
73
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 "
78
79 log Card size: ${card_size} sectors
80 rm -f "${card_img}"
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
85
86 compute_label "${card_size}" "${boot_img}" "${root_img}" | bsdlabel -R -B "${md}" /dev/stdin
87 log Applied label:
88 bsdlabel "${md}"
89
90 log Writing boot image from "${boot_img}"
91 dd if="${boot_img}" of="/dev/${md}a" bs=131072 2>/dev/null
92
93 log Writing root image from "${root_img}"
94 dd if="${root_img}" of="/dev/${md}d" bs=131072 2>/dev/null
95
96 log Writing conf image
97 newfs -U -L conf "/dev/${md}e"
98
99 log Populating conf from "${conf_dir}"
100 mount "/dev/${md}e" "${mnt}"
101 ( cd "${conf_dir}"; find . | cpio -p "${mnt}" )
102
103 sync