#!/bin/sh
# Filename: grml2ram
# Purpose: copy compressed GRML image to RAM
-# Authors: grml-team (grml.org), (c) Michael Schierl <schierlm-public@gmx.de>, (c) Michael Prokop <mika@grml.org>
+# Authors: (c) Michael Schierl <schierlm-public@gmx.de>, (c) Michael Prokop <mika@grml.org>
# Bug-Reports: see http://grml.org/bugs/
# License: This file is licensed under the GPL v2.
-# Latest change: Die Dez 12 19:55:05 CET 2006 [mika]
################################################################################
set -e
exit 1
fi
-if ! [ -r /cdrom/GRML/GRML ] ; then
- eerror "Can not access /cdrom/GRML/GRML. Looks like you did run grml2ram already?" ; eend 1
- exit 1
+# old linuxrc version:
+[ -d /cdrom ] && export LIVECD_PATH=/cdrom
+# initramfs layout until around December 2012:
+[ -d /live/image ] && export LIVECD_PATH=/live/image
+# initramfs layout since around December 2012:
+[ -d /lib/live/mount/medium ] && export LIVECD_PATH=/lib/live/mount/medium
+
+if [ -z "$CMDLINE" ]; then
+ # if CMDLINE was set from the outside, we're debugging.
+ # otherwise, take CMDLINE from Kernel and config files.
+ CMDLINE="$(cat /proc/cmdline)"
+ [ -d ${LIVECD_PATH}/bootparams/ ] && CMDLINE="$CMDLINE $(cat ${LIVECD_PATH}/bootparams/* | tr '\n' ' ')"
fi
-GRMLSIZE=$(du /cdrom/GRML/GRML | awk '{print $1}')
+getbootparam(){
+ local line
+ local ws
+ ws=' '
+ line=" $CMDLINE "
+ case "$line" in
+ *[${ws}]"$1="*)
+ result="${line##*[$ws]$1=}"
+ result="${result%%[$ws]*}"
+ echo "$result"
+ return 0 ;;
+ *) # no match?
+ return 1 ;;
+ esac
+}
+
+MEDIA_PATH="$(getbootparam live-media-path)"
+MEDIA_PATH="${MEDIA_PATH:-.}"
+
+IMAGE=$(find ${LIVECD_PATH}/${MEDIA_PATH}/ -name *.squashfs 2>/dev/null | head -1)
+if ! [ -r "$IMAGE" ] ; then
+ if [ -r /cdrom/GRML/GRML ] ; then
+ IMAGE='/cdrom/GRML/GRML'
+ else
+ eerror "Can not access grml squashfs file."
+ eerror "Looks like you did run grml2ram already?" ; eend 1
+ exit 1
+ fi
+fi
+
+GRMLSIZE="$(du $IMAGE | awk '{print $1}')"
RAM=$(/usr/bin/gawk '/MemFree/{print $2}' /proc/meminfo)
case "$*" in -f|--force)
;;
esac
-einfo "Copying /cdrom/GRML/GRML to RAM, this might take a while."
-rsync -a --progress /cdrom/GRML/GRML /tmp/GRML
+einfo "Copying $IMAGE to RAM, this might take a while."
+rsync -a --progress $IMAGE /tmp/GRML
LANGUAGE=C LANG=C LC_ALL=C perl << EOF
open LOOP, '</dev/loop0' or die $!;
open DEST, '</tmp/GRML' or die $!;
-ioctl(LOOP, 0x4C06, fileno(DEST)) or die $!
+ioctl(LOOP, 0x4C06, fileno(DEST)) or die $!;
close LOOP;
close DEST;
EOF
-einfo "Unmounting /cdrom."
-umount /cdrom ; eend $?
-einfo "Now you can eject your grml-cd (e.g. run 'eject /dev/cdrom')." ; eend 0
+# identify cd-rom:
+GRMLDEV=$(awk '{if ($2 ~ /^\/lib\/live\/mount\/medium$/ ) print $1}' /etc/mtab)
+if [ -z "$GRMLDEV" ]; then
+ GRMLDEV=$(awk '{if ($2 ~ /^\/live\/image$/ ) print $1}' /etc/mtab)
+fi
+if [ -z "$GRMLDEV" ]; then
+ GRMLDEV=$(awk '{if ($2 ~ /^\/cdrom$/ ) print $1}' /etc/mtab)
+fi
+[ -n "$GRMLDEV" ] || GRMLDEV='/dev/cdrom'
+
+einfo "Unmounting cdrom"
+[ -d $LIVECD_PATH ] && umount $LIVECD_PATH || umount /cdrom
+eend $?
+einfo "Now you can eject your grml-cd (e.g. run 'eject $GRMLDEV')." ; eend 0
## END OF FILE #################################################################