#!/bin/sh -e
# Copyright @BABOLO (C)2002,2003
# All rights reserved.
#
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions
# are met:
# 1. Redistributions of source code must retain the above copyright
#    notice, this list of conditions and the following disclaimer.
# 2. Redistributions in binary form must reproduce the above copyright
#    notice, this list of conditions and the following disclaimer in the
#    documentation and/or other materials provided with the distribution.
#
# THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
# ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
# ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
# OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
# HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
# LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
# OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
# SUCH DAMAGE.
#
# $Id: addon,v 1.2 2003/02/08 00:16:56 babolo Exp $

. ${JAILUP_CONF}
    newfs=/sbin/newfs
    mount=/sbin/mount
   umount=/sbin/umount
disklabel=/sbin/disklabel
     zcat=/usr/bin/zcat
     gzip=/usr/bin/gzip
   mktemp=/usr/bin/mktemp
 truncate=/usr/bin/truncate
 vnconfig=/usr/sbin/vnconfig

if  [ -z "${1}" ]; then
    ${cat} << EOF
#     Usage:
# jailup vndevice [temp_mount_point]
#     !!! Parameters are different from other jailups !!!
# This command does not establish any jail at all, it just
# prepare vn device full of /dev filesystems for jails
EOF
    exit
fi

umask 022
disk="${1:-vn7}"
if [ -d /mnt/v ]; then
    mp="${2:-/mnt/v}"
else
    mp="${2:-/mnt}"
fi

t=`${mktemp} -t full`
f=`mktemp -t sl`

uudecode -p << "EOF" | dd of=${t} bs=1b count=1
begin 644 full
M_#'`CL".V([0O`!\OAI\OQH&N>8!\Z3I`(HQ]KN^![$$."]T"']XA?9U=(G>
M@,,0XN^%]G4"S1B`^H!R"XHV=02`QH`X\G("BA2)YXIT`8M,`KL`?(#^_W4R
M@_G_=2U14[NJ5;1!S1-R(('[5:IU&O;!`7056V9J`&;_=`@&4VH!:A")YK@`
M0NL%6UFX`0+-$XG\<@^!O_X!5:IU#/_COKP&ZQ&^U`;K#+[S!NL'NP<`M`[-
M$*R$P'7TZ_Y);G9A;&ED('!A<G1I=&EO;B!T86)L90!%<G)O<B!L;V%D:6YG
M(&]P97)A=&EN9R!S>7-T96T`36ES<VEN9R!O<&5R871I;F<@<WES=&5M````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M````````````````````````````````````````````````````````````
M``````````````````````````````````````````````````````````$`
MI0`_#P````#P`P`````!$*4`/Q_P`P``\`,``````2"E`#\OX`<``/`#````
1``$PI0`_/]`+``#P`P``5:H`
`
end
EOF

${truncate} -s 2016K ${t}
${vnconfig} -cs labels /dev/${disk} ${t}

${cat} > ${f} << "EOF"
type: unknown
disk: amnesiac
label: 
flags:
bytes/sector: 512
sectors/track: 63
tracks/cylinder: 1
sectors/cylinder: 63
cylinders: 16
sectors/unit: 1008
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0
track-to-track seek: 0
drivedata: 0 

8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:      126        0    4.2BSD      512  4096     8
  b:      126      252    4.2BSD      512  4096     8
  c:     1008        0    unused        0     0
  d:      126      378    4.2BSD      512  4096     8
  e:      126      504    4.2BSD      512  4096     8
  f:      126      630    4.2BSD      512  4096     8
  g:      126      756    4.2BSD      512  4096     8
  h:      126      882    4.2BSD      512  4096     8
EOF

for i in 1 2 3 4; do
    ${disklabel} -r -w ${disk}s${i} auto
    ${disklabel} -R ${disk}s${i} ${f}
    ${newfs} -t1 -u63 -c8 -f512 -b4096 -i300 ${disk}s${i}a
    ${mount} /dev/${disk}s${i}a ${mp}
    cd ${mp}
    ${ln} -fs /dev/MAKEDEV
    ./MAKEDEV jail
    ${rm} MAKEDEV
    cd ${TMPDIR:-/tmp}
    ${umount} /dev/${disk}s${i}a
done

cat > ${f} << "EOF"
type: unknown
disk: amnesiac
label: 
flags:
bytes/sector: 512
sectors/track: 63
tracks/cylinder: 1
sectors/cylinder: 63
cylinders: 16
sectors/unit: 1008
rpm: 3600
interleave: 1
trackskew: 0
cylinderskew: 0
headswitch: 0
track-to-track seek: 0
drivedata: 0 

8 partitions:
#        size   offset    fstype   [fsize bsize bps/cpg]
  a:      126      126    4.2BSD      512  4096     8
  b:      126      252    4.2BSD      512  4096     8
  c:     1008        0    unused        0     0
  d:      126      378    4.2BSD      512  4096     8
  e:      126      504    4.2BSD      512  4096     8
  f:      126      630    4.2BSD      512  4096     8
  g:      126      756    4.2BSD      512  4096     8
  h:      126      882    4.2BSD      512  4096     8
EOF

for i in 1 2 3 4; do
    ${disklabel} -R ${disk}s${i} ${f}
    for j in a b d e f g h; do
        ${newfs} -t1 -u63 -c8 -f512 -b4096 -i300 ${disk}s${i}${j}
        ${mount} /dev/${disk}s${i}${j} ${mp}
        cd ${mp}
        ${ln} -fs /dev/MAKEDEV
        ./MAKEDEV jail
        ${rm} MAKEDEV
        cd ${TMPDIR:-/tmp}
        ${umount} /dev/${disk}s${i}${j}
    done
done

${vnconfig} -d /dev/${disk}
${vnconfig} -u /dev/${disk}

${mtree} -dUeqxp / << "EOF"
/set type=file uname=root gname=wheel mode=0644 flags=none
.		type=dir mode=0755
    full.gz
    full
..
EOF

${gzip} -9 ${t}
${mv} -f ${t}.gz /full.gz

${mtree} -Ueqxp / << "EOF"
/set type=file uname=root gname=wheel mode=0644 flags=none
.		type=dir mode=0755
    full.gz	flags=schg
..
EOF

${cat} << EOF >> /etc/rc.local
#${zcat} /full.gz > /full
#${vnconfig} -cs labels ${disk} /full
EOF
