Home | History | Annotate | Line # | Download | only in embedded
mkimage revision 1.3.2.2
      1 #! /bin/sh
      2 
      3 # $NetBSD: mkimage,v 1.3.2.2 2012/04/17 00:02:49 yamt Exp $
      4 
      5 # Copyright (c) 2012 Alistair Crooks <agc (at] 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
     30 next_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
     72 sizeone() {
     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 
     86 bar="==="
     87 custom=custom
     88 h=usermode1.$(uname -n)
     89 image=usermode.img
     90 overhead=8 # in MB
     91 sets="base etc modules"
     92 setsdir=/usr/build/release/$(uname -m)/binary/sets
     93 size=0	# in MB
     94 specialdirs="/kern /proc"
     95 usermodedirs="/var.cow /etc.cow /root.cow /pkgs"
     96 sudo="sudo"
     97 
     98 while [ $# -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
    108 done
    109 
    110 if [ $# -gt 0 ]; then
    111 	# take the next argument as being the image name
    112 	image="$1"
    113 	shift
    114 fi
    115 
    116 total=0
    117 for s in ${sets}; do
    118 	total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz))
    119 done
    120 # calculate size of custom files
    121 custsize=0
    122 if [ -d "${custom}" ]; then
    123 	custsize=$(ls -lR "${custom}" | awk 'NF == 9 { tot += $5 } END { print tot }')
    124 fi
    125 total=$(expr \( \( ${total} + ${custsize} \) / 1000000 \) + ${overhead})
    126 if [ $size -eq 0 ]; then
    127         # auto-size the pkgs fs
    128         size=${total}
    129 else
    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
    135 fi
    136 
    137 echo "${bar} making a new ${size} MB image in ${image} ${bar}"
    138 dd if=/dev/zero of=${image} bs=1m count=${size}
    139 
    140 vnddev=$(next_avail vnd)
    141 echo "${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 
    146 echo "${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 
    154 echo "${bar} performing customisations ${bar}"
    155 ${sudo} rm -f /mnt/etc/motd
    156 
    157 tmp=/tmp/usermode.$$
    158 cat > ${tmp} << EOF
    159 # NetBSD/usermode /etc/fstab
    160 /dev/ld0a       /               ffs     ro              1 1
    161 /dev/ld1a	/pkgs		ffs	ro		1 2
    162 kernfs          /kern           kernfs  rw
    163 ptyfs           /dev/pts        ptyfs   rw
    164 procfs          /proc           procfs  rw
    165 # mount /root as tmpfs on top of existing dir
    166 tmpfs           /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
    169 tmpfs           /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
    172 tmpfs           /var.cow        tmpfs   rw,-s32M         0 0
    173 /var.cow        /var            union   rw,hidden       - -
    174 tmpfs           /tmp            tmpfs   rw,-s32M        0 0
    175 /dev/cd0a       /cdrom          cd9660  ro,noauto
    176 EOF
    177 ${sudo} mv ${tmp} /mnt/etc/fstab
    178 
    179 cat > ${tmp} << EOF
    180 #       $NetBSD: mkimage,v 1.3.2.2 2012/04/17 00:02:49 yamt 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 #
    191 if [ -r /etc/defaults/rc.conf ]; then
    192         . /etc/defaults/rc.conf
    193 fi
    194 
    195 # If this is not set to YES, the system will drop into single-user mode.
    196 #
    197 rc_configured=YES
    198 
    199 # make sure we have the right rw filesystem at boot
    200 critical_filesystems_local="/var.cow /var /etc.cow /etc /root.cow /root"
    201 
    202 # Add local overrides below
    203 #
    204 dhcpcd=YES
    205 sshd=YES
    206 
    207 hostname=${h}
    208 EOF
    209 ${sudo} mv ${tmp} /mnt/etc/rc.conf
    210 
    211 echo "${bar} making extra directories ${bar}"
    212 for d in ${usermodedirs}; do
    213 	${sudo} mkdir -p /mnt/${d}
    214 done
    215 for d in ${specialdirs}; do
    216 	${sudo} mkdir -p /mnt/${d}
    217 done
    218 
    219 echo "${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
    233 if [ -d ${custom} ]; then
    234 	echo "${bar} user customisations from files in ${custom} ${bar}"
    235 	(cd ${custom} && ${sudo} pax -rwpe . /mnt)
    236 fi
    237 
    238 df /mnt
    239 
    240 ${sudo} umount /mnt
    241 ${sudo} vnconfig -u ${vnddev}
    242 
    243 exit 0
    244