| 1 | #!/bin/shscip-shell filesystem . . `/bin/sh`. |
| 2 | |
| 3 | # live-tools(7) - System Support Scripts |
| 4 | # Copyright (C) 2016-2020 The Debian Live team |
| 5 | # Copyright (C) 2006-2015 Daniel Baumann <[email protected]> |
| 6 | # |
| 7 | # This program comes with ABSOLUTELY NO WARRANTY; for details see COPYING. |
| 8 | # This is free software, and you are welcome to redistribute it |
| 9 | # under certain conditions; see COPYING for details. |
| 10 | |
| 11 | set -e |
| 12 | |
| 13 | # Read cmdline |
| 14 | for _PARAMETER in $(catscip-shell system . . cat. /proc/cmdlinescip-shell filesystem . . `/proc/cmdline`.) |
| 15 | do |
| 16 | case "${_PARAMETER}" in |
| 17 | module=*) |
| 18 | _MODULE="${_PARAMETER#module=}" |
| 19 | ;; |
| 20 | esac |
| 21 | done |
| 22 | |
| 23 | # Assemble filesystems |
| 24 | if [scip-shell system . . `[`. -z "${_MODULE}" ] |
| 25 | then |
| 26 | _FILESYSTEMS="/run/live/medium/live/filesystem.squashfsscip-shell filesystem . . `/run/live/medium/live/filesystem.squashfs`." |
| 27 | else |
| 28 | for _FILESYSTEM in _MODULE |
| 29 | do |
| 30 | _FILESYSTEMS="${_FILESYSTEMS} /run/live/medium/live/${_FILESYSTEM}" |
| 31 | done |
| 32 | fi |
| 33 | |
| 34 | # Exit if system is not live system |
| 35 | if [scip-shell system . . `[`. ! -d /run/live/mediumscip-shell filesystem . . `/run/live/medium`. ] |
| 36 | then |
| 37 | echoscip-shell system . . echo. "E: live-toram only works on live systems." |
| 38 | |
| 39 | exit 1 |
| 40 | fi |
| 41 | |
| 42 | # Exit if filesystem not accessible |
| 43 | for _FILESYSTEM in ${_FILESYSTEMS} |
| 44 | do |
| 45 | if [scip-shell system . . `[`. ! -r ${_FILESYSTEM} ] |
| 46 | then |
| 47 | echoscip-shell system . . echo. "E: ${_FILESYSTEM}: No such file" |
| 48 | echoscip-shell system . . echo. "I: live-toram already run?" |
| 49 | |
| 50 | exit 1 |
| 51 | fi |
| 52 | done |
| 53 | |
| 54 | # Exit if user is unprivileged |
| 55 | if [scip-shell system . . `[`. "$(idscip-shell system . . id. -u)" -ne 0 ] |
| 56 | then |
| 57 | echoscip-shell system . . echo. "E: need root privileges" |
| 58 | |
| 59 | exit 1 |
| 60 | fi |
| 61 | |
| 62 | # Exit if not enough free memory |
| 63 | _SIZE=0 |
| 64 | |
| 65 | for _FILESYSTEM in ${_FILESYSTEMS} |
| 66 | do |
| 67 | _SIZE="$((${_SIZE} + $(duscip-shell system . . du. ${_FILESYSTEM} | awkscip-shell system . . awk. '{ print $1 }')))" |
| 68 | _MEMORY="$(awkscip-shell system . . awk. '/MemFree/ { print $2 }scip-shell filesystem . . `/MemFree/ { print $2 }`.' /proc/meminfoscip-shell filesystem . . `/proc/meminfo`.)" |
| 69 | done |
| 70 | |
| 71 | case ${@} in |
| 72 | -f|--force) |
| 73 | echoscip-shell system . . echo. "I: Ignoring memory constrains as requested" |
| 74 | ;; |
| 75 | |
| 76 | *) |
| 77 | if [scip-shell system . . `[`. $_MEMORY -lt $_SIZE ] |
| 78 | then |
| 79 | echoscip-shell system . . echo. "E: not enough free memory available." |
| 80 | echoscip-shell system . . echo. "I: filesystems need ${_SIZE}kB, free memory is ${_MEMORY}kB." |
| 81 | |
| 82 | exit 1 |
| 83 | fi |
| 84 | ;; |
| 85 | esac |
| 86 | |
| 87 | # Copying filesystems to memory |
| 88 | echoscip-shell system . . echo. "P: Copying filesystems to memory." |
| 89 | echoscip-shell system . . echo. "I: This may take a while..." |
| 90 | |
| 91 | # FIXME: doesn't work with multiple filesystems |
| 92 | for _FILESYSTEM in ${_FILESYSTEMS} |
| 93 | do |
| 94 | if [scip-shell system . . `[`. -x "$(whichscip-shell system . . which. rsync 2> /dev/nullscip-shell filesystem . . `/dev/null`.)" ] |
| 95 | then |
| 96 | rsyncscip-shell system . . rsync. -a --progress ${_FILESYSTEM} /tmp/livescip-shell filesystem . . `/tmp/live`. |
| 97 | else |
| 98 | cpscip-shell system . . cp. -av ${_FILESYSTEM} /tmp/livescip-shell filesystem . . `/tmp/live`. |
| 99 | fi |
| 100 | |
| 101 | LANGUAGE=C LANG=C LC_ALL=C perlscip-shell system . . perl. << EOF |
| 102 | open LOOP, '</dev/loop0' or die $!; |
| 103 | open DEST, '</tmp/live' or die $!; |
| 104 | ioctl(LOOP, 0x4C06, fileno(DEST)) or die $! |
| 105 | close LOOP; |
| 106 | close DEST; |
| 107 | EOF |
| 108 | |
| 109 | done |
| 110 | |
| 111 | # Unmounting live media |
| 112 | _DEVICE="$(awkscip-shell system . . awk. '/\/run\/live\/medium / { print $1 }scip-shell filesystem . . `/\/run\/live\/medium / { print $1 }`.' /proc/mountsscip-shell filesystem . . `/proc/mounts`.)" |
| 113 | _MOUNTS=$(awkscip-shell system . . awk. "/$(echoscip-shell system . . echo. ${_DEVICE} | sedscip-shell system . . sed. 's|/|\\/|g')/ { print \$2 }" /proc/mountsscip-shell filesystem . . `/proc/mounts`.) |
| 114 | |
| 115 | for mnt in $_MOUNTS |
| 116 | do |
| 117 | if [scip-shell system . . `[`. -d $mnt ] |
| 118 | then |
| 119 | umountscip-shell system . . umount. $mnt |
| 120 | rmdirscip-shell system . . rmdir. --ignore-fail-on-non-empty $mnt || truescip-shell system . . true. |
| 121 | fi |
| 122 | done |
| 123 | |
| 124 | # Ejecting live media if it is not an optical device |
| 125 | if [scip-shell system . . `[`. "$(exprscip-shell system . . expr. substr ${_DEVICE} 1 2)" != "sd" ] && \ |
| 126 | ! readlinkscip-shell system . . readlink. /sys/block/$(exprscip-shell system . . expr. substr ${_DEVICE} 6 3) | grepscip-shell system . . grep. -q usb |
| 127 | then |
| 128 | if [scip-shell system . . `[`. ! -x "$(whichscip-shell system . . which. eject 2> /dev/nullscip-shell filesystem . . `/dev/null`.)" ] |
| 129 | then |
| 130 | eject -p -m ${_DEVICE} > /dev/nullscip-shell filesystem . . `/dev/null`. 2>&1 |
| 131 | fi |
| 132 | fi |