slowusb: ease the code in order to reduce entropy, one place for one thing.
[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         # busybox udev replacement
311         mdev -s
312 }
313
314 test_success() {
315         retcode=$?
316         # If last command failed send error message and fall back to a shell    
317         if [ "$retcode" != '0' ]
318         then
319                 error_string=$1
320                 error_string="${error_string:-run command}"
321                 bad_msg 'Failed to $1; failing back to the shell...'
322                 run_shell
323         fi
324 }
325
326
327 # msg functions arguments
328 # $1 string
329 # $2 hide flag
330
331 good_msg() {    
332         msg_string=$1
333         msg_string="${msg_string:-...}"
334         [ "$2" != 1 ] && echo -e "${GOOD}>>${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
335 }
336
337 bad_msg() {
338         msg_string=$1
339         msg_string="${msg_string:-...}"
340         if [ "$2" != 1 ]
341         then
342                 splash 'verbose' > /dev/null &
343                 echo -e "${BAD}!!${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
344         fi
345
346
347 warn_msg() {
348         msg_string=$1
349         msg_string="${msg_string:-...}"
350         [ "$2" != 1 ] && echo -e "${WARN}**${NORMAL}${BOLD} ${msg_string} ${NORMAL}"
351 }
352
353 crypt_filter() {
354         if [ "${CRYPT_SILENT}" = '1' ]
355         then
356                 eval $1 >/dev/null 2>/dev/null
357         else
358                 splash 'verbose' > /dev/null &
359                 eval $1
360                 if [ $? -eq 0 ]
361                 then
362                         splash set_msg 'Disk unlocked.'
363                 fi
364         fi
365 }
366
367 whereis(){
368         # $1 = variable whose value is the path (examples: "REAL_ROOT",
369         #      "LUKS_KEYDEV")
370         # $2 = label
371         # $3 = optional explanations for failure
372
373         eval local oldvalue='$'${1}
374
375         [ \( $# != 2 \) -a \( $# != 3 \) ] && \
376                 bad_msg "Bad invocation of function whereis, please file a bug \
377                 report with this message" && exit 1
378         [ -n "${3}" ] && local explnt=" or : ${3}" || local explnt="."
379         
380         bad_msg "Could not find the ${2} in ${oldvalue}${explnt}"
381         echo '   Please specify another value or: press Enter for the same, type "shell" for a shell, or "q" to skip...'
382         echo -n "${2}(${oldvalue}) :: "
383         read ${1}
384         case `eval echo '$'${1}` in
385                 'q')
386                         eval ${1}'='${oldvalue}
387                         warn_msg "Skipping step, this will likely cause a boot failure."
388                         break
389                         ;;
390                 'shell')
391                         eval ${1}'='${oldvalue}
392                         echo "To leave and try again just press <Ctrl>+D"
393                         run_shell
394                         ;;
395                 '')
396                         eval ${1}'='${oldvalue}
397                         ;;
398         esac
399 }
400
401 bind_mount_dev() {
402         # bind-mount /dev/ so that loop devices can be found
403         mount -o bind ${NEW_ROOT}/dev /dev
404 }
405
406 setup_hotplug() {
407         if [ "${KV_2_6_OR_GREATER}" ]
408         then
409                 echo /sbin/mdev > /proc/sys/kernel/hotplug
410         fi
411 }
412
413 setup_slowusb() {
414         # slowusb already set?
415         if [ "${DO_slowusb}" = "1" ]
416         then
417                 return
418         fi
419
420         local usb_storage_dir="/sys/bus/usb/drivers/usb-storage"
421         if [ ! -d "${usb_storage_dir}" ]
422         then
423                 # no automated slowusb required. no usb-storage devices attached.
424                 return
425         fi
426         for x in "${usb_storage_dir}"/*
427         do
428                 [ -d "${x}" ]  && [ "${x}" != "${usb_storage_dir}/module" ] \
429                         && { DO_slowusb="1" ; break ; }
430         done
431 }
432
433 start_dev_mgr() {
434         if [ "${KV_2_6_OR_GREATER}" ]
435         then
436                 cd /sys
437                 good_msg 'Activating mdev'
438                 runmdev
439                 cd /
440         fi
441 }
442
443 cmdline_hwopts() {
444         # Scan CMDLINE for any "doscsi" or "noscsi"-type arguments
445         local FOUND
446         local TMP_HWOPTS
447
448         for x in $HWOPTS
449         do
450                 for y in $CMDLINE
451                 do
452                         if [ "${y}" = "do${x}" ]
453                         then
454                                 MY_HWOPTS="${MY_HWOPTS} $x"
455                         elif [ "${y}" = "no${x}" ]
456                         then
457                                 MY_HWOPTS="`echo ${MY_HWOPTS} | sed -e \"s/${x}//g\" -`"
458                         fi
459                         if [ "$(echo ${y} | cut -b -7)" = "keymap=" ]
460                         then
461                                 MY_HWOPTS="${MY_HWOPTS} dokeymap"
462                         fi
463                 done
464         done
465    
466         # Shouldnt need to sort this as the following loop should figure out the
467         # duplicates and strip them out
468         #MY_HWOPTS=`echo ${MY_HWOPTS}|  sort`
469         
470         for x in ${MY_HWOPTS}
471         do
472                 FOUND=0
473                 for y in ${TMP_HWOPTS}
474                 do
475                         if [ "${y}" = "${x}" ]
476                         then 
477                                 continue 2
478                         fi
479                 done
480                 TMP_HWOPTS="${TMP_HWOPTS} ${x}"
481                 eval DO_`echo ${x} | sed 's/-//'`=1
482         done
483
484         MY_HWOPTS=${TMP_HWOPTS}
485 }
486
487 load_modules() {
488         # Load modules listed in MY_HWOPTS if /lib/modules exists for the running
489         # kernel version
490         if [ -d "/lib/modules/${KV}" ]
491         then
492                 good_msg 'Loading modules'
493                 # Load appropriate kernel modules
494                 for modules in $MY_HWOPTS
495                 do
496                         modules_scan $modules
497                 done
498         else
499                 good_msg 'Skipping module load; no modules in the ramdisk!'
500         fi
501 }
502
503 setup_keymap() {
504         if [ "${DO_keymap}" ]
505         then
506                 if [ ! -e /dev/vc/0 -a ! -e /dev/tty0 ]
507                 then
508                         DEVBIND=1
509                         mount -o bind ${NEW_ROOT}/dev /dev
510                 fi
511                 [ ! -e /dev/tty0 ] && ln -s /dev/tty1 /dev/tty0
512
513                 [ -f /lib/keymaps/keymapList ] && chooseKeymap
514
515                 [ "${DEVBIND}" = '1' ] && umount /dev
516                 
517                 if [ -e /etc/sysconfig/keyboard -a "${CDROOT}" = '1' ]
518                 then
519                         mkdir -p ${NEW_ROOT}/etc/sysconfig/
520                         cp /etc/sysconfig/keyboard ${NEW_ROOT}/etc/sysconfig/keyboard
521                 fi
522         fi
523 }
524
525 chooseKeymap() {
526         good_msg "Loading keymaps"
527         if [ -z "${keymap}" ]
528         then
529                 splash 'verbose' > /dev/null &
530                 cat /lib/keymaps/keymapList
531                 read -t 10 -p '<< Load keymap (Enter for default): ' keymap
532                 case ${keymap} in
533                         1|azerty) keymap=azerty ;;
534                         2|be) keymap=be ;;
535                         3|bg) keymap=bg ;;
536                         4|br-a) keymap=br-a ;;
537                         5|br-l) keymap=br-l ;;
538                         6|by) keymap=by ;;
539                         7|cf) keymap=cf ;;
540                         8|croat) keymap=croat ;;
541                         9|cz) keymap=cz ;;
542                         10|de) keymap=de ;;
543                         11|dk) keymap=dk ;;
544                         12|dvorak) keymap=dvorak ;;
545                         13|es) keymap=es ;;
546                         14|et) keymap=et ;;
547                         15|fi) keymap=fi ;;
548                         16|fr) keymap=fr ;;
549                         17|gr) keymap=gr ;;
550                         18|hu) keymap=hu ;;
551                         19|il) keymap=il ;;
552                         20|is) keymap=is ;;
553                         21|it) keymap=it ;;
554                         22|jp) keymap=jp ;;
555                         23|la) keymap=la ;;
556                         24|lt) keymap=lt ;;
557                         25|mk) keymap=mk ;;
558                         26|nl) keymap=nl ;;
559                         27|no) keymap=no ;;
560                         28|pl) keymap=pl ;;
561                         29|pt) keymap=pt ;;
562                         30|ro) keymap=ro ;;
563                         31|ru) keymap=ru ;;
564                         32|se) keymap=se ;;
565                         33|sg) keymap=sg ;;
566                         34|sk-y) keymap=sk-y ;;
567                         35|sk-z) keymap=sk-z ;;
568                         36|slovene) keymap=slovene ;;
569                         37|trf) keymap=trf ;;
570                         38|trq) keymap=trq ;;
571                         39|ua) keymap=ua ;;
572                         40|uk) keymap=uk ;;
573                         41|us) keymap=us ;;
574                         42|wangbe) keymap=wangbe ;;
575                 esac
576         fi
577         if [ -e /lib/keymaps/${keymap}.map ]
578         then
579                 good_msg "Loading the ''${keymap}'' keymap"
580                 loadkmap < /lib/keymaps/${keymap}.map
581 #               xkeymap=${keymap}
582 #               echo ${keymap} | egrep -e "[0-9]+" >/dev/null 2>&1
583 #               if [ $? -eq 0 ]
584 #               then
585 #                       xkeymap=`tail -n 7 /lib/keymaps/keymapList | grep ${keymap} | sed -r "s/.*\s+${keymap}\s+([a-z-]+).*/\1/g" | egrep -v 1`
586 #               fi
587                 mkdir -p /etc/sysconfig
588 #               echo "XKEYBOARD=${xkeymap}" > /etc/sysconfig/keyboard
589                 echo "XKEYBOARD=${keymap}" > /etc/sysconfig/keyboard
590                 splash set_msg "Set keymap to ${keymap}"
591         elif [ -z "${keymap}" ]
592         then
593                 echo
594                 good_msg "Keeping default keymap"
595                 splash set_msg "Keeping default keymap"
596         else
597                 bad_msg "Sorry, but keymap ''${keymap}'' is invalid!"
598                 unset keymap
599                 chooseKeymap
600         fi
601 }
602
603 startVolumes() {
604         #good_msg 'Checking if volumes need to be started...'
605
606         # Here, we check for /dev/device-mapper, and if it exists, we setup a
607         # a symlink, which should hopefully fix bug #142775 and bug #147015
608         if [ -e /dev/device-mapper ] && [ ! -e /dev/mapper/control ]
609         then
610                 mkdir -p /dev/mapper
611                 ln -sf /dev/device-mapper /dev/mapper/control
612         fi
613         
614         if [ "${USE_MDADM}" = '1' ]
615         then
616                 if [ ! -e '/etc/mdadm.conf' ]
617                 then
618                         /sbin/mdadm --examine > /etc/mdadm.conf
619                 fi
620                 /sbin/mdadm --assemble
621         fi
622
623         if [ "${USE_DMRAID_NORMAL}" = '1' ]
624         then
625                 if [ -e '/sbin/dmraid' ]
626                 then
627                         good_msg "Activating Device-Mapper RAID(s)"
628                         if [ '${DMRAID_OPTS}' = '' ]
629                         then
630                                 /sbin/dmraid -ay
631                         else
632                                 /sbin/dmraid -ay ${DMRAID_OPTS}
633                         fi
634                 fi
635         fi
636
637         if [ "${USE_LVM_NORMAL}" = '1' ]
638         then
639                 if [ -e '/bin/vgscan' -a -e '/bin/vgchange' ]
640                 then
641                         for dev in ${RAID_DEVICES}
642                         do
643                                 setup_md_device "${dev}"
644                         done
645
646                         good_msg "Scanning for Volume Groups"
647                         /bin/vgscan --ignorelockingfailure --mknodes 2>/dev/null
648                         sleep 2
649                         good_msg "Activating Volume Groups"
650                         /bin/vgchange -ay --ignorelockingfailure 2>/dev/null
651
652                         # Disable EVMS since lvm is activated and they dont work together.
653                         if [ "${USE_EVMS_NORMAL}" = '1' ]
654                         then
655                                 bad_msg "Disabling EVMS Support because LVM started"
656                                 bad_msg "Do not add dolvm to the cmdline if this is not what you want"
657                                 bad_msg "LVM and EVMS do not work well together"
658                                 USE_EVMS_NORMAL=0
659                         fi
660                 else
661                         bad_msg "vgscan or vgchange not found: skipping LVM volume group activation!"
662                 fi
663         fi
664
665         if [ "${USE_EVMS_NORMAL}" = '1' ]
666         then
667                 if [ -e '/sbin/evms_activate' ]
668                 then
669                         good_msg "Activating EVMS"
670                         evms_activate
671                 fi
672         fi
673 }
674
675 startiscsi() {
676
677         if [ -n "${ISCSI_INITIATORNAME}" ] && [ -n "${ISCSI_TARGET}" ] && [ -n "${ISCSI_ADDRESS}" ]
678         then
679                 good_msg "Activating iSCSI"
680
681                 if [ "${ISCSI_TGPT}" ]
682                 then
683                         ADDITIONAL="${ADDITIONAL} -g ${ISCSI_TGPT}"
684                 else
685                         ADDITIONAL="${ADDITIONAL} -g 1"
686                 fi
687                 
688                 if [ "${ISCSI_PORT}" ]
689                 then
690                         ADDITIONAL="${ADDITIONAL} -p ${ISCSI_PORT}"
691                 fi
692
693                 if [ "${ISCSI_USERNAME}" ]
694                 then
695                         ADDITIONAL="${ADDITIONAL} -u ${ISCSI_USERNAME}"
696                 fi
697
698                 if [ "${ISCSI_PASSWORD}" ]
699                 then
700                         ADDITIONAL="${ADDITIONAL} -w ${ISCSI_PASSWORD}"
701                 fi
702
703                 if [ "${ISCSI_USERNAME_IN}" ]
704                 then
705                         ADDITIONAL="${ADDITIONAL} -U ${ISCSI_USERNAME_IN}"
706                 fi
707
708                 if [ "${ISCSI_PASSWORD_IN}" ]
709                 then
710                         ADDITIONAL="${ADDITIONAL} -W ${ISCSI_PASSWORD_IN}"
711                 fi
712
713                 if [ "${ISCSI_DEBUG}" ]
714                 then
715                         ADDITIONAL="${ADDITIONAL} -d ${ISCSI_DEBUG}"
716                 fi
717
718                 iscsistart -i "${ISCSI_INITIATORNAME}" -t "${ISCSI_TARGET}" -a "${ISCSI_ADDRESS}" ${ADDITIONAL}
719         fi
720 }
721
722
723 # Open a LUKS device
724 # It is either the root or a swap, other devices are supported in the scripts provided with sys-fs/cryptsetup-luks
725 # $1 - root/swap
726 openLUKS() {
727         # please use 'tr' and this line, or remove it
728         # eval local TYPE=`uppercase $1`
729
730         case $1 in
731                 root)
732                         local TYPE=ROOT
733                         ;;
734                 swap)
735                         local TYPE=SWAP
736                         ;;
737         esac
738
739         eval local LUKS_DEVICE='"${CRYPT_'${TYPE}'}"' LUKS_NAME="$1" LUKS_KEY='"${CRYPT_'${TYPE}'_KEY}"' LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"'
740         local DEV_ERROR=0 KEY_ERROR=0 KEYDEV_ERROR=0
741         local mntkey="/mnt/key/" cryptsetup_options=''
742
743         [ ! -e /sbin/cryptsetup ] && bad_msg "The ramdisk does not support LUKS" && exit 1
744         while [ 1 ]
745         do
746                 # if crypt_silent=1 and some error occurs, enter shell quietly
747                 if [ \( ${CRYPT_SILENT} -eq 1 \) -a \( \( \( ${DEV_ERROR} -eq 1 \) -o \( ${KEY_ERROR} -eq 1 \) \) -o \( ${KEYDEV_ERROR} -eq 1 \) \) ]
748                 then
749                         run_shell
750                 elif [ ${DEV_ERROR} -eq 1 ]
751                 then
752                         whereis "LUKS_DEVICE" "${LUKS_NAME}"
753                         DEV_ERROR=0
754                 elif [ ${KEY_ERROR} -eq 1 ]
755                 then
756                         whereis "LUKS_KEY" "${LUKS_NAME} key"
757                         KEY_ERROR=0
758                 elif [ ${KEYDEV_ERROR} -eq 1 ]
759                 then
760                         whereis "LUKS_KEYDEV" "${LUKS_NAME} key device"
761                         KEYDEV_ERROR=0
762                 else
763                         setup_md_device ${LUKS_DEVICE}
764                         cryptsetup isLuks ${LUKS_DEVICE}
765                         if [ $? -ne 0 ]
766                         then
767                                 bad_msg "The LUKS device ${LUKS_DEVICE} does not contain a LUKS header" ${CRYPT_SILENT}
768                                 DEV_ERROR=1
769                                 continue
770                         else
771                                 # Handle keys
772                                 if [ -n "${LUKS_KEY}" ] 
773                                 then
774                                         if [ ! -e "${mntkey}${LUKS_KEY}" ] 
775                                         then
776                                                 if [ -b "${LUKS_KEYDEV}" ]
777                                                 then good_msg "Using key device ${LUKS_KEYDEV}." ${CRYPT_SILENT}
778                                                 else
779                                                         good_msg "Please insert removable device ${LUKS_KEYDEV} for ${LUKS_NAME}" ${CRYPT_SILENT}
780                                                         # abort after 10 secs
781                                                         local count=10
782                                                         while [ ${count} -gt 0 ]
783                                                         do 
784                                                                 count=$((count-1))
785                                                                 sleep 1
786                                                                 if [ -b "${LUKS_KEYDEV}" ]
787                                                                 then
788                                                                         good_msg "Removable device ${LUKS_KEYDEV} detected." ${CRYPT_SILENT}
789                                                                         break
790                                                                 fi
791                                                         done
792                                                         if [ ! -b "${LUKS_KEYDEV}" ]
793                                                         then
794                                                                 eval CRYPT_${TYPE}_KEY=${LUKS_KEY}
795                                                                 bootstrapKey ${TYPE}
796                                                                 eval LUKS_KEYDEV='"${CRYPT_'${TYPE}'_KEYDEV}"'
797                                                                 if [ ! -b "${LUKS_KEYDEV}" ]; then
798                                                                         KEYDEV_ERROR=1
799                                                                         bad_msg "Removable device ${LUKS_KEYDEV} not found." ${CRYPT_SILENT}
800                                                                         continue
801                                                                 fi
802                                                                 # continue otherwise will mount keydev which is mounted by bootstrap
803                                                                 continue
804                                                         fi
805                                                 fi
806                                                 # At this point a device was recognized, now let's see if the key is there
807                                                 [ ! -d "$mntkey" ] && mkdir -p ${mntkey} 2>/dev/null >/dev/null
808
809                                                 mount -n -o ro ${LUKS_KEYDEV} ${mntkey} >/dev/null 2>/dev/null
810                                                 if [ "$?" != '0' ]
811                                                 then
812                                                         KEYDEV_ERROR=1
813                                                         bad_msg "Mounting of device ${LUKS_KEYDEV} failed." ${CRYPT_SILENT}
814                                                         continue
815                                                 else
816                                                         good_msg "Removable device ${LUKS_KEYDEV} mounted." ${CRYPT_SILENT}
817                                                         sleep 2
818                                                         # keyfile exists?
819                                                         if [ ! -e "${mntkey}${LUKS_KEY}" ]; then
820                                                                 umount -n ${mntkey} 2>/dev/null >/dev/null
821                                                                 KEY_ERROR=1
822                                                                 KEYDEV_ERROR=1
823                                                                 bad_msg "Key {LUKS_KEY} on device ${LUKS_KEYDEV} not found." ${CRYPT_SILENT}
824                                                                 continue
825                                                         fi
826                                                 fi
827                                         fi
828                                         # At this point a candidate key exists (either mounted before or not)
829                                         good_msg "${LUKS_KEY} on device ${LUKS_KEYDEV} found" ${CRYPT_SILENT}
830                                         cryptsetup_options="-d ${mntkey}${LUKS_KEY}"
831                                 fi
832                                 # At this point, keyfile or not, we're ready!
833                                 crypt_filter "cryptsetup ${cryptsetup_options} luksOpen ${LUKS_DEVICE} ${LUKS_NAME}"
834                                 if [ $? -eq 0 ]
835                                 then
836                                         good_msg "LUKS device ${LUKS_DEVICE} opened" ${CRYPT_SILENT}
837                                         break
838                                 else
839                                         bad_msg "Failed to open LUKS device ${LUKS_DEVICE}" ${CRYPT_SILENT}
840                                         DEV_ERROR=1
841                                         KEY_ERROR=1
842                                         KEYDEV_ERROR=1
843                                 fi
844                         fi
845                 fi
846         done
847         umount ${mntkey} 2>/dev/null >/dev/null
848         rmdir -p ${mntkey} 2>/dev/null >/dev/null
849 }
850
851 startLUKS() {
852
853         # if key is set but key device isn't, find it
854         
855         [ -n "${CRYPT_ROOT_KEY}" ] && [ -z "${CRYPT_ROOT_KEYDEV}" ] \
856                 && sleep 6 && bootstrapKey "ROOT"
857
858         if [ -n "${CRYPT_ROOT}" ]; then
859                 openLUKS "root"
860                 if [ -n "${REAL_ROOT}" ]
861                 then
862                         # Rescan volumes
863                         startVolumes
864                 else
865                         REAL_ROOT="/dev/mapper/root"
866                 fi
867         fi
868
869         # same for swap, but no need to sleep if root was unencrypted
870         [ -n "${CRYPT_SWAP_KEY}" ] && [ -z "${CRYPT_SWAP_KEYDEV}" ] \
871                 && { [ -z "${CRYPT_ROOT}" ] && sleep 6; bootstrapKey "SWAP"; }
872
873         if [ -n "${CRYPT_SWAP}" ]; then
874                 openLUKS "swap"
875                 if [ -z "${REAL_RESUME}" ]
876                 then
877                         # Resume from swap as default
878                         REAL_RESUME="/dev/mapper/swap"
879                 fi
880         fi
881 }
882
883 sdelay() {
884         # Sleep a specific number of seconds if SDELAY is set otherwise only sleep
885         # 3 seconds, which is a much better default than 1 second (previous default)
886         if [ "${SDELAY}" ]
887         then
888                 sleep ${SDELAY}
889         else
890                 sleep 3
891         fi
892 }
893
894 quiet_kmsg() {
895         # if QUIET is set make the kernel less chatty
896         [ -n "$QUIET" ] && echo '0' > /proc/sys/kernel/printk
897 }
898
899 verbose_kmsg() {
900         # if QUIET is set make the kernel less chatty
901         [ -n "$QUIET" ] && echo '6' > /proc/sys/kernel/printk
902 }
903
904
905 cdupdate() {
906         if [ "${CDROOT}" = '1' ]
907         then
908                 if [ -x /${NEW_ROOT}/mnt/cdrom/cdupdate.sh ]
909                 then
910                         good_msg "Running cdupdate.sh"
911                         ${NEW_ROOT}/mnt/cdrom/cdupdate.sh
912                         if [ "$?" != '0' ]
913                         then
914                                 bad_msg "Executing cdupdate.sh failed!"
915                                 run_shell
916                         fi
917                 else
918                         good_msg 'No cdupdate.sh script found, skipping...'
919                 fi
920         fi
921 }
922
923 setup_md_device() {
924         local device
925
926         [ -z "$1" ] && device="${REAL_ROOT}" || device="$1"
927         [ -z "${device}" ] && return # LiveCD
928
929         if [ `echo ${device}|sed -e 's#\(luks:\)\?\(/dev/md\)[[:digit:]]\+#\2#'` = "/dev/md" ]
930         then
931                 good_msg 'Detected real_root as a md device. Setting up the device node...'
932                 MD_NUMBER=`echo ${device}|sed -e 's#\(luks:\)\?/dev/md\([[:digit:]]\+\)#\2#'`
933                 if [ ! -e /dev/md${MD_NUMBER} ]
934                 then
935                         mknod /dev/md${MD_NUMBER} b 9 ${MD_NUMBER} >/dev/null 2>&1
936                         [ $? -ne 0 ] && bad_msg "Creation of /dev/md${MD_NUMBER} failed..."
937                 fi
938                 mdstart ${MDPART} /dev/md${MD_NUMBER}
939         fi
940 }
941
942 rundebugshell() {
943         if [ -n "$DEBUG" ]
944         then
945                 good_msg 'Starting debug shell as requested by "debug" option.'
946                 good_msg 'Type "exit" to continue with normal bootup.'
947                 [ -x /bin/sh ] && /bin/sh || /bin/ash
948         fi
949 }
950
951 do_resume() {
952         if [ -d /proc/suspend2 -o -d /sys/power/suspend2 -o -d /sys/power/tuxonice ]; then
953                 tuxonice_resume
954         else
955                 swsusp_resume
956         fi
957 }
958
959 swsusp_resume() {
960         # determine swap resume partition
961         local device=$(ls -lL "${REAL_RESUME}" | sed 's/\  */ /g' | cut -d \  -f 5-6 | sed 's/,\ */:/')
962         [ -f /sys/power/resume ] && echo "${device}" > /sys/power/resume
963 }
964
965 tuxonice_resume() {
966         local splash_theme
967         if grep "splash=" /proc/cmdline > /dev/null 2>&1; then
968                 splash_theme=$(cat /proc/cmdline | sed 's/.*splash=/splash=/' | sed 's/ .*//' | sed 's/.*theme://' | sed 's/,.*//')
969         fi
970
971         local tuxonice_userui_program="/sys/power/tuxonice/user_interface/program"
972         local tuxonice_do_resume="/sys/power/tuxonice/do_resume"
973         local tuxonice_resumedev="/sys/power/tuxonice/resume"
974         local tuxonice_replace_swsusp="/sys/power/tuxonice/replace_swsusp"
975
976         #
977         # Backward compatibility
978         #
979         if [ -e /sys/power/suspend2 ]; then
980                 tuxonice_userui_program="/sys/power/suspend2/user_interface/program"
981                 tuxonice_do_resume="/sys/power/suspend2/do_resume"
982                 tuxonice_resumedev="/sys/power/suspend2/resume"
983                 tuxonice_replace_swsusp="/sys/power/suspend2/replace_swsusp"
984         elif [ -e /proc/suspend2 ]; then
985                 tuxonice_userui_program="/proc/suspend2/userui_program"
986                 tuxonice_do_resume="/proc/suspend2/do_resume"
987                 tuxonice_resumedev="/proc/suspend2/resume"
988                 tuxonice_replace_swsusp="/proc/suspend2/replace_swsusp"
989         fi
990
991         # if 'use_swsusp' is given, use swsusp instead
992         if grep "use_swsusp" /proc/cmdline > /dev/null 2>&1; then
993                 echo 0 > ${tuxonice_replace_swsusp}
994                 swsusp_resume
995                 return
996         fi
997
998         modules_scan tuxonice
999
1000         # we both configure tuxonice and activate resuming,
1001         # however the kernel will resume only if an image is found
1002
1003         if ! grep suspend_noui /proc/cmdline > /dev/null 2>&1; then
1004                 which suspend2ui_text > /dev/null 2>&1 && which suspend2ui_text > "${tuxonice_userui_program}"
1005                 which tuxoniceui_text > /dev/null 2>&1 && which tuxoniceui_text > "${tuxonice_userui_program}"
1006
1007                 if [ -n "${splash_theme}" ]; then
1008                         ln -s /etc/splash/${splash_theme} /etc/splash/suspend2
1009                         ln -s /etc/splash/${splash_theme} /etc/splash/tuxonice
1010
1011                         which suspend2ui_fbsplash > /dev/null 2>&1 && which suspend2ui_fbsplash > "${tuxonice_userui_program}"
1012                         which tuxoniceui_fbsplash > /dev/null 2>&1 && which tuxoniceui_fbsplash > "${tuxonice_userui_program}"
1013                 fi
1014
1015         fi
1016         echo "${REAL_RESUME}" > "${tuxonice_resumedev}"
1017         echo > "${tuxonice_do_resume}"
1018 }
1019
1020 find_loop() {
1021         CDROM="${NEW_ROOT}/mnt/cdrom"
1022         for loop in ${LOOPS}
1023         do
1024                 if [ -e "${CDROM}""${loop}" ]
1025                 then
1026                         LOOP="${loop}"
1027                 fi
1028         done
1029 }
1030
1031 find_looptype() {
1032         LOOPTYPE="${LOOP##*.}"
1033         [ "${LOOPTYPE}" == "loop" ] && LOOPTYPE="normal"
1034         [ "${LOOP}" == "/zisofs" ] && LOOPTYPE="${LOOP#/}"
1035         [ -z "${LOOPTYPE}" ] && LOOPTYPE="noloop"
1036 }
1037
1038 getdvhoff() {
1039         echo $(( $(hexdump -n 4 -s $((316 + 12 * $2)) -e '"%i"' $1) * 512))
1040 }
1041
1042 setup_unionfs() {
1043         local rw_dir=$1
1044         local ro_dir=$2
1045         if [ "${USE_UNIONFS_NORMAL}" = '1' ]
1046         then
1047                 # Directory used for rw changes in union mount filesystem
1048                 UNION=/union
1049 #               MEMORY=/memory
1050 #               if [ -z "$UID" ]
1051 #               then
1052 #                       CHANGES=$MEMORY/unionfs_changes/default
1053 #               else
1054 #                       CHANGES=$MEMORY/unionfs_changes/$UID
1055 #               fi
1056
1057 #               mkdir -p ${MEMORY}
1058                 mkdir -p ${UNION}
1059                 good_msg "Loading fuse module"
1060                 modprobe fuse > /dev/null 2>&1
1061 #                 if [ -n "${UNIONFS}" ]         
1062 #                 then   
1063 #                         CHANGESDEV=${UNIONFS}          
1064 #                         good_msg "mounting $CHANGESDEV to $MEMORY for unionfs support"         
1065 #                         mount -t auto $CHANGESDEV $MEMORY      
1066 #                         # mount tmpfs only in the case when changes= boot parameter was        
1067 #                         # empty or we were not able to mount the storage device        
1068 #                         ret=$?         
1069 #                         if [ ${ret} -ne 0 ]
1070 #                         then   
1071 #                                 bad_msg "mount of $CHANGESDEV failed falling back to ramdisk based unionfs"    
1072 #                                 mount -t tmpfs tmpfs $MEMORY   
1073 #                         fi     
1074 #                         if [ "${CDROOT}" -eq '1' -a ! -f ${MEMORY}/livecd.unionfs  ]   
1075 #                         then   
1076 #                                 umount $MEMORY         
1077 #                                 bad_msg "failed to find livecd.unionfs file on $CHANGESDEV"    
1078 #                                 bad_msg "create a livecd.unionfs file on this device if you wish to use it for unionfs"        
1079 #                                 bad_msg "falling back to ramdisk based unionfs for safety"     
1080 #                                 mount -t tmpfs tmpfs $MEMORY   
1081 #                         fi     
1082 #                 else   
1083 #                         good_msg "Mounting ramdisk to $MEMORY for unionfs support..."          
1084 #                         mount -t tmpfs tmpfs $MEMORY   
1085 #                 fi     
1086          
1087                 mkdir /tmp
1088                 mkdir -p ${UNION}
1089 #               mkdir -p $CHANGES
1090 #               mount -t unionfs -o dirs=$CHANGES=rw unionfs ${UNION}
1091                 good_msg "Creating union mount"
1092                 unionfs -o allow_other,cow,noinitgroups,suid,dev,default_permissions,use_ino ${rw_dir}=RW:${ro_dir}=RO ${UNION} 2>/dev/null
1093                 ret=$?
1094                 if [ ${ret} -ne 0 ]
1095                 then
1096                         bad_msg "Can't setup union mount!"
1097                         USE_UNIONFS_NORMAL=0
1098                 fi
1099         else
1100                 USE_UNIONFS_NORMAL=0
1101         fi
1102 }