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