mkimage revision 1.1
1#! /bin/sh
2
3# $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 agc Exp $
4
5# Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
6# All rights reserved.
7#
8# Redistribution and use in source and binary forms, with or without
9# modification, are permitted provided that the following conditions
10# are met:
11# 1. Redistributions of source code must retain the above copyright
12#    notice, this list of conditions and the following disclaimer.
13# 2. Redistributions in binary form must reproduce the above copyright
14#    notice, this list of conditions and the following disclaimer in the
15#    documentation and/or other materials provided with the distribution.
16#
17# THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19# OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20# IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21# INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22# NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27#
28
29# find next available vnd, from kre
30next_avail ()
31{
32	local dev="$1"
33	local N=$(( ${#dev} + 1 ))
34	local unit units
35
36	units=$(
37		sysctl -n hw.disknames		|
38			tr ' ' '\012'		|
39			grep '^'"${dev}"'[0-9]'	|
40			sort -n -k 1.$N			)
41
42	test -z "${units}" && {
43		test -e "/dev/${dev}0a" || {
44			echo >&2 "No ${dev}s available!"
45			return 1
46		}
47		echo "${dev}0"
48		return
49	}
50
51	N=0
52	for unit in ${units}
53	do
54		if [ "${unit}" = "${dev}${N}" ]
55		then
56			N=$(( N + 1 ))
57		else
58			echo "${dev}${N}"
59			return
60		fi
61	done
62
63	test -e /dev/"${dev}${N}a" || {
64		echo >&2 "All ${dev}s in use"
65		return 1
66	}
67
68	echo "${dev}${N}"
69}
70
71# find the size of the gzipped files in a .tgz archive
72sizeone() {
73        case "$1" in 
74        *.tgz|*.tar.gz)
75                tar tvzf "$1" | awk '{ tot += $5 } END { print tot }'
76                ;;
77        *.tbz|*.tar.bz2)
78                tar tvjf "$1" | awk '{ tot += $5 } END { print tot }' 
79                ;;
80        *)
81                echo 0
82                ;; 
83        esac
84}
85
86setsdir=/usr/build/release/i386/binary/sets
87size=0	# in MB
88overhead=8 # in MB
89image=usermode.img
90h=usermode1.$(uname -n)
91sets="base etc modules"
92usermodedirs="/var.run /var.log /etc.cow /root.cow /pkgs"
93specialdirs="/kern /proc"
94bar="==="
95
96while [ $# -gt 0 ]; do
97	case "$1" in
98	-S)	setsdir=$2; shift ;;
99	-h)	h=$2; shift ;;
100	-s)	size=$2; shift ;;
101	-x)	set -x ;;
102	*)	break ;;
103	esac
104	shift
105done
106
107if [ $# -gt 0 ]; then
108	# take the next argument as being the image name
109	image="$1"
110	shift
111fi
112
113total=0
114for s in ${sets}; do
115	total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz))
116done
117total=$(expr \( ${total} / 1000000 \) + ${overhead})
118if [ $size -eq 0 ]; then
119        # auto-size the pkgs fs
120        size=${total}
121else
122        # check that we've been given enough space
123        if [ ${total} -gt ${size} ]; then
124                echo "File system size given as ${size} MB, but it needs ${total} MB" >&2
125                exit 1
126        fi
127fi
128
129echo "${bar} making a new ${size} MB image in ${image} ${bar}"
130dd if=/dev/zero of=${image} bs=1m count=${size}
131
132vnddev=$(next_avail vnd)
133echo "${bar} mounting image via vnd ${vnddev} ${bar}"
134sudo vnconfig ${vnddev} ${image}
135sudo newfs /dev/r${vnddev}a
136sudo mount /dev/${vnddev}a /mnt
137
138echo "${bar} installing sets ${bar}"
139(cd /mnt &&
140	for s in ${sets}; do
141		echo ${s}
142		sudo tar xpzf ${setsdir}/${s}.tgz
143	done
144)
145
146echo "${bar} performing customisations ${bar}"
147sudo rm -f /mnt/etc/motd
148
149tmp=/tmp/usermode.$$
150cat > ${tmp} << EOF
151# NetBSD/usermode /etc/fstab
152/dev/ld0a       /               ffs     ro              1 1
153/dev/ld1a	/pkgs		ffs	ro		1 2
154kernfs          /kern           kernfs  rw
155ptyfs           /dev/pts        ptyfs   rw
156procfs          /proc           procfs  rw
157# mount /root as tmpfs on top of existing dir
158tmpfs           /root.cow       tmpfs   rw,-s2M         0 0
159/root.cow       /root           union   rw,hidden       0 0
160# mount /etc as tmpfs on top of existing dir
161tmpfs           /etc.cow        tmpfs   rw,-s12M        0 0
162/etc.cow        /etc            union   rw,hidden       0 0
163# mount /var/run as tmpfs on top of existing dir
164tmpfs           /var.run        tmpfs   rw,-s1M         0 0
165/var.run        /var/run        union   rw,hidden       - -
166# mount /var/log as tmpfs on top of existing dir
167tmpfs           /var.log        tmpfs   rw,-s10M        0 0
168/var.log        /var/log        union   rw,hidden       - -
169tmpfs           /var/db         tmpfs   rw,-s8M         0 0
170tmpfs           /tmp            tmpfs   rw,-s32M        0 0
171/dev/cd0a       /cdrom          cd9660  ro,noauto
172EOF
173sudo mv ${tmp} /mnt/etc/fstab
174
175cat > ${tmp} << EOF
176#       $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 agc Exp $
177#
178# see rc.conf(5) for more information.
179#
180# Use program=YES to enable program, NO to disable it. program_flags are
181# passed to the program on the command line.
182#
183
184# Load the defaults in from /etc/defaults/rc.conf (if it's readable).
185# These can be overridden below.
186#
187if [ -r /etc/defaults/rc.conf ]; then
188        . /etc/defaults/rc.conf
189fi
190
191# If this is not set to YES, the system will drop into single-user mode.
192#
193rc_configured=YES
194
195# make sure we have the right rw filesystem at boot
196critical_filesystems_local="/var/db /var.run /var/run /var.log /var/log /etc.cow /etc /root.cow /root"
197
198# Add local overrides below
199#
200dhcpcd=YES
201sshd=YES
202
203hostname=${h}
204EOF
205sudo mv ${tmp} /mnt/etc/rc.conf
206
207echo "${bar} making extra directories ${bar}"
208for d in ${usermodedirs}; do
209	sudo mkdir -p /mnt/${d}
210done
211for d in ${specialdirs}; do
212	sudo mkdir -p /mnt/${d}
213done
214
215echo "${bar} customising /var/tmp ${bar}"
216sudo rm -rf /mnt/var/tmp
217(cd /mnt/var && sudo ln -s /tmp tmp)
218
219# package-related stuff
220(cat /mnt/etc/csh.cshrc;echo "setenv PKG_DBDIR /usr/pkg/.dbdir") > ${tmp}
221sudo mv ${tmp} /mnt/etc/csh.cshrc
222(cat /mnt/etc/profile;echo "export PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
223sudo mv ${tmp} /mnt/etc/profile
224(echo "PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
225sudo mv ${tmp} /mnt/etc/mk.conf
226(cd /mnt/usr && sudo ln -s /pkgs/usr/pkg pkg)
227
228df /mnt
229
230sudo umount /mnt
231sudo vnconfig -u ${vnddev}
232
233exit 0
234