#!/bin/bash # # Randy - Copy a RedHat Initial Ram Disk # # The point of this script is to make a copy of an initial RAM disk (initrd) # from one of the RedHat installation bootdisks while making it possible # to remove unneeded modules to make space on the disk for a Kickstart # configuration file. # # Note: This is not an automatic tool, it just provides a lot of # assistance. You still have to know what the hell you are doing. # # Eric Myers - 9 April 2001 # @(#) $Id: Randy,v 1.2 2001/04/10 15:03:36 myers Exp myers $ ###################################################################### compress=1 # compress the final target? YES! IMAGESIZE=1250 # smaller is better MODDIR=/tmp/2.2.14-5.0BOOT MODULES=/tmp/modules.cgz BOOTDIR=`pwd`/bootnet IMAGE0=/tmp/initrd.img.orig IMAGE1=/tmp/initrd.img.new target=initrd.img /bin/rm -f $target INITRD=$BOOTDIR/initrd.img if [ $# -gt 0 ]; then INITRD=$1 shift fi echo "initrd is $INITRD" /bin/cp $INITRD ${IMAGE0}.gz gunzip --force ${IMAGE0}.gz if [ ! -f $IMAGE0 ]; then echo "Cannot unpack image initrd." exit 2 fi # First mount original intrd on loop device for devnum in 0 1 2 3 4 5 6 7 8; do if losetup /dev/loop$devnum $IMAGE0 2>/dev/null ; then break; fi done if [ "$devnum" = "8" ]; then echo "Cannot get loopback device for Original initrd!" >&2 exit 1 fi LODEV0=/dev/loop$devnum MNTPOINT0=/tmp/mnt$devnum echo "Using loopback device $LODEV0 for Original initrd:" losetup $LODEV0 clean0 () { umount $MNTPOINT0 losetup -d $LODEV0 rm $IMAGE0 } trap "clean0; exit" 2 3 15 mkdir -p $MNTPOINT0 mount -t ext2 $LODEV0 $MNTPOINT0 || { echo "Cannot mount loopback device $LODEV0" exit 1 } ORIG=$MNTPOINT0 if [ -d $ORIG ]; then echo "=============== =" echo "$ORIG:" ls $ORIG echo "=============== =" else echo "Cannot find $ORIG" exit 2 fi echo " " echo "Original initrd now mounted on $ORIG (press return to continue)" read JUNK ################## # Extract the modules from original MODCGZ=$ORIG/modules/modules.cgz if [ -f $MODCGZ ]; then gunzip < $MODCGZ > /tmp/modules.cpio ( cd /tmp; cpio -idv < modules.cpio ) else echo "Cannot find $MODCGZ" fi ################ # Now mount new empty filesystem on next loop device /bin/rm -f $IMAGE1 dd if=/dev/zero of=$IMAGE1 bs=1k count=$IMAGESIZE 2> /dev/null ls -s $IMAGE1 for devnum in 0 1 2 3 4 5 6 7 8; do if losetup /dev/loop$devnum $IMAGE1 2>/dev/null ; then break; fi done if [ "$devnum" = "8" ]; then echo "Cannot get loopback device for NEW!" >&2 clean0; exit 1 fi LODEV1=/dev/loop$devnum MNTPOINT1=/tmp/mnt$devnum echo "Using loopback device $LODEV1 for new initrd." losetup $LODEV1 clean1 () { umount $MNTPOINT1 losetup -d $LODEV1 rm $IMAGE1 } trap "clean0; clean1; exit " 2 3 15 # make a filesystem on new file # We have to "echo y |" so that it doesn't complain about $IMAGE not # being a block device echo y | mke2fs $LODEV1 $IMAGESIZE >/dev/null 2>/dev/null echo "Made ext2 filesystem on $LODEV1" mkdir -p $MNTPOINT1 mount -t ext2 $LODEV1 $MNTPOINT1 || { echo "Cannot mount loopback device $LODEV1" clean 0; clean1; exit 1 } NEW=$MNTPOINT1 ############ # copy the original initrd /bin/cp -a $ORIG/* $NEW if [ -d $NEW ]; then echo "=============== " ls $NEW echo "=============== " else echo "Cannot find $NEW" exit 2 fi echo " " echo "New initrd is now mounted on $NEW " echo "Edit modules etc as you like and then press Enter." read JUNK # Now collect modules from module dir and replace find $MODDIR -print | cpio -H crc -ov | gzip -9 > $MODULES /bin/cp -i $MODULES $NEW/modules/ #################### # Compress target and clean up if [ -n "$compress" ]; then echo "compressing the image..." gzip -9 < $IMAGE1 > $target else cp -a $IMAGE1 $target fi echo "Final result is $target" clean0 clean1 exit 0