Home | History | Annotate | Line # | Download | only in embedded
      1  1.1  agc #! /bin/sh
      2  1.1  agc 
      3  1.1  agc # $NetBSD: mkpkgs,v 1.1 2012/01/15 02:01:02 agc Exp $
      4  1.1  agc 
      5  1.1  agc # Copyright (c) 2012 Alistair Crooks <agc (at] NetBSD.org>
      6  1.1  agc # All rights reserved.
      7  1.1  agc #
      8  1.1  agc # Redistribution and use in source and binary forms, with or without
      9  1.1  agc # modification, are permitted provided that the following conditions
     10  1.1  agc # are met:
     11  1.1  agc # 1. Redistributions of source code must retain the above copyright
     12  1.1  agc #    notice, this list of conditions and the following disclaimer.
     13  1.1  agc # 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  agc #    notice, this list of conditions and the following disclaimer in the
     15  1.1  agc #    documentation and/or other materials provided with the distribution.
     16  1.1  agc #
     17  1.1  agc # THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
     18  1.1  agc # IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
     19  1.1  agc # OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
     20  1.1  agc # IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
     21  1.1  agc # INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
     22  1.1  agc # NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
     23  1.1  agc # DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
     24  1.1  agc # THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
     25  1.1  agc # (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
     26  1.1  agc # THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
     27  1.1  agc #
     28  1.1  agc 
     29  1.1  agc # find next available vnd device, from kre
     30  1.1  agc next_avail ()
     31  1.1  agc {
     32  1.1  agc 	local dev="$1"
     33  1.1  agc 	local N=$(( ${#dev} + 1 ))
     34  1.1  agc 	local unit units
     35  1.1  agc 
     36  1.1  agc 	units=$(
     37  1.1  agc 		sysctl -n hw.disknames		|
     38  1.1  agc 			tr ' ' '\012'		|
     39  1.1  agc 			grep '^'"${dev}"'[0-9]'	|
     40  1.1  agc 			sort -n -k 1.$N			)
     41  1.1  agc 
     42  1.1  agc 	test -z "${units}" && {
     43  1.1  agc 		test -e "/dev/${dev}0a" || {
     44  1.1  agc 			echo >&2 "No ${dev}s available!"
     45  1.1  agc 			return 1
     46  1.1  agc 		}
     47  1.1  agc 		echo "${dev}0"
     48  1.1  agc 		return
     49  1.1  agc 	}
     50  1.1  agc 
     51  1.1  agc 	N=0
     52  1.1  agc 	for unit in ${units}
     53  1.1  agc 	do
     54  1.1  agc 		if [ "${unit}" = "${dev}${N}" ]
     55  1.1  agc 		then
     56  1.1  agc 			N=$(( N + 1 ))
     57  1.1  agc 		else
     58  1.1  agc 			echo "${dev}${N}"
     59  1.1  agc 			return
     60  1.1  agc 		fi
     61  1.1  agc 	done
     62  1.1  agc 
     63  1.1  agc 	test -e /dev/"${dev}${N}a" || {
     64  1.1  agc 		echo >&2 "All ${dev}s in use"
     65  1.1  agc 		return 1
     66  1.1  agc 	}
     67  1.1  agc 
     68  1.1  agc 	echo "${dev}${N}"
     69  1.1  agc }
     70  1.1  agc 
     71  1.1  agc # find the size of the gzipped files in a .tgz archive
     72  1.1  agc sizeone() {
     73  1.1  agc         case "$1" in
     74  1.1  agc         *.tgz|*.tar.gz)
     75  1.1  agc                 tar tvzf "$1" | awk '{ tot += $5 } END { print tot }'
     76  1.1  agc                 ;;
     77  1.1  agc         *.tbz|*.tar.bz2)
     78  1.1  agc                 tar tvjf "$1" | awk '{ tot += $5 } END { print tot }'
     79  1.1  agc                 ;;
     80  1.1  agc         *)
     81  1.1  agc                 echo 0
     82  1.1  agc                 ;;
     83  1.1  agc         esac
     84  1.1  agc }
     85  1.1  agc 
     86  1.1  agc pkgdir=/usr/pkgsrc/packages/All
     87  1.1  agc size=0	# in MB
     88  1.1  agc image=pkgs.img
     89  1.1  agc pkgs="digest screen sudo"
     90  1.1  agc bar="==="
     91  1.1  agc 
     92  1.1  agc while [ $# -gt 0 ]; do
     93  1.1  agc 	case "$1" in
     94  1.1  agc 	-S)	pkgdir=$2; shift ;;
     95  1.1  agc 	-o)	image=$2; shift ;;
     96  1.1  agc 	-s)	size=$2; shift ;;
     97  1.1  agc 	-x)	set -x ;;
     98  1.1  agc 	*)	break ;;
     99  1.1  agc 	esac
    100  1.1  agc 	shift
    101  1.1  agc done
    102  1.1  agc 
    103  1.1  agc while [ $# -gt 0 ]; do
    104  1.1  agc 	# take the next argument as being the image name
    105  1.1  agc 	pkgs="${pkgs} $1"
    106  1.1  agc 	shift
    107  1.1  agc done
    108  1.1  agc 
    109  1.1  agc # find the size of the fs needed
    110  1.1  agc total=0
    111  1.1  agc for p in ${pkgs}; do
    112  1.1  agc 	total=$(expr $total + $(sizeone ${pkgdir}/${p}-*.tgz))
    113  1.1  agc done
    114  1.1  agc total=$(expr \( $total / 1000000 \) + 2)
    115  1.1  agc if [ $size -eq 0 ]; then
    116  1.1  agc 	# auto-size the pkgs fs
    117  1.1  agc 	size=${total}
    118  1.1  agc else
    119  1.1  agc 	# check that we've been given enough space
    120  1.1  agc 	if [ ${total} -gt ${size} ]; then
    121  1.1  agc 		echo "File system size given as ${size} MB, but it needs ${total} MB" >&2
    122  1.1  agc 		exit 1
    123  1.1  agc 	fi
    124  1.1  agc fi
    125  1.1  agc 
    126  1.1  agc echo "${bar} making a new ${size} MB image in ${image} ${bar}"
    127  1.1  agc dd if=/dev/zero of=${image} bs=1m count=${size}
    128  1.1  agc 
    129  1.1  agc vnddev=$(next_avail vnd)
    130  1.1  agc echo "${bar} mounting image via vnd ${vnddev} ${bar}"
    131  1.1  agc sudo vnconfig ${vnddev} ${image}
    132  1.1  agc sudo newfs /dev/r${vnddev}a
    133  1.1  agc sudo mount /dev/${vnddev}a /mnt
    134  1.1  agc 
    135  1.1  agc echo "${bar} installing packages ${bar}"
    136  1.1  agc sudo mkdir -p /mnt/usr/pkg/.dbdir
    137  1.1  agc for p in ${pkgs}; do
    138  1.1  agc 	echo "${bar} Installing ${p} ${bar}"
    139  1.1  agc 	sudo pkg_add -K /usr/pkg/.dbdir -P /mnt -v ${pkgdir}/${p}-*.tgz
    140  1.1  agc done
    141  1.1  agc 
    142  1.1  agc df /mnt
    143  1.1  agc 
    144  1.1  agc sudo umount /mnt
    145  1.1  agc sudo vnconfig -u ${vnddev}
    146  1.1  agc 
    147  1.1  agc exit 0
    148