Raspberry Pi 3でyumが使えなくなる問題

どうも。
Raspberry Pi 3にCentOSを入れたもののyumが使えなくなり、3回も初期化した者です。
いやぁー、ラズパイにものすごく悪戦苦闘していますが、
ネットワーク関係に関する知識が非常に身につきます。

今回発生した問題として、yumでいろいろとやっていたら、

# yum list
Loaded plugins:fastestmirror
http://mirror.centos.org/altarch/7-2.1511.el7.centos.0.4/os/armhfp/repodata/repomd.xml:
[Errno14] curl#6 - "Could not resolve host: mirrorlist.centos.org; Name or service not known"
Trying other mirror.

One of the configured repositories failed (CentOS-7-2.1511.el7.centos.0.4 - Base),
and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:

    1. Contact the upstream for the repository and get them to fix the problem.

    2. Reconfigure the baseurl/etc. for the repository, to point to a working
       upstream. This is most often useful if you are using a newer
       distribution release than is supported by the repository (and the
       packages for the previous distribution release still work).

    3. Disable the repository, so yum won't use it by default. Yum will then
       just ignore the repository until you permanently enable it again or use
       --enablerepo for temporary usage:

           yum-config-manager --disable <repoid>

    4. Configure the failing repository to be skipped, if it is unavailable.
       Note that yum will try to contact the repo. when it runs most commands,
       so will have to try and fail each time (and thus. yum will be be much
       slower). If it is a very temporary problem though, this is often a nice
       compromise:

           yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

failure: repodata/repomd.xml from base: [Errno 256] No more mirrors to try.
[Errno14] curl#6 - "Could not resolve host: mirrorlist.centos.org; Name or service not known"

というエラーが出るようになってしまいました。


そこで今回、最も苦しんだyumに関することを今後も忘れない、
かつyumの仕組みを理解するために記事にしたいと思います。

環境

Raspberry Pi 3
CentOS 7.2

そもそもyumとは…

RedHatディストリビューションのパッケージ管理システムの事です。
リポジトリと呼ばれるパッケージ置き場からのパッケージをダウンロードし、インストール、アンインストール、更新をする事ができます。
その際、各パッケージの依存関係を自動的に解決し、関連するパッケージがどれなのか教えてくれたり、
必要な場合は、関連パッケージをダウンロードしてそのまま更新してくれたりしてくれます。

引用:パッケージの管理|yumとは

yumは、パッケージを管理するコマンドとなっています。
Ubuntuで言う、apt-getと同じです。
で、yumで覚えてほしいのはrpmコマンドと異なり、ネットワーク経由でパッケージをダウンロードし、インストールします。
なので、基本的にyumでパッケージをインストールし、問題が発生したらrpmでインストールするというのが一般的だと思います。

リポジトリの設定

パッケージのダウンロードに用いるリポジトリの設定ファイルは、「/etc/yum.repos.d/」にあります。
まず、リポジトリには、2種類あります。
1つ目が、CentOSのデフォルトのリポジトリです。
それに関する設定は、基本的に「CentOS-Base.repo 」に記述されています。
2つ目が、サードパーティが公開しているリポジトリです。
一般的に、remiやepelが有名だと思います。
これらを用いることで、デフォルトのリポジトリで対応していないパッケージなどをダウンロードすることができます。

設定ファイルの説明

それでは、実際にリポジトリの設定ファイルの中身を見てみましょう!

# vim CentOS-Base.repo

〜〜 省略 〜〜

[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
baseurl=http://mirror.centos.org/altarch/$releasever/os/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32

#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
baseurl=http://mirror.centos.org/altarch/$releasever/updates/$basearch/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
       file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-SIG-AltArch-Arm32

〜〜 省略 〜〜


mirrorlistやbaseurlがリポジトリ参照先のURLとなっています。
あと、覚えとくのは、「enabled=1」だとリポジトリを有効にするって意味です。
無効にしたい場合は、「enabled=0」にすれば大丈夫です。
ちなみに、無効にしたけど、一時的にそのリポジトリを使いたい場合は以下の通りにすれば使えます。

# yum --enablerepo=remi install php

他にも、ここで重要なのが$releaseverなどのYum変数と呼ばれる変数です。
個人的に覚えた方がいいと思うのは、2つだけだと思います。

$releasever CentOSのバージョンの値
$basearch システムのベースアーキテクチャーの値(x86_64など)

問題の原因

http://mirror.centos.org/altarch/ にアクセスしてみたら分かりますが、
「7-2.1511.el7.centos.0.4」というディレクトリはありません。
同様に、Raspberry Pi 3は64bitなので、「$basearch=x86_64」だと思ってしまうんですが、異なるアーキテクチャの値が代入されています。
そう、問題の原因はラズパイのアーキテクチャリポジトリの参照URLがうまく対応してないので、ダウンロードできないからです。

解決策

なので、問題が発生したら

# baseurl=http://mirror.centos.org/altarch/$releasever/updates/$basearch/
                    ↓
                    ↓
                    ↓
# baseurl=http://mirror.centos.org/altarch/$releasever/updates/x86_64/

にすれば大丈夫だと思います。

あと、他にも
サードパーティリポジトリは、ラズパイに対応していません!
サードパーティリポジトリの設定ファイルで「enabled=1」となっていることが原因で
Not Foundなどのエラーが発生することがあります。