Debian Codegraph @

lib/vmlinuz-get-version

raw
1 #!/bin/sh
2
3 # ----------------------------------------------------------------------
4 # This file was taken from the Linux kernel source tree (scripts/extract-vmlinux)
5 # and has been adopted for the use within needrestart.
6 #
7 # extract-vmlinux - Extract uncompressed vmlinux from a kernel image
8 #
9 # Inspired from extract-ikconfig
10 # (c) 2009,2010 Dick Streefland <[email protected]>
11 #
12 # (c) 2011 Corentin Chary <[email protected]>
13 #
14 # Adopted for needrestart
15 # (c) 2016 - 2017 Thomas Liske <[email protected]>
16 #
17 # Licensed under the GNU General Public License, version 2 (GPLv2).
18 # ----------------------------------------------------------------------
19
20 get_version()
21 {
22 # search and output version string pattern,
23 # prefer the final version string (linux 6.1+)
24 grep -aoEm 1 'Linux version [[:digit:]].*#[^[:space:]]' "$1" && exit 0
25 grep -aoEm 1 'Linux version [[:digit:]].*#' "$1" && exit 0
26 }
27
28 try_decompress()
29 {
30 # The obscure use of the "tr" filter is to work around older versions of
31 # "grep" that report the byte offset of the line instead of the pattern.
32
33 # Try to find the header ($1) and decompress from here
34 for pos in $(tr "$1\n$2" "\n$2=" < "$img" | grep -abo "^$2"); do
35 pos=${pos%%:*}
36 tail -c+"$pos" "$img" | $3 > "$tmp" 2>/dev/null
37 get_version "$tmp"
38 done
39 }
40
41 # Check invocation:
42 me=${0##*/}
43 img=$1
44 if [ $# -lt 1 ] || [ $# -gt 2 ] || [ ! -s "$img" ]; then
45 echo "Usage: $me <kernel-image> [debug]" >&2
46 exit 2
47 fi
48
49 if [ "$2" = "1" ]; then
50 set -x
51 fi
52
53 # Prepare temp files:
54 tmp=$(mktemp)
55 trap 'rm -f "$tmp"' 0
56
57 # Initial attempt for uncompressed images or objects:
58 get_version "$img"
59
60 # That didn't work, so retry after decompression.
61 which gunzip > /dev/null && try_decompress '\037\213\010' xy gunzip
62 which unxz > /dev/null && try_decompress '\3757zXZ\000' abcde unxz
63 which bunzip2 > /dev/null && try_decompress 'BZh' xy bunzip2
64 which unlzma > /dev/null && try_decompress '\135\0\0\0' xxx unlzma
65 which lzop > /dev/null && try_decompress '\211\114\132' xy 'lzop -d'