#!/bin/sh
#
#  Copyright (C) 1997 - 2000  Heinz Mauelshagen, Sistina Software
# 
#  June 1999
#  December 1999
#  January 2000
# 
#  LVM is free software; you can redistribute it and/or modify
#  it under the terms of the GNU General Public License as published by
#  the Free Software Foundation; either version 2, or (at your option)
#  any later version.
#  
#  LVM is distributed in the hope that it will be useful,
#  but WITHOUT ANY WARRANTY; without even the implied warranty of
#  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#  GNU General Public License for more details.
#  
#  You should have received a copy of the GNU General Public License
#  along with LVM; see the file COPYING.  If not, write to
#  the Free Software Foundation, 59 Temple Place - Suite 330,
#  Boston, MA 02111-1307, USA. 
# 

#
# Changelog
#
#   16/11/1999 - corrected -N type with mke2fs
#   19/12/1999 - use correct ramdisk size
#                strip shared library to save space
#   04/01/2000 - support /proc mount, because lvm_dir_cache now uses it
#

cmd=`basename $0`
VERSION=$1
[ -z "$VERSION" ] && VERSION=`uname -r`
DEVRAM=/tmp/initrd.$$
INITRD=/boot/initrd.gz
INITRDSIZE=8192
INITRDFILES="/sbin/modprobe /sbin/vgchange /sbin/vgscan /bin/bash /bin/mount /bin/umount /bin/sh /bin/rm /sbin/insmod /sbin/modprobe /dev/* /lib/modules/${VERSION}/modules.dep"
if [ `echo ${VERSION}|cut -b0-3` = "2.4" ]
then
  INITRDFILES="$INITRDFILES /lib/modules/${VERSION}/kernel/drivers/md/lvm-mod.o"
else
  INITRDFILES="$INITRDFILES /lib/modules/${VERSION}/block/lvm-mod.o"
fi

TMP=/tmp/mnt.$$

trap "
  cd /
  umount $DEVRAM
  rm -f $DEVRAM $TMP
  echo -e 'Bye bye...\n'
" 1 2 3 15


function create_fstab {
   echo "
/dev/ram        /                         ext2            defaults   1   0
none            /proc                     proc            defaults   0   0
   " > etc/fstab
   chmod 644 etc/fstab
}

function create_linuxrc {
   echo "#!/bin/sh
   /sbin/modprobe lvm-mod
   /bin/mount /proc
   /sbin/vgscan
   /sbin/vgchange -a y
   /bin/umount /proc" > linuxrc
   chmod 555 linuxrc
}

#
# Main
#
echo -e "\nLogical Volume Manager 0.8 by Heinz Mauelshagen  11/11/1999\n"
echo -e "$cmd -- this script creates a LVM initial ram disk in $INITRD\n"

echo "$cmd -- making ram filesystem"
dd if=/dev/zero of=$DEVRAM count=8192 bs=1024 2>/dev/null >/dev/null
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR creating loopback file\n"
   exit 1
fi

mke2fs -F -m0 -i 1024 $DEVRAM $INITRDSIZE >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR making ram disk filesystem\n"
   exit 1
fi

mkdir -p $TMP/ram >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR making $TMP/ram\n"
   exit 1
fi

echo "$cmd -- mounting ram filesystem"
mount -t ext2 -oloop $DEVRAM $TMP/ram >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR mounting $DEVRAM on $TMP/ram\n"
   exit 1
fi

cd $TMP/ram
mkdir etc proc

#
# create new modules configuration to avoid kmod complaining
# about nonexsisting modules.
#
MODSCONF=etc/conf.modules
[ -r /etc/modules.conf ] && MODSCONF=etc/modules.conf
echo "$cmd -- creating $MODSCONF"
i=0
while [ $i -lt 256 ]; do
   echo "alias	block-major-$i	off"
   echo "alias	char-major-$i	off"
   let i=i+1
done > $MODSCONF

# to ensure, that modprobe doesn complain about timestamps
echo "$cmd -- creating new modules.dep"
depmod -a ${VERSION} >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR depmod\n"
   exit 1
fi

# copy necessary files to ram disk
echo "$cmd -- copying files to ram disk"
find $INITRDFILES|cpio -pdm $TMP/ram >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR cpio to ram disk\n"
   exit 1
fi

# figure out which actual shared libraries we need in our initrd
echo "$cmd -- figuring out shared libraries"
SHLIBS=`ldd sbin/vg* bin/sh|awk '{if(/=>/){print $3}}'|sort -u 2>/dev/null`
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR figuring out needed shared libraries\n"
   exit 1
fi

echo "$cmd -- copying shared libraries to ram disk"
find $SHLIBS|cpio -Lpdm $TMP/ram >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR copying needed shared libraries to ram disk\n"
   exit 1
fi
for lib in $SHLIBS
do
   strip $TMP/ram$lib >/dev/null
done

echo "$cmd -- creating linuxrc"
create_linuxrc >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR creating linuxrc\n"
   exit 1
fi

echo "$cmd -- creating etc/fstab"
create_fstab >/dev/null 2>&1
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR creating etc/fstab\n"
   exit 1
fi

cd /
echo "$cmd -- ummounting ram disk"
umount $DEVRAM
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR umounting $DEVRAM\n"
   exit 1
fi

echo "$cmd -- creating compressed initrd in $INITRD"
dd if=$DEVRAM bs=1k count=$INITRDSIZE 2>/dev/null | gzip -9 > $INITRD 2>/dev/null
if [ $? -ne 0 ]; then
   echo -e "$cmd -- ERROR creating $INITRD\n"
   exit 1
fi

rm -f $DEVRAM $TMP
