summaryrefslogtreecommitdiff
path: root/initramfs/init
blob: 036b92fbb4de728e11eba3c38affc173d1b30e81 (plain)
    1 #!/bin/sh
    2 
    3 #
    4 # initramfs /init (busybox ash)
    5 #
    6 
    7 # color codes and some predefined texts
    8 BOLD="\033[1m"
    9 NORM="\033[0m"
   10 RED="\033[31m"
   11 GREEN="\033[32m"
   12 YELLOW="\033[33m"
   13 BLUE="\033[34m"
   14 OK="${BOLD}${GREEN}OK${NORM}"
   15 FAILED="${BOLD}${RED}FAILED${NORM}"
   16 DONE="${BOLD}${GREEN}DONE${NORM}"
   17 
   18 # helper functions
   19 
   20 # check an exit value and print a colored status
   21 checkReturn() {
   22 	if [ $? -ne 0 ]
   23 	then
   24 		echo -e $FAILED
   25 	else
   26 		echo -e $OK
   27 	fi
   28 }
   29 
   30 # print a colored done status
   31 echoDone() {
   32 	echo -e $DONE
   33 }
   34 
   35 # search for and mount the crux media, populate a tmpfs from it,
   36 # and prepare /newroot for switch_root at the end of the script
   37 find_and_mount_media() {
   38 	grep -q "prefix=*" /proc/cmdline
   39 	if [ $? -eq 0 ]
   40 	then
   41 		for opt in `cat /proc/cmdline`
   42 		do
   43 			echo $opt | grep -q "prefix="
   44 			if [ $? -eq 0 ]
   45 			then
   46 				PREFIX=`echo $opt | cut -d= -f2`
   47 			fi
   48 		done
   49 	fi
   50 
   51 	MEDIAMNT=/media
   52 	mkdir -p ${MEDIAMNT}
   53 
   54 	echo -e " ${BOLD}${BLUE}*${NORM} Searching for the CRUX media..."
   55 	CRUXMEDIA=""
   56 	CDROM_DEVICES="`awk '/drive name:/ { for (i=3;i<=NF;i++) { printf("%s ", $i) } }' /proc/sys/dev/cdrom/info`"
   57 	BLOCK_DEVICES="`grep -E '[shv]d' /proc/partitions | awk '{ print $4 }'`"
   58 	for DEV in $CDROM_DEVICES $BLOCK_DEVICES
   59 	do
   60 		DEV="/dev/$DEV"
   61 		mount -r $DEV ${MEDIAMNT} 2> /dev/null
   62 		if [ $? -eq 0 ]
   63 			then
   64 			echo -e -n "   ${BOLD}${GREEN}*${NORM} Found media on $DEV"
   65 			if [ -e ${MEDIAMNT}/${PREFIX}/crux-media ]
   66 			then
   67 				echo ", CRUX media."
   68 				CRUXMEDIA=$DEV
   69 				break
   70 			else
   71 				echo ", but it's not the CRUX media."
   72 				umount ${MEDIAMNT}
   73 			fi
   74 		else
   75 			echo -e "   ${BOLD}${YELLOW}*${NORM} No media found on $DEV."
   76 		fi
   77 	done
   78 
   79 	# check if the media was mounted properly. if not, spawn a shell
   80 	if [ ! -e ${MEDIAMNT}/${PREFIX}/crux-media ]
   81 	then
   82 		echo -e " ${BOLD}${RED}*${NORM} The CRUX media was not properly mounted!"
   83 		echo " Spawning a shell for you to attempt to fix this problem. If"
   84 		echo " you are able to find the correct device, mount it at"
   85 		echo " ${MEDIAMNT} and then log out of this shell to continue."
   86 		echo " If you are NOT able to fix the problem, installation will"
   87 		echo " not be possible."
   88 		echo
   89 		/bin/sh
   90 	fi
   91 
   92 	# check again and stop if it's still not there
   93 	if [ ! -e ${MEDIAMNT}/${PREFIX}/crux-media ]
   94 	then
   95 		echo -e " ${BOLD}${RED}*${NORM} The CRUX media still appears not to be"
   96 		echo " found and installation will not continue."
   97 		echo
   98 		sleep 99999
   99 		exit 1
  100 	fi
  101 
  102 	echo -e " ${BOLD}${BLUE}*${NORM} Populating root filesystem..."
  103 	mkdir -p /newroot
  104 	echo -e -n "   ${BOLD}${BLUE}*${NORM} Mounting new root filesystem... "
  105 	mount -t tmpfs -o size=320m tmpfs /newroot
  106 	checkReturn
  107 	echo -e -n "   ${BOLD}${BLUE}*${NORM} Copying files from rootfs.tar.xz... "
  108 	tar -xJf ${MEDIAMNT}/${PREFIX}/rootfs.tar.xz -C /newroot
  109 	checkReturn
  110 	echo -e -n "   ${BOLD}${BLUE}*${NORM} Copying devices from rootfs... "
  111 	cp -af /dev/* /newroot/dev/
  112 	checkReturn
  113 	mkdir -p /newroot/dev/pts /newroot/media
  114 
  115 	# the media gets unmounted here and remounted after switch_root
  116 	umount ${MEDIAMNT}
  117 	rmdir ${MEDIAMNT}
  118 	sed -i -e "s,#CRUXMEDIA#,${CRUXMEDIA}," /newroot/etc/fstab
  119 }
  120 
  121 /bin/busybox --install -s /bin
  122 
  123 # main script
  124 echo ""
  125 echo -e "${BOLD}CRUX 3.3 - ${BLUE}http://crux.nu/${NORM}"
  126 echo ""
  127 
  128 exec >/dev/console </dev/console 2>&1
  129 
  130 echo -e -n " ${BOLD}${BLUE}*${NORM} Mounting "
  131 echo -e -n "${BOLD}${GREEN}/proc${NORM}"
  132 mount -t proc proc /proc
  133 
  134 PRINTK="`cat /proc/sys/kernel/printk`"
  135 echo "0" > /proc/sys/kernel/printk
  136 
  137 echo -e ", ${BOLD}${GREEN}/sys${NORM}."
  138 mount -t sysfs sysfs /sys
  139 
  140 grep -q "debug" /proc/cmdline
  141 if [ $? -eq 0 ]
  142 then
  143 	DEBUG=1
  144 fi
  145 
  146 grep -q "dshell" /proc/cmdline
  147 if [ $? -eq 0 ]
  148 then
  149     DSHELL=1
  150 fi
  151 
  152 echo -e -n " ${BOLD}${BLUE}*${NORM} Populating /dev via mdev... "
  153 mdev -s
  154 checkReturn
  155 echo -e -n "   ${BOLD}${BLUE}*${NORM} Registering mdev as hotplug agent... "
  156 echo "/bin/mdev" > /proc/sys/kernel/hotplug
  157 checkReturn
  158 
  159 if [ -d /lib/modules ]
  160 then
  161 	echo -e -n " ${BOLD}${BLUE}*${NORM} Loading modules... "
  162 
  163 	# filesystem
  164 	load_fs_modules() {
  165 		# no notification for fs modules since they're generic
  166 		#echo -e -n "${BOLD}${BLUE}File Systems${NORM}"
  167 		for mod in crc32c libcrc32c isofs ext2 ext4 reiserfs xfs jfs \
  168 			nls_cp437 nls_iso8859-1 vfat msdos btrfs
  169 		do
  170 			if [ ! -z "$DEBUG" ]
  171 			then
  172 				echo -n " $mod"
  173 			fi
  174 			modprobe $mod
  175 		done
  176 		#echo -n ", "
  177 	}
  178 
  179 	# cdrom
  180 	load_cdrom_modules() {
  181 		# no notification for cdrom modules since they're generic
  182 		#echo -e -n "${BOLD}${BLUE}CDROM${NORM}"
  183 		# there's only one cdrom module for now but the loop allows for adding
  184 		# more if ever needed
  185 		for mod in cdrom
  186 		do
  187 			if [ ! -z "$DEBUG" ]
  188 			then
  189 				echo -n " $mod"
  190 			fi
  191 			modprobe $mod
  192 		done
  193 		#echo -n ", "
  194 	}
  195 
  196 	# SCSI
  197 	load_scsi_modules() {
  198 		echo -e -n "${BOLD}${BLUE}SCSI${NORM}"
  199 		for mod in scsi_mod sr_mod sd_mod scsi_transport_fc scsi_transport_iscsi \
  200 			scsi_transport_sas scsi_transport_spi 3w-9xxx 3w-xxxx 53c700 BusLogic \
  201 			NCR53c406a a100u2w aacraid advansys aha152x aha1542 aha1740 aic79xx \
  202 			aic7xxx aic94xx arcmsr atp870u dc395x dmx3191d dpt_i2o dtc eata fdomain \
  203 			gdth hptiop in2000 initio ipr ips lpfc megaraid megaraid_mbox megaraid_sas \
  204 			nsp32 pas16 psi240i qla1280 qla2xxx qla4xxx qlogicfas qlogicfas408 seagate \
  205 			sim710 stex sym53c416 sym53c8xx t128 u14-34f ultrastor \
  206 			wd7000 mptsas mptspi hpsa hv_storvsc virtio_scsi vmw_pvscsi
  207 		do
  208 			if [ ! -z "$DEBUG" ]
  209 			then
  210 				echo -n " $mod"
  211 			fi
  212 			modprobe $mod > /dev/null 2>&1
  213 		done
  214 		echo -n ", "
  215 	}
  216 
  217 	# USB
  218 	load_usb_modules() {
  219 		echo -e -n "${BOLD}${BLUE}USB${NORM}"
  220 		for mod in usb-storage hid-generic hid-apple hid-logitech-dj
  221 		do
  222 			if [ ! -z "$DEBUG" ]
  223 			then
  224 				echo -n " $mod"
  225 			fi
  226 			modprobe $mod
  227 		done
  228 		echo -n ", "
  229 	}
  230 
  231 	# ATA
  232 	load_ata_modules() {
  233 		echo -e -n "${BOLD}${BLUE}ATA${NORM}"
  234 		for mod in libata libahci ahci sata_promise sata_sil sata_svw sata_via \
  235 			sata_mv sata_nv sata_sil24 sata_sx4 sata_sis sata_uli \
  236 			sata_vsc sata_qstor sata_inic162x ata_piix pata_mpiix \
  237 			pata_pdc2027x pata_radisys pata_sil680 pata_rz1000 pata_cmd640 \
  238 			pata_efar pata_cmd64x pata_hpt366 pata_hpt37x pata_hpt3x3 \
  239 			pata_opti pata_optidma pata_it8213 pata_ninja32 pata_it821x \
  240 			pata_artop pata_cypress pata_serverworks pata_ns87410 \
  241 			pata_ns87415 pata_ali pata_amd pdc_adma ata_generic \
  242 			pata_atiixp pata_triflex pata_netcell pata_sch pata_sis \
  243 			pata_hpt3x2n pata_marvell pata_jmicron pata_via pata_sl82c105
  244 		do
  245 			if [ ! -z "$DEBUG" ]
  246 			then
  247 				echo -n " $mod"
  248 			fi
  249 			modprobe $mod
  250 		done
  251 		echo -n ", "
  252 	}
  253 
  254 	# FireWire
  255 	load_firewire_modules() {
  256 		echo -e -n "${BOLD}${BLUE}FireWire${NORM}"
  257 		for mod in core ohci sbp2
  258 		do
  259 			if [ ! -z "$DEBUG" ]
  260 			then
  261 				echo -n " $mod"
  262 			fi
  263 			modprobe firewire-$mod
  264 		done
  265 		echo -n ", "
  266 	}
  267 
  268 	MODORDER="fs cdrom ata scsi usb firewire"
  269 
  270 	for MODS in $MODORDER
  271 	do
  272 		grep -q "no$MODS" /proc/cmdline
  273 		[ $? -ne 0 ] && load_${MODS}_modules
  274 	done
  275 
  276 	echoDone
  277 else
  278 	echo -e " ${BOLD}${YELLOW}*${NORM} No modules were found in the initial RAM filesystem."
  279 fi
  280 
  281 grep -q "devicetimeout=*" /proc/cmdline
  282 if [ $? -eq 0 ]
  283 then
  284 	for opt in `cat /proc/cmdline`
  285 	do
  286 		echo $opt | grep -q "devicetimeout"
  287 		if [ $? -eq 0 ]
  288 		then
  289 			DEVTIMEOUT=`echo $opt | cut -d= -f2`
  290 		fi
  291 	done
  292 else
  293 	DEVTIMEOUT=10
  294 fi
  295 echo -e " ${BOLD}${BLUE}*${NORM} Waiting $DEVTIMEOUT seconds for devices to settle..."
  296 sleep $DEVTIMEOUT
  297 
  298 # if root=/dev/XXX was specified on the command line, use that as the new root
  299 # instead of searching for the media and using it. if it fails, fall back to
  300 # the media
  301 grep -q "root=/dev/*" /proc/cmdline
  302 if [ $? -eq 0 ]
  303 then
  304 	#echo "root= was specified"
  305 	for opt in `cat /proc/cmdline`
  306 	do
  307 		echo "$opt" | grep -q root=
  308 		if [ $? -eq 0 ]
  309 		then
  310 			ROOTDEV=`echo $opt | cut -d= -f2`
  311 		fi
  312 	done
  313 	mkdir -p /newroot
  314 	# check the specified root device to see if it has an init
  315 	mount $ROOTDEV /newroot
  316 	if [ $? -ne 0 ]
  317 	then
  318 		echo -e " ${BOLD}${RED}*${NORM} Unable to mount the specified root device! Falling back to the live media."
  319 		find_and_mount_media
  320 	else
  321 		if [ -x /newroot/sbin/init ]
  322 		then
  323 			echo -e " ${BOLD}${BLUE}*${NORM} Mounted root device $ROOTDEV."
  324 		else
  325 			echo -e " ${BOLD}${RED}*${NORM} The specified root device ($ROOTDEV) does not appear to be usable! Falling back to the live media."
  326 			umount /newroot
  327 			find_and_mount_media
  328 		fi
  329 	fi
  330 else
  331 	find_and_mount_media
  332 fi
  333 
  334 # copy busybox for debugging
  335 if [ ! -z "${DEBUG}" ]
  336 then
  337 	cp /bin/busybox /newroot/bin/
  338 fi
  339 
  340 if [ ! -z "$DSHELL" ]
  341 then
  342     echo "Starting debug shell."
  343     /bin/ash
  344 fi
  345 
  346 echo -e " ${BOLD}${BLUE}*${NORM} Switching root.\n"
  347 echo "$PRINTK" > /proc/sys/kernel/printk
  348 echo > /proc/sys/kernel/hotplug
  349 umount /sys
  350 umount /proc
  351 exec /bin/switch_root /newroot /sbin/init
  352 
  353 echo "Something's broken, here's a shell."
  354 exec /bin/sh

Generated by cgit