cvs commit: hints/ATTACHMENTS/initrd-diskcheck mkfsckinitrd

tushar at linuxfromscratch.org tushar at linuxfromscratch.org
Wed Oct 29 22:51:58 PST 2003


tushar      03/10/29 23:51:58

  Added:       ATTACHMENTS/initrd-diskcheck mkfsckinitrd
  Log:
  Added Attachments for Hint: initrd-diskcheck
  
  Revision  Changes    Path
  1.1                  hints/ATTACHMENTS/initrd-diskcheck/mkfsckinitrd
  
  Index: mkfsckinitrd
  ===================================================================
  #!/bin/bash
  
  # mkfsckinitrd for LFS by Jim Gifford <scripts at j555.com>
  # Release 3.0
  
  # Define Swap and ZIP Drives
  #
  REMOVE_PARTITIONS="SWAPDRIVE ZIPDRIVE"
  
  # Usage Menu
  #
  usage () {
  	echo "$0 : options"
  	echo "options available"
  	echo "  Required"
  	echo "	[--kernel_version={version} = use kernel version]"
  	echo "	Optional"
  	echo "	[--ide = remove ide modules] [--scsi = remove scsi modules]"
  	echo "	[--usb = remove usb modules] "
  	echo "	[--initrdfs={ext2|ext3} = use either ext2 or ext3]"
  	echo "	[--remove-module={module} = remove module]"
  	echo "	[--add-module={module} - add module]"
  	exit
  }
  
  # Input Check
  #
  if [ "$1" == "" ]
  	then
  		usage
  fi
  
  
  echo "Checking Input..."
  while [ $# -gt 0 ]
  	do
  		case $1 in
  		--ide)
  			REMOVE_IDE="1"
  			echo "	Removing IDE Modules."
  			;;
  		--scsi)
  			REMOVE_SCSI="1"
  			echo "	Removing SCSI Modules."
  			;;
  		--usb)
  			REMOVE_USB="1"
  			echo "	Removing USB Modules."
  			;;
  		--kernel_version=*)
  			KERNEL_VERSION="`echo $1 | awk -F= '{print $2;}'`"
  			echo "	Making image for kernel $KERNEL_VERSION."
  			;;
  		--initrdfs=*)
  			INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
  			echo "	Initrd will be using $INITRDFS filesystem."
  			;;
  		--remove-module=*)
  			if [ "$REMOVE_MODULE" == "" ]
  				then
  					REMOVE_MODULE="`echo $1 | awk -F= '{print $2;}'`"
  					echo "	Removing Module $REMOVE_MODULE."
  
  				else
  					NEW_MODULE="`echo $1 | awk -F= '{print $2;}'`"
  					echo "	Removing Module $NEW_MODULE."
  					REMOVE_MODULE="$REMOVE_MODULE $NEW_MODULE"
  			fi
  			;;
  		--add-module=*)
  			if [ "$ADD_MODULE" == "" ]
  				then
  					ADD_MODULE="`echo $1 | awk -F= '{print $2;}'`"
  					echo "	Adding Module $ADD_MODULE."
  				else
  					NEW_MODULE="`echo $1 | awk -F= '{print $2;}'`"
  					echo "	Adding Module $NEW_MODULE"
  					ADD_MODULE="$ADD_MODULE $NEW_MODULE."
  			fi
  			;;
  		*)
  			usage
  			;;
  		esac
  	shift
  done
  
  echo "Setting up Parameters..."
  
  # Files
  #
  CONFIG_FILE="/etc/modules.conf"
  rm -f /tmp/foundmodules
  rm -f /tmp/copiedmodules
  
  # Find Dependencies
  #
  finddep () {
  echo "depfile=/lib/modules/$KERNEL_VERSION/modules.dep" > /tmp/config
  
  for module in $MODULES
  	do
  		modprobe -C /tmp/config $module -v -n 2> /dev/null | \
  		sed -ne 's#^/sbin/insmod.*/\(.*\)\.o.*#\1#p' >> /tmp/foundmodules
  	done
  }
  
  # Set Root Device and File System
  #
  ROOT_DEVICE="$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' /etc/fstab)"
  
  # Add or Remove IDE Modules
  #
  if [ "$REMOVE_IDE" == "1" ]
  	then
  		PRE_IDE=""
  		IDE_MODULES=""
  		POST_IDE=""
  	else
  		PRE_IDE=""
  		if [ -e /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/pci ]
  			then
  				CHECK="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/pci`"
  				for check in $CHECK
  					do	
  						IDE_MODULE="`echo $check | cut -f1 -d'.'`"
  						IDE_MODULES="$IDE_MODULES $IDE_MODULE"
  					done
  		fi
  		if [ -e /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/isa ]
  			then
  				CHECK="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/isa`"
  				for check in $CHECK
  					do	
  						IDE_MODULE="`echo $check | cut -f1 -d'.'`"
  						IDE_MODULES="$IDE_MODULES $IDE_MODULE"
  					done
  		fi
  		POST_IDE="ide-disk ide-floppy"
  fi
  
  # Add or Remove SCSI Modules
  #
  if [ "$REMOVE_SCSI" == "1" ]
  	then
  		PRE_SCSI=""
  		SCSI_MODULES=""
  		POST_SCSI=""
  	else
  		PRE_SCSI="sd_mod"
  		SCSI_MODULE="`grep scsi_hostadapter $CONFIG_FILE | grep -v '^[    ]*#' | awk '{ print $3 }'`"
  		SCSI_MODULES="$SCSI_MODULE"
  		POST_SCSI=""
  fi
  
  # Add or Remove USB Modules
  #
  if [ "$REMOVE_USB" == "1" ]
  	then
  		PRE_USB=""
  		USB_MODULES=""
  		POST_USB=""
  	else
  		PRE_USB="usbcore"
  		USB_MODULES="`grep usb-controller $CONFIG_FILE | grep -v '^[    ]*#' | awk '{ print $3 }'`"
  		POST_USB="usb-storage"
  fi
  
  # Modules checked
  #
  MODULES="$PRE_IDE $IDE_MODULES $POST_IDE
  	 $PRE_SCSI $SCSI_MODULES $POST_SCSI
  	 $IDE_SCSI_MODULE
  	 $PRE_USB $USB_MODULES $POST_USB
  	 $ADD_MODULE"
  
  finddep
  
  # Set initrd name and size
  #
  IMAGE_SIZE=4000
  MOUNT_IMAGE="/tmp/initrd-fsck.$$"
  IMAGE="/tmp/initrd-fsck.img-$$"
  MOUNT_POINT="/tmp/initrd-fsck.mnt-$$"
  LINUXRC="$MOUNT_IMAGE/linuxrc"
  INITRD="/boot/initrd-fsck-$KERNEL_VERSION.img"
  
  # Getting Disk Information
  #
  DISKS="`ls -1 /dev/discs`"
  SWAP="`cat /etc/fstab | grep swap | cut -f1`"
  
  # Check for filesystem for initrd
  #
  if [ "$INITRDFS" == "" ]
  	then
  		echo "	Autoselecting ext3 File System for initrd."
  		INITRDFS="ext3"
  fi
  
  # Check for initrd Directory
  #
  if ! [ -e /initrd ]
  	then
  		mkdir /initrd
  fi
  		
  # Check for RAM Disk Device
  #
  if [ -e /dev/.devfsd ]
  	then
  		RAM_DEVICE="rd"
  	else
  		RAM_DEVICE="ram0"
  fi
  
  # Create Image and setup Loopback
  #
  dd if=/dev/zero of=$IMAGE bs=1k count=$IMAGE_SIZE 2> /dev/null
  
  if [ "$INITRDFS" == "ext2" ] || [ "$INITRDFS" == "ext3" ]
  	then
  		# Creating LoopBack Device
  		#
  		echo "	Setting up Lookback Device..."
  		for device_number in 0 1 2 3 4 5 6 7 8
  			do
  				if losetup /dev/loop$device_number $IMAGE 2>/dev/null
  					then
  						break
  				fi
  			done
  
  		if [ "$device_number" = "8" ]
  			then
  				rm -rf $MOUNT_POINT $IMAGE
  				echo "		All of your loopback devices are in use!" >&2
  			exit 1
  		fi
  
  	LOOP_DEVICE=/dev/loop$device_number
  	echo "		Using loopback device $LOOP_DEVICE"
  fi
  
  echo "Checking for Initrd FileSystem..."
  
  # Make sure the initrd filesystem is in the kernel.
  #
  if [ "$INITRDFS" == "ext2" ]
  	then
  		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  		if [ "$SEARCH" == "ext2" ]
  			then
  				echo "	$INITRDFS is compiled as a module."
  				exit
  			else
  				echo "	$INITRDFS is compiled into the kernel."
  				echo y | mkfs.ext2 $LOOP_DEVICE $IMAGE_SIZE > /dev/null 2> /dev/null
  		fi
  fi
  
  if [ "$INITRDFS" == "ext3" ]
  	then
  		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  		if [ "$SEARCH" == "ext3" ]
  			then
  				echo "	$INITRDFS is compiled as a module."
  				exit
  			else
  				echo "	$INITRDFS is compiled into the kernel."
  				echo y | mkfs.ext3 $LOOP_DEVICE $IMAGE_SIZE > /dev/null 2> /dev/null
  		fi
  fi
  
  if [ "$INITRDFS" == "romfs" ]
  	then
  		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  		if [ "$SEARCH" == "romfs" ]
  			then
  				echo "	$INITRDFS is compiled as a module."
  				exit
  			else
  				echo "	$INITRDFS is compiled into the kernel."
  		fi
  fi
  
  if [ "$INITRDFS" == "cramfs" ]
  	then
  		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  		if [ "$SEARCH" == "cramfs" ]
  			then
  				echo "	$INITRDFS is compiled as a module."
  				exit
  			else
  				echo "	$INITRDFS is compiled into the kernel."
  
  		fi
  fi
  
  echo "Creating Initrd..."
  
  # Creating initrd directory
  #
  echo "	Creating Mount Point..."
  mkdir -p $MOUNT_POINT
  if [ "$INITRDFS" == "ext2" ] || [ "$INITRDFS" == "ext3" ]
  	then
  		echo "	Creating $INITRDFS File System..."
  		mount -t $INITRDFS $LOOP_DEVICE $MOUNT_POINT || {
  							     echo "		Can't get a loopback device"
  							     exit 1
  					   }
  fi
  
  # Creating Directories
  #
  echo "	Creating Directories..."
  mkdir -p $MOUNT_IMAGE
  mkdir -p $MOUNT_IMAGE/{lib,bin,etc,dev,proc}
  
  rm -rf $MOUNT_POINT/lost+found
  
  # Copying Static Programs
  #
  cp -a /etc/modules.conf $MOUNT_IMAGE/etc/modules.conf
  cp -a /etc/fstab $MOUNT_IMAGE/etc/fstab
  cp -a /sbin/e2fsck.static $MOUNT_IMAGE/bin/fsck
  cp -a /sbin/busybox.static.fsck $MOUNT_IMAGE/bin/busybox
  cp -a /sbin/busybox.static.fsck /bin/busybox
  cp -a /bin/busybox $MOUNT_IMAGE/bin/busybox
  ln -s /bin/busybox $MOUNT_IMAGE/bin/echo
  ln -s /bin/busybox $MOUNT_IMAGE/bin/ln
  ln -s /bin/busybox $MOUNT_IMAGE/bin/mount
  ln -s /bin/busybox $MOUNT_IMAGE/bin/sh
  ln -s /bin/busybox $MOUNT_IMAGE/bin/umount
  ln -s /bin/busybox $MOUNT_IMAGE/bin/insmod
  rm -rf /bin/busybox
  
  # Copying Modules
  #
  rm -f /tmp/copiedmodules
  
  MODULES="`cat /tmp/foundmodules`"
  
  for MODULE in $MODULES
  	do
  		echo "$MODULE" | {
  			IFS=':' read module
  			module=$module
  			SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $module.o`"
  			REMOVE_MOD="0"
  			if ! [ "$SEARCH" == "" ]
  				then
  					if ! [ -e $MOUNT_IMAGE/lib/$module.o ]
  						then
  							for remove in $REMOVE_MODULE
  								do
  									if [ "$remove" == "$module" ]
  										then
  											REMOVE_MOD="1"
  									fi
  								done
  							if [ "$REMOVE_MOD" != "1" ]
  								then
  									cp $SEARCH $MOUNT_IMAGE/lib
  									echo "$module" >> /tmp/copiedmodules
  							fi
  					fi
  			fi
  	           }
  	done
  
  for i in console null $RAM_DEVICE tty[1234]
  	do
  		cp -a /dev/$i $MOUNT_IMAGE/dev
  done
  
  # Creating linuxrc File
  #
  echo "#!/bin/sh" > $LINUXRC
  echo "" >> $LINUXRC
  
  echo "echo \"Initial RAMDISK Loading Starting...\"" >> $LINUXRC
  
  MODULES="`cat /tmp/copiedmodules`"
  for MODULE in $MODULES
  	do
  		echo "$MODULE" | {
  		IFS=':' read module
  		module=$module
  
  		echo "	Adding $module to initrd."
  		echo "insmod /lib/$module.o" >> $LINUXRC
  		}
  done
  echo "mount -n -t proc none /proc" >> $LINUXRC
  echo "ln -s /proc/mounts /etc/mtab" >> $LINUXRC
  for disc in $DISKS
          do
  		if [ -e /dev/discs/$disc/part1 ]
  			then
  				PARTITIONS="`ls -1 /dev/discs/$disc/part*`"
  				
  				for partition in $PARTITIONS
  					do
  						REMOVE_PART="0"
  						for remove in $REMOVE_PARTITIONS
  							do
  								if [ "$partition" = "$remove" ]
  									then
  										REMOVE_PART="1"
  								fi
  								if [ "$REMOVE_PART" != "1" ]
  									then
  										TEST="`cat $LINUXRC | grep $partition`"
  										if [ "$TEST" == "" ]
  											then
  												echo "echo \"Checking Partition $partition...\"" >> $LINUXRC
  												echo "fsck -f -y $partition" >> $LINUXRC
  										fi
  								fi
  							done
  					done
  		fi
          done
  echo "umount /proc" >> $LINUXRC
  
  chmod +x $LINUXRC
  
  # Unmount and Compress initrd
  #
  (cd $MOUNT_IMAGE; tar cf - .) | (cd $MOUNT_POINT; tar xf -)
  
  if [ "$INITRDFS" == "ext2" ] || [ "$INITRDFS" == "ext3" ]
  	then
  		umount $MOUNT_POINT
  		losetup -d $LOOP_DEVICE
  fi
  
  gzip -9 < $IMAGE > $INITRD
  
  # Remove all temp files
  #
  rm -rf $MOUNT_IMAGE $MOUNT_POINT $IMAGE
  rm -f /tmp/foundmodules
  rm -f /tmp/copiedmodules
  rm -f /tmp/config
  
  if [ -e $INITRD ]
  	then
  		echo "Initrd was created.."
  fi
  
  
  



More information about the hints mailing list