While the resulting image from mkimage can always be mounted with vnd,

and then modified, it is much easier to let this late, user-oriented
customisation be done automatically.  These mods let mkimage change
information based on files in a "custom" directory, which can be set
in mkimage using -c customdir.

After all other configuration, the files under custom/ will be copied
into the main image.  The files under custom/ are taken into account
when performing autosizing.

Also, generalise the sets directory to point to the current machine
architecture, although this can be overwritten from the command line
by anyone making cross-images.

An example to make a user image with an embedded read-only 100 MB
iSCSI target:

% ./mkimage -c custom
=== making a new 272 MB image in usermode.img ===
272+0 records in
272+0 records out
285212672 bytes transferred in 7.616 secs (37449142 bytes/sec)
=== mounting image via vnd vnd0 ===
/dev/rvnd0a: 272.0MB (557056 sectors) block size 8192, fragment size 1024
        using 6 cylinder groups of 45.34MB, 5803 blks, 11264 inodes.
super-block backups (for fsck_ffs -b #) at:
32, 92880, 185728, 278576, 371424, 464272,
=== installing sets ===
base
etc
modules
=== performing customisations ===
=== making extra directories ===
=== customising /var/tmp ===
=== user customisations from files in custom ===
Filesystem    1K-blocks       Used      Avail %Cap Mounted on
/dev/vnd0a       269967     263958      -7489 102% /mnt
% ls -l -R custom
total 8
drwxr-xr-x  3 agc  agc  512 Jan 18 03:58 etc
drwxr-xr-x  2 agc  agc  512 Jan 18 04:00 iscsi

custom/etc:
total 8
drwxr-xr-x  2 agc  agc  512 Jan 18 03:58 iscsi
-rw-r--r--  1 agc  agc  564 Jan 18 03:58 rc.conf

custom/etc/iscsi:
total 6
-r--r--r--  1 agc  agc  799 Jan 18 04:27 targets

custom/iscsi:
total 102484
-rw-r--r--  1 agc  agc  104857600 Jan 18 04:00 iscsi-target0
% sudo vnconfig vnd0 usermode.img
% sudo mount /dev/vnd0a /mnt
% cat /mnt/etc/iscsi/targets
# $NetBSD: targets,v 1.2 2006/02/16 19:30:57 agc Exp $
#
# Structure of this file:
#
# + an extent is a straight (offset, length) pair of a file or device
#   it's the lowest common storage denominator
#   at least one is needed
# + a device is made up of one or more extents or other devices
#   devices can be added in a hierachical manner, to enhance resilience
# + in this example, no device definitions are necessary, as the target
#   will just use a simple extent for persistent storage
# + a target is made up of 1 or more devices
# The code does not support RAID1 recovery at present

# Simple file showing 1 extent, mapped straight into 1 target

# extent        file or device          start           length
extent0         /iscsi/iscsi-target0    0               100MB

# target        flags   storage         netmask
target0         ro      extent0         0.0.0.0/0
% ls -l /mnt/iscsi/iscsi-target0
-rw-r--r--  1 agc  agc  104857600 Jan 18 04:00 /mnt/iscsi/iscsi-target0
% diff custom/etc/iscsi/targets /mnt/etc/iscsi/targets
%
This commit is contained in:
agc 2012-01-20 02:19:47 +00:00
parent cd209751f3
commit d2e3329102

View File

@ -1,6 +1,6 @@
#! /bin/sh
# $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 agc Exp $
# $NetBSD: mkimage,v 1.2 2012/01/20 02:19:47 agc Exp $
# Copyright (c) 2012 Alistair Crooks <agc@NetBSD.org>
# All rights reserved.
@ -83,19 +83,22 @@ sizeone() {
esac
}
setsdir=/usr/build/release/i386/binary/sets
size=0 # in MB
overhead=8 # in MB
image=usermode.img
h=usermode1.$(uname -n)
sets="base etc modules"
usermodedirs="/var.run /var.log /etc.cow /root.cow /pkgs"
specialdirs="/kern /proc"
bar="==="
custom=custom
h=usermode1.$(uname -n)
image=usermode.img
overhead=8 # in MB
sets="base etc modules"
setsdir=/usr/build/release/$(uname -m)/binary/sets
size=0 # in MB
specialdirs="/kern /proc"
usermodedirs="/var.run /var.log /etc.cow /root.cow /pkgs"
sudo="sudo"
while [ $# -gt 0 ]; do
case "$1" in
-S) setsdir=$2; shift ;;
-c) custom=$2; shift ;;
-h) h=$2; shift ;;
-s) size=$2; shift ;;
-x) set -x ;;
@ -114,7 +117,12 @@ total=0
for s in ${sets}; do
total=$(expr ${total} + $(sizeone ${setsdir}/${s}.tgz))
done
total=$(expr \( ${total} / 1000000 \) + ${overhead})
# calculate size of custom files
custsize=0
if [ -d "${custom}" ]; then
custsize=$(ls -lR "${custom}" | awk 'NF == 9 { tot += $5 } END { print tot }')
fi
total=$(expr \( \( ${total} + ${custsize} \) / 1000000 \) + ${overhead})
if [ $size -eq 0 ]; then
# auto-size the pkgs fs
size=${total}
@ -131,20 +139,20 @@ dd if=/dev/zero of=${image} bs=1m count=${size}
vnddev=$(next_avail vnd)
echo "${bar} mounting image via vnd ${vnddev} ${bar}"
sudo vnconfig ${vnddev} ${image}
sudo newfs /dev/r${vnddev}a
sudo mount /dev/${vnddev}a /mnt
${sudo} vnconfig ${vnddev} ${image}
${sudo} newfs /dev/r${vnddev}a
${sudo} mount /dev/${vnddev}a /mnt
echo "${bar} installing sets ${bar}"
(cd /mnt &&
for s in ${sets}; do
echo ${s}
sudo tar xpzf ${setsdir}/${s}.tgz
${sudo} tar xpzf ${setsdir}/${s}.tgz
done
)
echo "${bar} performing customisations ${bar}"
sudo rm -f /mnt/etc/motd
${sudo} rm -f /mnt/etc/motd
tmp=/tmp/usermode.$$
cat > ${tmp} << EOF
@ -170,10 +178,10 @@ tmpfs /var/db tmpfs rw,-s8M 0 0
tmpfs /tmp tmpfs rw,-s32M 0 0
/dev/cd0a /cdrom cd9660 ro,noauto
EOF
sudo mv ${tmp} /mnt/etc/fstab
${sudo} mv ${tmp} /mnt/etc/fstab
cat > ${tmp} << EOF
# $NetBSD: mkimage,v 1.1 2012/01/15 02:01:02 agc Exp $
# $NetBSD: mkimage,v 1.2 2012/01/20 02:19:47 agc Exp $
#
# see rc.conf(5) for more information.
#
@ -202,32 +210,38 @@ sshd=YES
hostname=${h}
EOF
sudo mv ${tmp} /mnt/etc/rc.conf
${sudo} mv ${tmp} /mnt/etc/rc.conf
echo "${bar} making extra directories ${bar}"
for d in ${usermodedirs}; do
sudo mkdir -p /mnt/${d}
${sudo} mkdir -p /mnt/${d}
done
for d in ${specialdirs}; do
sudo mkdir -p /mnt/${d}
${sudo} mkdir -p /mnt/${d}
done
echo "${bar} customising /var/tmp ${bar}"
sudo rm -rf /mnt/var/tmp
(cd /mnt/var && sudo ln -s /tmp tmp)
${sudo} rm -rf /mnt/var/tmp
(cd /mnt/var && ${sudo} ln -s /tmp tmp)
# package-related stuff
(cat /mnt/etc/csh.cshrc;echo "setenv PKG_DBDIR /usr/pkg/.dbdir") > ${tmp}
sudo mv ${tmp} /mnt/etc/csh.cshrc
${sudo} mv ${tmp} /mnt/etc/csh.cshrc
(cat /mnt/etc/profile;echo "export PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
sudo mv ${tmp} /mnt/etc/profile
${sudo} mv ${tmp} /mnt/etc/profile
(echo "PKG_DBDIR=/usr/pkg/.dbdir") > ${tmp}
sudo mv ${tmp} /mnt/etc/mk.conf
(cd /mnt/usr && sudo ln -s /pkgs/usr/pkg pkg)
${sudo} mv ${tmp} /mnt/etc/mk.conf
(cd /mnt/usr && ${sudo} ln -s /pkgs/usr/pkg pkg)
# last, customisation stage
if [ -d ${custom} ]; then
echo "${bar} user customisations from files in ${custom} ${bar}"
(cd ${custom} && ${sudo} pax -rwpe . /mnt)
fi
df /mnt
sudo umount /mnt
sudo vnconfig -u ${vnddev}
${sudo} umount /mnt
${sudo} vnconfig -u ${vnddev}
exit 0