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