Debian Codegraph @

bin/live-toram

raw symbols SCIP
1 #!/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 $(cat /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 [ -z "${_MODULE}" ]
25 then
26 _FILESYSTEMS="/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 [ ! -d /run/live/medium ]
36 then
37 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 [ ! -r ${_FILESYSTEM} ]
46 then
47 echo "E: ${_FILESYSTEM}: No such file"
48 echo "I: live-toram already run?"
49
50 exit 1
51 fi
52 done
53
54 # Exit if user is unprivileged
55 if [ "$(id -u)" -ne 0 ]
56 then
57 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} + $(du ${_FILESYSTEM} | awk '{ print $1 }')))"
68 _MEMORY="$(awk '/MemFree/ { print $2 }' /proc/meminfo)"
69 done
70
71 case ${@} in
72 -f|--force)
73 echo "I: Ignoring memory constrains as requested"
74 ;;
75
76 *)
77 if [ $_MEMORY -lt $_SIZE ]
78 then
79 echo "E: not enough free memory available."
80 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 echo "P: Copying filesystems to memory."
89 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 [ -x "$(which rsync 2> /dev/null)" ]
95 then
96 rsync -a --progress ${_FILESYSTEM} /tmp/live
97 else
98 cp -av ${_FILESYSTEM} /tmp/live
99 fi
100
101 LANGUAGE=C LANG=C LC_ALL=C 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="$(awk '/\/run\/live\/medium / { print $1 }' /proc/mounts)"
113 _MOUNTS=$(awk "/$(echo ${_DEVICE} | sed 's|/|\\/|g')/ { print \$2 }" /proc/mounts)
114
115 for mnt in $_MOUNTS
116 do
117 if [ -d $mnt ]
118 then
119 umount $mnt
120 rmdir --ignore-fail-on-non-empty $mnt || true
121 fi
122 done
123
124 # Ejecting live media if it is not an optical device
125 if [ "$(expr substr ${_DEVICE} 1 2)" != "sd" ] && \
126 ! readlink /sys/block/$(expr substr ${_DEVICE} 6 3) | grep -q usb
127 then
128 if [ ! -x "$(which eject 2> /dev/null)" ]
129 then
130 eject -p -m ${_DEVICE} > /dev/null 2>&1
131 fi
132 fi