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