11.1Sagc#! /bin/sh
21.1Sagc
31.1Sagc# $NetBSD: mkpkgs,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 device, 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.1Sagcpkgdir=/usr/pkgsrc/packages/All
871.1Sagcsize=0	# in MB
881.1Sagcimage=pkgs.img
891.1Sagcpkgs="digest screen sudo"
901.1Sagcbar="==="
911.1Sagc
921.1Sagcwhile [ $# -gt 0 ]; do
931.1Sagc	case "$1" in
941.1Sagc	-S)	pkgdir=$2; shift ;;
951.1Sagc	-o)	image=$2; shift ;;
961.1Sagc	-s)	size=$2; shift ;;
971.1Sagc	-x)	set -x ;;
981.1Sagc	*)	break ;;
991.1Sagc	esac
1001.1Sagc	shift
1011.1Sagcdone
1021.1Sagc
1031.1Sagcwhile [ $# -gt 0 ]; do
1041.1Sagc	# take the next argument as being the image name
1051.1Sagc	pkgs="${pkgs} $1"
1061.1Sagc	shift
1071.1Sagcdone
1081.1Sagc
1091.1Sagc# find the size of the fs needed
1101.1Sagctotal=0
1111.1Sagcfor p in ${pkgs}; do
1121.1Sagc	total=$(expr $total + $(sizeone ${pkgdir}/${p}-*.tgz))
1131.1Sagcdone
1141.1Sagctotal=$(expr \( $total / 1000000 \) + 2)
1151.1Sagcif [ $size -eq 0 ]; then
1161.1Sagc	# auto-size the pkgs fs
1171.1Sagc	size=${total}
1181.1Sagcelse
1191.1Sagc	# check that we've been given enough space
1201.1Sagc	if [ ${total} -gt ${size} ]; then
1211.1Sagc		echo "File system size given as ${size} MB, but it needs ${total} MB" >&2
1221.1Sagc		exit 1
1231.1Sagc	fi
1241.1Sagcfi
1251.1Sagc
1261.1Sagcecho "${bar} making a new ${size} MB image in ${image} ${bar}"
1271.1Sagcdd if=/dev/zero of=${image} bs=1m count=${size}
1281.1Sagc
1291.1Sagcvnddev=$(next_avail vnd)
1301.1Sagcecho "${bar} mounting image via vnd ${vnddev} ${bar}"
1311.1Sagcsudo vnconfig ${vnddev} ${image}
1321.1Sagcsudo newfs /dev/r${vnddev}a
1331.1Sagcsudo mount /dev/${vnddev}a /mnt
1341.1Sagc
1351.1Sagcecho "${bar} installing packages ${bar}"
1361.1Sagcsudo mkdir -p /mnt/usr/pkg/.dbdir
1371.1Sagcfor p in ${pkgs}; do
1381.1Sagc	echo "${bar} Installing ${p} ${bar}"
1391.1Sagc	sudo pkg_add -K /usr/pkg/.dbdir -P /mnt -v ${pkgdir}/${p}-*.tgz
1401.1Sagcdone
1411.1Sagc
1421.1Sagcdf /mnt
1431.1Sagc
1441.1Sagcsudo umount /mnt
1451.1Sagcsudo vnconfig -u ${vnddev}
1461.1Sagc
1471.1Sagcexit 0
148