Debian Codegraph @

update-grml-rescueboot

raw
1 #!/bin/bash
2 # Simple script to download a Grml ISO image to use with grml-rescueboot
3 # Needs the Debian keyring, sopv + wget
4 # Licensed under GPL v2+
5
6 set -eu -o pipefail
7
8 # defaults
9 grmlflavor=full
10 force=0
11 retrieved_iso=0
12
13 declare -A bin_to_pkg=( [update-grub]=grub2-common [wget]=wget [sopv]=sqopv )
14 declare -a missing_packages=()
15 for binary in "${!bin_to_pkg[@]}" ; do
16 if ! command -v "${binary}" &>/dev/null ; then
17 echo "ERROR: Binary $binary not found." >&2
18 missing_packages+=( "${bin_to_pkg[$binary]}" )
19 fi
20 done
21
22 keyringname="/usr/share/keyrings/debian-keyring"
23 keyring=""
24 for pgpext in pgp gpg; do
25 keyring="${keyringname}.${pgpext}"
26 if [ -r "${keyring}" ] ; then
27 break
28 fi
29 keyring=""
30 done
31 if [ -z "${keyring}" ] ; then
32 echo "ERROR: File ${keyringname}.{pgp,gpg} not found." >&2
33 missing_packages+=( debian-keyring )
34 fi
35
36 if [ ${#missing_packages[@]} -ne 0 ] ; then
37 echo "TIP: Try running \`apt install ${missing_packages[*]}\` to fix this." >&2
38 exit 1
39 fi
40
41 usage() {
42 echo "Usage: $(basename "$0") [-f] [-t <small|full>]"
43 }
44
45 while getopts ":t:fh" opt ; do
46 case ${opt} in
47 t)
48 if [ "${OPTARG}" = "full" ] || [ "${OPTARG}" = "small" ] ; then
49 grmlflavor="${OPTARG}"
50 else
51 echo "ERROR: Invalid value '${OPTARG}'. Supported values: small, full" >&2
52 usage >&2 ; exit 1
53 fi
54 ;;
55 f)
56 force=1
57 ;;
58 h)
59 usage ; exit 0
60 ;;
61 \?)
62 echo "ERROR: Invalid Option: -${OPTARG}" >&2
63 usage >&2 ; exit 1
64 ;;
65 :)
66 echo "ERROR: Option -${OPTARG} requires an argument." >&2
67 ;;
68 esac
69 done
70
71 arch="$(uname -m)"
72 case ${arch} in
73 aarch64)
74 grmlarch=arm64
75 ;;
76 x86_64)
77 grmlarch=amd64
78 ;;
79 *)
80 echo "ERROR: Unsupported architecture '${arch}'." >&2
81 usage >&2
82 exit 1
83 ;;
84 esac
85
86 echo "Finding out latest ISO image..."
87 date=$(wget --quiet -O- https://download.grml.org/ | sed --regex -n 's/.*grml-('"$grmlflavor"')-([0-9]{4}\.[0-9]{2})-('"$grmlarch"')\.iso.*/\2/p' | sort | tail -1)
88
89 if [ -z "${date}" ] ; then
90 echo "ERROR: Could not find out latest ISO." >&2
91 exit 1
92 fi
93
94 output_directory="/boot/grml"
95 mkdir -p "${output_directory}"
96
97 diskfree=$(df --output=avail "${output_directory}" | tail -1)
98 if [ -z "${diskfree}" ] ; then
99 echo "ERROR: couldn't calculate free disk space in /boot." >&2
100 exit 1
101 fi
102
103 if [ "${grmlflavor}" = "full" ] && [ "${diskfree}" -lt 1048576 ] ; then
104 if [ "${force}" = "1" ] ; then
105 echo "WARN: There might not be enough free disk space in /boot, continuing anyway as requested via -f."
106 else
107 echo "ERROR: there doesn't seem to be enough free disk space in /boot." >&2
108 echo "Note: >=1GB for grml-full recommended (use -f to force download anyway)."
109 exit 1
110 fi
111 elif [ "${grmlflavor}" = "small" ] && [ "${diskfree}" -lt 524288 ] ; then
112 if [ "${force}" = "1" ] ; then
113 echo "WARN: There might not be enough free disk space in /boot, continuing anyway as requested via -f."
114 else
115 echo "ERROR: there doesn't seem to be enough free disk space in /boot." >&2
116 echo "Note: >=512MB for grml-small recommended (use -f to force download anyway)."
117 exit 1
118 fi
119 fi
120
121 isoname="grml-${grmlflavor}-${date}-${grmlarch}.iso"
122 isofile="${output_directory}/${isoname}"
123 isofiletmp="${isofile}.tmp"
124
125 if [ "${force}" = "1" ] || ! [ -f "${isofile}" ] ; then
126 echo "Downloading Grml ISO to '${isofile}'."
127 wget -O "${isofiletmp}" "https://download.grml.org/${isoname}"
128 retrieved_iso=1
129 elif [ -f "${isofile}" ] ; then
130 echo "Found local ${isofile}, skipping download (use -f to force download)."
131 fi
132
133 if [ "${retrieved_iso}" = "1" ] ; then
134 sig="$(mktemp)"
135 echo "Verifying ISO..."
136 wget --quiet -O "${sig}" "https://download.grml.org/${isoname}.asc"
137
138 if ! sopv verify "${sig}" "${keyring}" <"${isofiletmp}" ; then
139 echo "ERROR: ISO file will be left in '${isofiletmp}'." >&2
140 rm "${sig}"
141 exit 1
142 fi
143
144 rm "${sig}"
145 mv "${isofiletmp}" "${isofile}"
146
147 echo "ISO file is OK."
148 fi
149
150 echo "Invoking 'update-grub' now."
151 update-grub
152
153 echo "Successfully finished grml-rescueboot integration."