| 1 | #!/bin/bash |
| 2 | # Steven Shiau <steven _at_ clonezilla org>, Ceasar Sun <ceasar _at_ nchc org tw> |
| 3 | # License: GPL |
| 4 | # Description: prepare the pxelinux 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 | # Not existing in ubuntu syslinux package: gpxelinux.0 sanboot.c32 hdt.c32 |
| 13 | # The files are in /usr/lib/syslinux/ on Ubuntu 10.04 syslinux 3.63. |
| 14 | # Debian has syslinux-common, and the above files are in syslinux-common, but ubuntu 10.04 does not, and ubuntu 11.04 does. |
| 15 | # For Syslinux 5, new .c32 are required: ldlinux.c32, libcom32.c32, libutil.c32 |
| 16 | DRBL_NEED_PXELINUX_common="isolinux.bin memdisk pxelinux.0 lpxelinux.0 gpxelinux.0 mbr.bin" |
| 17 | DRBL_NEED_PXELINUX_modules="menu.c32 vesamenu.c32 chain.c32 mboot.c32 sanboot.c32 hdt.c32 $sys_pxelinux_v5p_required_c32" |
| 18 | DRBL_NEED_PXELINUX="$DRBL_NEED_PXELINUX_common $DRBL_NEED_PXELINUX_modules" |
| 19 | # By default we only copy the required files to $pxelinux_binsrc_dir. This option will allow us to copy files to dir $pxecfg_pd. |
| 20 | deploy_to_system_too="no" |
| 21 | # default not verbose |
| 22 | verbose="no" |
| 23 | |
| 24 | # Functions |
| 25 | USAGE() { |
| 26 | echo "Put the pxelinux files in DRBL package repository" |
| 27 | echo "Usage: $0 [OPTION]" |
| 28 | echo "OPTION:" |
| 29 | echo "-u, --from-upstream Use the binary from upstream ($syslinux_binsrc_url). If this option is not assigned, DRBL will try to use the pxelinux from your GNU/Linux distribution." |
| 30 | echo "-pv, --pxelinux-version VERSION The pxelinux version to be used with -u|--from-upstream. If not assigned, the version number specified in drbl.conf will be used." |
| 31 | echo "-d, --deploy-to-system-too Deploy the files to DRBL system ($pxecfg_pd), too." |
| 32 | echo "-v, --verbose show verbose messages." |
| 33 | echo "Ex: To use the pxelinux 4.04 from $syslinux_binsrc_url, run '$0 -u -pv 4.04'" |
| 34 | } |
| 35 | |
| 36 | # |
| 37 | put_pxelinux_bin_2_drbl_repo() { |
| 38 | local pxelinux_extracted_bin_abs_path="$1" # source dir name |
| 39 | local pxelinux_system_abs_path="$2" # destination dir name |
| 40 | [ -z "$pxelinux_extracted_bin_abs_path" ] && return 1 |
| 41 | [ -z "$pxelinux_system_abs_path" ] && return 1 |
| 42 | echo -n "Putting required pxelinux files to $pxelinux_system_abs_path... " |
| 43 | mkdir -p $pxelinux_system_abs_path |
| 44 | install -o root -g root -m 644 $pxelinux_extracted_bin_abs_path/* $pxelinux_system_abs_path |
| 45 | pxelinux_v="$(LC_ALL=C strings $pxelinux_file | grep "^PXELINUX")" |
| 46 | echo $pxelinux_v > $pxelinux_binsrc_dir/VERSION |
| 47 | echo "done!" |
| 48 | } # end of put_pxelinux_bin_2_drbl_repo |
| 49 | |
| 50 | # |
| 51 | put_upstream_pxelinux(){ |
| 52 | # Function to use the upstream binary directly |
| 53 | local ver="$1" |
| 54 | local tmp_wd syslinux_tarball syslinux_ver_subdir |
| 55 | if [ -z "$ver" ]; then |
| 56 | [ "$BOOTUP" = "color" ] && $SETCOLOR_FAILURE |
| 57 | echo "No upstream syslinux/pxelinux version was assigned. Program terminated." |
| 58 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 59 | USAGE |
| 60 | exit 1 |
| 61 | fi |
| 62 | syslinux_tarball="syslinux-${ver}.tar.xz" |
| 63 | tmp_wd="$(LC_ALL=C mktemp -d /tmp/syslinux_tmp.XXXXXXX)" |
| 64 | mkdir -p $tmp_wd/upstream |
| 65 | if [ -z "$(echo $ver | grep -iE -- "-pre[[:digit:]]+")" ]; then |
| 66 | # Stable release, not testing version. |
| 67 | # Convert the version number to subdir name, e..g 4.04 -> 4.xx |
| 68 | syslinux_ver_subdir="$(LC_ALL=C echo "$ver" | sed -r -e "s/\.[[:digit:]]*$/\.xx/g")" |
| 69 | else |
| 70 | # Testing release. File name is like: syslinux-6.02-pre16.tar.xz |
| 71 | # The URL for such a testing version is like: http://free.nchc.org.tw/syslinux/Testing/6.02/ |
| 72 | syslinux_ver_subdir="$(LC_ALL=C echo "$ver" | sed -r -e "s/-pre[[:digit:]]+$//g")" |
| 73 | syslinux_ver_subdir="Testing/$syslinux_ver_subdir" |
| 74 | fi |
| 75 | #echo -n "Downloading ${ver} syslinux/pxelinux files from $syslinux_binsrc_url... " |
| 76 | #LC_ALL=C wget $wget_opt -P $tmp_wd $syslinux_binsrc_url/$branch_path/$syslinux_tarball |
| 77 | echo "Downloading $syslinux_binsrc_url/$syslinux_ver_subdir/$syslinux_tarball..." |
| 78 | LC_ALL=C wget $wget_opt -P $tmp_wd $syslinux_binsrc_url/$syslinux_ver_subdir/$syslinux_tarball |
| 79 | echo "done!" |
| 80 | LC_ALL=C tar -xJf $tmp_wd/$syslinux_tarball -C $tmp_wd/upstream |
| 81 | mkdir -p $tmp_wd/extracted/syslinux |
| 82 | echo "Extracting the required files for DRBL..." |
| 83 | # Check if the tarball contains multiple arch (bios, efi32, and efi64) support. |
| 84 | if [ -n "$(find $tmp_wd/upstream -iname "efi64" -type d 2>/dev/null)" ]; then |
| 85 | syslinux_arch="multiple" |
| 86 | else |
| 87 | syslinux_arch="single" |
| 88 | fi |
| 89 | case "$syslinux_arch" in |
| 90 | "single") |
| 91 | for iprog in $DRBL_NEED_PXELINUX; do |
| 92 | find $tmp_wd/upstream -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted/syslinux/ \; |
| 93 | done |
| 94 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted/syslinux/ $pxelinux_binsrc_dir/bios/ |
| 95 | ;; |
| 96 | "multiple") |
| 97 | # For syslinux 6.x, *.c32 modules for different arch are put in bios/, efi32, or efi64/ |
| 98 | for iprog in $DRBL_NEED_PXELINUX_common; do |
| 99 | find $tmp_wd/upstream -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted/syslinux/ \; |
| 100 | done |
| 101 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted/syslinux/ $pxelinux_binsrc_dir/bios/ |
| 102 | rm -f $v_opt $tmp_wd/extracted/syslinux/* |
| 103 | for iarch in bios efi32 efi64; do |
| 104 | for iprog in $DRBL_NEED_PXELINUX_modules; do |
| 105 | # Here we search the files under syslinux/ |
| 106 | find $tmp_wd/upstream/*/$iarch/ -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted/syslinux/ \; |
| 107 | done |
| 108 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted/syslinux/ $pxelinux_binsrc_dir/$iarch/ |
| 109 | rm -f $v_opt $tmp_wd/extracted/syslinux/* |
| 110 | done |
| 111 | # Some more extra files for efi32 and efi64 |
| 112 | find $tmp_wd/upstream/*/efi32/ -name "syslinux.efi" -type f -exec \ |
| 113 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi32/ \; |
| 114 | find $tmp_wd/upstream/*/efi32/ -name "ldlinux.e32" -type f -exec \ |
| 115 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi32/ \; |
| 116 | find $tmp_wd/upstream/*/efi64/ -name "syslinux.efi" -type f -exec \ |
| 117 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi64/ \; |
| 118 | find $tmp_wd/upstream/*/efi64/ -name "ldlinux.e64" -type f -exec \ |
| 119 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi64/ \; |
| 120 | ;; |
| 121 | esac |
| 122 | if [ -d "$tmp_wd" -a -n "$(echo "$tmp_wd" | grep -i syslinux_tmp)" ]; then |
| 123 | rm -rf $tmp_wd |
| 124 | fi |
| 125 | } # end of put_upstream_pxelinux |
| 126 | # |
| 127 | put_distribution_pxelinux() { |
| 128 | # Function to use the package from distribution. |
| 129 | local possible_syslinux_pkgs req_syslinux_pkg_name syslinux_pkgs |
| 130 | # First we find pxelinux, then syslinux-common, then syslinux. The order is important. |
| 131 | possible_syslinux_pkgs="pxelinux syslinux-common syslinux-nonlinux syslinux" |
| 132 | # Package name: |
| 133 | # Debian: syslinux-common, for syslinux 6.x, sub-packages, pxelinux is available. |
| 134 | # Ubuntu: 10.04: syslinux, > 10.10: syslinux-common |
| 135 | # Fedora <=20: syslinux, >= 21: syslinux-nonlinux, by Ceasar |
| 136 | # Suse: syslinux |
| 137 | for i in $possible_syslinux_pkgs; do |
| 138 | if $query_pkglist_exist_cmd $i &>/dev/null; then |
| 139 | req_syslinux_pkg_name="$i" |
| 140 | break |
| 141 | fi |
| 142 | done |
| 143 | tmp_wd="$(LC_ALL=C mktemp -d /tmp/pxelinux_tmp.XXXXXXX)" |
| 144 | if [ -n "$req_syslinux_pkg_name" ]; then |
| 145 | # Found on the system |
| 146 | # The syslinux-common version on Debian might be like: 2:4.02+dfsg-7, to compare the version, we'd better to strip the "2:" |
| 147 | # syslinux_pkg_ver="$(LC_ALL=C dpkg-query -W -f='${Version}\n' $req_syslinux_pkg_name | sed -r -e "s/^[[:digit:]]*://g")" |
| 148 | # echo "Syslinux package version: $syslinux_pkg_ver" |
| 149 | # For Redhat-like system, there might return more than one file on a system, i.e. one file belongs to 2 rpm packages. |
| 150 | pxelinux_bin="$(LC_ALL=C $query_pkglist_cmd $req_syslinux_pkg_name | grep -Ew "pxelinux.0$" | sort | head -n 1)" |
| 151 | if [ -n "$pxelinux_bin" ]; then |
| 152 | pxelinux_v="$(LC_ALL=C strings $pxelinux_bin | grep "^PXELINUX")" |
| 153 | echo "Found $pxelinux_bin in this system:" |
| 154 | echo "PXELinux version: $pxelinux_v" |
| 155 | echo "Copying the PXELinux files to DRBL local repository..." |
| 156 | # For syslinux 6.x, on Debian another sub-package "pxelinux" from syslinux is separated, and pxelinux.0 is put in /usr/lib/PXELINUX/pxelinux.0, while those *.c32 moduels are in /usr/lib/syslinux/modules/{bios,efi32,efi64} |
| 157 | pxelinux_bin_dir="$(LC_ALL=C dirname $pxelinux_bin)" |
| 158 | |
| 159 | # This part works for both syslinux <= 5 and >=6 |
| 160 | # For syslinux >=5, collect and put pxelinux.0, lpxelinux.0, gpxelinux.0, and all *.c32 |
| 161 | # For syslinux >=6, collect and put only pxelinux.0, lpxelinux.0, and gpxelinux.0 because only these 3 files are under $pxelinux_bin_dir/ |
| 162 | for iprog in $DRBL_NEED_PXELINUX; do |
| 163 | find $pxelinux_bin_dir -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/ \; |
| 164 | done |
| 165 | put_pxelinux_bin_2_drbl_repo $tmp_wd $pxelinux_binsrc_dir/bios/ |
| 166 | rm -f $v_opt $tmp_wd/* |
| 167 | # Centos 7 only provides syslinux 4 which lacks lpxelinux.0. |
| 168 | # For compability with DRBL which requires syslinux >= v5, just link it to pxelinux.0 |
| 169 | if [ ! -e "$pxelinux_binsrc_dir/bios/lpxelinux.0" ]; then |
| 170 | (cd $pxelinux_binsrc_dir/bios/; ln -fs pxelinux.0 lpxelinux.0) |
| 171 | fi |
| 172 | |
| 173 | # This part works for syslinux >=6 |
| 174 | # Those *.c32, isolinux.bin, memdisk, and mbr.bin are copied in this part for syslinux >=6 |
| 175 | # The above find command only find /usr/lib/PXELINUX for syslinux >=6, but isolinux.bin and memdisk are required. It will search /usr/lib/syslinux if syslinux <=5 |
| 176 | if [ -d /usr/lib/ISOLINUX/ ]; then |
| 177 | find /usr/lib/ISOLINUX/ -name "isolinux.bin" -type f -exec \ |
| 178 | cp -af $v_opt {} $tmp_wd/ \; |
| 179 | fi |
| 180 | # When syslinux <=6, memdisk will be put by the above find command. No need to copy again. |
| 181 | if [ -d /usr/lib/syslinux/ -a ! -e $pxelinux_binsrc_dir/bios/memdisk ]; then |
| 182 | find /usr/lib/syslinux/ -name "memdisk" -type f -exec \ |
| 183 | cp -af $v_opt {} $tmp_wd/ \; |
| 184 | fi |
| 185 | for iarch in bios efi32 efi64; do |
| 186 | for iprog in $DRBL_NEED_PXELINUX; do |
| 187 | if [ -d $pxelinux_bin_dir/../syslinux/modules/$iarch/ ]; then |
| 188 | find $pxelinux_bin_dir/../syslinux/modules/$iarch/ -name "$iprog" \ |
| 189 | -type f -exec cp -af $v_opt {} $tmp_wd/ \; |
| 190 | fi |
| 191 | if [ "$iarch" = "bios" ]; then |
| 192 | # This part is only for mbr.bin of bios, not for efi32 or efi64. |
| 193 | # For Debian/Ubuntu, mbr.bin is located in /usr/lib/syslinux/mbr/mbr.bin. Therefore we need to find /usr/lib/syslinux/mbr/, too. |
| 194 | # For Fedora/Centos, mbr.bin is located in /usr/share/syslinux/ |
| 195 | # while since pxelinux_bin=/usr/share/syslinux/pxelinux.0 so pxelinux_bin_dir=/usr/share/syslinux |
| 196 | find $pxelinux_bin_dir/../syslinux/{,mbr/} -name "mbr.bin" \ |
| 197 | -type f -exec cp -af $v_opt {} $tmp_wd/ \; &>/dev/null |
| 198 | fi |
| 199 | done |
| 200 | if [ -n "$(ls $tmp_wd/* 2>/dev/null)" ]; then |
| 201 | put_pxelinux_bin_2_drbl_repo $tmp_wd $pxelinux_binsrc_dir/$iarch/ |
| 202 | fi |
| 203 | rm -f $v_opt $tmp_wd/* |
| 204 | done |
| 205 | # Some more extra files for efi32 and efi64 |
| 206 | if [ -d /usr/lib/SYSLINUX.EFI/ ]; then |
| 207 | find /usr/lib/SYSLINUX.EFI/efi32/ -name "syslinux.efi" -type f -exec \ |
| 208 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi32/ \; |
| 209 | find /usr/lib/SYSLINUX.EFI/efi64/ -name "syslinux.efi" -type f -exec \ |
| 210 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi64/ \; |
| 211 | fi |
| 212 | if [ -d /usr/lib/syslinux/modules/ ]; then |
| 213 | find /usr/lib/syslinux/modules/efi32/ -name "ldlinux.e32" -type f -exec \ |
| 214 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi32/ \; |
| 215 | find /usr/lib/syslinux/modules/efi64/ -name "ldlinux.e64" -type f -exec \ |
| 216 | install -o root -g root -m 644 {} $pxelinux_binsrc_dir/efi64/ \; |
| 217 | fi |
| 218 | else |
| 219 | [ "$BOOTUP" = "color" ] && $SETCOLOR_WARNING |
| 220 | echo "Package of pxelinux was installed, but pxelinux boot file not found!" |
| 221 | [ "$BOOTUP" = "color" ] && $SETCOLOR_NORMAL |
| 222 | fi |
| 223 | else |
| 224 | # Download and extract it. The reason we do not install it is we do not want to bother the server's boot menu |
| 225 | mkdir -p $tmp_wd/{extracted,upstream} |
| 226 | echo "Trying to download syslinux from distribution repository..." |
| 227 | if [ -e /etc/debian_version ]; then |
| 228 | # Debian |
| 229 | syslinux_pkgs="" |
| 230 | for ipkg in $possible_syslinux_pkgs; do |
| 231 | if [ -n "$(LC_ALL=C apt-cache show $ipkg 2>/dev/null)" ]; then |
| 232 | syslinux_pkgs="$syslinux_pkgs $ipkg" |
| 233 | fi |
| 234 | done |
| 235 | echo "Related syslinux package(s) on repository: $syslinux_pkgs" |
| 236 | # Clean all stale deb files in the cache dir. Otherwise there might be some old version of debs exist in the cache dir. |
| 237 | LC_ALL=C apt-get clean |
| 238 | LC_ALL=C apt-get -d --reinstall install $syslinux_pkgs |
| 239 | for i in $syslinux_pkgs; do |
| 240 | LC_ALL=C dpkg --extract /var/cache/apt/archives/${i}_*.deb $tmp_wd/upstream |
| 241 | done |
| 242 | for iprog in $DRBL_NEED_PXELINUX; do |
| 243 | find $tmp_wd/upstream -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted \; |
| 244 | done |
| 245 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted $pxelinux_binsrc_dir/bios/ |
| 246 | elif [ -e /etc/SuSE-release ]; then |
| 247 | # SuSE |
| 248 | # The binary for syslinux-common in openSuse 11.3 is: /boot/pxelinux.bin |
| 249 | # TODO: Clean cached files in /var/cache/zypp/packages/ |
| 250 | LC_ALL=C zypper install -d -f -y syslinux |
| 251 | pxelinux_rpm="$(LC_ALL=C find /var/cache/zypp/packages/ -name "syslinux-*.rpm" -print)" |
| 252 | ( |
| 253 | cd $tmp_wd/upstream |
| 254 | LC_ALL=C rpm2cpio "$pxelinux_rpm" | cpio -idm |
| 255 | ) |
| 256 | for iprog in $DRBL_NEED_PXELINUX; do |
| 257 | find $tmp_wd/upstream -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted \; |
| 258 | done |
| 259 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted $pxelinux_binsrc_dir/bios/ |
| 260 | else |
| 261 | # RH-like |
| 262 | LC_ALL=C yumdownloader --destdir $tmp_wd syslinux |
| 263 | ( |
| 264 | cd $tmp_wd/upstream |
| 265 | LC_ALL=C rpm2cpio $tmp_wd/syslinux*.rpm | cpio -idm |
| 266 | ) |
| 267 | for iprog in $DRBL_NEED_PXELINUX; do |
| 268 | find $tmp_wd/upstream -name "$iprog" -type f -exec cp -af $v_opt {} $tmp_wd/extracted \; |
| 269 | done |
| 270 | put_pxelinux_bin_2_drbl_repo $tmp_wd/extracted $pxelinux_binsrc_dir/bios/ |
| 271 | fi |
| 272 | fi |
| 273 | if [ -d "$tmp_wd" -a -n "$(LC_ALL=C echo "$tmp_wd" | grep -i pxelinux_tmp)" ]; then |
| 274 | rm -rf $tmp_wd |
| 275 | fi |
| 276 | } # end of put_distribution_pxelinux |
| 277 | |
| 278 | ############ |
| 279 | ### MAIN ### |
| 280 | ############ |
| 281 | |
| 282 | while [ $# -gt 0 ]; do |
| 283 | case "$1" in |
| 284 | -u|--from-upstream) shift; mode="from-upstream" ;; |
| 285 | -pv|--pxelinux-version) |
| 286 | shift; |
| 287 | if [ -z "$(echo $1 |grep ^-.)" ]; then |
| 288 | # skip the -xx option, in case |
| 289 | pxelinux_ver_requested="$1" |
| 290 | [ -z "$pxelinux_ver_requested" ] && USAGE && exit 1 |
| 291 | shift |
| 292 | fi |
| 293 | ;; |
| 294 | -d|--deploy-to-system-too) shift; deploy_to_system_too="yes" ;; |
| 295 | -v|--verbose) shift; verbose="yes" ;; |
| 296 | *) USAGE && exit 1 ;; |
| 297 | esac |
| 298 | done |
| 299 | # mode is essential |
| 300 | [ -z "$mode" ] && mode="from-distribution" |
| 301 | [ -z "$pxelinux_ver_requested" ] && pxelinux_ver_requested="$SYS_PXELINUX_VER_DEF" |
| 302 | |
| 303 | if [ "$verbose" = "yes" ]; then |
| 304 | v_opt="-v" |
| 305 | fi |
| 306 | |
| 307 | case "$mode" in |
| 308 | from-distribution) put_distribution_pxelinux ;; |
| 309 | from-upstream) put_upstream_pxelinux $pxelinux_ver_requested ;; |
| 310 | esac |
| 311 | |
| 312 | if [ "$deploy_to_system_too" = "yes" ]; then |
| 313 | echo "Putting required pxelinux files in DRBL system dir $pxecfg_pd... " |
| 314 | prepare-files-for-PXE-client -p |
| 315 | echo "done!" |
| 316 | fi |
| 317 | |
| 318 | exit 0 |