pacmanのミラーサーバーを切り替える

TL;DR

  • pacmanのアップデートが上手く行かなかった.
  • 原因は通信速度が遅すぎるため,とのこと.
  • しかしインターネット環境は速度ともに悪くはない.
  • pacmanのミラーサーバーを手動で切り替えることに.
  • 最速のミラーサーバーを調べるスクリプトを実行.
  • 最速のミラーサーバーに変更してアップデートすると上手く行った.
追記(Feb 11, 2017): この記録に書いてある対策を施して数日後,再びアップデートを試したところ同じ症状が発症. このRaspberry PiではHomebridgeが走っているので,Homebridgeを停止させてからアップデートをしたところ上手く行った. sudo systemctl stop homebridge sudo pacman -Syu どうやらHomebridgeが干渉して通信に支障をきたしていた模様. アップデート後, sudo systemctl stop homebridge で元通りに. ミラーサーバー側の切り替え不良という腑に落ちない"めでたし"であったが,これで心から"めでたし"と言える. 謎が解けて良かった.
Raspberry PiでArch Linuxを動かしている. 先日久しぶりに sudo pacman -Syu すると,
error: failed retrieving file 'パッケージ名.tar.xz' from mirror.archlinuxarm.org :
Operation too slow. Less than 1 bytes/sec transferred the last 10 seconds
とエラーを吐いた. 通信速度が遅すぎると怒っているのだ. 何度か試してみると,上手くアップグレード出来るパッケージと,やはり通信が遅くアップグレードに失敗するパッケージがある. 我が家の通信速度はそこまで低速ではないし,Raspberry Piからpingしてみた印象からも,手元の通信環境が悪いわけではなさそうだった. ということでパッケージを配信しているミラーサーバーを手動で変更してみることにした. 通常ミラーサーバーは私たち(受け手)のIPアドレスを元に,自動的に最適な地域のミラーサーバーへと通信を切り替えてくれる. しかし,何かの都合でそれが上手く行っていないのだろうと考え,物は試しと変更してみることにした. ミラーサーバーのリストは/etc/pacman.d/mirrorlistにある. 編集して壊してしまっては怖いのでまずはバックアップがてらコピーをとる. cd /etc/pacman.d/ sudo cp -p mirrorlist mirrorlist.bak コピーする際,-pオプションを使うことでファイルの属性(モード,所有者,タイムスタンプ)も同じままコピーを取ることができる.
これでもしmirrorlistを編集して動作がおかしくなった場合や初期設定へ戻したくなった場合は/etc/pacman.dディレクトリで, sudo cp -p mirrorlist.bak mirrorlist すれば元通りに戻るというわけ.
さて,mirrorlistの中身を見てみるとこんな感じになっている.

mirrorlist

#
# Arch Linux ARM repository mirrorlist
# Generated on 2016-10-21
#

## Geo-IP based mirror selection and load balancing
Server = http://mirror.archlinuxarm.org/$arch/$repo

### Mirrors by country

### Australia (not Austria!)
## Sydney
# Server = http://au.mirror.archlinuxarm.org/$arch/$repo

### Brazil
## Florianopolis
# Server = http://br.mirror.archlinuxarm.org/$arch/$repo
## Sao Paulo
# Server = http://br2.mirror.archlinuxarm.org/$arch/$repo

### Finland
## Tampere
# Server = http://fi.mirror.archlinuxarm.org/$arch/$repo

### France
## Ain
# Server = http://fr.mirror.archlinuxarm.org/$arch/$repo

...
このように国と地域毎にサーバーがある. コメントアウトされていないサーバーが今利用しているサーバーとなる. 国/地域ごとのミラーサーバーはデフォルトで選択されていない(コメントアウトされている). 今やりたいことはこのリストの中からレスポンスの最も速いサーバーを探して,それを選択(アンコメント)することである. どのサーバーからのレスポンスが最速かを調べるためには次のスクリプトを使うと良い. Raspberry Piフォーラムのfinaldutyさんがそのためのスクリプトをつくってくれた. それを使おう. ホームディレクトリかどこかにそのスクリプトを作ろう. cd ~ nano -w fastestmirror.sh そして次のスクリプトを全部コピペ.

fastestmirror.sh

#####
## fastestmirror.sh
## finalduty 19/12/12
##
## This script is used to test for the fastest ping to ALARMPI mirrors.
## You can also have it automatically write to your mirrorlist
#####

#!/bin/bash

## Creates .tmp file if it doesn't exist and writes a blank line into it. (Useful if the script is cancelled before the rm function)
touch .massping.tmp
echo -n > .massping.tmp

## Collect the mirrors from /etc/pacman.d/mirrorlist, strip them and enter them into the $mirrors array
mirrors=$(cat /etc/pacman.d/mirrorlist | grep "Server =" | sed 's/^.*\=//')

