| 1 | #!/bin/bash |
| 2 | # Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: prepare the memtest86+ by using the files on the system or download and extract the required programs for DRBL. |
| 5 | DRBL_SCRIPT_PATH="${DRBL_SCRIPT_PATH:-/usr/share/drbl}" |
| 6 | |
| 7 | . $DRBL_SCRIPT_PATH/sbin/drbl-conf-functions |
| 8 | [ -e /etc/drbl/drbl-ocs.conf ] && . /etc/drbl/drbl-ocs.conf |
| 9 | [ -e $DRBL_SCRIPT_PATH/sbin/ocs-functions ] && . $DRBL_SCRIPT_PATH/sbin/ocs-functions |
| 10 | |
| 11 | # Local settings |
| 12 | # By default we only copy the required files to $pxelinux_binsrc_dir. This option will allow us to copy files to dir $pxecfg_pd. |
| 13 | deploy_to_system_too="no" |
| 14 | |
| 15 | # Functions |
| 16 | USAGE() { |
| 17 | echo "Put the memtest86+ files in DRBL package repository" |
| 18 | echo "Usage: $0 [OPTION]" |
| 19 | echo "OPTION:" |
| 20 | echo "-u, --from-upstream Use the binary from upstream ($MEMTEST86_URL). If this option is not assigned, DRBL will try to use the memtest86+ from your GNU/Linux distribution." |
| 21 | echo "-mv, --memtest86-version VERSION The memtest86+ version to be used with -u|--from-upstream. If not assigned, the version number specified in drbl.conf will be used." |
| 22 | echo "-d, --deploy-to-system-too Deploy the file to DRBL system ($pxecfg_pd), too." |
| 23 | echo "Ex: To use the memtest86+ 4.20 from $MEMTEST86_URL, run '$0 -u -mv 4.20'" |
| 24 | } |
| 25 | |
| 26 | # |
| 27 | put_memtest_bin_2_drbl_repo() { |
| 28 | local memtest_bin_abs_path="$1" |
| 29 | [ -z "$memtest_bin_abs_path" ] && return 1 |
| 30 | echo "Putting memtest86+ in DRBL package repository $memtest86_dir/... " |
| 31 | memtest_f="$(shorten_memtest_filename `basename $memtest_bin_abs_path`)" |
| 32 | cp -af $memtest_bin_abs_path $memtest86_dir/$memtest_f |
| 33 | memtest_v="$(LC_ALL=C strings $memtest_bin_abs_path | grep -Ei 'Memtest86\+[[:space:]]+v' |\ |
| 34 | sed -r -e "s/^[[:space:]]*//g" | sort | head -n 1)" |
| 35 | echo "Memtest86+ version: $memtest_v" |
| 36 | echo $memtest_v > $memtest86_dir/VERSION |
| 37 | echo "done!" |
| 38 | if [ "$deploy_to_system_too" = "yes" ]; then |
| 39 | echo -n "Putting memtest86+ in DRBL system dir $pxecfg_pd/... " |
| 40 | cp -af $memtest_f $pxecfg_pd/ |
| 41 | echo $memtest_v > $pxecfg_pd/MEMTEST86+_VERSION |
| 42 | echo "done!" |
| 43 | fi |
| 44 | } # end of put_memtest_bin_2_drbl_repo |
| 45 | |
| 46 | # |
| 47 | put_upstream_memtest(){ |
| 48 | # Function to use the upstream binary directly |
| 49 | local ver="$1" |
| 50 | if [ -z "$ver" ]; then |
| 51 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 52 | echo "No upstream memtest version was assigned. Program terminated." |
| 53 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 54 | USAGE |
| 55 | exit 1 |
| 56 | fi |
| 57 | memfile="memtest86+-${ver}.bin" |
| 58 | tmp_wd="$(LC_ALL=C mktemp -d /tmp/memtest_tmp.XXXXXXX)" |
| 59 | echo -n "Downloading ${ver} memtest86+ files from $MEMTEST86_URL... " |
| 60 | LC_ALL=C wget $wget_opt -P $tmp_wd $MEMTEST86_URL/$ver/$memfile |
| 61 | echo "done!" |
| 62 | put_memtest_bin_2_drbl_repo $tmp_wd/$memfile |
| 63 | if [ -d "$tmp_wd" -a -n "$(echo "$tmp_wd" | grep -i memtest_tmp)" ]; then |
| 64 | rm -rf $tmp_wd |
| 65 | fi |
| 66 | } # end of put_upstream_memtest |
| 67 | # |
| 68 | shorten_memtest_filename() { |
| 69 | local inp_fn="$1" |
| 70 | # memtest86+.bin -> mt86+x32.mbr |
| 71 | # memtest86+x32.bin -> mt86+x32.mbr |
| 72 | # memtest86+x32.efi -> mt86+x32.efi |
| 73 | # memtest86+ia32.bin -> mt86+x32.mbr |
| 74 | # memtest86+ia32.efi -> mt86+x32.efi |
| 75 | # memtest86+x64.bin -> mt86+x64.mbr |
| 76 | # memtest86+x64.efi -> mt86+x64.efi |
| 77 | case "$inp_fn" in |
| 78 | memtest86+.bin) echo mt86+x32.mbr;; |
| 79 | memtest86+x32.bin) echo mt86+x32.mbr;; |
| 80 | memtest86+x32.efi) echo mt86+x32.efi;; |
| 81 | memtest86+ia32.bin) echo mt86+x32.mbr;; |
| 82 | memtest86+ia32.efi) echo mt86+x32.efi;; |
| 83 | memtest86+x64.bin) echo mt86+x64.mbr;; |
| 84 | memtest86+x64.efi) echo mt86+x64.efi;; |
| 85 | *) echo $inp_fn;; |
| 86 | esac |
| 87 | } # end of shorten_memtest_filename |
| 88 | # |
| 89 | put_distribution_memtest() { |
| 90 | # Function to use the package from distribution. |
| 91 | # Memtest86+ file name on different distributions: |
| 92 | # Debian/Ubuntu |
| 93 | # <= v7.2: /boot/memtest86+x64.bin, /boot/memtest86+x64.efi; |
| 94 | # >= v8.0: /boot/mt86+ia32, /boot/mt86+x64 (Unified Binary, no longer separate .bin (Legacy) and .efi (UEFI) files |
| 95 | # i.e., a single binary for both UEFI and legacy boot.) |
| 96 | # SuSE: "memtest.bin" |
| 97 | # Fedora 14: "/boot/memtest86+" |
| 98 | # Centos 5.6: "/boot/memtest86+-1.65" |
| 99 | if $query_pkglist_exist_cmd memtest86+ &>/dev/null; then |
| 100 | # Found on the system |
| 101 | # For Redhat-like system, there might return more than one file on a system, i.e. one file belongs to 2 rpm packages. |
| 102 | # 2022/11/23, 2023/05/17 On Debian, memtest86+ v6 is available: |
| 103 | # /boot/memtest86+x32.bin |
| 104 | # /boot/memtest86+x32.efi |
| 105 | # /boot/memtest86+ia32.bin (For newer package) |
| 106 | # /boot/memtest86+ia32.efi (For newer package) |
| 107 | # /boot/memtest86+x64.bin |
| 108 | # /boot/memtest86+x64.efi |
| 109 | # While for memtest86+ <= v5, the file is: |
| 110 | # /boot/memtest86+.bin |
| 111 | |
| 112 | # For syslinux in FAT file system, |
| 113 | # "memtest86+.bin" is 9 characters, will go wrong when it's FAT (usb flash drive). We use memtest to overwrite the one comes from Debian live. |
| 114 | # In addition, do NOT use memtest.bin, use memtest instead of memtest.bin |
| 115 | # since "*.bin" has specially purpose for syslinux. |
| 116 | # So here they are: |
| 117 | # memtest86+.bin -> mt86+x32.mbr |
| 118 | # memtest86+x32.bin -> mt86+x32.mbr |
| 119 | # memtest86+x32.efi -> mt86+x32.efi |
| 120 | # memtest86+ia32.bin -> mt86+x32.mbr |
| 121 | # memtest86+ia32.efi -> mt86+x32.efi |
| 122 | # memtest86+x64.bin -> mt86+x64.mbr |
| 123 | # memtest86+x64.efi -> mt86+x64.efi |
| 124 | |
| 125 | # For memtest <= v5.0 |
| 126 | memtest_bin="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "(memtest86\+.bin$|memtest\.bin$|^/boot/memtest86+)" | sort | head -n 1)" |
| 127 | # For memtest >= v6.0 |
| 128 | memtest_x32_bin="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+x32\.bin" | sort | head -n 1)" |
| 129 | memtest_x32_efi="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+x32\.efi" | sort | head -n 1)" |
| 130 | memtest_x32_bin="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+ia32\.bin" | sort | head -n 1)" |
| 131 | memtest_x32_efi="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+ia32\.efi" | sort | head -n 1)" |
| 132 | memtest_x64_bin="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+x64\.bin" | sort | head -n 1)" |
| 133 | memtest_x64_efi="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/memtest86\+x64\.efi" | sort | head -n 1)" |
| 134 | # For memtest >= v8.0 |
| 135 | memtest_x64="$(LC_ALL=C $query_pkglist_cmd memtest86+ | grep -Ew "^/boot/mt86\+x64$" | sort | head -n 1)" |
| 136 | for im in $memtest_x64 $memtest_bin $memtest_x32_bin $memtest_x32_efi $memtest_x64_bin $memtest_x64_efi; do |
| 137 | eval imfile="\$im" |
| 138 | if [ -n "$imfile" ]; then |
| 139 | echo "Found $imfile in this system, copying the memtest file to DRBL local repository..." |
| 140 | put_memtest_bin_2_drbl_repo $imfile |
| 141 | else |
| 142 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 143 | echo "Package of memtest was installed, but memtest boot file not found!" |
| 144 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 145 | fi |
| 146 | done |
| 147 | else |
| 148 | # Download and extract it. The reason we do not install it is we do not want to bother the server's boot menu |
| 149 | tmp_wd="$(LC_ALL=C mktemp -d /tmp/memtest_tmp.XXXXXXX)" |
| 150 | echo "Trying to download memtest86+ from distribution repository..." |
| 151 | if [ -e /etc/debian_version ]; then |
| 152 | # Debian |
| 153 | # The binary for memtest86+ in Debian Squeeze: /boot/memtest86+.bin |
| 154 | # Clean all stale deb files in the cache dir. Otherwise there might be some old version of debs exist in the cache dir. |
| 155 | LC_ALL=C apt-get clean |
| 156 | LC_ALL=C apt-get -d --reinstall install memtest86+ |
| 157 | ( |
| 158 | cd $tmp_wd |
| 159 | LC_ALL=C dpkg --extract /var/cache/apt/archives/memtest86+*.deb . |
| 160 | if [ -e "$(pwd)/boot/mt86+x64" ]; then |
| 161 | put_memtest_bin_2_drbl_repo "$(pwd)"/boot/mt86+x64 |
| 162 | elif [ -e "$(pwd)/boot/memtest86+64.bin" ]; then |
| 163 | put_memtest_bin_2_drbl_repo "$(pwd)"/boot/memtest86+x64.bin |
| 164 | elif [ -e "$(pwd)/boot/memtest86+64.efi" ]; then |
| 165 | put_memtest_bin_2_drbl_repo "$(pwd)"/boot/memtest86+x64.efi |
| 166 | fi |
| 167 | ) |
| 168 | elif [ -e /etc/SuSE-release ]; then |
| 169 | # SuSE |
| 170 | # The binary for memtest86+ in openSuse 11.3 is: /boot/memtest.bin |
| 171 | LC_ALL=C zypper install -d -f -y memtest86+ |
| 172 | memtest86_rpm="$(LC_ALL=C find /var/cache/zypp/packages/ -name "memtest86+-*.rpm" -print)" |
| 173 | memtest_bin="$(LC_ALL=C rpm -qpl "$memtest86_rpm" | grep -Ew "memtest.bin$")" |
| 174 | ( |
| 175 | cd $tmp_wd |
| 176 | LC_ALL=C rpm2cpio "$memtest86_rpm" | cpio -idm |
| 177 | put_memtest_bin_2_drbl_repo "$(pwd)"/$memtest_bin |
| 178 | ) |
| 179 | else |
| 180 | # RH-like |
| 181 | # The binary for memtest86+ in Fedora 14 is: /boot/memtest86+-4.10 |
| 182 | LC_ALL=C yumdownloader --destdir $tmp_wd memtest86+ |
| 183 | memtest_bin="$(LC_ALL=C rpm -qpl "$tmp_wd/memtest86*.rpm" | grep -Ew "^/boot/memtest86+")" |
| 184 | ( |
| 185 | cd $tmp_wd |
| 186 | LC_ALL=C rpm2cpio $tmp_wd/memtest86*.rpm | cpio -idm |
| 187 | put_memtest_bin_2_drbl_repo "$(pwd)"/$memtest_bin |
| 188 | ) |
| 189 | fi |
| 190 | if [ -d "$tmp_wd" -a -n "$(LC_ALL=C echo "$tmp_wd" | grep -i memtest_tmp)" ]; then |
| 191 | rm -rf $tmp_wd |
| 192 | fi |
| 193 | fi |
| 194 | } # end of put_distribution_memtest |
| 195 | |
| 196 | ############ |
| 197 | ### MAIN ### |
| 198 | ############ |
| 199 | |
| 200 | check_if_root |
| 201 | ask_and_load_lang_set |
| 202 | |
| 203 | # |
| 204 | while [ $# -gt 0 ]; do |
| 205 | case "$1" in |
| 206 | -u|--from-upstream) shift; mode="from-upstream" ;; |
| 207 | -mv|--memtest86-version) |
| 208 | shift; |
| 209 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 210 | # skip the -xx option, in case |
| 211 | memtest_ver_requested="$1" |
| 212 | [ -z "$memtest_ver_requested" ] && USAGE && exit 1 |
| 213 | shift |
| 214 | fi |
| 215 | ;; |
| 216 | -d|--deploy-to-system-too) shift; deploy_to_system_too="yes" ;; |
| 217 | *) USAGE && exit 1 ;; |
| 218 | esac |
| 219 | done |
| 220 | |
| 221 | # Check arch |
| 222 | cpu_arch="$(LC_ALL=C uname -m)" |
| 223 | if [ "$cpu_arch" != "i686" -a "$cpu_arch" != "x86_64" ]; then |
| 224 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 225 | echo "CPU arch is not i386/i686 or x86_64!" |
| 226 | echo "Skipping preparing memtest+!" |
| 227 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 228 | echo "$msg_program_stop" |
| 229 | exit 3 |
| 230 | fi |
| 231 | |
| 232 | # mode is essential |
| 233 | [ -z "$mode" ] && mode="from-distribution" |
| 234 | [ -z "$memtest_ver_requested" ] && memtest_ver_requested="$MEMTEST86_VER_DEF" |
| 235 | |
| 236 | case "$mode" in |
| 237 | from-distribution) put_distribution_memtest ;; |
| 238 | from-upstream) put_upstream_memtest $memtest_ver_requested ;; |
| 239 | esac |
| 240 | |
| 241 | exit 0 |