Debian Codegraph @

usr/share/rear/lib/linux-functions.sh

raw symbols SCIP
1 # linux-functions.sh
2 #
3 # linux functions for Relax-and-Recover
4 #
5 # This file is part of Relax-and-Recover, licensed under the GNU General
6 # Public License. Refer to the included COPYING for full text of license.
7
8 # The way how we use bash with lots of (nested) functions and read etc. seems to trigger a bash
9 # bug that causes leaked file descriptors. lvm likes to complain about that but since we
10 # cannot fix the bash we suppress these lvm warnings, see
11 # http://osdir.com/ml/bug-bash-gnu/2010-04/msg00080.html
12 # http://stackoverflow.com/questions/2649240/bash-file-descriptor-leak
13 # http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=466138
14 export LVM_SUPPRESS_FD_WARNINGS=1
15
16 # check if udev is available in a sufficiently recent version
17 # has_binary succeeds when one of its arguments exists
18 # newer systems (e.g. SLES11) have udevadm
19 # older systems (e.g. SLES10 or RHEL 4) have udevtrigger udevsettle udevinfo or udevstart
20 function have_udev () {
21 test -d /etc/udev/rules.d && has_binary udevadm udevtrigger udevsettle udevinfo udevstart && return 0
22 return 1
23 }
24
25 # try calling 'udevadm trigger' or 'udevtrigger' or fallback
26 # but waiting for udev and "kicking udev" both miss the point
27 # see https://github.com/rear/rear/issues/791
28 function my_udevtrigger () {
29 # first try the most current way, newer systems (e.g. SLES11) have 'udevadm trigger'
30 has_binary udevadm && udevadm trigger "$@" && return 0
31 # then try an older way, older systems (e.g. SLES10) have 'udevtrigger'
32 has_binary udevtrigger && udevtrigger "$@" && return 0
33 # as first fallback do what start_udev does on RHEL 4
34 if has_binary udevstart ; then
35 local udevd_pid=$( pidof -x udevd )
36 test "$udevd_pid" && kill $udevd_pid
37 udevstart </dev/null &>/dev/null && return 0
38 fi
39 # as final fallback just wait a bit and hope for the best
40 sleep 10
41 }
42
43 # try calling 'udevadm settle' or 'udevsettle' or fallback
44 # but waiting for udev and "kicking udev" both miss the point
45 # see https://github.com/rear/rear/issues/791
46 function my_udevsettle () {
47 # first try the most current way, newer systems (e.g. SLES11) have 'udevadm settle'
48 has_binary udevadm && udevadm settle "$@" && return 0
49 # then try an older way, older systems (e.g. SLES10) have 'udevsettle'
50 has_binary udevsettle && udevsettle "$@" && return 0
51 # as first fallback re-implement udevsettle for older systems
52 if [ -e /sys/kernel/uevent_seqnum ] && [ -e /dev/.udev/uevent_seqnum ] ; then
53 local tries=0
54 while [ "$( cat /sys/kernel/uevent_seqnum )" = "$( cat /dev/.udev/uevent_seqnum )" ] && [ "$tries" -lt 10 ] ; do
55 sleep 1
56 let tries=tries+1
57 done
58 return 0
59 fi
60 # as final fallback just wait a bit and hope for the best
61 sleep 10
62 }
63
64 # call 'udevadm info' or 'udevinfo'
65 function my_udevinfo () {
66 # first try the most current way, newer systems (e.g. SLES11) have 'udevadm info'
67 if has_binary udevadm ; then
68 udevadm info "$@"
69 return 0
70 fi
71 # then try an older way, older systems (e.g. SLES10) have 'udevinfo'
72 if has_binary udevinfo ; then
73 udevinfo "$@"
74 return 0
75 fi
76 # no fallback
77 return 1
78 }
79
80 # find out which are the storage drivers to use on this system
81 # returns a list of storage drivers on STDOUT
82 # optionally $1 specifies the directory where to search for
83 # drivers files
84 function FindStorageDrivers () {
85 # The special user setting MODULES=( 'no_modules' ) enforces that
86 # no kernel modules get included in the rescue/recovery system
87 # regardless of what modules are currently loaded.
88 # Test the first MODULES array element because other scripts
89 # in particular rescue/GNU/Linux/240_kernel_modules.sh
90 # already appended other modules to the MODULES array:
91 test "no_modules" = "$MODULES" && return
92
93 if (( ${#STORAGE_DRIVERS[@]} == 0 )) ; then
94 if ! grep -E 'kernel/drivers/(block|firewire|ide|ata|md|message|scsi|usb/storage)' /lib/modules/$KERNEL_VERSION/modules.builtin ; then
95 Error "FindStorageDrivers called but STORAGE_DRIVERS is empty and no builtin storage modules found"
96 fi
97 fi
98 {
99 while read module junk ; do
100 IsInArray "$module" "${STORAGE_DRIVERS[@]}" && echo $module
101 done < <(lsmod)
102 find ${1:-$VAR_DIR/recovery} -name storage_drivers -exec cat '{}' \; 2>/dev/null
103 } | grep -v -E '(loop)' | sort -u
104 # blacklist some more stuff here that came in the way on some systems
105 return 0
106 # always return 0 as the grep return code is meaningless
107 }
108
109 # Determine all required shared objects (shared/dynamic libraries)
110 # for programs and/or shared objects (binaries) specified in $@.
111 # RequiredSharedObjects outputs the set of required shared objects on STDOUT.
112 # The output are absolute paths to the required shared objects.
113 # The output can also be symbolic links (also as absolute paths).
114 # In case of symbolic links only the link but not the link target is output.
115 function RequiredSharedObjects () {
116 has_binary ldd || Error "Cannot run RequiredSharedObjects() because there is no ldd binary"
117 Log "RequiredSharedObjects: Determining required shared objects"
118 # It uses 'ldd' to determine all required shared objects because 'ldd' outputs
119 # also transitively required shared objects i.e. libraries needed by libraries,
120 # e.g. for /usr/sbin/parted also the libraries needed by the libparted library:
121 # # ldd /usr/sbin/parted
122 # linux-vdso.so.1 (0x00007ffd68fe1000)
123 # libparted.so.2 => /usr/lib64/libparted.so.2 (0x00007f0c72bee000)
124 # libtinfo.so.6 => /lib64/libtinfo.so.6 (0x00007f0c729c4000)
125 # libreadline.so.6 => /lib64/libreadline.so.6 (0x00007f0c72778000)
126 # libc.so.6 => /lib64/libc.so.6 (0x00007f0c723d5000)
127 # libuuid.so.1 => /usr/lib64/libuuid.so.1 (0x00007f0c721d0000)
128 # libdevmapper.so.1.02 => /lib64/libdevmapper.so.1.02 (0x00007f0c71f85000)
129 # libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f0c71d43000)
130 # libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007f0c71b0f000)
131 # /lib64/ld-linux-x86-64.so.2 (0x000055eff2882000)
132 # libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f0c718e8000)
133 # libudev.so.1 => /usr/lib64/libudev.so.1 (0x00007f0c716c8000)
134 # libpthread.so.0 => /lib64/noelision/libpthread.so.0 (0x00007f0c714ab000)
135 # libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f0c71244000)
136 # libdl.so.2 => /lib64/libdl.so.2 (0x00007f0c71040000)
137 # libcap.so.2 => /lib64/libcap.so.2 (0x00007f0c70e3b000)
138 # librt.so.1 => /lib64/librt.so.1 (0x00007f0c70c32000)
139 # libm.so.6 => /lib64/libm.so.6 (0x00007f0c70935000)
140 # libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f0c7071e000)
141 # # file /usr/lib64/libparted.so.2
142 # /usr/lib64/libparted.so.2: symbolic link to `libparted.so.2.0.0'
143 # # mv /usr/lib64/libparted.so.2.0.0 /usr/lib64/libparted.so.2.0.0.away
144 # # ldd /usr/sbin/parted /usr/lib64/libparted.so.2.0.0.away
145 # /usr/sbin/parted:
146 # linux-vdso.so.1 (0x00007ffc38505000)
147 # libparted.so.2 => not found
148 # libtinfo.so.6 => /lib64/libtinfo.so.6 (0x00007fe0f4b5e000)
149 # libreadline.so.6 => /lib64/libreadline.so.6 (0x00007fe0f4913000)
150 # libc.so.6 => /lib64/libc.so.6 (0x00007fe0f4570000)
151 # libtinfo.so.5 => /lib64/libtinfo.so.5 (0x00007fe0f433b000)
152 # /lib64/ld-linux-x86-64.so.2 (0x000055e2549e2000)
153 # /usr/lib64/libparted.so.2.0.0.away:
154 # linux-vdso.so.1 (0x00007fffdbb8f000)
155 # libuuid.so.1 => /usr/lib64/libuuid.so.1 (0x00007f3c9a87d000)
156 # libdevmapper.so.1.02 => /lib64/libdevmapper.so.1.02 (0x00007f3c9a633000)
157 # libblkid.so.1 => /usr/lib64/libblkid.so.1 (0x00007f3c9a3f0000)
158 # libc.so.6 => /lib64/libc.so.6 (0x00007f3c9a04d000)
159 # /lib64/ld-linux-x86-64.so.2 (0x0000563ffc5f1000)
160 # libselinux.so.1 => /lib64/libselinux.so.1 (0x00007f3c99e27000)
161 # libudev.so.1 => /usr/lib64/libudev.so.1 (0x00007f3c99c06000)
162 # libpthread.so.0 => /lib64/noelision/libpthread.so.0 (0x00007f3c999e9000)
163 # libpcre.so.1 => /usr/lib64/libpcre.so.1 (0x00007f3c99783000)
164 # libdl.so.2 => /lib64/libdl.so.2 (0x00007f3c9957e000)
165 # libcap.so.2 => /lib64/libcap.so.2 (0x00007f3c99379000)
166 # librt.so.1 => /lib64/librt.so.1 (0x00007f3c99171000)
167 # libm.so.6 => /lib64/libm.so.6 (0x00007f3c98e73000)
168 # libresolv.so.2 => /lib64/libresolv.so.2 (0x00007f3c98c5c000)
169 # The 'ldd' output (when providing more than one argument) has 5 cases.
170 # So we have to distinguish lines of the following form (indentation is done with tab '\t'):
171 # 1. Line: "/path/to/binary:" -> current file argument for ldd
172 # 2. Line: " lib (mem-addr)" -> virtual library
173 # 3. Line: " lib => not found" -> print error to stderr
174 # 4. Line: " lib => /path/to/lib (mem-addr)" -> print $3 '/path/to/lib'
175 # 5. Line: " /path/to/lib => /path/to/lib2 (mem-addr)" -> print $3 '/path/to/lib2'
176 # 6. Line: " /path/to/lib (mem-addr)" -> print $1 '/path/to/lib'
177 local file_for_ldd=""
178 local file_owner_name=""
179 # It is crucial to append to /dev/$DISPENSABLE_OUTPUT_DEV (cf. 'Print' in lib/_input-output-functions.sh):
180 for file_for_ldd in "$@" ; do
181 # Skip non-regular files like directories, device files, and non-existent files
182 # cf. similar code in build/GNU/Linux/100_copy_as_is.sh
183 # but here symbolic links must not be skipped (e.g. /sbin/mkfs.ext2 -> /usr/sbin/mkfs.ext2)
184 # otherwise there would be binaries in the recovery system without required libraries:
185 test -f "$file_for_ldd" || continue
186 # Skip the ldd test for kernel modules and firmware files
187 # which could happen via COPY_AS_IS+=( /lib/firmware/my_hardware )
188 # cf. the code in build/default/990_verify_rootfs.sh
189 grep -Eq '/lib/modules/|/lib.*/firmware/' <<<"$file_for_ldd" && continue
190 # Skip files that are not owned by a trusted user to mitigate possible ldd security issues
191 # because some versions of ldd may directly execute the file (see "man ldd")
192 # which could lead to the execution of arbitrary programs as user 'root'
193 # in particular when directories are specified in COPY_AS_IS that may contain
194 # unexpected files like programs from arbitrary (possibly untrusted) users
195 # like COPY_AS_IS+=( /home/JohnDoe ) when JohnDoe is not a trusted user.
196 if test "$TRUSTED_FILE_OWNERS" ; then
197 file_owner_name="$( stat -c %U $file_for_ldd )"
198 if ! IsInArray "$file_owner_name" "${TRUSTED_FILE_OWNERS[@]}" ; then
199 Log "RequiredSharedObjects: Skipping 'ldd' for '$file_for_ldd' (owner '$file_owner_name' not in TRUSTED_FILE_OWNERS)"
200 continue
201 fi
202 fi
203 ldd $file_for_ldd
204 # It is crucial to filter the output of all those ldd calls in the 'for' loop
205 # through one "awk ... | sort -u" pipe to output the set of required shared objects
206 # (a mathematical set does not contain duplicate elements):
207 done 2>>/dev/$DISPENSABLE_OUTPUT_DEV | awk ' /^\t.+ => not found/ { print "Shared object " $1 " not found" > "/dev/stderr" }
208 /^\t.+ => \// { print $3 }
209 /^\t\// && !/ => / { print $1 } ' | sort -u
210 }
211
212 # Provide a shell, with custom exit-prompt and history
213 function rear_shell () {
214 local prompt=$1
215 local history=$2
216 # Set fallback exit prompt:
217 test "$prompt" || prompt="Are you sure you want to exit the Relax-and-Recover shell ?"
218 # Set some history:
219 local histfile="$TMP_DIR/.bash_history"
220 echo "exit" >$histfile
221 test "$history" && echo -e "exit\n$history" >$histfile
222 # Setup .bashrc:
223 local bashrc="$TMP_DIR/.bashrc"
224 cat <<EOF >$bashrc
225 export PS1="rear> "
226 ask_exit() {
227 read -p "$prompt " REPLY
228 if [[ "\$REPLY" =~ ^[Yy1] ]] ; then
229 \exit
230 fi
231 }
232 rear() {
233 echo "ERROR: You cannot run rear from within the Relax-and-Recover shell !" >&2
234 }
235 alias exit=ask_exit
236 alias halt=ask_exit
237 alias poweroff=ask_exit
238 alias reboot=ask_exit
239 alias shutdown=ask_exit
240 cd $VAR_DIR
241 EOF
242 # Run 'bash' with the original STDIN STDOUT and STDERR when 'rear' was launched by the user
243 # to get input from the user and to show output to the user (cf. _input-output-functions.sh):
244 HISTFILE="$histfile" bash --noprofile --rcfile $bashrc 0<&6 1>&7 2>&8
245 }
246
247 # Return the filesystem name related to a path
248 function filesystem_name () {
249 local path=$1
250 local fs=$(df -Pl "$path" | awk 'END { print $6 }')
251 if [[ -z "$fs" ]]; then
252 echo "/"
253 else
254 echo "$fs"
255 fi
256 }
257