cvs commit: hints/ATTACHMENTS/initrd mkinitrd

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


tushar      03/10/29 23:51:32

  Added:       ATTACHMENTS/initrd mkinitrd
  Log:
  Added Attachments for Hint: initrd
  
  Revision  Changes    Path
  1.1                  hints/ATTACHMENTS/initrd/mkinitrd
  
  Index: mkinitrd
  ===================================================================
  #!/bin/bash
  
  # mkinitrd for LFS by Jim Gifford <scripts at j555.com>
  # Release 3.02
  
  # 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] [--cdrom = remove cdrom modules]"
  	echo "	[--ide-scsi = remove ide-scsi modules]"
  	echo "	[--initrdfs={romfs|ext2|ext3|cramfs} = use either romfs,ext2,ext3, or cramfs]"
  	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."
  			;;
  		--ide-scsi)
  			REMOVE_IDE_SCSI="1"
  			echo "	Removing IDE-SCSI Module."
  			;;
  		--cdrom)
  			REMOVE_CDROM="1"
  			echo "	Removing CDROM 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
  		if [ "$REMOVE_CDROM" == "1" ]
  			then
  				POST_IDE="ide-disk ide-floppy"
  			else
  				POST_IDE="ide-disk cdrom ide-cd ide-floppy"
  		fi
  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"
  		if [ "$REMOVE_CDROM" == "1" ]
  			then
  				POST_SCSI=""
  			else
  				POST_SCSI="cdrom sr_mod"
  		fi
  fi
  
  # Add or Remove IDE-SCSI Module
  #
  if [ "$REMOVE_IDE_SCSI" == "1" ]
  	then
  		IDE_SCSI_MODULE=""
  	else
  		if [ "$REMOVE_CDROM" == "1" ]
  			then
  				IDE_SCSI_MODULE="ide-scsi"
  			else
  				IDE_SCSI_MODULE="ide-scsi cdrom sr_mod"
  		fi
  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.$$"
  IMAGE="/tmp/initrd.img-$$"
  MOUNT_POINT="/tmp/initrd.mnt-$$"
  LINUXRC="$MOUNT_IMAGE/linuxrc"
  INITRD="/boot/initrd-$KERNEL_VERSION.img"
  
  # Check for filesystem for initrd
  #
  if [ "$INITRDFS" == "" ]
  	then
  		echo "	Autoselecting romfs File System for initrd."
  		INITRDFS="romfs"
  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}
  
  rm -rf $MOUNT_POINT/lost+found
  
  # Copying Static Programs
  #
  echo "	Copying Files..."
  cp -a /var/lfsupdate/Executeables/busybox.initrd $MOUNT_IMAGE/bin/busybox
  cp -a /var/lfsupdate/Executeables/busybox.initrd /bin/busybox
  ln -s /bin/busybox $MOUNT_IMAGE/bin/echo
  ln -s /bin/busybox $MOUNT_IMAGE/bin/sh
  ln -s /bin/busybox $MOUNT_IMAGE/bin/insmod
  cp -a /etc/fstab $MOUNT_IMAGE/etc/fstab
  cp -a /etc/modules.conf $MOUNT_IMAGE/etc/modules.conf
  rm -f /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 "		Module $module added to initrd."
  		echo "insmod /lib/$module.o" >> $LINUXRC
  		}
  done
  
  echo "echo \"Initial RAMDISK Loading Completed...\"" >> $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
  
  if [ "$INITRDFS" == "romfs" ]
  	then
  		echo "	Creating romfs FileSystem..."
  		genromfs -f "$IMAGE" -d "$MOUNT_POINT" -V "LFS initrd for kernel $KERNEL_VERSION"
  fi
  
  if [ "$INITRDFS" == "cramfs" ]
  	then
  		echo "	Creating cramfs FileSystem..."
  		mkfs.cramfs "$MOUNT_POINT" "$IMAGE"
  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