| @@ -1,16 +1,16 @@ | | | @@ -1,16 +1,16 @@ |
1 | #! /bin/sh | | 1 | #! /bin/sh |
2 | | | 2 | |
3 | # $NetBSD: mkimage,v 1.11 2013/01/15 21:04:41 christos Exp $ | | 3 | # $NetBSD: mkimage,v 1.12 2013/01/16 15:58:19 christos Exp $ |
4 | | | 4 | |
5 | # Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> | | 5 | # Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> |
6 | # All rights reserved. | | 6 | # All rights reserved. |
7 | # | | 7 | # |
8 | # Redistribution and use in source and binary forms, with or without | | 8 | # Redistribution and use in source and binary forms, with or without |
9 | # modification, are permitted provided that the following conditions | | 9 | # modification, are permitted provided that the following conditions |
10 | # are met: | | 10 | # are met: |
11 | # 1. Redistributions of source code must retain the above copyright | | 11 | # 1. Redistributions of source code must retain the above copyright |
12 | # notice, this list of conditions and the following disclaimer. | | 12 | # notice, this list of conditions and the following disclaimer. |
13 | # 2. Redistributions in binary form must reproduce the above copyright | | 13 | # 2. Redistributions in binary form must reproduce the above copyright |
14 | # notice, this list of conditions and the following disclaimer in the | | 14 | # notice, this list of conditions and the following disclaimer in the |
15 | # documentation and/or other materials provided with the distribution. | | 15 | # documentation and/or other materials provided with the distribution. |
16 | # | | 16 | # |
| @@ -58,62 +58,73 @@ next_avail () | | | @@ -58,62 +58,73 @@ next_avail () |
58 | echo "${dev}${N}" | | 58 | echo "${dev}${N}" |
59 | return | | 59 | return |
60 | fi | | 60 | fi |
61 | done | | 61 | done |
62 | | | 62 | |
63 | test -e /dev/"${dev}${N}a" || { | | 63 | test -e /dev/"${dev}${N}a" || { |
64 | echo >&2 "All ${dev}s in use" | | 64 | echo >&2 "All ${dev}s in use" |
65 | return 1 | | 65 | return 1 |
66 | } | | 66 | } |
67 | | | 67 | |
68 | echo "${dev}${N}" | | 68 | echo "${dev}${N}" |
69 | } | | 69 | } |
70 | | | 70 | |
| | | 71 | usage() { |
| | | 72 | cat << EOF 1>&2 |
| | | 73 | Usage: $PROG [-S <setsdir>] [-c <custom-files-dir>] [-h <host-arch>] [-s <size>] |
| | | 74 | EOF |
| | | 75 | exit 1 |
| | | 76 | } |
| | | 77 | |
| | | 78 | # Return the filesystem size for an ls -l or tar -xvf list |
| | | 79 | # Directories and symlinks in tar are 0 size, we assume one block |
| | | 80 | # (which is too much), we round up by the fragment size the rest. |
| | | 81 | getfssize() { |
| | | 82 | local bsize="$1" |
| | | 83 | local fsize="$2" |
| | | 84 | |
| | | 85 | awk -v fsize=${fsize} -v bsize=${bsize} ' |
| | | 86 | NF >= 9 && $1 != "tar:" { |
| | | 87 | if ($5 == 0) |
| | | 88 | tot += bsize; |
| | | 89 | else |
| | | 90 | tot += ((int)(($5 + fsize - 1) / fsize)) * fsize; |
| | | 91 | } |
| | | 92 | END { |
| | | 93 | printf("%d\n", tot); |
| | | 94 | }' |
| | | 95 | } |
| | | 96 | |
71 | # find the size of the gzipped files in a .tgz archive | | 97 | # find the size of the gzipped files in a .tgz archive |
72 | # Directories appear as 0, so count them as one block | | 98 | # Directories appear as 0, so count them as one block |
73 | # and round up files to a fragment. | | 99 | # and round up files to a fragment. |
74 | sizeone() { | | 100 | sizeone() { |
75 | if [ ! -f "$1" ] | | 101 | if [ ! -f "$1" ] |
76 | then | | 102 | then |
77 | echo "$PROG: Missing set $1" 1>&2 | | 103 | echo "$PROG: Missing set $1" 1>&2 |
78 | echo 0 | | 104 | echo 0 |
79 | return; | | 105 | return; |
80 | fi | | 106 | fi |
| | | 107 | |
81 | case "$1" in | | 108 | case "$1" in |
82 | *.tgz|*.tar.gz|*.tbz|*.tar.bz2|*.txz|*.tar.xz) | | 109 | *.tgz|*.tar.gz|*.tbz|*.tar.bz2|*.txz|*.tar.xz) |
83 | tar tvzf "$1" | | | 110 | tar tvzf "$1" | getfssize ${bsize} ${fsize} |
84 | awk -v fsize=${fsize} -v bsize=${bsize} ' | | | |
85 | { | | | |
86 | if ($5 == 0) | | | |
87 | tot += bsize; | | | |
88 | else | | | |
89 | tot += ((int)(($5 + fsize - 1) / fsize)) * fsize; | | | |
90 | } | | | |
91 | END { | | | |
92 | printf("%d\n", tot); | | | |
93 | }' | | | |
94 | ;; | | 111 | ;; |
95 | *) | | 112 | *) |
96 | echo 0 | | 113 | echo 0 |
97 | ;; | | 114 | ;; |
98 | esac | | 115 | esac |
99 | } | | 116 | } |
100 | | | 117 | |
101 | usage() { | | | |
102 | cat << EOF 1>&2 | | | |
103 | Usage: $PROG [-S <setsdir>] [-c <custom-files-dir>] [-h <host-arch>] [-s <size>] | | | |
104 | EOF | | | |
105 | exit 1 | | | |
106 | } | | | |
107 | | | 118 | |
108 | # Return the usable filesystem size in bytes, given the total size in bytes, | | 119 | # Return the usable filesystem size in bytes, given the total size in bytes, |
109 | # and optionally block and fragment sizes | | 120 | # and optionally block and fragment sizes |
110 | getffssize() { | | 121 | getffssize() { |
111 | local bytes="$1" | | 122 | local bytes="$1" |
112 | local barg | | 123 | local barg |
113 | local farg | | 124 | local farg |
114 | local overhead | | 125 | local overhead |
115 | | | 126 | |
116 | if [ -n "$2" ] | | 127 | if [ -n "$2" ] |
117 | then | | 128 | then |
118 | barg="-b $2" | | 129 | barg="-b $2" |
119 | if [ -n "$3" ] | | 130 | if [ -n "$3" ] |
| @@ -171,26 +182,27 @@ finish() { | | | @@ -171,26 +182,27 @@ finish() { |
171 | | | 182 | |
172 | DIR="$(dirname "$0")" | | 183 | DIR="$(dirname "$0")" |
173 | PROG="$(basename "$0")" | | 184 | PROG="$(basename "$0")" |
174 | bar="===" | | 185 | bar="===" |
175 | sudo= | | 186 | sudo= |
176 | mnt="${TMPDIR:-/tmp}/image.$$" | | 187 | mnt="${TMPDIR:-/tmp}/image.$$" |
177 | src="/usr/src" | | 188 | src="/usr/src" |
178 | obj="/usr/obj" | | 189 | obj="/usr/obj" |
179 | | | 190 | |
180 | | | 191 | |
181 | # Presumable block and fragment size. | | 192 | # Presumable block and fragment size. |
182 | bsize=16384 | | 193 | bsize=16384 |
183 | fsize=2048 | | 194 | fsize=2048 |
| | | 195 | mtob=$(( 1024 * 1024 )) |
184 | | | 196 | |
185 | # First pass for options to get the host | | 197 | # First pass for options to get the host |
186 | OPTS="S:c:h:s:x" | | 198 | OPTS="S:c:h:s:x" |
187 | while getopts "$OPTS" f | | 199 | while getopts "$OPTS" f |
188 | do | | 200 | do |
189 | case $f in | | 201 | case $f in |
190 | h) h="$OPTARG";; | | 202 | h) h="$OPTARG";; |
191 | *) ;; | | 203 | *) ;; |
192 | esac | | 204 | esac |
193 | done | | 205 | done |
194 | | | 206 | |
195 | if [ -z "$h" ] | | 207 | if [ -z "$h" ] |
196 | then | | 208 | then |
| @@ -222,44 +234,43 @@ trap finish 0 1 2 3 15 | | | @@ -222,44 +234,43 @@ trap finish 0 1 2 3 15 |
222 | | | 234 | |
223 | shift $(( "$OPTIND" - 1 )) | | 235 | shift $(( "$OPTIND" - 1 )) |
224 | if [ -n "$1" ]; then | | 236 | if [ -n "$1" ]; then |
225 | # take the next argument as being the image name | | 237 | # take the next argument as being the image name |
226 | image="$1" | | 238 | image="$1" |
227 | shift | | 239 | shift |
228 | fi | | 240 | fi |
229 | | | 241 | |
230 | # calculate the set bytes | | 242 | # calculate the set bytes |
231 | setbytes=0 | | 243 | setbytes=0 |
232 | echo -n "${bar} computing set sizes (" | | 244 | echo -n "${bar} computing set sizes (" |
233 | for s in ${sets}; do | | 245 | for s in ${sets}; do |
234 | one="$(sizeone ${setsdir}/${s}.tgz)" | | 246 | one="$(sizeone ${setsdir}/${s}.tgz)" |
235 | echo -n " $s=$(( ${one} / 1024 / 1024 ))MB" | | 247 | echo -n " $s=$(( ${one} / ${mtob} ))MB" |
236 | setbytes=$(( ${setbytes} + ${one} )) | | 248 | setbytes=$(( ${setbytes} + ${one} )) |
237 | done | | 249 | done |
238 | echo "): $(( ${setbytes} / 1024 / 1024 ))MB ${bar}" | | 250 | echo "): $(( ${setbytes} / ${mtob} ))MB ${bar}" |
239 | | | 251 | |
240 | # calculate size of custom files | | 252 | # calculate size of custom files |
241 | custbytes=0 | | 253 | custbytes=0 |
242 | if [ -d "${custom}" ]; then | | 254 | if [ -d "${custom}" ]; then |
243 | custbytes=$(ls -lR "${custom}" | | | 255 | custbytes=$(ls -lR "${custom}" | getfssize ${bsize} ${fsize}) |
244 | awk 'NF == 9 { tot += $5 } END { print tot }') | | | |
245 | fi | | 256 | fi |
246 | echo "${bar} computing custom sizes: $(( ${custbytes} / 1024 / 1024 ))MB ${bar}" | | 257 | echo "${bar} computing custom sizes: $(( ${custbytes} / ${mtob} ))MB ${bar}" |
247 | | | 258 | |
248 | # how many bytes | | 259 | # how many bytes |
249 | rawbytes="$(( ${setbytes} + ${custbytes} ))" | | 260 | rawbytes="$(( ${setbytes} + ${custbytes} ))" |
250 | echo -n "${bar} computing ffs filesystem size for $(( ${rawbytes} / 1024 / 1024 ))MB: " | | 261 | echo -n "${bar} computing ffs filesystem size for $(( ${rawbytes} / ${mtob} ))MB: " |
251 | ffsbytes="$(makeffssize "${rawbytes}")" | | 262 | ffsbytes="$(makeffssize "${rawbytes}")" |
252 | ffsmb=$(( ${ffsbytes} / 1024 / 1024 )) | | 263 | ffsmb=$(( ${ffsbytes} / ${mtob} )) |
253 | echo " ${ffsmb}MB ${bar}" | | 264 | echo " ${ffsmb}MB ${bar}" |
254 | | | 265 | |
255 | # total in MB | | 266 | # total in MB |
256 | total=$(( ${ffsmb} + ${overhead} )) | | 267 | total=$(( ${ffsmb} + ${overhead} )) |
257 | echo "${bar} overhead: ${overhead}MB ${bar}" | | 268 | echo "${bar} overhead: ${overhead}MB ${bar}" |
258 | | | 269 | |
259 | if [ $size -eq 0 ]; then | | 270 | if [ $size -eq 0 ]; then |
260 | # auto-size the pkgs fs | | 271 | # auto-size the pkgs fs |
261 | newsize=${total} | | 272 | newsize=${total} |
262 | else | | 273 | else |
263 | # check that we've been given enough space | | 274 | # check that we've been given enough space |
264 | if [ ${total} -gt ${size} ]; then | | 275 | if [ ${total} -gt ${size} ]; then |
265 | echo "$PROG: Given size is ${size} MB, but we need ${total} MB" >&2 | | 276 | echo "$PROG: Given size is ${size} MB, but we need ${total} MB" >&2 |