10fdb137f4458dc11c4cd6950f904f83608f2c5d
[genkernel.git] / defaults / initrd.scripts
1 #!/bin/ash
2
3 . /etc/initrd.defaults
4
5 backup() {
6         echo -ne "\033[0G\033[0K"
7 }
8
9 parse_opt() {
10         case "$1" in
11                 *\=*)
12                         echo "$1" | cut -d= -f2-
13                 ;;
14         esac
15 }
16
17 modules_load() {
18         for module in $*
19         do
20                 echo ${module} >> /etc/modules/extra_load
21         done
22
23         modules_scan extra_load
24 }
25
26 modules_scan() {
27         local MODS
28         [ -d "/etc/modules/${1}" ] || touch /etc/modules/${1}
29
30         [ -f "/etc/modules/${1}" ] && MODS=`cat /etc/modules/${1}`
31         for x in ${MODS}
32         do
33                 MLOAD=`echo ${MLIST} | sed -e "s/.*${x}.*/${x}/"`
34                 if [ "${MLOAD}" = "${x}" ] # Only module to no-load
35                 then
36                         echo -e "${BOLD}   ::${NORMAL} Skipping ${x}..."
37                 elif [ "${MLOAD}" = "${MLIST}" ] # == No change == No specified no-load
38                 then
39                         [ -n "${DEBUG}" ] && echo -ne "${BOLD}   ::${NORMAL} Checking for ${x}..."
40                         # find -name does not work since the return status is always zero
41                         if find /lib/modules/${KV} | grep /"${x}${KSUFF}" >/dev/null 2>&1
42                         then
43                                 echo -ne "${BOLD}   ::${NORMAL} Scanning for ${x}..."
44                                 modprobe ${x} -n
45                                 backup
46                                 echo -ne "${NORMAL}"
47                         fi
48                 else
49                         echo -e "${BOLD}   ::${NORMAL} Skipping ${x}..."
50                 fi
51         done
52 }
53
54 uppercase(){
55         # needs tr on busybox
56         echo $1 | tr 'a-z' 'A-Z'
57 }
58
59
60 findmediamount() {
61         # $1 = mount dir name / media name
62         # $2 = recognition file
63         # $3 = variable to have the device path
64         # $4 = directory before /mnt, like NEW_ROOT
65         # args remaining are possible devices 
66
67         local media=$1 recon=$2 vrbl=$3
68         local mntdir="${4}/mnt/${media}"
69         shift 4
70
71         good_msg "Looking for the ${media}" ${CRYPT_SILENT}
72
73         if [ "$#" -gt "0" ]
74         then
75                 [ ! -d "${mntdir}" ] && mkdir -p ${mntdir} 2>/dev/null >/dev/null
76                 if [ -n "${ISOBOOT}" ]
77                 then
78                         mntcddir="${mntdir%${media}}iso"
79                         if [ ! -f ${mntcddir} ]
80                         then
81                                 mkdir ${mntcddir}
82                         fi
83                 else
84                         mntcddir=${mntdir}
85                 fi
86
87                 for x in $*
88                 do
89                         # Check for a block device to mount
90                         if [ -b "${x}" ]
91                         then
92                                 skip=0
93                                 bsn=`basename "${x}"`
94                                 #
95                                 # If disk and it has at least one partition, skip.
96                                 # We use /sys/block/${bsn}/${bsn}[0-9]* to make sure that we
97                                 # don't skip device mapper devices. Even the craziest scenario
98                                 # deserves a fair chance.
99                                 #
100                                 for part in `ls /sys/block/${bsn}/${bsn}[0-9]* 2>/dev/null`
101                                 do
102                                         skip=1
103                                         break;
104                                 done
105                                 if [ ${skip} -eq 1 ]
106                                 then
107                                         continue
108                                 fi
109                                 good_msg "Attempting to mount media:- ${x}" ${CRYPT_SILENT}
110
111 #                               if [ "${media}" = "cdrom" ]; then
112 #                                       mount -r -t iso9660 ${x} ${mntdir} &>/dev/null
113 #                               else
114 #                                       mount -r -t auto ${x} ${mntdir} &>/dev/null
115 #                               fi
116                                 mount -r -t ${CDROOT_TYPE} ${x} ${mntcddir} >/dev/null 2>&1
117                                 if [ "$?" = '0' ]
118                                 then
119                                         if [ -n "${ISOBOOT}" ]; then
120                                                 if [ -f ${mntcddir}/${ISOBOOT} ]; then
121                                                         mount -o loop ${mntcddir}/${ISOBOOT} ${mntdir}
122                                                         if [ "$?" = "0" ]; then
123                                                                 good_msg "iso mounted on ${mntdir}"
124                                                         fi
125                                                 fi
126                                         fi
127
128                                         # Check for the media
129                                         if [ -f "${mntdir}/${recon}" ]
130                                         then
131                                                 #set REAL_ROOT, CRYPT_ROOT_KEYDEV or whatever ${vrbl} is
132                                                 eval ${vrbl}'='"${x}"
133                                                 good_msg "Media found on ${x}" ${CRYPT_SILENT}
134                                                 break
135                                         else
136                                                 umount ${mntcddir}
137                                         fi
138                                 fi
139                         fi
140                 done
141         fi
142
143         eval local result='$'${vrbl}
144
145         [ -n "${result}" ] || bad_msg "Media not found" ${CRYPT_SILENT}
146 }
147
148 devicelist(){
149         # Locate the cdrom device with our media on it.
150         # CDROM DEVICES
151         local DEVICES="/dev/cdroms/* /dev/ide/cd/* /dev/sr*"
152         # USB Keychain/Storage
153         DEVICES="$DEVICES /dev/sd*"
154         # IDE devices
155         DEVICES="$DEVICES /dev/hd*"
156         # USB using the USB Block Driver
157         DEVICES="$DEVICES /dev/ubd* /dev/ubd/*"
158         # iSeries devices
159         DEVICES="$DEVICES /dev/iseries/vcd*"
160         echo ${DEVICES}
161 }
162
163 bootstrapCD() {
164         local DEVICES=`devicelist`
165         # The device was specified on the command line, so there's no need to scan
166         # a bunch of extra devices
167         [ -n "${CDROOT_DEV}" ] && DEVICES="${CDROOT_DEV}"
168
169         findmediamount "cdrom" "${SUBDIR}/livecd" "REAL_ROOT" "${NEW_ROOT}" ${DEVICES}
170 }
171
172 bootstrapKey() {
173         # $1 = ROOT/SWAP
174         local KEYDEVS=`devicelist`
175         eval local keyloc='"${CRYPT_'${1}'_KEY}"'
176
177         findmediamount "key" "${keyloc}" "CRYPT_${1}_KEYDEV" "" ${KEYDEVS}
178 }
179
180 cache_cd_contents() {
181         # Check loop file exists and cache to ramdisk if DO_cache is enabled
182         if [ "${LOOPTYPE}" != "noloop" ] && [ "${LOOPTYPE}" != "sgimips" ]
183         then
184                 check_loop
185                 if [ "${DO_cache}" ]
186                 then
187                         # TODO: Check the size of the image versus the size of our tmpfs
188                         # along with the amount of available RAM and increase tmpfs size
189                         # if necessary. (Not having awk sucks...)
190                         # z=0
191                         # for i in $(cat /proc/meminfo | grep -e ^MemFree -e ^Cached | \
192                         # cut -d: -f2 | cut -dk -f1 | sed -e "s/^\s*//") ; do
193                         # z=$(($z + $i)) ; done
194                         # echo $z
195                         good_msg "Copying loop file for caching..."
196                         # Verify that the needed directory exists
197                         mkdir -p "$(dirname ${NEW_ROOT}/mnt/${LOOP})"
198                         cp -a ${NEW_ROOT}/mnt/cdrom/${LOOP} ${NEW_ROOT}/mnt/${LOOP}
199                         if [ $? -ne 0 ]
200                         then
201                                 bad_msg "Failed to cache the loop file! Lack of space?"
202                                 rm -rf ${NEW_ROOT}/mnt/livecd.* 2>/dev/null
203                                 rm -rf ${NEW_ROOT}/mnt/image.* 2>/dev/null
204                                 rm -rf ${NEW_ROOT}/mnt/zisofs 2>/dev/null
205                         else
206                                 LOOPEXT='../'
207                         fi
208                 fi
209         fi
210 }
211
212 mount_sysfs() {
213         mount -t sysfs /sys /sys >/dev/null 2>&1
214         ret=$?
215         [ ${ret} -eq 0 ] || bad_msg "Failed to mount /sys!"
216 }
217
218 findnfsmount() {
219         if [ "${IP}" != '' ] || busybox udhcpc -n -T 15 -q
220         then
221                 [ -e /rootpath ] && NFSROOT=`cat /rootpath`
222
223                 if [ "${NFSROOT}" = '' ]
224                 then
225                         # Obtain NFSIP  
226                         OPTIONS=`busybox dmesg | grep rootserver | sed -e "s/,/ /g"`
227                         for OPTION in $OPTIONS
228                         do
229                                 if [ `echo $OPTION | sed -e "s/=/ /g" | cut -d " " -f 1` = 'rootserver' ]
230                                 then
231                                         NFSIP=`echo $OPTION | sed -e "s/=/ /g" | cut -d " " -f 2`
232                                 fi 
233                         done
234                         
235                         # Obtain NFSPATH
236                         OPTIONS=`busybox dmesg | grep rootpath | sed -e "s/,/ /g"`      
237                         for OPTION in $OPTIONS
238                         do
239                                 if [ `echo $OPTION | sed -e "s/=/ /g" | cut -d " " -f 1` = 'rootpath' ]
240                                 then
241                                         NFSPATH=`echo $OPTION | sed -e "s/=/ /g" | cut -d " " -f 2`
242                                 fi
243                         done
244
245                         # Setup NFSROOT
246                         if [ "${NFSIP}" != '' ] && [ "$NFSPATH" != '' ]
247                         then
248                                 NFSROOT="${NFSIP}:${NFSPATH}"
249                         else
250                                 bad_msg "The DHCP Server did not send a valid root-path."
251                                 bad_msg "Please check your DHCP setup, or provide a nfsroot=<...> parameter."
252                         fi
253                 fi
254
255                 if [ "${NFSROOT}" != '' ]
256                 then
257                         NFSOPTIONS=${NFSROOT#*,}
258                         NFSROOT=${NFSROOT%%,*}
259                         if [ "${NFSOPTIONS}" = "${NFSROOT}" ]
260                         then
261                                 NFSOPTIONS=$DEFAULT_NFSOPTIONS
262                         else
263                                 NFSOPTIONS="${DEFAULT_NFSOPTIONS},${NFSOPTIONS}"
264                         fi
265
266                         if [ "${CDROOT}" != '0' ]
267                         then
268                                 good_msg "Attempting to mount NFS CD image on ${NFSROOT} with options ${NFSOPTIONS}"
269                                 mount -t nfs -o ${NFSOPTIONS} ${NFSROOT} ${NEW_ROOT}/mnt/cdrom
270                                 if [ "$?" = '0' ]
271                                 then
272                                         REAL_ROOT="/dev/nfs"
273                                 else
274                                         bad_msg "NFS Mounting failed. Is the path corrent ?"
275                                 fi
276                         else    
277                                 good_msg "Attempting to mount NFS root on ${NFSROOT} with options ${NFSOPTIONS}"
278                                 mount -t nfs -o ${NFSOPTIONS} ${NFSROOT} ${NEW_ROOT}
279                                 if [ "$?" = '0' ]
280                                 then
281                                         REAL_ROOT="/dev/nfs"
282                                 else
283                                         bad_msg "NFS Mounting failed. Is the path correct ?"
284                                 fi
285                                 # FIXME: Need to start portmap and the other rpc daemons in
286                                 # order to remount rw.
287                         fi
288
289                 fi
290         fi
291 }
292
293 check_loop() {
294         if [ "${LOOP}" = '' -o ! -e "mnt/cdrom/${LOOP}" ]
295         then
296         
297                 bad_msg "Invalid loop location: ${LOOP}"
298                 bad_msg 'Please export LOOP with a valid location, or reboot and pass a proper loop=...'
299                 bad_msg 'kernel command line!'
300         
301                 run_shell
302         fi
303 }
304
305 run_shell() {
306         /bin/ash
307 }
308
309 runmdev() {
310         # Use devtmpfs if enabled in kernel,
311         # else tmpfs. Always run mdev just in case
312         devfs=tmpfs
313         if grep -qs devtmpfs /proc/filesystems ; then
314                 devfs=devtmpfs
315         fi
316
317         # Options copied from /etc/init.d/udev-mount, should probably be kept in sync
318         mount -t $devfs -o "exec,nosuid,mode=0755,size=10M" udev /dev \
319                 || bad_msg "Failed to mount /dev as ${devfs}"
320
321         # http://git.busybox.net/busybox/plain/docs/mdev.txt
322         mkdir /dev/pts
323         mount -t devpts devpts /dev/pts  || bad_msg "Failed to mount /dev/pts"
324         mdev -s  || bad_msg "Failed to receive dynamic updates from mdev"
325 }
326
327 test_success() {
328         retcode=$?
329         # If last command failed send error message and fall back to a shell    
330         if [ "$retcode" != '0' ]
331         then
332                 error_string=$1
333                 error_string="${error_string:-run command}"
334                 bad_msg 'Failed to $1; failing back to the shell...'
335                 run_shell
336         fi
337 }
338
339
340 # msg functions arguments
341 # $1 string
342 # $2 hide flag
343
344 good_msg() {    
345         msg_string=$1
346         msg_string="${msg_string:-...}"
347         [ "$2" != 1 ] && echo -e "${GOOD}>>${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
348 }
349
350 bad_msg() {
351         msg_string=$1
352         msg_string="${msg_string:-...}"
353         if [ "$2" != 1 ]
354         then
355                 splash 'verbose' > /dev/null &
356                 echo -e "${BAD}!!${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
357         fi
358
359
360 warn_msg() {
361         msg_string=$1
362         msg_string="${msg_string:-...}"
363         [ "$2" != 1 ] && echo -e "${WARN}**${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
364 }
365
366 crypt_filter() {
367         if [ "${CRYPT_SILENT}" = '1' ]
368         then
369                 eval $1 >/dev/null 2>/dev/null
370         else
371                 splash 'verbose' > /dev/null &
372                 eval $1
373                 if [ $? -eq 0 ]
374                 then
375                         splash set_msg 'Disk unlocked.'
376                 fi
377         fi
378 }
379
380 whereis(){
381         # $1 = variable whose value is the path (examples: "REAL_ROOT",
382         #      "LUKS_KEYDEV")
383         # $2 = label
384         # $3 = optional explanations for failure
385
386         eval local oldvalue='$'${1}
387
388         [ \( $# != 2 \) -a \( $# != 3 \) ] && \
389                 bad_msg "Bad invocation of function whereis, please file a bug \
390                 report with this message" && exit 1
391         [ -n "${3}" ] && local explnt=" or : ${3}" || local explnt="."
392         
393         bad_msg "Could not find the ${2} in ${oldvalue}${explnt}"
394         echo '   Please specify another value or: press Enter for the same, type "shell" for a shell, or "q" to skip...'
395         echo -n "${2}(${oldvalue}) :: "
396         read ${1}
397         case `eval echo '$'${1}` in
398                 'q')
399                         eval ${1}'='${oldvalue}
400                         warn_msg "Skipping step, this will likely cause a boot failure."
401                         break
402                         ;;
403                 'shell')
404                         eval ${1}'='${oldvalue}
405                         echo "To leave and try again just press <Ctrl>+D"
406                         run_shell
407                         ;;
408                 '')
409                         eval ${1}'='${oldvalue}
410                         ;;
411         esac
412 }
413
414 setup_hotplug() {
415         if [ "${KV_2_6_OR_GREATER}" ]
416         then
417                 echo /sbin/mdev > /proc/sys/kernel/hotplug
418         fi
419 }
420
421 setup_slowusb() {
422         # slowusb already set?
423         if [ "${DO_slowusb}" = "1" ]
424         then
425                 return
426         fi
427
428         local usb_storage_dir="/sys/bus/usb/drivers/usb-storage"
429         if [ ! -d "${usb_storage_dir}" ]
430         then
431                 # no automated slowusb required. no usb-storage devices attached.
432                 return
433         fi
434         for x in "${usb_storage_dir}"/*
435         do
436                 [ -d "${x}" ]  && [ "${x}" != "${usb_storage_dir}/module" ] \
437                         && { DO_slowusb="1" ; break ; }
438         done
439 }
440
441 start_dev_mgr() {
442         if [ "${KV_2_6_OR_GREATER}" ]
443         then
444                 cd /sys
445                 good_msg 'Activating mdev'
446                 runmdev
447                 cd /
448         fi
449 }
450
451 cmdline_hwopts() {
452         # Scan CMDLINE for any "doscsi" or "noscsi"-type arguments
453         local FOUND
454         local TMP_HWOPTS
455
456         for x in $HWOPTS
457         do
458                 for y in $CMDLINE
459                 do
460                         if [ "${y}" = "do${x}" ]
461                         then
462                                 MY_HWOPTS="${MY_HWOPTS} $x"
463                         elif [ "${y}" = "no${x}" ]
464                         then
465                                 MY_HWOPTS="`echo ${MY_HWOPTS} | sed -e \"s/${x}//g\" -`"
466                         fi
467                         if [ "$(echo ${y} | cut -b -7)" = "keymap=" ]
468                         then
469                                 MY_HWOPTS="${MY_HWOPTS} keymap"
470                         fi
471                 done
472         done
473    
474         # Shouldnt need to sort this as the following loop should figure out the
475         # duplicates and strip them out
476         #MY_HWOPTS=`echo ${MY_HWOPTS}|  sort`
477         
478         for x in ${MY_HWOPTS}
479         do
480                 FOUND=0
481                 for y in ${TMP_HWOPTS}
482                 do
483                         if [ "${y}" = "${x}" ]
484                         then 
485                                 continue 2
486                         fi
487                 done
488                 TMP_HWOPTS="${TMP_HWOPTS} ${x}"
489                 eval DO_`echo ${x} | sed 's/-//'`=1
490         done
491
492         MY_HWOPTS=${TMP_HWOPTS}
493 }
494
495 load_modules() {
496         # Load modules listed in MY_HWOPTS if /lib/modules exists for the running
497         # kernel version
498         if [ -d "/lib/modules/${KV}" ]
499         then
500                 good_msg 'Loading modules'
501                 # Load appropriate kernel modules
502                 for modules in $MY_HWOPTS
503                 do
504                         modules_scan $modules
505                 done
506         else
507                 good_msg 'Skipping module load; no modules in the ramdisk!'
508         fi
509 }
510
511 setup_keymap() {
512         if [ "${DO_keymap}" ]
513         then
514                 if [ ! -e /dev/vc/0 -a ! -e /dev/tty0 ]
515                 then
516                         DEVBIND=1
517                         mount -o bind ${NEW_ROOT}/dev /dev
518                 fi
519                 [ ! -e /dev/tty0 ] && ln -s /dev/tty1 /dev/tty0
520
521                 [ -f /lib/keymaps/keymapList ] && chooseKeymap
522
523                 [ "${DEVBIND}" = '1' ] && umount /dev
524                 
525                 if [ -e /etc/sysconfig/keyboard -a "${CDROOT}" = '1' ]
526                 then
527                         mkdir -p ${NEW_ROOT}/etc/sysconfig/
528                         cp /etc/sysconfig/keyboard ${NEW_ROOT}/etc/sysconfig/keyboard
529                 fi
530         fi
531 }
532
533 chooseKeymap() {
534         good_msg "Loading keymaps"
535         if [ -z "${keymap}" ]
536         then
537                 splash 'verbose' > /dev/null &
538                 cat /lib/keymaps/keymapList
539                 read -t 10 -p '<< Load keymap (Enter for default): ' keymap
540                 case ${keymap} in
541                         1|azerty) keymap=azerty ;;
542                         2|be) keymap=be ;;
543                         3|bg) keymap=bg ;;
544                         4|br-a) keymap=br-a ;;
545                         5|br-l) keymap=br-l ;;
546                         6|by) keymap=by ;;
547                         7|cf) keymap=cf ;;
548                         8|croat) keymap=croat ;;
549                         9|cz) keymap=cz ;;
550                         10|de) keymap=de ;;
551                         11|dk) keymap=dk ;;
552                         12|dvorak) keymap=dvorak ;;
553                         13|es) keymap=es ;;
554                         14|et) keymap=et ;;
555                         15|fi) keymap=fi ;;
556                         16|fr) keymap=fr ;;
557                         17|gr) keymap=gr ;;
558                         18|hu) keymap=hu ;;
559                         19|il) keymap=il ;;
560                         20|is) keymap=is ;;
561                         21|it) keymap=it ;;
562                         22|jp) keymap=jp ;;
563                         23|la) keymap=la ;;
564                         24|lt) keymap=lt ;;
565                         25|mk) keymap=mk ;;
566                         26|nl) keymap=nl ;;
567                         27|no) keymap=no ;;
568                         28|pl) keymap=pl ;;
569                         29|pt) keymap=pt ;;
570                         30|ro) keymap=ro ;;
571                         31|ru) keymap=ru ;;
572                         32|se) keymap=se ;;
573                         33|sg) keymap=sg ;;
574                         34|sk-y) keymap=sk-y ;;
575                         35|sk-z) keymap=sk-z ;;
576                         36|slovene) keymap=slovene ;;
577                         37|trf) keymap=trf ;;
578                         38|trq) keymap=trq ;;
579                         39|ua) keymap=ua ;;
580                         40|uk) keymap=uk ;;
581                         41|us) keymap=us ;;
582                         42|wangbe) keymap=wangbe ;;
583                 esac
584         fi
585         if [ -e /lib/keymaps/${keymap}.map ]
586         then
587                 good_msg "Loading the ''${keymap}'' keymap"
588                 loadkmap < /lib/keymaps/${keymap}.map
589 #               xkeymap=${keymap}
590 #               echo ${keymap} | egrep -e "[0-9]+" >/dev/null 2>&1
591 #               if [ $? -eq 0 ]
592 #               then
593 #                       xkeymap=`tail -n 7 /lib/keymaps/keymapList | grep ${keymap} | sed -r "s/.*\s+${keymap}\s+([a-z-]+).*/\1/g" | egrep -v 1`
594 #               fi
595                 mkdir -p /etc/sysconfig
596 #               echo "XKEYBOARD=${xkeymap}" > /etc/sysconfig/keyboard
597                 echo "XKEYBOARD=${keymap}" > /etc/sysconfig/keyboard
598                 splash set_msg "Set keymap to ${keymap}"
599         elif [ -z "${keymap}" ]
600         then
601                 echo
602                 good_msg "Keeping default keymap"
603                 splash set_msg "Keeping default keymap"
604         else
605                 bad_msg "Sorry, but keymap ''${keymap}'' is invalid!"
606                 unset keymap
607                 chooseKeymap
608         fi
609 }
610
611 startVolumes() {
612         #good_msg 'Checking if volumes need to be started...'
613
614         # Here, we check for /dev/device-mapper, and if it exists, we setup a
615         # a symlink, which should hopefully fix bug #142775 and bug #147015
616         if [ -e /dev/device-mapper ] && [ ! -e /dev/mapper/control ]
617         then
618                 mkdir -p /dev/mapper
619                 ln -sf /dev/device-mapper /dev/mapper/control
620         fi
621         
622         if [ "${USE_MDADM}" = '1' ]
623         then
624                 /sbin/mdadm --assemble --scan
625         fi
626
627         if [ "${USE_DMRAID_NORMAL}" = '1' ]
628         then
629                 if [ -e '/sbin/dmraid' ]
630                 then
631                         good_msg "Activating Device-Mapper RAID(s)"
632                         if [ '${DMRAID_OPTS}' = '' ]
633                         then
634                                 /sbin/dmraid -ay
635                         else
636                                 /sbin/dmraid -ay ${DMRAID_OPTS}
637                         fi
638                 fi
639         fi
640
641         if [ "${USE_LVM_NORMAL}" = '1' ]
642         then
643                 if [ -e '/bin/lvm' ]
644                 then
645                         for dev in ${RAID_DEVICES}
646                         do
647                                 setup_md_device "${dev}"
648                         done
649
650                         # This is needed for /bin/lvm to accept the following logic
651                         lvm_commands="#! /bin/lvm"
652
653                         # If there is a cahe, update it. Unbreak at least dmcrypt
654                         [ -d /etc/lvm/cache ] && lvm_commands="${lvm_commands} \nvgscan"
655
656                         # To activate volumegroups on all devices in the cache
657                         lvm_commands="${lvm_commands} \nvgchange -ay --sysinit"
658
659                         # And finally execute it all (/proc/... needed if lvm is compiled without readline)
660                         good_msg "Scanning for and activating Volume Groups"
661                         printf "%b\n" "${lvm_commands}" | /bin/lvm /proc/self/fd/0
662
663                         # Disable EVMS since lvm is activated and they dont work together.
664                         if [ "${USE_EVMS_NORMAL}" = '1' ]
665                         then
666                                 bad_msg "Disabling EVMS Support because LVM started"
667                                 bad_msg "Do not add dolvm to the cmdline if this is not what you want"
668                                 bad_msg "LVM and EVMS do not work well together"
669                                 USE_EVMS_NORMAL=0
670                         fi
671                 else
672                         bad_msg "vgscan or vgchange not found: skipping LVM volume group activation!"
673                 fi
674         fi
675
676         if [ "${USE_EVMS_NORMAL}" = '1' ]
677         then
678                 if [ -e '/sbin/evms_activate' ]
679                 then
680                         good_msg "Activating EVMS"
681                         evms_activate
682                 fi
683         fi
684 }
685
686 startiscsi() {
687         
688         if [ ! -n "${ISCSI_NOIBFT}" ]
689         then
690                 good_msg "Activating iSCSI via iBFT"
691                 iscsistart -b
692         fi
693
694         if [ -n "${ISCSI_INITIATORNAME}" ] && [ -n "${ISCSI_TARGET}" ] && [ -n "${ISCSI_ADDRESS}" ]
695         then
696                 good_msg "Activating iSCSI via cmdline"
697
698                 if [ "${ISCSI_TGPT}" ]
699                 then
700                         ADDITIONAL="${ADDITIONAL} -g ${ISCSI_TGPT}"
701                 else
702                         ADDITIONAL="${ADDITIONAL} -g 1"
703                 fi
704                 
705                 if [ "${ISCSI_PORT}" ]
706                 then
707                         ADDITIONAL="${ADDITIONAL} -p ${ISCSI_PORT}"
708                 fi
709
710                 if [ "${ISCSI_USERNAME}" ]
711                 then
712                         ADDITIONAL="${ADDITIONAL} -u ${ISCSI_USERNAME}"
713                 fi
714
715                 if [ "${ISCSI_PASSWORD}" ]
716                 then
717                         ADDITIONAL="${ADDITIONAL} -w ${ISCSI_PASSWORD}"
718                 fi
719
720                 if [ "${ISCSI_USERNAME_IN}" ]
721                 then
722                         ADDITIONAL="${ADDITIONAL} -U ${ISCSI_USERNAME_IN}"
723                 fi
724
725                 if [ "${ISCSI_PASSWORD_IN}" ]
726                 then
727                         ADDITIONAL="${ADDITIONAL} -W ${ISCSI_PASSWORD_IN}"
728                 fi
729
730                 if [ "${ISCSI_DEBUG}" ]
731                 then
732                         ADDITIONAL="${ADDITIONAL} -d ${ISCSI_DEBUG}"
733                 fi
734
735                 iscsistart -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -a "${ISCSI_ADDRESS}" ${ADDITIONAL}
736
737                 # let iscsid settle - otherwise mounting the iSCSI-disk will fail (very rarely, though)
738                 sleep 1
739         fi
740 }
741
742
743 # Open a LUKS device
744 # It is either the root or a swap, other devices are supported in the scripts provided with sys-fs/cryptsetup-luks
745 # $1 - root/swap
746 openLUKS() {
747         # please use 'tr' and this line, or remove it
748         # eval local TYPE=`uppercase $1`
749
750         case $1 in
751                 root)
752                         local TYPE=ROOT
753                         ;;
754                 swap)
755                         local TYPE=SWAP
756                         ;;
757         esac
758
759         eval local LUKS_DEVICE='"${CRYPT_'${TYPE}'}"' LUKS_NAME="$1" LUKS_KEY='"${CRYPT_'${TYPE}'_KEY}"' LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"'
760         local DEV_ERROR=0 KEY_ERROR=0 KEYDEV_ERROR=0
761         local mntkey="/mnt/key/" cryptsetup_options=''
762
763         [ ! -e /sbin/cryptsetup ] && bad_msg "The ramdisk does not support LUKS" && exit 1
764         while [ 1 ]
765         do
766                 local gpg_cmd=""
767                 # if crypt_silent=1 and some error occurs, enter shell quietly
768                 if [ \( ${CRYPT_SILENT} -eq 1 \) -a \( \( \( ${DEV_ERROR} -eq 1 \) -o \( ${KEY_ERROR} -eq 1 \) \) -o \( ${KEYDEV_ERROR} -eq 1 \) \) ]
769                 then
770                         run_shell
771                 elif [ ${DEV_ERROR} -eq 1 ]
772                 then
773                         whereis "LUKS_DEVICE" "${LUKS_NAME}"
774                         DEV_ERROR=0
775                 elif [ ${KEY_ERROR} -eq 1 ]
776                 then
777                         whereis "LUKS_KEY" "${LUKS_NAME} key"
778                         KEY_ERROR=0
779                 elif [ ${KEYDEV_ERROR} -eq 1 ]
780                 then
781                         whereis "LUKS_KEYDEV" "${LUKS_NAME} key device"
782                         KEYDEV_ERROR=0
783                 else
784                         case "${LUKS_DEVICE}" in
785                                 UUID\=*|LABEL\=*)
786                                         local REAL_LUKS=""
787                                         local retval=1
788
789                                         if [ "${retval}" -ne 0 ]; then
790                                                 REAL_LUKS=`findfs "${LUKS_DEVICE}" 2>/dev/null`
791                                                 retval=$?
792                                         fi
793
794                                         if [ "$retval" -ne 0 ]; then
795                                                 REAL_LUKS=`busybox findfs "${LUKS_DEVICE}" 2>/dev/null`
796                                                 retval=$?
797                                         fi
798
799                                         if [ "${retval}" -ne 0 ]; then
800                                                 REAL_LUKS=`blkid -l -t "${LUKS_DEVICE}" | cut -d ":" -f 1 2>/dev/null`
801                                                 retval=$?
802                                         fi
803
804                                         if [ "${retval}" -eq 0 ] && [ -n "${REAL_LUKS}" ]; then
805                                                 good_msg "Detected device ${REAL_LUKS}"
806                                                 LUKS_DEVICE="${REAL_LUKS}"
807                                         fi
808                                 ;;
809                         esac
810
811                         setup_md_device ${LUKS_DEVICE}
812                         cryptsetup isLuks ${LUKS_DEVICE}
813                         if [ $? -ne 0 ]
814                         then
815                                 bad_msg "The LUKS device ${LUKS_DEVICE} does not contain a LUKS header" ${CRYPT_SILENT}
816                                 DEV_ERROR=1
817                                 continue
818                         else
819                                 # Handle keys
820                                 if [ -n "${LUKS_KEY}" ] 
821                                 then
822                                         if [ ! -e "${mntkey}${LUKS_KEY}" ] 
823                                         then
824                                                 if [ -b "${LUKS_KEYDEV}" ]
825                                                 then good_msg "Using key device ${LUKS_KEYDEV}." ${CRYPT_SILENT}
826                                                 else
827                                                         good_msg "Please insert removable device ${LUKS_KEYDEV} for ${LUKS_NAME}" ${CRYPT_SILENT}
828                                                         # abort after 10 secs
829                                                         local count=10
830                                                         while [ ${count} -gt 0 ]
831                                                         do 
832                                                                 count=$((count-1))
833                                                                 sleep 1
834                                                                 if [ -b "${LUKS_KEYDEV}" ]
835                                                                 then
836                                                                         good_msg "Removable device ${LUKS_KEYDEV} detected." ${CRYPT_SILENT}
837                                                                         break
838                                                                 fi
839                                                         done
840                                                         if [ ! -b "${LUKS_KEYDEV}" ]
841                                                         then
842                                                                 eval CRYPT_${TYPE}_KEY=${LUKS_KEY}
843                                                                 bootstrapKey ${TYPE}
844                                                                 eval LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"'
845                                                                 if [ ! -b "${LUKS_KEYDEV}" ]; then
846                                                                         KEYDEV_ERROR=1
847                                                                         bad_msg "Removable device ${LUKS_KEYDEV} not found." ${CRYPT_SILENT}
848                                                                         continue
849                                                                 fi
850                                                                 # continue otherwise will mount keydev which is mounted by bootstrap
851                                                                 continue
852                                                         fi
853                                                 fi
854                                                 # At this point a device was recognized, now let's see if the key is there
855                                                 [ ! -d "$mntkey" ] && mkdir -p ${mntkey} 2>/dev/null >/dev/null
856
857                                                 mount -n -o ro ${LUKS_KEYDEV} ${mntkey} >/dev/null 2>/dev/null
858                                                 if [ "$?" != '0' ]
859                                                 then
860                                                         KEYDEV_ERROR=1
861                                                         bad_msg "Mounting of device ${LUKS_KEYDEV} failed." ${CRYPT_SILENT}
862                                                         continue
863                                                 else
864                                                         good_msg "Removable device ${LUKS_KEYDEV} mounted." ${CRYPT_SILENT}
865                                                         sleep 2
866                                                         # keyfile exists?
867                                                         if [ ! -e "${mntkey}${LUKS_KEY}" ]; then
868                                                                 umount -n ${mntkey} 2>/dev/null >/dev/null
869                                                                 KEY_ERROR=1
870                                                                 KEYDEV_ERROR=1
871                                                                 bad_msg "Key {LUKS_KEY} on device ${LUKS_KEYDEV} not found." ${CRYPT_SILENT}
872                                                                 continue
873                                                         fi
874                                                 fi
875                                         fi
876                                         # At this point a candidate key exists (either mounted before or not)
877                                         good_msg "${LUKS_KEY} on device ${LUKS_KEYDEV} found" ${CRYPT_SILENT}
878                                         if [ $(echo ${LUKS_KEY} | grep -o '.gpg$') = ".gpg" ] && [ -e /sbin/gpg ] ; then
879                                                 [ -e /dev/tty ] && mv /dev/tty /dev/tty.org
880                                                 mknod /dev/tty c 5 1
881                                                 cryptsetup_options="-d -"
882                                                 gpg_cmd="/sbin/gpg --logger-file /dev/null --quiet --decrypt ${mntkey}${LUKS_KEY} |"
883                                         else
884                                                 cryptsetup_options="-d ${mntkey}${LUKS_KEY}"
885                                         fi
886                                 fi
887                                 # At this point, keyfile or not, we're ready!
888                                 crypt_filter "${gpg_cmd}cryptsetup ${cryptsetup_options} luksOpen ${LUKS_DEVICE} ${LUKS_NAME}"
889                                 if [ $? -eq 0 ]
890                                 then
891                                         good_msg "LUKS device ${LUKS_DEVICE} opened" ${CRYPT_SILENT}
892                                         break
893                                 else
894                                         bad_msg "Failed to open LUKS device ${LUKS_DEVICE}" ${CRYPT_SILENT}
895                                         DEV_ERROR=1
896                                         KEY_ERROR=1
897                                         KEYDEV_ERROR=1
898                                 fi
899                         fi
900                 fi
901         done
902         umount ${mntkey} 2>/dev/null >/dev/null
903         rmdir -p ${mntkey} 2>/dev/null >/dev/null
904 }
905
906 startLUKS() {
907
908         # if key is set but key device isn't, find it
909         
910         [ -n "${CRYPT_ROOT_KEY}" ] && [ -z "${CRYPT_ROOT_KEYDEV}" ] \
911                 && sleep 6 && bootstrapKey "ROOT"
912
913         if [ -n "${CRYPT_ROOT}" ]; then
914                 openLUKS "root"
915                 if [ -n "${REAL_ROOT}" ]
916                 then
917                         # Rescan volumes
918                         startVolumes
919                 else
920                         REAL_ROOT="/dev/mapper/root"
921                 fi
922         fi
923
924         # same for swap, but no need to sleep if root was unencrypted
925         [ -n "${CRYPT_SWAP_KEY}" ] && [ -z "${CRYPT_SWAP_KEYDEV}" ] \
926                 && { [ -z "${CRYPT_ROOT}" ] && sleep 6; bootstrapKey "SWAP"; }
927
928         if [ -n "${CRYPT_SWAP}" ]; then
929                 openLUKS "swap"
930                 if [ -z "${REAL_RESUME}" ]
931                 then
932                         # Resume from swap as default
933                         REAL_RESUME="/dev/mapper/swap"
934                 fi
935         fi
936 }
937
938 sdelay() {
939         # Sleep a specific number of seconds if SDELAY is set
940         if [ "${SDELAY}" ]
941         then
942                 good_msg 'Waiting ${SDELAY} seconds...'
943                 sleep ${SDELAY}
944         else
945                 good_msg 'Hint: Use parameter scandelay[=seconds] if you need waiting here'
946         fi
947 }
948
949 quiet_kmsg() {
950         # if QUIET is set make the kernel less chatty
951         [ -n "$QUIET" ] && echo '0' > /proc/sys/kernel/printk
952 }
953
954 verbose_kmsg() {
955         # if QUIET is set make the kernel less chatty
956         [ -n "$QUIET" ] && echo '6' > /proc/sys/kernel/printk
957 }
958
959
960 cdupdate() {
961         if [ "${CDROOT}" = '1' ]
962         then
963                 if [ -x /${NEW_ROOT}/mnt/cdrom/cdupdate.sh ]
964                 then
965                         good_msg "Running cdupdate.sh"
966                         ${NEW_ROOT}/mnt/cdrom/cdupdate.sh
967                         if [ "$?" != '0' ]
968                         then
969                                 bad_msg "Executing cdupdate.sh failed!"
970                                 run_shell
971                         fi
972                 else
973                         good_msg 'No cdupdate.sh script found, skipping...'
974                 fi
975         fi
976 }
977
978 setup_btrfsctl() {
979         # start BTRFS volume detection, if available
980         [ -x /sbin/btrfsctl ] && /sbin/btrfsctl -a
981 }
982
983 setup_md_device() {
984         local device
985
986         [ -z "$1" ] && device="${REAL_ROOT}" || device="$1"
987         [ -z "${device}" ] && return # LiveCD
988
989         if [ `echo ${device}|sed -e 's#\(luks:\)\?\(/dev/md\)[[:digit:]]\+#\2#'` = "/dev/md" ]
990         then
991                 good_msg 'Detected real_root as a md device. Setting up the device node...'
992                 MD_NUMBER=`echo ${device}|sed -e 's#\(luks:\)\?/dev/md\([[:digit:]]\+\)#\2#'`
993                 if [ ! -e /dev/md${MD_NUMBER} ]
994                 then
995                         mknod /dev/md${MD_NUMBER} b 9 ${MD_NUMBER} >/dev/null 2>&1
996                         [ $? -ne 0 ] && bad_msg "Creation of /dev/md${MD_NUMBER} failed..."
997                 fi
998                 mdstart ${MDPART} /dev/md${MD_NUMBER}
999         fi
1000 }
1001
1002 rundebugshell() {
1003         if [ -n "$DEBUG" ]
1004         then
1005                 good_msg 'Starting debug shell as requested by "debug" option.'
1006                 good_msg 'Type "exit" to continue with normal bootup.'
1007                 [ -x /bin/sh ] && /bin/sh || /bin/ash
1008         fi
1009 }
1010
1011 do_resume() {
1012         if [ -d /proc/suspend2 -o -d /sys/power/suspend2 -o -d /sys/power/tuxonice ]; then
1013                 tuxonice_resume
1014         else
1015                 swsusp_resume
1016         fi
1017 }
1018
1019 swsusp_resume() {
1020         # determine swap resume partition
1021         local device=$(ls -lL "${REAL_RESUME}" | sed 's/\  */ /g' | cut -d \  -f 5-6 | sed 's/,\ */:/')
1022         [ -f /sys/power/resume ] && echo "${device}" > /sys/power/resume
1023 }
1024
1025 tuxonice_resume() {
1026         local splash_theme
1027         if grep "splash=" /proc/cmdline > /dev/null 2>&1; then
1028                 splash_theme=$(cat /proc/cmdline | sed 's/.*splash=/splash=/' | sed 's/ .*//' | sed 's/.*theme://' | sed 's/,.*//')
1029         fi
1030
1031         local tuxonice_userui_program="/sys/power/tuxonice/user_interface/program"
1032         local tuxonice_do_resume="/sys/power/tuxonice/do_resume"
1033         local tuxonice_resumedev="/sys/power/tuxonice/resume"
1034         local tuxonice_replace_swsusp="/sys/power/tuxonice/replace_swsusp"
1035
1036         #
1037         # Backward compatibility
1038         #
1039         if [ -e /sys/power/suspend2 ]; then
1040                 tuxonice_userui_program="/sys/power/suspend2/user_interface/program"
1041                 tuxonice_do_resume="/sys/power/suspend2/do_resume"
1042                 tuxonice_resumedev="/sys/power/suspend2/resume"
1043                 tuxonice_replace_swsusp="/sys/power/suspend2/replace_swsusp"
1044         elif [ -e /proc/suspend2 ]; then
1045                 tuxonice_userui_program="/proc/suspend2/userui_program"
1046                 tuxonice_do_resume="/proc/suspend2/do_resume"
1047                 tuxonice_resumedev="/proc/suspend2/resume"
1048                 tuxonice_replace_swsusp="/proc/suspend2/replace_swsusp"
1049         fi
1050
1051         # if 'use_swsusp' is given, use swsusp instead
1052         if grep "use_swsusp" /proc/cmdline > /dev/null 2>&1; then
1053                 echo 0 > ${tuxonice_replace_swsusp}
1054                 swsusp_resume
1055                 return
1056         fi
1057
1058         modules_scan tuxonice
1059
1060         # we both configure tuxonice and activate resuming,
1061         # however the kernel will resume only if an image is found
1062
1063         if ! grep suspend_noui /proc/cmdline > /dev/null 2>&1; then
1064                 which suspend2ui_text > /dev/null 2>&1 && which suspend2ui_text > "${tuxonice_userui_program}"
1065                 which tuxoniceui_text > /dev/null 2>&1 && which tuxoniceui_text > "${tuxonice_userui_program}"
1066
1067                 if [ -n "${splash_theme}" ]; then
1068                         ln -s /etc/splash/${splash_theme} /etc/splash/suspend2
1069                         ln -s /etc/splash/${splash_theme} /etc/splash/tuxonice
1070
1071                         which suspend2ui_fbsplash > /dev/null 2>&1 && which suspend2ui_fbsplash > "${tuxonice_userui_program}"
1072                         which tuxoniceui_fbsplash > /dev/null 2>&1 && which tuxoniceui_fbsplash > "${tuxonice_userui_program}"
1073                 fi
1074
1075         fi
1076         echo "${REAL_RESUME}" > "${tuxonice_resumedev}"
1077         echo > "${tuxonice_do_resume}"
1078 }
1079
1080 find_loop() {
1081         CDROM="${NEW_ROOT}/mnt/cdrom"
1082         for loop in ${LOOPS}
1083         do
1084                 if [ -e "${CDROM}""${loop}" ]
1085                 then
1086                         LOOP="${loop}"
1087                 fi
1088         done
1089 }
1090
1091 find_looptype() {
1092         LOOPTYPE="${LOOP##*.}"
1093         [ "${LOOPTYPE}" == "loop" ] && LOOPTYPE="normal"
1094         [ "${LOOP}" == "/zisofs" ] && LOOPTYPE="${LOOP#/}"
1095         [ -z "${LOOPTYPE}" ] && LOOPTYPE="noloop"
1096 }
1097
1098 getdvhoff() {
1099         echo $(( $(hexdump -n 4 -s $((316 + 12 * $2)) -e '"%i"' $1) * 512))
1100 }
1101
1102 setup_unionfs() {
1103         local rw_dir=$1
1104         local ro_dir=$2
1105         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
1106         then
1107                 # Directory used for rw changes in union mount filesystem
1108                 UNION=/union
1109 #               MEMORY=/memory
1110 #               if [ -z "$UID" ]
1111 #               then
1112 #                       CHANGES=$MEMORY/unionfs_changes/default
1113 #               else
1114 #                       CHANGES=$MEMORY/unionfs_changes/$UID
1115 #               fi
1116
1117 #               mkdir -p ${MEMORY}
1118                 mkdir -p ${UNION}
1119                 good_msg "Loading fuse module"
1120                 modprobe fuse > /dev/null 2>&1
1121 #                 if [ -n "${UNIONFS}" ]         
1122 #                 then   
1123 #                         CHANGESDEV=${UNIONFS}          
1124 #                         good_msg "mounting $CHANGESDEV to $MEMORY for unionfs support"         
1125 #                         mount -t auto $CHANGESDEV $MEMORY      
1126 #                         # mount tmpfs only in the case when changes= boot parameter was        
1127 #                         # empty or we were not able to mount the storage device        
1128 #                         ret=$?         
1129 #                         if [ ${ret} -ne 0 ]
1130 #                         then   
1131 #                                 bad_msg "mount of $CHANGESDEV failed falling back to ramdisk based unionfs"    
1132 #                                 mount -t tmpfs tmpfs $MEMORY   
1133 #                         fi     
1134 #                         if [ "${CDROOT}" -eq '1' -a ! -f ${MEMORY}/livecd.unionfs  ]   
1135 #                         then   
1136 #                                 umount $MEMORY         
1137 #                                 bad_msg "failed to find livecd.unionfs file on $CHANGESDEV"    
1138 #                                 bad_msg "create a livecd.unionfs file on this device if you wish to use it for unionfs"        
1139 #                                 bad_msg "falling back to ramdisk based unionfs for safety"     
1140 #                                 mount -t tmpfs tmpfs $MEMORY   
1141 #                         fi     
1142 #                 else   
1143 #                         good_msg "Mounting ramdisk to $MEMORY for unionfs support..."          
1144 #                         mount -t tmpfs tmpfs $MEMORY   
1145 #                 fi     
1146          
1147                 mkdir /tmp
1148                 mkdir -p ${UNION}
1149 #               mkdir -p $CHANGES
1150 #               mount -t unionfs -o dirs=$CHANGES=rw unionfs ${UNION}
1151                 good_msg "Creating union mount"
1152                 unionfs -o allow_other,cow,noinitgroups,suid,dev,default_permissions,use_ino ${rw_dir}=RW:${ro_dir}=RO ${UNION} 2>/dev/null
1153                 ret=$?
1154                 if [ ${ret} -ne 0 ]
1155                 then
1156                         bad_msg "Can't setup union mount!"
1157                         USE_UNIONFS_NORMAL=0
1158                 fi
1159         else
1160                 USE_UNIONFS_NORMAL=0
1161         fi
1162 }