From 212b8b77ce43d79f8309b780b520c2705661a91d Mon Sep 17 00:00:00 2001 From: natemaia Date: Sun, 17 May 2020 23:29:14 -0700 Subject: [PATCH] Make mirror country selection standard, tidy up and organize --- installer | 249 ++++++++++++++++++++++++++++++++++++------------------ 1 file changed, 166 insertions(+), 83 deletions(-) diff --git a/installer b/installer index d507e87..8cb3c02 100755 --- a/installer +++ b/installer @@ -6,7 +6,7 @@ # AIF, Calamares, and the Arch Wiki.. Credit where credit is due # shellcheck disable=SC2086,SC2046,SC2254,SC2164 -VER=2.1.50 +VER=2.1.51 # default values { @@ -293,6 +293,73 @@ declare -A PKG_EXT=( [zathura]='zathura-pdf-poppler' ) # } +# mirrorlist country codes to names table { +typeset -A COUNTRIES=( +[AU]="Australia" +[AT]="Austria" +[BD]="Bangladesh" +[BY]="Belarus" +[BE]="Belgium" +[BA]="Bosnia and Herzegovina" +[BR]="Brazil" +[BG]="Bulgaria" +[CA]="Canada" +[CL]="Chile" +[CN]="China" +[CO]="Colombia" +[HR]="Croatia" +[CZ]="Czechia" +[DK]="Denmark" +[EC]="Ecuador" +[FI]="Finland" +[FR]="France" +[GE]="Georgia" +[DE]="Germany" +[GR]="Greece" +[HK]="Hong Kong" +[HU]="Hungary" +[IS]="Iceland" +[IN]="India" +[ID]="Indonesia" +[IR]="Iran" +[IE]="Ireland" +[IL]="Israel" +[IT]="Italy" +[JP]="Japan" +[KZ]="Kazakhstan" +[KE]="Kenya" +[LV]="Latvia" +[LT]="Lithuania" +[LU]="Luxembourg" +[NL]="Netherlands" +[NC]="New Caledonia" +[NZ]="New Zealand" +[MK]="North Macedonia" +[NO]="Norway" +[PY]="Paraguay" +[PH]="Philippines" +[PL]="Poland" +[PT]="Portugal" +[RO]="Romania" +[RU]="Russia" +[RS]="Serbia" +[SG]="Singapore" +[SK]="Slovakia" +[SI]="Slovenia" +[ZA]="South Africa" +[KR]="South Korea" +[ES]="Spain" +[SE]="Sweden" +[CH]="Switzerland" +[TW]="Taiwan" +[TH]="Thailand" +[TR]="Turkey" +[UA]="Ukraine" +[GB]="United Kingdom" +[US]="United States" +[VN]="Vietnam" +) # } + # dialog text variables { # Basics (somewhat in order) _keymap="\nSelect which keymap to use from the list below.\n\nThis will determine the installed system keymap, NOT locale which is chosen later.\n\nSystem default: us" @@ -310,7 +377,6 @@ _format="is already formatted correctly.\n\nFor a clean install, existing partit _swapsize="\nEnter the size of the swapfile in megabytes (M) or gigabytes (G).\n\ne.g. 100M will create a 100 megabyte swapfile, while 10G will create a 10 gigabyte swapfile.\n\nFor ease of use and as an example it is filled in to match the size of your system memory (RAM).\n\nMust be greater than 1, contain only whole numbers, and end with either M or G." _expart="\nYou can now choose any additional partitions you want mounted, you'll be asked for a mountpoint after.\n\nSelect 'done' to finish the mounting step and begin unpacking the base system in the background." _exmnt="\nWhere do you want the partition mounted?\n\nEnsure the name begins with a slash (/).\nExamples include: /usr, /home, /var, etc." -_mirrors="\nAutomatic mirror selection using reflector is preferred and should yield fast mirrors, however\nyou may want to select which countries mirrors to use for faster sorting and more control\n\nUse automatic mirrors?\n" _bginstall="\nThe background base install will now start, select which base to use\n\nPacstrap - downloads all packages, avoids issues but can be slower depending on your network.\n\nCopy ISO - faster and not network dependant but can result in installation issues when using an older ISO.\n" _user="\nEnter a name and password for the new user account.\n\nThe name must not use capital letters, contain any periods (.), end with a hyphen (-), or include any colons (:)\n\nNOTE: Use [Up], [Down], or [Tab] to switch between fields, and [Enter] to accept." _hostname="\nEnter a hostname for the new system.\n\nA hostname is used to identify systems on the network.\n\nIt's restricted to alphanumeric characters (a-z, A-Z, 0-9).\nIt can contain hyphens (-) BUT NOT at the beginning or end." @@ -842,6 +908,57 @@ select_packages() return 0 } +select_mirror_country() +{ + codes='' + MIRROR_URL='' + typeset -ga MIRROR_COUNTRY + + while :; do + dlg codes check "Mirror Countries" "\nSelect which countries to use mirrors from.\n\nNot choosing any will result in an automatic selection." \ + AU Australia "$(ofn AU "$codes")" AT Austria "$(ofn AT "$codes")" \ + BA "Bosnia Herzegovina" "$(ofn BA "$codes")" BY Belarus "$(ofn BY "$codes")" \ + BE Belgium "$(ofn BE "$codes")" BR Brazil "$(ofn BR "$codes")" \ + BG Bulgaria "$(ofn BG "$codes")" CA Canada "$(ofn CA "$codes")" \ + CL Chile "$(ofn CL "$codes")" CN China "$(ofn CN "$codes")" \ + CO Colombia "$(ofn CO "$codes")" CZ "Czech Republic" "$(ofn CZ "$codes")" \ + DK Denmark "$(ofn DK "$codes")" EE Estonia "$(ofn EE "$codes")" \ + FI Finland "$(ofn FI "$codes")" FR France "$(ofn FR "$codes")" \ + DE Germany "$(ofn DE "$codes")" GB "United Kingdom" "$(ofn GB "$codes")" \ + GR Greece "$(ofn GR "$codes")" HU Hungary "$(ofn HU "$codes")" \ + IN India "$(ofn IN "$codes")" IE Ireland "$(ofn IE "$codes")" \ + IL Israel "$(ofn IL "$codes")" IT Italy "$(ofn IT "$codes")" \ + JP Japan "$(ofn JP "$codes")" KZ Kazakhstan "$(ofn KZ "$codes")" \ + KR Korea "$(ofn KR "$codes")" LT Lithuania "$(ofn LT "$codes")" \ + LV Latvia "$(ofn LV "$codes")" LU Luxembourg "$(ofn LU "$codes")" \ + MK Macedonia "$(ofn MK "$codes")" NL Netherlands "$(ofn NL "$codes")" \ + NC "New Caledonia" "$(ofn NC "$codes")" NZ "New Zealand" "$(ofn NZ "$codes")" \ + NO Norway "$(ofn NO "$codes")" PL Poland "$(ofn PL "$codes")" \ + PT Portugal "$(ofn PT "$codes")" RO Romania "$(ofn RO "$codes")" \ + RU Russia "$(ofn RU "$codes")" RS Serbia "$(ofn RS "$codes")" \ + SG Singapore "$(ofn SG "$codes")" SK Slovakia "$(ofn SK "$codes")" \ + ZA "South Africa" "$(ofn ZA "$codes")" ES Spain "$(ofn ES "$codes")" \ + LK "Sri Lanka" "$(ofn LK "$codes")" SE Sweden "$(ofn SE "$codes")" \ + CH Switzerland "$(ofn CH "$codes")" TW Taiwan "$(ofn TW "$codes")" \ + TR Turkey "$(ofn TR "$codes")" UA Ukraine "$(ofn UA "$codes")" \ + US "United States" "$(ofn US "$codes")" UZ Uzbekistan "$(ofn UZ "$codes")" \ + VN Vietnam "$(ofn VN "$codes")" || return 1 + + yesno "Mirror Countries" "\nConfirm the following countries: $(for i in $codes; do printf "%s, " "${COUNTRIES[$i]}"; done)" && break + done + + for i in $codes; do + if [[ $MIRROR_URL ]]; then + MIRROR_URL+="&country=$i" + else + MIRROR_URL="https://www.archlinux.org/mirrorlist/?country=$i" + fi + MIRROR_COUNTRY+=("--country" "${COUNTRIES[$i]}") + done + MIRROR_URL+='&use_mirror_status=on' + return 0 +} + ############################################################################### # device management menus # acts as an in-between function to avoid cluttering the main menu @@ -2058,14 +2175,47 @@ install_mkinitcpio() install_mirrorlist() { - if [[ $AUTO_MIR ]]; then - timedatectl set-ntp 1 && timedatectl > /dev/null 2>&1 - reflector --verbose --score 80 -l 40 -f 5 --sort rate --save /etc/pacman.d/mirrorlist + local url='' country='' ip_add='' + local key="access_key=5f29642060ab983b31fdf4c2935d8c56" + + # avoid SSL errors when the time is wrong + timedatectl set-ntp 1 && timedatectl > /dev/null 2>&1 + + [[ -f /etc/pacman.d/mirrorlist.bak ]] || cp -f /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.bak + + if hash reflector > /dev/null 2>&1; then + if [[ $AUTO_MIRROR ]]; then + reflector --verbose --connection-timeout 2 --threads 10 \ + --latest 150 --age 24 --score 75 --sort rate --fastest 6 --save /etc/pacman.d/mirrorlist + else + reflector --verbose --connection-timeout 2 --threads 10 "${MIRROR_COUNTRY[@]}" \ + --latest 100 --age 24 --score 50 --sort rate --fastest 6 --save /etc/pacman.d/mirrorlist + fi else - echo "ranking mirrorlist, this may take a while..." - cp -f /etc/pacman.d/mirrorlist /tmp/mirrors - rankmirrors -n 6 /tmp/mirrors > /etc/pacman.d/mirrorlist + echo "Ranking mirrorlist, This may take a while..." + if [[ $AUTO_MIRROR ]]; then + ip_add="$(curl -fsSL "http://api.ipstack.com/check&?$key&fields=ip" | + python -c "import sys, json; print(json.load(sys.stdin)['ip'])")" + country="$(curl -fsSL "http://api.ipstack.com/$ip_add?$key&fields=country_code" | + python -c "import sys, json; print(json.load(sys.stdin)['country_code'])")" + + if [[ $country && "${!COUNTRIES[*]}" =~ $country ]]; then + if [[ $country =~ (CA|US) ]]; then + url="https://www.archlinux.org/mirrorlist/?country=US&country=CA&use_mirror_status=on" + elif [[ $country =~ (AU|NZ) ]]; then + url="https://www.archlinux.org/mirrorlist/?country=AU&country=NZ&use_mirror_status=on" + else + url="https://www.archlinux.org/mirrorlist/?country=${country}&use_mirror_status=on" + fi + else # no country code so just grab all mirrors, will be a very slow sort but we don't have other options + url="https://www.archlinux.org/mirrorlist/?country=all&use_mirror_status=on" + fi + else + url="$MIRROR_URL" + fi + curl -fsSL "$url" | sed -e 's/^#Server/Server/' -e '/^#/d' | rankmirrors -n 6 - > /etc/pacman.d/mirrorlist fi + chmod +r /etc/pacman.d/mirrorlist } install_background() @@ -2075,16 +2225,12 @@ install_background() [[ ! -d /etc/NetworkManager/system-connections ]] && net='netctl' [[ $LUKS ]] && luks='cryptsetup' - if hash reflector > /dev/null 2>&1 && yesno 'Mirrorlist' "$_mirrors"; then - AUTO_MIR=true - else - AUTO_MIR='' - install_mirror_country - fi + install_mirror_country || AUTO_MIRROR=true - if hash rsync > /dev/null 2>&1; then + if hash rsync > /dev/null 2>&1; then # ask whether to pacstrap or copy if rsync is installed yesno 'Background Install' "$_bginstall" 'Pacstrap' 'Copy ISO' || PACSTRAP=0 fi + ( install_mirrorlist > /tmp/bgout 2>&1 @@ -2122,69 +2268,6 @@ install_background() trap "kill $BG_PID 2> /dev/null" EXIT } -install_mirror_country() -{ - local conf='' tmp='' - - until [[ $conf ]]; do - local url='' - dlg CODES check "Mirror Country" "\nSelect which countries to use for mirrors from the list below." \ - AU Australia "$(ofn AU "$CODES")" AT Austria "$(ofn AT "$CODES")" \ - BA "Bosnia Herzegovina" "$(ofn BA "$CODES")" BY Belarus "$(ofn BY "$CODES")" \ - BE Belgium "$(ofn BE "$CODES")" BR Brazil "$(ofn BR "$CODES")" \ - BG Bulgaria "$(ofn BG "$CODES")" CA Canada "$(ofn CA "$CODES")" \ - CL Chile "$(ofn CL "$CODES")" CN China "$(ofn CN "$CODES")" \ - CO Colombia "$(ofn CO "$CODES")" CZ "Czech Republic" "$(ofn CZ "$CODES")" \ - DK Denmark "$(ofn DK "$CODES")" EE Estonia "$(ofn EE "$CODES")" \ - FI Finland "$(ofn FI "$CODES")" FR France "$(ofn FR "$CODES")" \ - DE Germany "$(ofn DE "$CODES")" GB "United Kingdom" "$(ofn GB "$CODES")" \ - GR Greece "$(ofn GR "$CODES")" HU Hungary "$(ofn HU "$CODES")" \ - IN India "$(ofn IN "$CODES")" IE Ireland "$(ofn IE "$CODES")" \ - IL Israel "$(ofn IL "$CODES")" IT Italy "$(ofn IT "$CODES")" \ - JP Japan "$(ofn JP "$CODES")" KZ Kazakhstan "$(ofn KZ "$CODES")" \ - KR Korea "$(ofn KR "$CODES")" LT Lithuania "$(ofn LT "$CODES")" \ - LV Latvia "$(ofn LV "$CODES")" LU Luxembourg "$(ofn LU "$CODES")" \ - MK Macedonia "$(ofn MK "$CODES")" NL Netherlands "$(ofn NL "$CODES")" \ - NC "New Caledonia" "$(ofn NC "$CODES")" NZ "New Zealand" "$(ofn NZ "$CODES")" \ - NO Norway "$(ofn NO "$CODES")" PL Poland "$(ofn PL "$CODES")" \ - PT Portugal "$(ofn PT "$CODES")" RO Romania "$(ofn RO "$CODES")" \ - RU Russia "$(ofn RU "$CODES")" RS Serbia "$(ofn RS "$CODES")" \ - SG Singapore "$(ofn SG "$CODES")" SK Slovakia "$(ofn SK "$CODES")" \ - ZA "South Africa" "$(ofn ZA "$CODES")" ES Spain "$(ofn ES "$CODES")" \ - LK "Sri Lanka" "$(ofn LK "$CODES")" SE Sweden "$(ofn SE "$CODES")" \ - CH Switzerland "$(ofn CH "$CODES")" TW Taiwan "$(ofn TW "$CODES")" \ - TR Turkey "$(ofn TR "$CODES")" UA Ukraine "$(ofn UA "$CODES")" \ - US "United States" "$(ofn US "$CODES")" UZ Uzbekistan "$(ofn UZ "$CODES")" \ - VN Vietnam "$(ofn VN "$CODES")" || return 1 - - # build the url to use - for i in $CODES; do - if [[ $url ]]; then - url+="&country=$i" - else - url="https://www.archlinux.org/mirrorlist/?country=$i" - fi - done - url+="&use_mirror_status=on" - - msg "Mirror Country" "\nDownloading the latest mirrorlist for the chosen countries.\n\n$url\n" 0 - tmp="$(mktemp --suffix=-mirrorlist)" - curl -fsSL $url -o $tmp 2> "$ERR" - errshow 0 "curl -fsSL $url -o $tmp" || return 1 - sed -i 's/^#Server/Server/g' "$tmp" - - # let the user edit and confirm it - ${EDITOR:-vim} "$tmp" - if yesno "Mirror Country" "\nUse the newly created mirrorlist for installation?\n"; then - conf=true - else - rm -f "$tmp" - fi - done - cp -f "$tmp" /etc/pacman.d/mirrorlist - chmod +r /etc/pacman.d/mirrorlist -} - ############################################################################### # display manager config # these are called based on which DM is chosen after it is installed @@ -2900,7 +2983,8 @@ die() { # exit cleanly with exit code $1 or the last command's exit code # when $1 == 127 we unmount and reboot - local e="${1:-$?}" + local e="$1" + e="${e:-$?}" trap - INT tput cnorm @@ -3155,11 +3239,10 @@ errshow() local fatal=$1 shift 1 # always shift off the fatal level arg + local cmd="$1" - if [[ $1 == *chrun* ]]; then - local cmd="arch-chroot $MNT bash -c ${1##chrun }" - else - local cmd="$1" + if [[ $cmd == *chrun* ]]; then + local cmd="arch-chroot $MNT bash -c ${cmd##chrun }" fi local txt