| 1 | #!/bin/bash |
| 2 | # Author: Steven Shiau <steven _at_ clonezilla org> |
| 3 | # License: GPL |
| 4 | # Description: Find the url in yum repository setting. |
| 5 | # parameter: yum_repo_set releasever ARCH |
| 6 | # if the setting in "yum_repo_set" is like: |
| 7 | # baseurl=http://free.nchc.org.tw//fedora/linux/core//$releasever/$basearch/os |
| 8 | # then "releasever" and "ARCH" you assign are not important. But it's really |
| 9 | # important when the setting in "yum_repo_set" is like: |
| 10 | # baseurl=http://free.nchc.org.tw//fedora/linux/core/4/i386/os |
| 11 | |
| 12 | # Ex: find-url-in-yum-set /etc/yum.repos.d/fedora-core.repo 5 i386 2>/dev/null |
| 13 | |
| 14 | yum_repo_set="$1" |
| 15 | # TODO! maybe find a method to get this from yum ? |
| 16 | releasever="$2" |
| 17 | ARCH="$3" |
| 18 | |
| 19 | [ -f "$yum_repo_set" ] || exit 1 |
| 20 | [ -n "$releasever" ] || exit 1 |
| 21 | [ -n "$ARCH" ] || exit 1 |
| 22 | |
| 23 | get_yum_block() { |
| 24 | local CFG_FILE=$1 |
| 25 | local block="$2" |
| 26 | [ -f "$CFG_FILE" ] || exit 1 |
| 27 | # By using |
| 28 | # grep -n "^\[.*\]" yum.conf |grep -A1 "\[base\]" |
| 29 | # We can get the results like: |
| 30 | # 175:[base] |
| 31 | # 210:[update] |
| 32 | # so we know we can replace the one between line no. 175 and 210 |
| 33 | between_lines=$(grep -ni "^\[.*\]" $CFG_FILE |grep -i -A1 "\[$block\]" | cut -d":" -f1) |
| 34 | begin_line=$(echo $between_lines | awk -F" " '{print $1}') |
| 35 | end_line=$(echo $between_lines | awk -F" " '{print $2}') |
| 36 | # if end_line is nothing, it must be the last block, i.e. we can not find the next [.*] |
| 37 | if [ -z "$end_line" ]; then |
| 38 | end_line=$(wc -l $CFG_FILE | awk -F" " '{print $1}') |
| 39 | else |
| 40 | # if not nothing, backword one line |
| 41 | end_line=$(($end_line - 1)) |
| 42 | fi |
| 43 | echo "$begin_line $end_line" |
| 44 | } |
| 45 | |
| 46 | # |
| 47 | convert_url_os_path() { |
| 48 | local url="$1" |
| 49 | [ -n "$url" ] || exit 1 |
| 50 | # For FC: |
| 51 | # baseurl=http://free.nchc.org.tw//fedora/linux/core//$releasever/$basearch/os |
| 52 | # we want to get: |
| 53 | # url_os=http://free.nchc.org.tw/ |
| 54 | # os_ayo_path=fedora/linux/core/ |
| 55 | # For OpenSuSE 10.0 |
| 56 | # baseurl=http://free.nchc.org.tw/opensuse/opensuse/distribution/SL-$releasever-OSS/inst-source/suse/ |
| 57 | # we want to get: |
| 58 | # url_os=http://free.nchc.org.tw |
| 59 | # os_ayo_path=opensuse/opensuse/distribution |
| 60 | |
| 61 | # Note! Maybe in *.repo, releasever is shown as "$releasever", "4" or "10.0" |
| 62 | # Case 1: releasever is shown as "$releasever" |
| 63 | url_os_coreroot="$(echo $baseurl | sed -r -e 's@/(SL-|)*\$releasever(-OSS|)*/.*@@g')" |
| 64 | # Case 2: releasever is shown as "10.0". This is really a special case for OpenSuSE 10.0 and 10.1 only. |
| 65 | # Ex: http://opensource.nchc.org.tw/opensuse/distribution/SL-10.0-OSS/ |
| 66 | # or http://opensource.nchc.org.tw/opensuse/distribution/SL-10.1/ |
| 67 | # From OpenSuSE 10.2 or later, it's clean as: http://opensource.nchc.org.tw/opensuse/distribution/10.2/ |
| 68 | if [ -n "$(echo $url_os_coreroot | grep "SL-")" ]; then |
| 69 | url_os_coreroot="$(echo $url_os_coreroot | sed -r -e "s@/(SL-|)*$releasever(-OSS|)*/.*@@g")" |
| 70 | fi |
| 71 | # Case 3: releasever is shown as "8", for example. |
| 72 | # baseurl=http://free.nchc.org.tw/fedora/linux/releases/8/Everything/i386/os/ |
| 73 | url_os_coreroot="$(echo $url_os_coreroot | sed -r -e "s@/$releasever/.*@@g")" |
| 74 | |
| 75 | # |
| 76 | os_ayo_path="$(echo $url_os_coreroot | sed -re "s|(http\|ftp\|file)://[^/]*/||g")" |
| 77 | url_os="$(echo $url_os_coreroot | sed -re "s|$os_ayo_path||g")" |
| 78 | # remove the last / in $url_os |
| 79 | url_os="$(echo $url_os | sed -re "s|/$||g")" |
| 80 | # strip the /$releasever/$ARCH/os (http://opensource.nchc.org.tw/fedora/linux/core/5/i386/os/) or /$release/os/$ARCH (like CentOS: http://ftp.ncnu.edu.tw//Linux/CentOS/4.3/os/i386/) if found... just in case |
| 81 | os_ayo_path="$(echo $os_ayo_path | sed -re "s|/$releasever\.*[[:digit:]]*/(os/)*$ARCH/.*||g")" |
| 82 | echo $url_os $os_ayo_path |
| 83 | } |
| 84 | |
| 85 | # for yum |
| 86 | # For FC1-4, the name in yum repository config: "base", but in FC5, it's "core". |
| 87 | # We append update and updates, it's very important to keep this order (core base update updates, do NOT put update(s) earlier). |
| 88 | # For FC7, since core and extra are merged, now the block is [fedora]. |
| 89 | for ipart in fedora updates update core base; do |
| 90 | lines="$(get_yum_block $yum_repo_set $ipart)" |
| 91 | begin_line=$(echo $lines | awk -F" " '{print $1}') |
| 92 | end_line=$(echo $lines | awk -F" " '{print $2}') |
| 93 | [ -n "$begin_line" -a -n "$end_line" ] && break |
| 94 | done |
| 95 | |
| 96 | chk_cmd="if ($begin_line..$end_line) {print}" |
| 97 | baseurl="$(perl -n -e "$chk_cmd" $yum_repo_set | grep -Ei "^[[:space:]]*baseurl[[:space:]]*=[[:space:]]*" | sed -re "s/^[[:space:]]*baseurl[[:space:]]*=[[:space:]]*//gi")" |
| 98 | if [ -n "$baseurl" ]; then |
| 99 | # case 1, baseurl exists |
| 100 | convert_url_os_path $baseurl |
| 101 | else |
| 102 | # try to find mirrorlist |
| 103 | # Fedora 21,22 use 'metalink' not 'mirrorlist' |
| 104 | mirrorlist="$(LC_ALL=C perl -n -e "$chk_cmd" $yum_repo_set | grep -Ei "^[[:space:]]*(mirrorlist|metalink)=" | sed -re "s/^[[:space:]]*(mirrorlist|metalink)=//gi")" |
| 105 | |
| 106 | # eval mirrorlist=$mirrorlist |
| 107 | # Note! eval mirrorlist=$mirrorlist fails in CentOS 4.3, since there are "=" in the variable, |
| 108 | # For example: |
| 109 | # releasever=4 |
| 110 | # basearch=i386 |
| 111 | # mirrorlist='http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os' |
| 112 | # eval 'mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os' |
| 113 | # mirrorlist='http://mirrorlist.centos.org/?release=4' |
| 114 | # arch=i386 |
| 115 | # repo=os |
| 116 | # Hence we have to use sed to replace the variables. |
| 117 | mirrorlist="$(echo $mirrorlist | sed -e "s/\$releasever/$releasever/g" -e "s/\$basearch/$ARCH/g")" |
| 118 | # Use wget, lynx , lftp or curl ? wget is almost installed... |
| 119 | # 2006/06/24, use lftp or curl since they are better than lynx, and support |
| 120 | # http_proxy, ftp_proxy. |
| 121 | # But... In CentOS 4.3, mirrorlist='http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os', then lftp will fail. |
| 122 | # The best solution maybe is curl, since it supports environment proxy. |
| 123 | # A. wget |
| 124 | #tmp_dir="$(mktemp -d /tmp/yummirror_tmp.XXXXXX)" |
| 125 | #wget -r -l1 --no-parent --passive-ftp -e robots=off -q -nd --retr-symlinks -P "$tmp_dir" $mirrorlist |
| 126 | #mirror_file=`basename $mirrorlist` |
| 127 | #baseurl="$(head -n 1 $tmp_dir/$mirror_file)" |
| 128 | #[ -d "$tmp_dir" ] && rm -rf $tmp_dir |
| 129 | # B. lynx |
| 130 | #baseurl="$(lynx -dump $mirrorlist | head -n1)" |
| 131 | # C. lftp |
| 132 | #mirrorlist_url="$(dirname $mirrorlist)" |
| 133 | #mirrorlist_file="$(basename $mirrorlist)" |
| 134 | #baseurl="$(echo cat $mirrorlist_file | lftp $mirrorlist_url | head -n1)" |
| 135 | # E. curl |
| 136 | mlist_tmp="$(mktemp /tmp/mirrorlst_tmp.XXXXXX)" |
| 137 | curl $mirrorlist 2>/dev/null > $mlist_tmp |
| 138 | if [ -n "$(grep -E "xml version" $mlist_tmp)" ]; then |
| 139 | baseurl="$(LC_ALL=C curl $mirrorlist 2>/dev/null | grep -vE "^[[:space:]]*#" | head -n1)" |
| 140 | # From Fedora 12, the repository list got from mirrorlist is XML data forma. |
| 141 | # Like: |
| 142 | # <url protocol="ftp" type="ftp" location="TW" preference="100">ftp://opensource.nchc.org.tw/distributions/Fedora/linux/releases/12/Everything/i386/os/repodata/repomd.xml</url> |
| 143 | # <url protocol="http" type="http" location="TW" preference="100">http://opensource.nchc.org.tw/fedora/linux/releases/12/Everything/i386/os/repodata/repomd.xml</url> |
| 144 | baseurl="$(LC_ALL=C grep -E "url protocol=" $mlist_tmp | head -n 1 | grep -Eo "(http://|ftp://).*" | sed -e "s/<\/url>//g")" |
| 145 | else |
| 146 | # Old format |
| 147 | baseurl="$(LC_ALL=C grep -vE "^[[:space:]]*#" $mlist_tmp | head -n1)" |
| 148 | fi |
| 149 | [ -e "$mlist_tmp" ] && rm -f $mlist_tmp |
| 150 | |
| 151 | convert_url_os_path $baseurl |
| 152 | fi |