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