mkimage revision 1.1
11.1Sagc#! /bin/sh
21.1Sagc
31.1Sagc# $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 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.1Sagcsetsdir=/usr/build/release/i386/binary/sets
871.1Sagcsize=0	# in MB
881.1Sagcoverhead=8 # in MB
891.1Sagcimage=usermode.img
901.1Sagch=usermode1.$(uname -n)
911.1Sagcsets="base etc modules"
921.1Sagcusermodedirs="/var.run /var.log /etc.cow /root.cow /pkgs"
931.1Sagcspecialdirs="/kern /proc"
941.1Sagcbar="==="
951.1Sagc
961.1Sagcwhile [ $# -gt 0 ]; do
971.1Sagc	case "$1" in
981.1Sagc	-S)	setsdir=$2; shift ;;
991.1Sagc	-h)	h=$2; shift ;;
1001.1Sagc	-s)	size=$2; shift ;;
1011.1Sagc	-x)	set -x ;;
1021.1Sagc	*)	break ;;
1031.1Sagc	esac
1041.1Sagc	shift
1051.1Sagcdone
1061.1Sagc
1071.1Sagcif [ $# -gt 0 ]; then
1081.1Sagc	# take the next argument as being the image name
1091.1Sagc	image="$1"
1101.1Sagc	shift
1111.1Sagcfi
1121.1Sagc
1131.1Sagctotal=0
1141.1Sagcfor s in ${sets}; do
1151.1Sagc	total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz))
1161.1Sagcdone
1171.1Sagctotal=$(expr \( ${total} / 1000000 \) + ${overhead})
1181.1Sagcif [ $size -eq 0 ]; then
1191.1Sagc        # auto-size the pkgs fs
1201.1Sagc        size=${total}
1211.1Sagcelse
1221.1Sagc        # check that we've been given enough space
1231.1Sagc        if [ ${total} -gt ${size} ]; then
1241.1Sagc                echo "File system size given as ${size} MB, but it needs ${total} MB" >&2
1251.1Sagc                exit 1
1261.1Sagc        fi
1271.1Sagcfi
1281.1Sagc
1291.1Sagcecho "${bar} making a new ${size} MB image in ${image} ${bar}"
1301.1Sagcdd if=/dev/zero of=${image} bs=1m count=${size}
1311.1Sagc
1321.1Sagcvnddev=$(next_avail vnd)
1331.1Sagcecho "${bar} mounting image via vnd ${vnddev} ${bar}"
1341.1Sagcsudo vnconfig ${vnddev} ${image}
1351.1Sagcsudo newfs /dev/r${vnddev}a
1361.1Sagcsudo mount /dev/${vnddev}a /mnt
1371.1Sagc
1381.1Sagcecho "${bar} installing sets ${bar}"
1391.1Sagc(cd /mnt &&
1401.1Sagc	for s in ${sets}; do
1411.1Sagc		echo ${s}
1421.1Sagc		sudo tar xpzf ${setsdir}/${s}.tgz
1431.1Sagc	done
1441.1Sagc)
1451.1Sagc
1461.1Sagcecho "${bar} performing customisations ${bar}"
1471.1Sagcsudo rm -f /mnt/etc/motd
1481.1Sagc
1491.1Sagctmp=/tmp/usermode.$$
1501.1Sagccat > ${tmp} << EOF
1511.1Sagc# NetBSD/usermode /etc/fstab
1521.1Sagc/dev/ld0a       /               ffs     ro              1 1
1531.1Sagc/dev/ld1a	/pkgs		ffs	ro		1 2
1541.1Sagckernfs          /kern           kernfs  rw
1551.1Sagcptyfs           /dev/pts        ptyfs   rw
1561.1Sagcprocfs          /proc           procfs  rw
1571.1Sagc# mount /root as tmpfs on top of existing dir
1581.1Sagctmpfs           /root.cow       tmpfs   rw,-s2M         0 0
1591.1Sagc/root.cow       /root           union   rw,hidden       0 0
1601.1Sagc# mount /etc as tmpfs on top of existing dir
1611.1Sagctmpfs           /etc.cow        tmpfs   rw,-s12M        0 0
1621.1Sagc/etc.cow        /etc            union   rw,hidden       0 0
1631.1Sagc# mount /var/run as tmpfs on top of existing dir
1641.1Sagctmpfs           /var.run        tmpfs   rw,-s1M         0 0
1651.1Sagc/var.run        /var/run        union   rw,hidden       - -
1661.1Sagc# mount /var/log as tmpfs on top of existing dir
1671.1Sagctmpfs           /var.log        tmpfs   rw,-s10M        0 0
1681.1Sagc/var.log        /var/log        union   rw,hidden       - -
1691.1Sagctmpfs           /var/db         tmpfs   rw,-s8M         0 0
1701.1Sagctmpfs           /tmp            tmpfs   rw,-s32M        0 0
1711.1Sagc/dev/cd0a       /cdrom          cd9660  ro,noauto
1721.1SagcEOF
1731.1Sagcsudo mv ${tmp} /mnt/etc/fstab
1741.1Sagc
1751.1Sagccat > ${tmp} << EOF
1761.1Sagc#       $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 agc Exp $
1771.1Sagc#
1781.1Sagc# see rc.conf(5) for more information.
1791.1Sagc#
1801.1Sagc# Use program=YES to enable program, NO to disable it. program_flags are
1811.1Sagc# passed to the program on the command line.
1821.1Sagc#
1831.1Sagc
1841.1Sagc# Load the defaults in from /etc/defaults/rc.conf (if it's readable).
1851.1Sagc# These can be overridden below.
1861.1Sagc#
1871.1Sagcif [ -r /etc/defaults/rc.conf ]; then
1881.1Sagc        . /etc/defaults/rc.conf
1891.1Sagcfi
1901.1Sagc
1911.1Sagc# If this is not set to YES, the system will drop into single-user mode.
1921.1Sagc#
1931.1Sagcrc_configured=YES
1941.1Sagc
1951.1Sagc# make sure we have the right rw filesystem at boot
1961.1Sagccritical_filesystems_local="/var/db /var.run /var/run /var.log /var/log /etc.cow /etc /root.cow /root"
1971.1Sagc
1981.1Sagc# Add local overrides below
1991.1Sagc#
2001.1Sagcdhcpcd=YES
2011.1Sagcsshd=YES
2021.1Sagc
2031.1Sagchostname=${h}
2041.1SagcEOF
2051.1Sagcsudo mv ${tmp} /mnt/etc/rc.conf
2061.1Sagc
2071.1Sagcecho "${bar} making extra directories ${bar}"
2081.1Sagcfor d in ${usermodedirs}; do
2091.1Sagc	sudo mkdir -p /mnt/${d}
2101.1Sagcdone
2111.1Sagcfor d in ${specialdirs}; do
2121.1Sagc	sudo mkdir -p /mnt/${d}
2131.1Sagcdone
2141.1Sagc
2151.1Sagcecho "${bar} customising /var/tmp ${bar}"
2161.1Sagcsudo rm -rf /mnt/var/tmp
2171.1Sagc(cd /mnt/var && sudo ln -s /tmp tmp)
2181.1Sagc
2191.1Sagc# package-related stuff
2201.1Sagc(cat /mnt/etc/csh.cshrc;echo "setenv PKG_DBDIR /usr/pkg/.dbdir") > ${tmp}
2211.1Sagcsudo mv ${tmp} /mnt/etc/csh.cshrc
2221.1Sagc(cat /mnt/etc/profile;echo "export PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
2231.1Sagcsudo mv ${tmp} /mnt/etc/profile
2241.1Sagc(echo "PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
2251.1Sagcsudo mv ${tmp} /mnt/etc/mk.conf
2261.1Sagc(cd /mnt/usr && sudo ln -s /pkgs/usr/pkg pkg)
2271.1Sagc
2281.1Sagcdf /mnt
2291.1Sagc
2301.1Sagcsudo umount /mnt
2311.1Sagcsudo vnconfig -u ${vnddev}
2321.1Sagc
2331.1Sagcexit 0
234