mkimage revision 1.2
11.1Sagc#! /bin/sh 21.1Sagc 31.2Sagc# $NetBSD: mkimage,v 1.2 2012/01/20 02:19:47 agc Exp $ 41.1Sagc 51.1Sagc# Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org> 61.1Sagc# All rights reserved. 71.1Sagc# 81.1Sagc# Redistribution and use in source and binary forms, with or without 91.1Sagc# modification, are permitted provided that the following conditions 101.1Sagc# are met: 111.1Sagc# 1. Redistributions of source code must retain the above copyright 121.1Sagc# notice, this list of conditions and the following disclaimer. 131.1Sagc# 2. Redistributions in binary form must reproduce the above copyright 141.1Sagc# notice, this list of conditions and the following disclaimer in the 151.1Sagc# documentation and/or other materials provided with the distribution. 161.1Sagc# 171.1Sagc# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 181.1Sagc# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES 191.1Sagc# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 201.1Sagc# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, 211.1Sagc# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 221.1Sagc# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 231.1Sagc# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 241.1Sagc# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 251.1Sagc# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF 261.1Sagc# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 271.1Sagc# 281.1Sagc 291.1Sagc# find next available vnd, from kre 301.1Sagcnext_avail () 311.1Sagc{ 321.1Sagc local dev="$1" 331.1Sagc local N=$(( ${#dev} + 1 )) 341.1Sagc local unit units 351.1Sagc 361.1Sagc units=$( 371.1Sagc sysctl -n hw.disknames | 381.1Sagc tr ' ' '\012' | 391.1Sagc grep '^'"${dev}"'[0-9]' | 401.1Sagc sort -n -k 1.$N ) 411.1Sagc 421.1Sagc test -z "${units}" && { 431.1Sagc test -e "/dev/${dev}0a" || { 441.1Sagc echo >&2 "No ${dev}s available!" 451.1Sagc return 1 461.1Sagc } 471.1Sagc echo "${dev}0" 481.1Sagc return 491.1Sagc } 501.1Sagc 511.1Sagc N=0 521.1Sagc for unit in ${units} 531.1Sagc do 541.1Sagc if [ "${unit}" = "${dev}${N}" ] 551.1Sagc then 561.1Sagc N=$(( N + 1 )) 571.1Sagc else 581.1Sagc echo "${dev}${N}" 591.1Sagc return 601.1Sagc fi 611.1Sagc done 621.1Sagc 631.1Sagc test -e /dev/"${dev}${N}a" || { 641.1Sagc echo >&2 "All ${dev}s in use" 651.1Sagc return 1 661.1Sagc } 671.1Sagc 681.1Sagc echo "${dev}${N}" 691.1Sagc} 701.1Sagc 711.1Sagc# find the size of the gzipped files in a .tgz archive 721.1Sagcsizeone() { 731.1Sagc case "$1" in 741.1Sagc *.tgz|*.tar.gz) 751.1Sagc tar tvzf "$1" | awk '{ tot += $5 } END { print tot }' 761.1Sagc ;; 771.1Sagc *.tbz|*.tar.bz2) 781.1Sagc tar tvjf "$1" | awk '{ tot += $5 } END { print tot }' 791.1Sagc ;; 801.1Sagc *) 811.1Sagc echo 0 821.1Sagc ;; 831.1Sagc esac 841.1Sagc} 851.1Sagc 861.2Sagcbar="===" 871.2Sagccustom=custom 881.2Sagch=usermode1.$(uname -n) 891.2Sagcimage=usermode.img 901.1Sagcoverhead=8 # in MB 911.1Sagcsets="base etc modules" 921.2Sagcsetsdir=/usr/build/release/$(uname -m)/binary/sets 931.2Sagcsize=0 # in MB 941.2Sagcspecialdirs="/kern /proc" 951.1Sagcusermodedirs="/var.run /var.log /etc.cow /root.cow /pkgs" 961.2Sagcsudo="sudo" 971.1Sagc 981.1Sagcwhile [ $# -gt 0 ]; do 991.1Sagc case "$1" in 1001.1Sagc -S) setsdir=$2; shift ;; 1011.2Sagc -c) custom=$2; shift ;; 1021.1Sagc -h) h=$2; shift ;; 1031.1Sagc -s) size=$2; shift ;; 1041.1Sagc -x) set -x ;; 1051.1Sagc *) break ;; 1061.1Sagc esac 1071.1Sagc shift 1081.1Sagcdone 1091.1Sagc 1101.1Sagcif [ $# -gt 0 ]; then 1111.1Sagc # take the next argument as being the image name 1121.1Sagc image="$1" 1131.1Sagc shift 1141.1Sagcfi 1151.1Sagc 1161.1Sagctotal=0 1171.1Sagcfor s in ${sets}; do 1181.1Sagc total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz)) 1191.1Sagcdone 1201.2Sagc# calculate size of custom files 1211.2Sagccustsize=0 1221.2Sagcif [ -d "${custom}" ]; then 1231.2Sagc custsize=$(ls -lR "${custom}" | awk 'NF == 9 { tot += $5 } END { print tot }') 1241.2Sagcfi 1251.2Sagctotal=$(expr \( \( ${total} + ${custsize} \) / 1000000 \) + ${overhead}) 1261.1Sagcif [ $size -eq 0 ]; then 1271.1Sagc # auto-size the pkgs fs 1281.1Sagc size=${total} 1291.1Sagcelse 1301.1Sagc # check that we've been given enough space 1311.1Sagc if [ ${total} -gt ${size} ]; then 1321.1Sagc echo "File system size given as ${size} MB, but it needs ${total} MB" >&2 1331.1Sagc exit 1 1341.1Sagc fi 1351.1Sagcfi 1361.1Sagc 1371.1Sagcecho "${bar} making a new ${size} MB image in ${image} ${bar}" 1381.1Sagcdd if=/dev/zero of=${image} bs=1m count=${size} 1391.1Sagc 1401.1Sagcvnddev=$(next_avail vnd) 1411.1Sagcecho "${bar} mounting image via vnd ${vnddev} ${bar}" 1421.2Sagc${sudo} vnconfig ${vnddev} ${image} 1431.2Sagc${sudo} newfs /dev/r${vnddev}a 1441.2Sagc${sudo} mount /dev/${vnddev}a /mnt 1451.1Sagc 1461.1Sagcecho "${bar} installing sets ${bar}" 1471.1Sagc(cd /mnt && 1481.1Sagc for s in ${sets}; do 1491.1Sagc echo ${s} 1501.2Sagc ${sudo} tar xpzf ${setsdir}/${s}.tgz 1511.1Sagc done 1521.1Sagc) 1531.1Sagc 1541.1Sagcecho "${bar} performing customisations ${bar}" 1551.2Sagc${sudo} rm -f /mnt/etc/motd 1561.1Sagc 1571.1Sagctmp=/tmp/usermode.$$ 1581.1Sagccat > ${tmp} << EOF 1591.1Sagc# NetBSD/usermode /etc/fstab 1601.1Sagc/dev/ld0a / ffs ro 1 1 1611.1Sagc/dev/ld1a /pkgs ffs ro 1 2 1621.1Sagckernfs /kern kernfs rw 1631.1Sagcptyfs /dev/pts ptyfs rw 1641.1Sagcprocfs /proc procfs rw 1651.1Sagc# mount /root as tmpfs on top of existing dir 1661.1Sagctmpfs /root.cow tmpfs rw,-s2M 0 0 1671.1Sagc/root.cow /root union rw,hidden 0 0 1681.1Sagc# mount /etc as tmpfs on top of existing dir 1691.1Sagctmpfs /etc.cow tmpfs rw,-s12M 0 0 1701.1Sagc/etc.cow /etc union rw,hidden 0 0 1711.1Sagc# mount /var/run as tmpfs on top of existing dir 1721.1Sagctmpfs /var.run tmpfs rw,-s1M 0 0 1731.1Sagc/var.run /var/run union rw,hidden - - 1741.1Sagc# mount /var/log as tmpfs on top of existing dir 1751.1Sagctmpfs /var.log tmpfs rw,-s10M 0 0 1761.1Sagc/var.log /var/log union rw,hidden - - 1771.1Sagctmpfs /var/db tmpfs rw,-s8M 0 0 1781.1Sagctmpfs /tmp tmpfs rw,-s32M 0 0 1791.1Sagc/dev/cd0a /cdrom cd9660 ro,noauto 1801.1SagcEOF 1811.2Sagc${sudo} mv ${tmp} /mnt/etc/fstab 1821.1Sagc 1831.1Sagccat > ${tmp} << EOF 1841.2Sagc# $NetBSD: mkimage,v 1.2 2012/01/20 02:19:47 agc Exp $ 1851.1Sagc# 1861.1Sagc# see rc.conf(5) for more information. 1871.1Sagc# 1881.1Sagc# Use program=YES to enable program, NO to disable it. program_flags are 1891.1Sagc# passed to the program on the command line. 1901.1Sagc# 1911.1Sagc 1921.1Sagc# Load the defaults in from /etc/defaults/rc.conf (if it's readable). 1931.1Sagc# These can be overridden below. 1941.1Sagc# 1951.1Sagcif [ -r /etc/defaults/rc.conf ]; then 1961.1Sagc . /etc/defaults/rc.conf 1971.1Sagcfi 1981.1Sagc 1991.1Sagc# If this is not set to YES, the system will drop into single-user mode. 2001.1Sagc# 2011.1Sagcrc_configured=YES 2021.1Sagc 2031.1Sagc# make sure we have the right rw filesystem at boot 2041.1Sagccritical_filesystems_local="/var/db /var.run /var/run /var.log /var/log /etc.cow /etc /root.cow /root" 2051.1Sagc 2061.1Sagc# Add local overrides below 2071.1Sagc# 2081.1Sagcdhcpcd=YES 2091.1Sagcsshd=YES 2101.1Sagc 2111.1Sagchostname=${h} 2121.1SagcEOF 2131.2Sagc${sudo} mv ${tmp} /mnt/etc/rc.conf 2141.1Sagc 2151.1Sagcecho "${bar} making extra directories ${bar}" 2161.1Sagcfor d in ${usermodedirs}; do 2171.2Sagc ${sudo} mkdir -p /mnt/${d} 2181.1Sagcdone 2191.1Sagcfor d in ${specialdirs}; do 2201.2Sagc ${sudo} mkdir -p /mnt/${d} 2211.1Sagcdone 2221.1Sagc 2231.1Sagcecho "${bar} customising /var/tmp ${bar}" 2241.2Sagc${sudo} rm -rf /mnt/var/tmp 2251.2Sagc(cd /mnt/var && ${sudo} ln -s /tmp tmp) 2261.1Sagc 2271.1Sagc# package-related stuff 2281.1Sagc(cat /mnt/etc/csh.cshrc;echo "setenv PKG_DBDIR /usr/pkg/.dbdir") > ${tmp} 2291.2Sagc${sudo} mv ${tmp} /mnt/etc/csh.cshrc 2301.1Sagc(cat /mnt/etc/profile;echo "export PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp} 2311.2Sagc${sudo} mv ${tmp} /mnt/etc/profile 2321.1Sagc(echo "PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp} 2331.2Sagc${sudo} mv ${tmp} /mnt/etc/mk.conf 2341.2Sagc(cd /mnt/usr && ${sudo} ln -s /pkgs/usr/pkg pkg) 2351.2Sagc 2361.2Sagc# last, customisation stage 2371.2Sagcif [ -d ${custom} ]; then 2381.2Sagc echo "${bar} user customisations from files in ${custom} ${bar}" 2391.2Sagc (cd ${custom} && ${sudo} pax -rwpe . /mnt) 2401.2Sagcfi 2411.1Sagc 2421.1Sagcdf /mnt 2431.1Sagc 2441.2Sagc${sudo} umount /mnt 2451.2Sagc${sudo} vnconfig -u ${vnddev} 2461.1Sagc 2471.1Sagcexit 0 248