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

Generated by cgit