cvs commit: hints/ATTACHMENTS/initrd mkinitrd

tushar at linuxfromscratch.org tushar at linuxfromscratch.org
Sun Feb 15 15:49:15 PST 2004


tushar      04/02/15 16:49:15

  Modified:    ATTACHMENTS/initrd mkinitrd
  Log:
  Added: initrd attachment
  
  Revision  Changes    Path
  1.3       +171 -154  hints/ATTACHMENTS/initrd/mkinitrd
  
  Index: mkinitrd
  ===================================================================
  RCS file: /home/cvsroot/hints/ATTACHMENTS/initrd/mkinitrd,v
  retrieving revision 1.2
  retrieving revision 1.3
  diff -u -u -r1.2 -r1.3
  --- mkinitrd	3 Dec 2003 18:36:40 -0000	1.2
  +++ mkinitrd	15 Feb 2004 23:49:15 -0000	1.3
  @@ -1,7 +1,7 @@
   #!/bin/bash
   
  -# mkinitrd for LFS by Jim Gifford <scripts at j555.com>
  -# Release 3.10
  +# mkinitrd for 2.4/2.6 Kernel by Jim Gifford <scripts at j555.com>
  +# Release 4.02
   
   # Usage Menu
   #
  @@ -10,17 +10,16 @@
   	echo "options available"
   	echo "  Required"
   	echo "	[--kernel_version={version} = use kernel version]"
  -	echo "  Root Change Method - one must be selected"
  -	echo "  [--change-root]"
  -	echo "  [--pivot-root]"
   	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 "	[--devfs = remove devfs settings from initrd]"
  +	echo "	[--initrdfs={ext2|ext3} = use either ext2 or ext3]"
   	echo "	[--remove-module={module} = remove module]"
   	echo "	[--add-module={module} - add module]"
  -	exit
  +	echo "	[--root-device={device} - skips auto detection of the root device]"
  +	exit 1
   }
   
   # Input Check
  @@ -35,16 +34,6 @@
   while [ $# -gt 0 ]
   	do
   		case $1 in
  -		--change-root)
  -			CHANGE_ROOT="1"
  -			PIVOT_ROOT="0"
  -			echo "	Change Root Method Selected."
  -			;;
  -		--pivot-root)
  -			PIVOT_ROOT="1"
  -			CHANGE_ROOT="0"
  -			echo "	Pivot Root Method Selected."
  -			;;
   		--ide)
   			REMOVE_IDE="1"
   			echo "	Removing IDE Modules."
  @@ -57,6 +46,10 @@
   			REMOVE_IDE_SCSI="1"
   			echo "	Removing IDE-SCSI Module."
   			;;
  +		--devfs)
  +			REMOVE_DEVFS="1"
  +			echo "	Removing devfs settings."
  +			;;
   		--cdrom)
   			REMOVE_CDROM="1"
   			echo "	Removing CDROM Modules"
  @@ -68,6 +61,11 @@
   		--kernel_version=*)
   			KERNEL_VERSION="`echo $1 | awk -F= '{print $2;}'`"
   			echo "	Making image for kernel $KERNEL_VERSION."
  +			KER_VER_REL="`echo $KERNEL_VERSION | grep -o 2.6`"
  +			if [ "$KER_VER_REL" != "2.6" ]
  +				then
  +					KER_VER_REL="2.4"
  +			fi			
   			;;
   		--initrdfs=*)
   			INITRDFS="`echo $1 | awk -F= '{print $2;}'`"
  @@ -96,6 +94,9 @@
   					ADD_MODULE="$ADD_MODULE $NEW_MODULE."
   			fi
   			;;
  +		--root-device=*)
  +				ROOT_DEVICE="`echo $1 | awk -F= '{print $2;}'`"
  +			;;
   		*)
   			usage
   			;;
  @@ -105,51 +106,65 @@
   
   echo "Setting up Parameters..."
   
  -
   # Show Root Device Information
   #
  -ROOT_DEVICE="$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' /etc/fstab)"
  +if [ "$ROOT_DEVICE" == "" ]
  +	then
  +		ROOT_DEVICE="$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $1; }}' /etc/fstab)"
  +fi
  +
   echo "	Root Device is $ROOT_DEVICE."
   
  -# Check for Method
  +# Get Disk Information
   #
  -METHOD="0"
  -
  -if [ "$PIVOT_ROOT" == "1" ] && [ "$CHANGE_ROOT" == "0" ]
  -	then
  -		METHOD="1"
  -fi
  +SD_DEVICE="`awk '/^[ \t]*[^#]/ { print $1; }' /etc/fstab | grep sd`"
  +HD_DEVICE="`awk '/^[ \t]*[^#]/ { print $1; }' /etc/fstab | grep hd`"
   
  -if [ "$CHANGE_ROOT" == "1" ] && [ "$PIVOT_ROOT" == "0" ]
  +# Module Files
  +#
  +if [ "$KER_VER_REL" == "2.4" ]
   	then
  -		METHOD="1"
  +		CONFIG_FILE="/etc/modules.conf"
  +		EXTENSION="o"
   fi
   
  -if [ "$METHOD" != "1" ]
  +if [ "$KER_VER_REL" == "2.6" ]
   	then
  -		echo "	No Root Change Method Choosen."
  -		echo "......"
  -		usage
  +		CONFIG_FILE="/etc/modprobe.conf"
  +		EXTENSION="ko"
   fi
   
  -# 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
  +		if [ "$KER_VER_REL" == "2.4" ]
  +			then
  +				echo "depfile=/lib/modules/$KERNEL_VERSION/modules.dep" > /tmp/config
  +				modprobe -C /tmp/config $module -v -n 2> /dev/null | \
  +				sed -ne "s#^/sbin/insmod.*/\(.*\)\.$EXTENSION.*#\1#p" >> /tmp/foundmodules
  +		fi
  +		if [ "$KER_VER_REL" == "2.6" ]
  +			then
  +				modprobe --set-version=$KERNEL_VERSION --show-depends $module 2> /dev/null | \
  +				sed -ne "s#^insmod.*/\(.*\)\.$EXTENSION.*#\1#p" >> /tmp/foundmodules
  +		fi
   	done
   }
   
  +# Find Modules
  +#
  +find_module() {
  +
  +	FIND_MOD=$1
  +	SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $FIND_MOD`"
  +}
  +
   # Add or Remove IDE Modules
   #
   if [ "$REMOVE_IDE" == "1" ]
  @@ -158,22 +173,13 @@
   		IDE_MODULES=""
   		POST_IDE=""
   	else
  -		PRE_IDE=""
  -		if [ -e /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/pci ]
  +		PRE_IDE="ide-probe-mod"
  +		if [ -e /proc/ide ]
   			then
  -				CHECK="`ls -1 /lib/modules/$KERNEL_VERSION/kernel/drivers/ide/pci`"
  +				CHECK="`ls -1 /proc/ide`"
   				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_MODULE="`echo $check`"
   						IDE_MODULES="$IDE_MODULES $IDE_MODULE"
   					done
   		fi
  @@ -239,6 +245,8 @@
   	 $PRE_USB $USB_MODULES $POST_USB
   	 $ADD_MODULE"
   
  +# Find the module Dependencies
  +#
   finddep
   
   # Set initrd name and size
  @@ -250,12 +258,51 @@
   LINUXRC="$MOUNT_IMAGE/linuxrc"
   INITRD="/boot/initrd-$KERNEL_VERSION.img"
   
  +# Check for Valid Kernel
  +#
  +if [ -e /lib/modules/$KERNEL_VERSION ]
  +	then
  +			echo "	/lib/modules/$KERNEL_VERSION does exist."
  +	else
  +			echo "	/lib/modules/$KERNEL_VERSION does not exist."
  +			exit 1
  +fi
  +
   # Check for filesystem for initrd
   #
   if [ "$INITRDFS" == "" ]
   	then
  -		echo "	Autoselecting romfs File System for initrd."
  -		INITRDFS="romfs"
  +		FSYSCHECK="$(awk '/^[ \t]*[^#]/ { if ($2 == "/") { print $3; }}' /etc/fstab)"
  +		if [ "$FSYSCHECK" == "ext2" ]
  +			then
  +				INITRDFS="ext2"
  +		fi
  +		if [ "$FSYSCHECK" == "ext3" ] && [ "$INITRDFS" == "" ]
  +			then
  +				INITRDFS="ext3"
  +		fi
  +		if [ "$FSYSCHECK" == "auto" ] && [ "$INITRDFS" == "" ]
  +			then
  +				find_module "ext3.$EXTENSION"
  +				if [ "$SEARCH" != "ext3.$EXTENSION" ]
  +					then
  +						INITRDFS="ext3"
  +				fi
  +				if [ "$INITRDFS" == "" ]
  +					then
  +						find_module "ext2.$EXTENSION"
  +						if [ "$SEARCH" != "ext2.$EXTENSION" ]
  +							then
  +								INITRDFS="ext2"
  +						fi
  +				fi
  +		fi
  +		if [ "$INITRDFS" == "" ]
  +			then
  +				echo "Cannot autodetect file system for initrd."
  +				exit 1
  +		fi
  +		echo "	Autoselecting $INITRDFS File System for initrd."
   fi
   
   # Check for initrd Directory
  @@ -265,22 +312,11 @@
   		mkdir /initrd
   fi
   		
  -# Check for Pivot Root Method Fixesx
  +# Pivot Root Fixes
   #
  -if [ "$PIVOT_ROOT" == "1" ]
  -	then
  -		cd /sbin
  -		ln -sf init linuxrc
  -fi
  -
  -# Check for RAM Disk Device
  -#
  -if [ -e /dev/.devfsd ]
  -	then
  -		RAM_DEVICE="rd"
  -	else
  -		RAM_DEVICE="ram0"
  -fi
  +cd /sbin
  +ln -sf init linuxrc
  +cp /usr/sbin/chroot /sbin/chroot
   
   # Create Image and setup Loopback
   #
  @@ -316,11 +352,11 @@
   #
   if [ "$INITRDFS" == "ext2" ]
   	then
  -		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  -		if [ "$SEARCH" == "ext2" ]
  +		find_module "ext2.$EXTENSION"
  +		if [ "$SEARCH" == "ext2.$EXTENSION" ]
   			then
   				echo "	$INITRDFS is compiled as a module."
  -				exit
  +				exit 1
   			else
   				echo "	$INITRDFS is compiled into the kernel."
   				echo y | mkfs.ext2 $LOOP_DEVICE $IMAGE_SIZE > /dev/null 2> /dev/null
  @@ -329,42 +365,17 @@
   
   if [ "$INITRDFS" == "ext3" ]
   	then
  -		SEARCH="`find /lib/modules/$KERNEL_VERSION/kernel -name $INITRDFS.o`"
  -		if [ "$SEARCH" == "ext3" ]
  +		find_module "ext3.$EXTENSION"
  +		if [ "$SEARCH" == "ext3.$EXTENSION" ]
   			then
   				echo "	$INITRDFS is compiled as a module."
  -				exit
  +				exit 1
   			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
  @@ -384,28 +395,33 @@
   #
   echo "	Creating Directories..."
   mkdir -p $MOUNT_IMAGE
  -mkdir -p $MOUNT_IMAGE/{lib,bin,etc,dev}
  +mkdir -p $MOUNT_IMAGE/{bin,dev,etc,new_root,proc,lib}
   
   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
  -if [ "$PIVOT_ROOT" == "1" ]
  +
  +if [ "$KER_VER_REL" == "2.4" ]
   	then
  -		ln -s /bin/busybox $MOUNT_IMAGE/bin/mkdir
  -		ln -s /bin/busybox $MOUNT_IMAGE/bin/mount
  -		ln -s /bin/busybox $MOUNT_IMAGE/bin/pivot_root
  -		ln -s /bin/busybox $MOUNT_IMAGE/bin/umount
  +		cp -a /etc/modules.conf $MOUNT_IMAGE/etc/modules.conf
  +fi
  +if [ "$KER_VER_REL" == "2.6" ]
  +	then
  +		cp -a /etc/modprobe.conf $MOUNT_IMAGE/etc/modprobe.conf
   fi
   
  +cp -a /bin/busybox.static $MOUNT_IMAGE/bin/busybox
  +cp -a /bin/busybox.static /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/insmod
  +ln -s /bin/busybox $MOUNT_IMAGE/bin/mount
  +ln -s /bin/busybox $MOUNT_IMAGE/bin/pivot_root
  +ln -s /bin/busybox $MOUNT_IMAGE/bin/sh
  +ln -s /bin/busybox $MOUNT_IMAGE/bin/[
   rm -f /bin/busybox
   
   # Copying Modules
  @@ -413,17 +429,15 @@
   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`"
  +			find_module "$module.$EXTENSION"
   			REMOVE_MOD="0"
   			if ! [ "$SEARCH" == "" ]
   				then
  -					if ! [ -e $MOUNT_IMAGE/lib/$module.o ]
  +					if ! [ -e $MOUNT_IMAGE/lib/$module.$EXTENSION ]
   						then
   							for remove in $REMOVE_MODULE
   								do
  @@ -442,16 +456,35 @@
   	           }
   	done
   
  -for i in console null $RAM_DEVICE tty[1234]
  +for i in console null ram0 tty[1234]
   	do
   		cp -a /dev/$i $MOUNT_IMAGE/dev
   done
   
  +# Create non DEVFSD devices
  +#
  +if ! [ -e /dev/.devfsd ]
  +	then
  +		if [ -e /dev/sda ]
  +			then
  +				for drive in $SD_DEVICE
  +					do
  +							cp -a $drive $MOUNT_IMAGE/dev
  +					done
  +		fi
  +
  +		if [ -e /dev/hda ]
  +			then
  +				for drive in $HD_DEVICE
  +					do
  +							cp -a $drive $MOUNT_IMAGE/dev
  +					done
  +		fi
  +fi
  +
   # Creating linuxrc File
   #
   echo "#!/bin/sh" > $LINUXRC
  -echo "" >> $LINUXRC
  -
   echo "echo \"Initial RAMDISK Loading Starting...\"" >> $LINUXRC
   
   MODULES="`cat /tmp/copiedmodules`"
  @@ -459,39 +492,35 @@
   	do
   		echo "$MODULE" | {
   		IFS=':' read module
  -		module=$module
   
   		echo "		Module $module added to initrd."
  -		echo "insmod /lib/$module.o" >> $LINUXRC
  +		echo "insmod /lib/$module.$EXTENSION" >> $LINUXRC
   		}
   done
   
  -if [ "$PIVOT_ROOT" == "1" ]
  +echo "echo \"Mounting proc...\"" >> $LINUXRC
  +echo "mount -n -t proc none /proc" >> $LINUXRC
  +echo "echo 0x0100 > /proc/sys/kernel/real-root-dev" >> $LINUXRC
  +echo "echo \"Mounting real root dev...\"" >> $LINUXRC
  +echo "mount -n -o ro $ROOT_DEVICE /new_root" >> $LINUXRC
  +echo "echo \"Running pivot_root...\"" >> $LINUXRC
  +echo "pivot_root /new_root /new_root/initrd" >> $LINUXRC
  +if [ "$REMOVE_DEVFS" == "1" ]
   	then
  -		echo "mkdir /new_root" >> $LINUXRC
  -		echo "echo \"Mounting proc...\"" >> $LINUXRC
  -		echo "mount -n -t proc none /proc" >> $LINUXRC
  -		echo "echo 0x0100 > /proc/sys/kernel/real-root-dev" >> $LINUXRC
  -		echo "echo \"Mounting real root dev...\"" >> $LINUXRC
  -		echo "mount -n -o ro $ROOT_DEVICE /new_root" >> $LINUXRC
  -		echo "umount /proc" >> $LINUXRC
  -		echo "cd /new_root" >> $LINUXRC
  -		echo "echo \"Running pivot_root...\"" >> $LINUXRC
  -		echo "pivot_root . initrd" >> $LINUXRC
   		echo "if [ -c initrd/dev/.devfsd ]" >> $LINUXRC
  -		echo "	then" >> $LINUXRC
  -		echo "		echo \"Mounting devfs...\"" >> $LINUXRC
  -		echo "		mount -n -t devfs none dev" >> $LINUXRC
  -		echo "fi" >> $LINUXRC
  -		echo "if [ \$\$ = 1 ]" >> $LINUXRC
  -		echo "	then" >> $LINUXRC
  -		echo "		echo \"Running init...\"" >> $LINUXRC
  -		echo "		exec chroot . sbin/init dev/console 2>&1" >> $LINUXRC
  -		echo "	else" >> $LINUXRC
  -		echo "		echo \"Using bug circumvention for busybox...\"" >> $LINUXRC
  -		echo "		exec chroot . sbin/linuxrc dev/console 2>&1" >> $LINUXRC
  +		echo "  then" >> $LINUXRC
  +		echo "          echo \"Mounting devfs...\"" >> $LINUXRC
  +		echo "          mount -n -t devfs none dev" >> $LINUXRC
   		echo "fi" >> $LINUXRC
   fi
  +echo "if [ \$\$ = 1 ]" >> $LINUXRC
  +echo "  then" >> $LINUXRC
  +echo "          echo \"Running init...\"" >> $LINUXRC
  +echo "          exec chroot . sbin/init dev/console 2>&1" >> $LINUXRC
  +echo "  else" >> $LINUXRC
  +echo "          echo \"Using bug circumvention for busybox...\"" >> $LINUXRC
  +echo "          exec chroot . sbin/linuxrc dev/console 2>&1" >> $LINUXRC
  +echo "fi" >> $LINUXRC
   
   chmod +x $LINUXRC
   
  @@ -505,18 +534,6 @@
   	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
  
  
  



More information about the hints mailing list