blacklist: replace 'modprobe -l' command with modinfo(8) command line
[grml-scripts.git] / usr_sbin / grml2ram
1 #!/bin/sh
2 # Filename:      grml2ram
3 # Purpose:       copy compressed GRML image to RAM
4 # Authors:       (c) Michael Schierl <schierlm-public@gmx.de>, (c) Michael Prokop <mika@grml.org>
5 # Bug-Reports:   see http://grml.org/bugs/
6 # License:       This file is licensed under the GPL v2.
7 ################################################################################
8
9 set -e
10
11 . /etc/grml/lsb-functions
12 . /etc/grml/script-functions
13
14 check4root || exit 1
15 check4progs rsync gawk || exit 2
16
17 if ! isgrmlcd ; then
18    eerror "Not running grml in live-cd mode. Nothing to be done, exiting." ; eend 1
19    exit 1
20 fi
21
22 if [ -z "$CMDLINE" ]; then
23   # if CMDLINE was set from the outside, we're debugging.
24   # otherwise, take CMDLINE from Kernel and config files.
25   CMDLINE="$(cat /proc/cmdline)"
26   [ -d /cdrom/bootparams/ ]      && CMDLINE="$CMDLINE $(cat /cdrom/bootparams/* | tr '\n' ' ')"
27   [ -d /live/image/bootparams/ ] && CMDLINE="$CMDLINE $(cat /live/image/bootparams/* | tr '\n' ' ')"
28 fi
29
30 getbootparam(){
31   local line
32   local ws
33   ws='   '
34   line=" $CMDLINE "
35   case "$line" in
36     *[${ws}]"$1="*)
37       result="${line##*[$ws]$1=}"
38       result="${result%%[$ws]*}"
39       echo "$result"
40       return 0 ;;
41     *) # no match?
42       return 1 ;;
43   esac
44 }
45
46 MEDIA_PATH="$(getbootparam live-media-path)"
47 MEDIA_PATH="${MEDIA_PATH:-.}"
48
49 IMAGE=$(find /live/image/${MEDIA_PATH}/ -name *.squashfs 2>/dev/null | head -1)
50 if ! [ -r "$IMAGE" ] ; then
51    if [ -r /cdrom/GRML/GRML ] ; then
52       IMAGE='/cdrom/GRML/GRML'
53    else
54       eerror "Can not access grml squashfs file."
55       eerror "Looks like you did run grml2ram already?" ; eend 1
56       exit 1
57    fi
58 fi
59
60 GRMLSIZE="$(du $IMAGE | awk '{print $1}')"
61 RAM=$(/usr/bin/gawk '/MemFree/{print $2}' /proc/meminfo)
62
63 case "$*" in -f|--force)
64   ewarn "Forcing copy process for grml-image (${GRMLSIZE}kB) as requested via force option." ; eend 0
65    ;;
66   *)
67    if test $RAM -lt $GRMLSIZE ; then
68       eerror "Sorry, not enough free RAM (${RAM}kB) available for grml-image (${GRMLSIZE}kB)." ; eend 1
69       exit 1
70    fi
71    ;;
72 esac
73
74 einfo "Copying $IMAGE to RAM, this might take a while."
75 rsync -a --progress $IMAGE /tmp/GRML
76 LANGUAGE=C LANG=C LC_ALL=C perl << EOF
77 open LOOP, '</dev/loop0' or die $!;
78 open DEST, '</tmp/GRML' or die $!;
79 ioctl(LOOP, 0x4C06, fileno(DEST)) or die $!;
80 close LOOP;
81 close DEST;
82 EOF
83
84 # identify cd-rom:
85 GRMLDEV=$(awk '{if ($2 ~ /^\/live\/image$/ ) print $1}' /etc/mtab)
86 if [ -z "$GRMLDEV" ]; then
87    GRMLDEV=$(awk '{if ($2 ~ /^\/cdrom$/ ) print $1}' /etc/mtab)
88 fi     
89 [ -n "$GRMLDEV" ] || GRMLDEV='/dev/cdrom'
90
91 einfo "Unmounting cdrom"
92 [ -d /live/image ] && umount /live/image || umount /cdrom
93 eend $?
94 einfo "Now you can eject your grml-cd (e.g. run 'eject $GRMLDEV')." ; eend 0
95
96 ## END OF FILE #################################################################