1 1.31 jmcneill #!/bin/sh 2 1.98 brook # $NetBSD: mkimage,v 1.98 2026/08/02 02:59:14 brook Exp $ 3 1.13 christos # 4 1.45 christos # Copyright (c) 2013, 2014 The NetBSD Foundation, Inc. 5 1.1 agc # All rights reserved. 6 1.1 agc # 7 1.17 christos # This code is derived from software contributed to The NetBSD Foundation 8 1.17 christos # by Christos Zoulas. 9 1.17 christos # 10 1.1 agc # Redistribution and use in source and binary forms, with or without 11 1.1 agc # modification, are permitted provided that the following conditions 12 1.1 agc # are met: 13 1.1 agc # 1. Redistributions of source code must retain the above copyright 14 1.1 agc # notice, this list of conditions and the following disclaimer. 15 1.1 agc # 2. Redistributions in binary form must reproduce the above copyright 16 1.1 agc # notice, this list of conditions and the following disclaimer in the 17 1.1 agc # documentation and/or other materials provided with the distribution. 18 1.17 christos # 3. Neither the name of The NetBSD Foundation nor the names of its 19 1.17 christos # contributors may be used to endorse or promote products derived 20 1.17 christos # from this software without specific prior written permission. 21 1.17 christos # 22 1.17 christos # THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS 23 1.17 christos # ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED 24 1.17 christos # TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 25 1.17 christos # PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS 26 1.17 christos # BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR 27 1.17 christos # CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF 28 1.17 christos # SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 29 1.17 christos # INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN 30 1.17 christos # CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 31 1.17 christos # ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE 32 1.17 christos # POSSIBILITY OF SUCH DAMAGE. 33 1.1 agc # 34 1.1 agc 35 1.58 hubertf # 36 1.58 hubertf # Makes a bootable image for the host architecture given. 37 1.92 gutterid # The host-specific functions are pulled in from a /bin/sh script in the 38 1.92 gutterid # "conf" directory, which is expected to provide the following shell 39 1.58 hubertf # functions, which are called in the following order: 40 1.58 hubertf # 41 1.58 hubertf # - make_fstab: Creates the host's /etc/fstab with / on ${rootdev}. 42 1.58 hubertf # If -m is given, a number of directories are put on a tmpfs RAM disk 43 1.58 hubertf # - customize: After unpacking the sets, this gets the system to 44 1.92 gutterid # a working state, e.g., by setting up /etc/rc.conf and /dev 45 1.58 hubertf # - populate: Add common goods like kernel and bootloader 46 1.58 hubertf # - make_label: Prints disklabel to stdout 47 1.58 hubertf # 48 1.58 hubertf 49 1.95 kre set -e 50 1.33 jmcneill 51 1.17 christos DIR="$(cd "$(dirname "$0")" && pwd)" 52 1.17 christos PROG="$(basename "$0")" 53 1.17 christos 54 1.66 jmcneill MAKE=${TOOL_MAKE:-make} 55 1.21 christos DISKLABEL=${TOOL_DISKLABEL:-disklabel} 56 1.24 christos FDISK=${TOOL_FDISK:-fdisk} 57 1.73 jmcneill GPT=${TOOL_GPT:-gpt} 58 1.21 christos MAKEFS=${TOOL_MAKEFS:-makefs} 59 1.25 jmcneill MTREE=${TOOL_MTREE:-mtree} 60 1.45 christos INSTALLBOOT=${TOOL_INSTALLBOOT:-installboot} 61 1.59 christos MKUBOOTIMAGE=${TOOL_MKUBOOTIMAGE:-mkubootimage} 62 1.44 ast GZIP_CMD=${TOOL_GZIP:-gzip} # ${GZIP} is special to gzip(1) 63 1.88 jmmv SED=${TOOL_SED:-sed} 64 1.88 jmmv PWD_MKDB=${TOOL_PWD_MKDB:-pwd_mkdb} 65 1.21 christos 66 1.87 jmmv postfix=false 67 1.87 jmmv [ "${MKPOSTFIX:-yes}" = no ] || postfix=true 68 1.87 jmmv 69 1.17 christos src="/usr/src" 70 1.84 skrll sets="base comp etc games gpufw man manhtml misc modules rescue tests text" 71 1.83 skrll xsets="xbase xcomp xetc xfont xserver" 72 1.17 christos minfree="10%" 73 1.17 christos bar="===" 74 1.17 christos 75 1.67 kre tmp="$(mktemp -d "${TMPDIR:-/tmp}/$PROG.XXXXXX")" 76 1.17 christos mnt="${tmp}/mnt" 77 1.46 christos mkdir -p "${mnt}/etc" "${mnt}/dev" 78 1.23 christos 79 1.17 christos trap "cleanup" 0 1 2 3 15 80 1.17 christos 81 1.17 christos cleanup() { 82 1.17 christos case "$tmp" in 83 1.68 kre "${TMPDIR:-/tmp}/$PROG."*) rm -fr "$tmp";; 84 1.17 christos esac 85 1.17 christos } 86 1.1 agc 87 1.69 kre fail() { 88 1.69 kre IFS=' ' 89 1.69 kre echo >&2 "${PROG}: $*" 90 1.69 kre exit 1 91 1.69 kre } 92 1.69 kre 93 1.17 christos getsize() { 94 1.17 christos set -- $(ls -l $1) 95 1.17 christos echo $5 96 1.1 agc } 97 1.1 agc 98 1.63 jmcneill getsectors() { 99 1.63 jmcneill case "$1" in 100 1.63 jmcneill *g) 101 1.63 jmcneill m=1073741824 102 1.63 jmcneill v=${1%g} 103 1.63 jmcneill ;; 104 1.63 jmcneill *m) 105 1.63 jmcneill m=1048576 106 1.63 jmcneill v=${1%m} 107 1.63 jmcneill ;; 108 1.63 jmcneill *k) 109 1.63 jmcneill m=1024 110 1.63 jmcneill v=${1%k} 111 1.63 jmcneill ;; 112 1.63 jmcneill *[0-9b]) 113 1.63 jmcneill m=1 114 1.63 jmcneill v=${1%b} 115 1.63 jmcneill ;; 116 1.63 jmcneill esac 117 1.63 jmcneill echo $((m * v / 512)) 118 1.63 jmcneill } 119 1.63 jmcneill 120 1.85 christos minwrites_fstab_entries() { 121 1.85 christos $minwrites || return 0 122 1.85 christos cat << EOF 123 1.85 christos tmpfs /var/log tmpfs rw,union,-s32M 124 1.85 christos tmpfs /var/run tmpfs rw,union,-s1M 125 1.85 christos tmpfs /var/mail tmpfs rw,union,-s10M 126 1.87 jmmv tmpfs /var/chroot tmpfs rw,union,-s10M 127 1.87 jmmv EOF 128 1.87 jmmv if $postfix; then 129 1.87 jmmv cat << EOF 130 1.85 christos tmpfs /var/spool/postfix tmpfs rw,union,-s20M 131 1.85 christos tmpfs /var/db/postfix tmpfs rw,union,-s1M 132 1.85 christos EOF 133 1.87 jmmv fi 134 1.85 christos } 135 1.85 christos 136 1.85 christos make_fstab_gpt() { 137 1.85 christos local boot=$1 138 1.85 christos local rootopts= 139 1.85 christos if $minwrites; then 140 1.85 christos rootopts=,log,nodevmtime 141 1.85 christos fi 142 1.85 christos 143 1.85 christos cat > ${mnt}/etc/fstab << EOF 144 1.85 christos # NetBSD /etc/fstab 145 1.85 christos # See /usr/share/examples/fstab/ for more examples. 146 1.85 christos NAME=${gpt_label_ffs:-netbsd-root} / ffs rw,noatime${rootopts} 1 1 147 1.85 christos NAME=${gpt_label_boot:-$boot} /boot msdos rw 1 1 148 1.85 christos ptyfs /dev/pts ptyfs rw 149 1.85 christos procfs /proc procfs rw 150 1.85 christos tmpfs /var/shm tmpfs rw,-m1777,-sram%25 151 1.85 christos EOF 152 1.85 christos minwrites_fstab_entries >> ${mnt}/etc/fstab 153 1.85 christos } 154 1.85 christos 155 1.85 christos # From Richard Neswold's: 156 1.85 christos # http://rich-tbp.blogspot.com/2013/03/netbsd-on-rpi-minimizing-disk-writes.html 157 1.85 christos # Also for the postfix stuff below 158 1.85 christos make_fstab_normal() { 159 1.85 christos local rootopts= 160 1.85 christos if $minwrites; then 161 1.85 christos rootopts=,nodevmtime 162 1.85 christos fi 163 1.85 christos cat > ${mnt}/etc/fstab << EOF 164 1.85 christos # NetBSD /etc/fstab 165 1.85 christos # See /usr/share/examples/fstab/ for more examples. 166 1.90 jmcneill ROOT.a / ffs rw,noatime${rootopts} 1 1 167 1.85 christos ROOT.e /boot msdos rw 1 1 168 1.85 christos ptyfs /dev/pts ptyfs rw 169 1.85 christos procfs /proc procfs rw 170 1.85 christos tmpfs /tmp tmpfs rw,-s32M 171 1.85 christos tmpfs /var/shm tmpfs rw,-m1777,-sram%25 172 1.85 christos EOF 173 1.85 christos minwrites_fstab_entries >> ${mnt}/etc/fstab 174 1.85 christos } 175 1.85 christos 176 1.85 christos make_fstab_default() { 177 1.85 christos if $gpt; then 178 1.85 christos make_fstab_gpt "$@" 179 1.85 christos else 180 1.85 christos make_fstab_normal 181 1.85 christos fi 182 1.85 christos echo "./etc/fstab type=file uname=root gname=wheel mode=0644" \ 183 1.85 christos >> "$tmp/selected_sets" 184 1.85 christos 185 1.85 christos # Missing mount points from fstab 186 1.85 christos echo "./proc type=dir uname=root gname=wheel mode=0755" \ 187 1.85 christos >> "$tmp/selected_sets" 188 1.85 christos } 189 1.85 christos 190 1.12 christos usage() { 191 1.12 christos cat << EOF 1>&2 192 1.60 martin Usage: $PROG -h <host-arch> [-bdmx] [-B <byte-order>] [-K <kerneldir>] [-S <srcdir>] [-D <destdir>] [-c <custom-files-dir>] [-s <Mb size>] [<image>] 193 1.39 christos 194 1.39 christos -b Boot only, no sets loaded 195 1.47 christos -r root device kind (sd, wd, ld) 196 1.39 christos -d Add the debug sets 197 1.96 andvar -m Optimize the OS installation to minimize disk writes for SSDs 198 1.57 hubertf -x Load the X sets too, not just the base ones 199 1.12 christos EOF 200 1.13 christos exit 1 201 1.12 christos } 202 1.12 christos 203 1.32 jmcneill # First pass for options to get the host and src directories 204 1.97 riastrad OPTS="B:C:D:K:S:bc:dh:mr:s:x" 205 1.4 christos while getopts "$OPTS" f 206 1.4 christos do 207 1.4 christos case $f in 208 1.97 riastrad C) conf="$OPTARG" 209 1.97 riastrad # Take -C relative to working directory; don't search 210 1.97 riastrad # in PATH. 211 1.97 riastrad case $conf in 212 1.97 riastrad /*) ;; 213 1.97 riastrad *) conf=./"$conf";; 214 1.97 riastrad esac 215 1.97 riastrad ;; 216 1.4 christos h) h="$OPTARG";; 217 1.32 jmcneill S) src="$OPTARG";; 218 1.4 christos *) ;; 219 1.4 christos esac 220 1.4 christos done 221 1.4 christos 222 1.4 christos if [ -z "$h" ] 223 1.4 christos then 224 1.4 christos usage 225 1.4 christos fi 226 1.4 christos 227 1.97 riastrad : ${conf:=${DIR}/conf/${h}.conf} 228 1.97 riastrad if [ ! -f "$conf" ] 229 1.4 christos then 230 1.97 riastrad echo $PROG: $conf is not present 1>&2 231 1.4 christos exit 1 232 1.4 christos fi 233 1.4 christos 234 1.56 jmcneill resize=false 235 1.73 jmcneill gpt=false 236 1.74 jmcneill gpt_hybrid=false 237 1.89 jmcneill fsize=8192 238 1.89 jmcneill bsize=65536 239 1.89 jmcneill ffsversion=1 240 1.56 jmcneill 241 1.97 riastrad . "$conf" 242 1.45 christos release="/usr/obj/${MACHINE}/release" 243 1.4 christos 244 1.17 christos selected_sets="$sets" 245 1.50 skrll dsets_p=false 246 1.50 skrll xsets_p=false 247 1.39 christos minwrites=false 248 1.47 christos rootdev=ld 249 1.60 martin endian= 250 1.17 christos 251 1.4 christos OPTIND=1 252 1.4 christos while getopts "$OPTS" f 253 1.4 christos do 254 1.4 christos case $f in 255 1.60 martin B) endian="-B $OPTARG";; 256 1.97 riastrad C) ;; 257 1.18 christos D) release="$OPTARG";; 258 1.18 christos K) kernel="$OPTARG";; 259 1.32 jmcneill S) ;; 260 1.41 christos b) bootonly=true;; 261 1.50 skrll d) dsets_p=true 262 1.39 christos selected_sets="$selected_sets debug" 263 1.50 skrll if $xsets_p; then 264 1.39 christos selected_sets="$selected_sets xdebug" 265 1.39 christos fi 266 1.39 christos ;; 267 1.4 christos c) custom="$OPTARG";; 268 1.4 christos h) ;; 269 1.39 christos m) minwrites=true;; 270 1.47 christos r) rootdev="$OPTARG";; 271 1.4 christos s) size="$OPTARG";; 272 1.50 skrll x) xsets_p=true 273 1.39 christos selected_sets="$selected_sets $xsets" 274 1.50 skrll if $dsets_p; then 275 1.39 christos selected_sets="$selected_sets xdebug" 276 1.39 christos fi 277 1.39 christos ;; 278 1.6 christos *) usage;; 279 1.1 agc esac 280 1.1 agc done 281 1.81 mlelstv if [ -n "${MKREPRO_TIMESTAMP}" ]; then 282 1.80 christos timestamp_opt="-T ${MKREPRO_TIMESTAMP}" 283 1.80 christos volume_opt=",volume_id=$((${MKREPRO_TIMESTAMP} & 0xffff))" 284 1.80 christos fi 285 1.1 agc 286 1.20 jmcneill shift $(( $OPTIND - 1 )) 287 1.4 christos if [ -n "$1" ]; then 288 1.1 agc # take the next argument as being the image name 289 1.1 agc image="$1" 290 1.1 agc shift 291 1.1 agc fi 292 1.1 agc 293 1.22 christos case "$image" in 294 1.22 christos *.gz) compress=true; image="${image%.gz}";; 295 1.22 christos *) compress=false;; 296 1.22 christos esac 297 1.22 christos 298 1.45 christos if [ -z "${bootonly}" ]; then 299 1.36 garbled echo ${bar} configuring sets ${bar} 300 1.51 skrll (cat "${release}/etc/mtree/NetBSD.dist" 301 1.36 garbled for i in $selected_sets; do 302 1.36 garbled s="${release}/etc/mtree/set.$i" 303 1.36 garbled if [ -f "$s" ]; then 304 1.36 garbled cat "$s" 305 1.36 garbled fi 306 1.36 garbled done) > "$tmp/selected_sets" 307 1.36 garbled fi 308 1.1 agc 309 1.4 christos make_fstab 310 1.17 christos customize 311 1.17 christos populate 312 1.1 agc 313 1.72 jmcneill if [ ! "${MKDTB}" = "no" ]; then 314 1.72 jmcneill # 315 1.72 jmcneill # Part of the dtb set resides on the FAT partition (/boot/dtb/*), and 316 1.72 jmcneill # the rest on FFS. Split it up here. 317 1.72 jmcneill # 318 1.72 jmcneill echo ${bar} Installing devicetree blobs ${bar} 319 1.72 jmcneill mkdir -p "${mnt}/boot" 320 1.72 jmcneill cp -r "${release}/boot/dtb" "${mnt}/boot/dtb" 321 1.72 jmcneill 322 1.72 jmcneill mkdir -p "${mnt}/etc/mtree" 323 1.72 jmcneill cp "${release}/etc/mtree/set.dtb" "${mnt}/etc/mtree/set.dtb" 324 1.72 jmcneill echo "./etc/mtree/set.dtb type=file uname=root gname=wheel mode=0444" >> "$tmp/selected_sets" 325 1.72 jmcneill 326 1.72 jmcneill mkdir -p "${mnt}/var/db/obsolete" 327 1.72 jmcneill cp "${release}/var/db/obsolete/dtb" "${mnt}/var/db/obsolete/dtb" 328 1.72 jmcneill echo "./var/db/obsolete/dtb type=file uname=root gname=wheel mode=0644" >>"$tmp/selected_sets" 329 1.72 jmcneill fi 330 1.72 jmcneill 331 1.45 christos if [ -n "${msdosid}" ]; then 332 1.17 christos echo ${bar} Populating msdos filesystem ${bar} 333 1.72 jmcneill 334 1.71 martin case $(( ${msdosid} )) in 335 1.71 martin 1) fat_opt=",fat_type=12";; 336 1.71 martin 4|6|14) fat_opt=",fat_type=16";; 337 1.71 martin 11|12) fat_opt=",fat_type=32";; 338 1.71 martin *) fat_opt=;; 339 1.71 martin esac 340 1.71 martin ${MAKEFS} -N ${release}/etc -t msdos \ 341 1.80 christos -o "volume_label=NETBSD${fat_opt}${volume_opt}" ${timestamp_opt} \ 342 1.63 jmcneill -O $((${init} / 2))m -s $((${boot} / 2))m \ 343 1.41 christos ${image} ${mnt}/boot 344 1.1 agc fi 345 1.1 agc 346 1.45 christos if [ -z "${bootonly}" ]; then 347 1.36 garbled echo ${bar} Populating ffs filesystem ${bar} 348 1.60 martin ${MAKEFS} -rx ${endian} -N ${release}/etc -t ffs \ 349 1.80 christos -O ${ffsoffset} ${timestamp_opt} \ 350 1.89 jmcneill -o d=4096,f=${fsize},b=${bsize},v=${ffsversion} -b $((${extra}))m \ 351 1.36 garbled -F "$tmp/selected_sets" ${image} "${release}" "${mnt}" 352 1.36 garbled fi 353 1.1 agc 354 1.23 christos if [ "${size}" = 0 ]; then 355 1.77 rin size="$(getsize "${image}")" 356 1.76 riastrad # Round up to a multiple of 4m and add 1m of slop. 357 1.76 riastrad alignunit=$((4*1024*1024)) 358 1.77 rin alignsize=$((alignunit*((size + alignunit - 1)/alignunit))) 359 1.76 riastrad alignsize=$((alignsize + 1024*1024)) 360 1.77 rin if [ "${size}" -lt "${alignsize}" ]; then 361 1.93 riastrad dd bs="$((alignsize - size))" count=1 if=/dev/zero conv=sync \ 362 1.77 rin >> "${image}" 2> /dev/null 363 1.77 rin size="${alignsize}" 364 1.76 riastrad fi 365 1.17 christos fi 366 1.1 agc 367 1.73 jmcneill if $gpt; then 368 1.74 jmcneill if $gpt_hybrid; then 369 1.74 jmcneill gpt_flags="-H" 370 1.74 jmcneill fi 371 1.80 christos gpt_flags="${gpt_flags} ${timestamp_opt}" 372 1.30 jmcneill initsecs=$((${init} * 1024)) 373 1.30 jmcneill bootsecs=$((${boot} * 1024)) 374 1.73 jmcneill ffsstart="$(getsectors ${ffsoffset})" 375 1.73 jmcneill 376 1.73 jmcneill echo ${bar} Clearing existing partitions ${bar} 377 1.74 jmcneill ${GPT} ${gpt_flags} ${image} destroy || true 378 1.73 jmcneill 379 1.73 jmcneill echo ${bar} Creating partitions ${bar} 380 1.74 jmcneill ${GPT} ${gpt_flags} ${image} create ${gpt_create_flags} 381 1.75 jmcneill ${GPT} ${gpt_flags} ${image} add -b ${initsecs} -s ${bootsecs} -l ${gpt_label_boot:-EFI} -t ${gpt_boot_type:-efi} 382 1.74 jmcneill ${GPT} ${gpt_flags} ${image} set -a required -i 1 383 1.74 jmcneill ${GPT} ${gpt_flags} ${image} add -a 4m -b ${ffsstart} -l ${gpt_label_ffs:-netbsd-root} -t ffs 384 1.74 jmcneill ${GPT} ${gpt_flags} ${image} show 385 1.74 jmcneill if $gpt_hybrid; then 386 1.74 jmcneill echo ${bar} Creating hybrid MBR ${bar} 387 1.74 jmcneill ${FDISK} -f -g -u -0 -a -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 388 1.74 jmcneill ${FDISK} -f -g -u -3 -s 238/1/$((${initsecs} - 1)) -F ${image} 389 1.74 jmcneill ${FDISK} -F ${image} 390 1.74 jmcneill fi 391 1.73 jmcneill else 392 1.73 jmcneill if [ -n "${msdosid}" ]; then 393 1.73 jmcneill echo ${bar} Running fdisk ${bar} 394 1.73 jmcneill initsecs=$((${init} * 1024)) 395 1.73 jmcneill bootsecs=$((${boot} * 1024)) 396 1.73 jmcneill ${FDISK} -f -i ${image} 397 1.73 jmcneill ${FDISK} -f -a -u -0 -s ${msdosid}/${initsecs}/${bootsecs} -F ${image} 398 1.73 jmcneill if [ -z "${bootonly}" ]; then 399 1.73 jmcneill ffsstart="$(getsectors ${ffsoffset})" 400 1.73 jmcneill imagesize="$(getsize "${image}")" 401 1.73 jmcneill imagesecs="$(getsectors ${imagesize})" 402 1.73 jmcneill ffssize="$(expr ${imagesecs} - ${ffsstart})" 403 1.73 jmcneill ${FDISK} -f -u -1 -s 169/${ffsstart}/${ffssize} -F ${image} 404 1.73 jmcneill fi 405 1.73 jmcneill 406 1.73 jmcneill echo ${bar} Adding label ${bar} 407 1.73 jmcneill make_label > ${tmp}/label 408 1.79 jmcneill ${DISKLABEL} -m -R -F ${image} ${tmp}/label 409 1.73 jmcneill elif [ -n "${netbsdid}" ]; then 410 1.73 jmcneill echo ${bar} Adding label ${bar} 411 1.73 jmcneill make_label > ${tmp}/label 412 1.79 jmcneill ${DISKLABEL} -m -R -F ${image} ${tmp}/label 413 1.73 jmcneill 414 1.73 jmcneill echo ${bar} Running fdisk ${bar} 415 1.73 jmcneill ${FDISK} -f -i ${image} 416 1.73 jmcneill ${FDISK} -f -a -u -0 -s 169/${init} ${image} 417 1.73 jmcneill ${INSTALLBOOT} -f -v ${image} ${release}/usr/mdec/bootxx_ffsv1 418 1.63 jmcneill fi 419 1.2 agc fi 420 1.22 christos 421 1.22 christos if $compress; then 422 1.22 christos echo ${bar} Compressing image ${bar} 423 1.23 christos rm -f "${image}.gz" 424 1.80 christos ${GZIP_CMD} -n -9 ${image} 425 1.22 christos image="${image}.gz" 426 1.22 christos fi 427 1.22 christos 428 1.17 christos echo ${bar} Image is ${image} ${bar} 429