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