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