xref: /netbsd-src/distrib/utils/embedded/mkimage (revision b757af438b42b93f8c6571f026d8b8ef3eaf5fc9)
1#! /bin/sh
2
3# $NetBSD: mkimage,v 1.3 2012/02/29 04:49:50 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
86bar="==="
87custom=custom
88h=usermode1.$(uname -n)
89image=usermode.img
90overhead=8 # in MB
91sets="base etc modules"
92setsdir=/usr/build/release/$(uname -m)/binary/sets
93size=0	# in MB
94specialdirs="/kern /proc"
95usermodedirs="/var.cow /etc.cow /root.cow /pkgs"
96sudo="sudo"
97
98while [ $# -gt 0 ]; do
99	case "$1" in
100	-S)	setsdir=$2; shift ;;
101	-c)	custom=$2; shift ;;
102	-h)	h=$2; shift ;;
103	-s)	size=$2; shift ;;
104	-x)	set -x ;;
105	*)	break ;;
106	esac
107	shift
108done
109
110if [ $# -gt 0 ]; then
111	# take the next argument as being the image name
112	image="$1"
113	shift
114fi
115
116total=0
117for s in ${sets}; do
118	total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz))
119done
120# calculate size of custom files
121custsize=0
122if [ -d "${custom}" ]; then
123	custsize=$(ls -lR "${custom}" | awk 'NF == 9 { tot += $5 } END { print tot }')
124fi
125total=$(expr \( \( ${total} + ${custsize} \) / 1000000 \) + ${overhead})
126if [ $size -eq 0 ]; then
127        # auto-size the pkgs fs
128        size=${total}
129else
130        # check that we've been given enough space
131        if [ ${total} -gt ${size} ]; then
132                echo "File system size given as ${size} MB, but it needs ${total} MB" >&2
133                exit 1
134        fi
135fi
136
137echo "${bar} making a new ${size} MB image in ${image} ${bar}"
138dd if=/dev/zero of=${image} bs=1m count=${size}
139
140vnddev=$(next_avail vnd)
141echo "${bar} mounting image via vnd ${vnddev} ${bar}"
142${sudo} vnconfig ${vnddev} ${image}
143${sudo} newfs /dev/r${vnddev}a
144${sudo} mount /dev/${vnddev}a /mnt
145
146echo "${bar} installing sets ${bar}"
147(cd /mnt &&
148	for s in ${sets}; do
149		echo ${s}
150		${sudo} tar xpzf ${setsdir}/${s}.tgz
151	done
152)
153
154echo "${bar} performing customisations ${bar}"
155${sudo} rm -f /mnt/etc/motd
156
157tmp=/tmp/usermode.$$
158cat > ${tmp} << EOF
159# NetBSD/usermode /etc/fstab
160/dev/ld0a       /               ffs     ro              1 1
161/dev/ld1a	/pkgs		ffs	ro		1 2
162kernfs          /kern           kernfs  rw
163ptyfs           /dev/pts        ptyfs   rw
164procfs          /proc           procfs  rw
165# mount /root as tmpfs on top of existing dir
166tmpfs           /root.cow       tmpfs   rw,-s2M         0 0
167/root.cow       /root           union   rw,hidden       0 0
168# mount /etc as tmpfs on top of existing dir
169tmpfs           /etc.cow        tmpfs   rw,-s12M        0 0
170/etc.cow        /etc            union   rw,hidden       0 0
171# mount /var as tmpfs on top of existing dir
172tmpfs           /var.cow        tmpfs   rw,-s32M         0 0
173/var.cow        /var            union   rw,hidden       - -
174tmpfs           /tmp            tmpfs   rw,-s32M        0 0
175/dev/cd0a       /cdrom          cd9660  ro,noauto
176EOF
177${sudo} mv ${tmp} /mnt/etc/fstab
178
179cat > ${tmp} << EOF
180#       $NetBSD: mkimage,v 1.3 2012/02/29 04:49:50 agc Exp $
181#
182# see rc.conf(5) for more information.
183#
184# Use program=YES to enable program, NO to disable it. program_flags are
185# passed to the program on the command line.
186#
187
188# Load the defaults in from /etc/defaults/rc.conf (if it's readable).
189# These can be overridden below.
190#
191if [ -r /etc/defaults/rc.conf ]; then
192        . /etc/defaults/rc.conf
193fi
194
195# If this is not set to YES, the system will drop into single-user mode.
196#
197rc_configured=YES
198
199# make sure we have the right rw filesystem at boot
200critical_filesystems_local="/var.cow /var /etc.cow /etc /root.cow /root"
201
202# Add local overrides below
203#
204dhcpcd=YES
205sshd=YES
206
207hostname=${h}
208EOF
209${sudo} mv ${tmp} /mnt/etc/rc.conf
210
211echo "${bar} making extra directories ${bar}"
212for d in ${usermodedirs}; do
213	${sudo} mkdir -p /mnt/${d}
214done
215for d in ${specialdirs}; do
216	${sudo} mkdir -p /mnt/${d}
217done
218
219echo "${bar} customising /var/tmp ${bar}"
220${sudo} rm -rf /mnt/var/tmp
221(cd /mnt/var && ${sudo} ln -s /tmp tmp)
222
223# package-related stuff
224(cat /mnt/etc/csh.cshrc;echo "setenv PKG_DBDIR /usr/pkg/.dbdir") > ${tmp}
225${sudo} mv ${tmp} /mnt/etc/csh.cshrc
226(cat /mnt/etc/profile;echo "export PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
227${sudo} mv ${tmp} /mnt/etc/profile
228(echo "PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
229${sudo} mv ${tmp} /mnt/etc/mk.conf
230(cd /mnt/usr && ${sudo} ln -s /pkgs/usr/pkg pkg)
231
232# last, customisation stage
233if [ -d ${custom} ]; then
234	echo "${bar} user customisations from files in ${custom} ${bar}"
235	(cd ${custom} && ${sudo} pax -rwpe . /mnt)
236fi
237
238df /mnt
239
240${sudo} umount /mnt
241${sudo} vnconfig -u ${vnddev}
242
243exit 0
244