Wed Jan 16 15:58:19 2013 UTC ()
tidy up some more by factoring out the sizing code.


(christos)
diff -r1.11 -r1.12 src/distrib/utils/embedded/mkimage

cvs diff -r1.11 -r1.12 src/distrib/utils/embedded/mkimage (expand / switch to unified diff)

--- src/distrib/utils/embedded/mkimage 2013/01/15 21:04:41 1.11
+++ src/distrib/utils/embedded/mkimage 2013/01/16 15:58:19 1.12
@@ -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
 71usage() {
 72 cat << EOF 1>&2
 73Usage: $PROG [-S <setsdir>] [-c <custom-files-dir>] [-h <host-arch>] [-s <size>]
 74EOF
 75exit 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.
 81getfssize() {
 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.
74sizeone() { 100sizeone() {
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
101usage() { 
102 cat << EOF 1>&2 
103Usage: $PROG [-S <setsdir>] [-c <custom-files-dir>] [-h <host-arch>] [-s <size>] 
104EOF 
105exit 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
110getffssize() { 121getffssize() {
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
172DIR="$(dirname "$0")" 183DIR="$(dirname "$0")"
173PROG="$(basename "$0")" 184PROG="$(basename "$0")"
174bar="===" 185bar="==="
175sudo= 186sudo=
176mnt="${TMPDIR:-/tmp}/image.$$" 187mnt="${TMPDIR:-/tmp}/image.$$"
177src="/usr/src" 188src="/usr/src"
178obj="/usr/obj" 189obj="/usr/obj"
179 190
180 191
181# Presumable block and fragment size. 192# Presumable block and fragment size.
182bsize=16384 193bsize=16384
183fsize=2048 194fsize=2048
 195mtob=$(( 1024 * 1024 ))
184 196
185# First pass for options to get the host 197# First pass for options to get the host
186OPTS="S:c:h:s:x" 198OPTS="S:c:h:s:x"
187while getopts "$OPTS" f 199while getopts "$OPTS" f
188do 200do
189 case $f in 201 case $f in
190 h) h="$OPTARG";; 202 h) h="$OPTARG";;
191 *) ;; 203 *) ;;
192 esac 204 esac
193done 205done
194 206
195if [ -z "$h" ] 207if [ -z "$h" ]
196then 208then
@@ -222,44 +234,43 @@ trap finish 0 1 2 3 15 @@ -222,44 +234,43 @@ trap finish 0 1 2 3 15
222 234
223shift $(( "$OPTIND" - 1 )) 235shift $(( "$OPTIND" - 1 ))
224if [ -n "$1" ]; then 236if [ -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
228fi 240fi
229 241
230# calculate the set bytes 242# calculate the set bytes
231setbytes=0 243setbytes=0
232echo -n "${bar} computing set sizes (" 244echo -n "${bar} computing set sizes ("
233for s in ${sets}; do 245for 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} ))
237done 249done
238echo "): $(( ${setbytes} / 1024 / 1024 ))MB ${bar}" 250echo "): $(( ${setbytes} / ${mtob} ))MB ${bar}"
239 251
240# calculate size of custom files 252# calculate size of custom files
241custbytes=0 253custbytes=0
242if [ -d "${custom}" ]; then 254if [ -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 }') 
245fi 256fi
246echo "${bar} computing custom sizes: $(( ${custbytes} / 1024 / 1024 ))MB ${bar}" 257echo "${bar} computing custom sizes: $(( ${custbytes} / ${mtob} ))MB ${bar}"
247 258
248# how many bytes 259# how many bytes
249rawbytes="$(( ${setbytes} + ${custbytes} ))" 260rawbytes="$(( ${setbytes} + ${custbytes} ))"
250echo -n "${bar} computing ffs filesystem size for $(( ${rawbytes} / 1024 / 1024 ))MB: " 261echo -n "${bar} computing ffs filesystem size for $(( ${rawbytes} / ${mtob} ))MB: "
251ffsbytes="$(makeffssize "${rawbytes}")" 262ffsbytes="$(makeffssize "${rawbytes}")"
252ffsmb=$(( ${ffsbytes} / 1024 / 1024 )) 263ffsmb=$(( ${ffsbytes} / ${mtob} ))
253echo " ${ffsmb}MB ${bar}" 264echo " ${ffsmb}MB ${bar}"
254 265
255# total in MB 266# total in MB
256total=$(( ${ffsmb} + ${overhead} )) 267total=$(( ${ffsmb} + ${overhead} ))
257echo "${bar} overhead: ${overhead}MB ${bar}" 268echo "${bar} overhead: ${overhead}MB ${bar}"
258 269
259if [ $size -eq 0 ]; then 270if [ $size -eq 0 ]; then
260 # auto-size the pkgs fs 271 # auto-size the pkgs fs
261 newsize=${total} 272 newsize=${total}
262else 273else
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