## Loop through the array values and query them and ping them.
for i in ${mirrors[@]}; do

        ## Show > symbol to indicate progress and write the URL to temporary file without a newline
        echo -n ">" && echo -n $i >> .massping.tmp

        ## Ping the actual server, retrieve the average from the last line, strip it and write to the temporary file
        ping -fc5 $(echo $i | awk -F '/' '{print $3}') | tail -1| awk -F '/' '{print " " $5 " ms"}' >> .massping.tmp
done

## Set variable $fastest by sorting the temp file based off numerical pings, removes blanks/timeouts and returns the top value.
fastest=$(sort -nk2 .massping.tmp | grep -v "repo  ms" | head -1)

## Prints to screen the fastest average ping in green and clears color formatting, ready for next output
echo && echo -e "\e[0;32m"$fastest && tput sgr0

## Sorts the temporary file and outputs all but the fastest result (because it has already been printed in colour)
sort -nk2 .massping.tmp | grep -v "repo  ms" | tail -n+2

## Tidy up - Remove Temporary File
rm .massping.tmp

## Strip the ping time from the $fastest variable so we can search for it in the mirrors list
fast=$(echo $fastest | sed 's/ .*$//')


# WARNING - UNCOMMENTING BELOW THIS LINE WILL CAUSE CHANGES TO BE WRITTEN TO YOUR MIRRORLIST
# PLEASE MAKE SURE THAT YOU UNDERSTAND THE SCRIPT BEFORE ALLOWING THIS.
# USE AT YOUR OWN RISK AND MAKE SURE YOU TAKE A BACKUP. I WILL NOT CONSOLE YOU WHEN YOU CRY

## Comments all uncommented "Server =" entries in /etc/pacman.d/mirrorlist
#sed -i 's/^.*Server/\# Server/' /etc/pacman.d/mirrorlist

## Uncomments only the fastest server in /etc/pacman.d/mirrorlist
#sed -i "s|^# Server = $fast|Server = $fast|" /etc/pacman.d/mirrorlist && echo && echo "Server "$fast" has been set as the default mirror"
1番最後の行をアンコメントすると,最速と判断したサーバーを利用するようにmirrorlistを自動更新してくれる. 手動でこの作業を行いたい場合はスクリプトを上のまま保存し,ページ下方"手動でmirrorlistを設定する"の章を参照してほしい. ちなみにnanoの場合,保存はCtrl+Xの次にyを押しEnterする. sudo sh fastestmirror.sh でスクリプトの実行. 少し待つと,
>>>>>>>>>>>>>>>
http://tw.mirror.archlinuxarm.org/\$arch/\$repo 58.390 ms
http://vn.mirror.archlinuxarm.org/\$arch/\$repo 88.806 ms
http://sg.mirror.archlinuxarm.org/\$arch/\$repo 89.091 ms
http://au.mirror.archlinuxarm.org/\$arch/\$repo 135.683 ms
http://co.us.mirror.archlinuxarm.org/\$arch/\$repo 156.477 ms
http://ca.us.mirror.archlinuxarm.org/\$arch/\$repo 172.268 ms
http://mi.us.mirror.archlinuxarm.org/\$arch/\$repo 179.074 ms
http://mirror.archlinuxarm.org/\$arch/\$repo 187.184 ms
http://fl.us.mirror.archlinuxarm.org/\$arch/\$repo 190.149 ms
...
のような結果を得られる. 見ると私の場合tw.mirror.archlinuxarm.org (tw: 台湾) が最速らしい.

手動でmirrorlistを設定する

早速mirrorlistを編集しよう. sudo nano /etc/pacman.d/mirrorlist まず今までのサーバーをコメントアウト(実はコメントアウトしなくても変数Serverが上書きされるだけなので問題ないが,気分的にコメントアウトした)

mirrorlist

## Geo-IP based mirror selection and load balancing
Server = http://mirror.archlinuxarm.org/\$arch/\$repo

↓ 次のようにコメントアウト

## Geo-IP based mirror selection and load balancing
# Server = http://mirror.archlinuxarm.org/\$arch/\$repo
そして最速だったサーバーをアンコメント

mirrorlist

### Taiwan
## New Taipei City
# Server = http://tw.mirror.archlinuxarm.org/\$arch/\$repo

↓ 次のようにアンコメント

### Taiwan
## New Taipei City
Server = http://tw.mirror.archlinuxarm.org/\$arch/\$repo
Ctrl+X,y,Enterで保存.
これで, sudo pacman -Syu してもエラーを吐かずにアップグレードは完了した. ミラーサーバーの切り替えが上手く行かなかったことが原因だと思い込んで,めでたし,ということにしよう(追記: 原因はHomebridgeの干渉であったことが後日判明,その旨をページトップに追記しました).
5322305131445065490 https://www.storange.jp/2017/02/pacman.html https://www.storange.jp/2017/02/pacman.html pacmanのミラーサーバーを切り替える 2017-02-10T12:00:00+09:00 https://www.storange.jp/2017/02/pacman.html Hideyuki Tabata 200 200 72 72