5
7

More than 3 years have passed since last update.

CentOS7系でyumリポジトリサーバを立ててローカルと他サーバから参照するなど

Last updated at Posted at 2019-12-17

はじめに

他にもこの様な記事は沢山ありふれてるような気がしてならないのと、
今時ローカルでyumリポジトリをローカルに立てる人がいるのかわかりませんが、
最近独自buildのrpm配布サーバを構築したため、
その時の内容のメモついでにLinux Advent Calender 2019に参加させて頂くことにしました。

なお、Webでのレポジトリ公開部分の構築詳細はひとまず省きますので、
レポジトリフォルダを公開する設定はapacheやnginx等で各自でご対応ください。
記載内容は初心者レベルの内容です。簡単に作成できます。

今回の環境

  • CentOS7.7.1908 (on AWS)
  • CentOS6 (on AWS)
    今更6,7かよってところで、CentOS8でやれば良かったと前日に後悔しております。
    後日アップデートするかもしれません。

レポジトリを作成する準備をしよう

repositoryを作成するためのcreaterepoパッケージをインストールします。

# yum install createrepo

依存関係で以下のパッケージもインストールされます。

deltarpm
python-deltarpm

今回インストールされたcreaterepoのバージョンはこちら

[root@yum-server ~]# rpm -qa | fgrep createrepo
createrepo-0.9.9-28.el7.noarch
[root@yum-server ~]#

レポジトリを作成しよう

前準備

まずレポジトリのデータベースを作成する領域を検討しましょう。
diskサイズも考えなら場所を作成するのがオススメかと思います。

オンプレなサーバであればdisk玉が沢山ある領域など。
もしくはAWS等であれば追加で領域をぶち込むのも良いかもしれません。
別にローカルでもI/Oなどゴリゴリに使わなければ問題は無いと思うので各自のポリシーで。

ディレクトリを作成する

今回は以下に領域を確保することにします。
/var/yum-repo

なお、今回は複数のCentOSのバージョン向けに公開できる様に
以下の様なポリシーにもしてみます。

/var/yum-repo/centos/[CentOSバージョン番号]/rpms

architecture単位でも分ける場合はCentOSバージョン番号の後にx86_64
noarchなどで分けると良いかと思います。
(rpmbuildをするとrpmはarch階層単位で作成されたりするのでそちらのが良さそうです)

実際にrpmを配置する

たとえば自分でrebuildしたものがあれば置いたりしてみましょう。

今回はサンプルとしてCentOS-Baseレポジトリからrpmを落として利用します。
rpmをダウンロードする際は# yumdownloader [パッケージ名]等を使うと便利です。
今回はわかりやすく依存関係も出ないwgetのrpmを利用します。

[root@yum-server rpms]# ls -ltr
total 548
-rw-r--r-- 1 root root 560272 May 17 00:48 wget-1.14-18.el7_6.1.x86_64.rpm
[root@yum-server rpms]#

レポジトリのメタデータ情報を作成する

ようやくcreaterpoコマンドの出番です。

スキャンを指定した配下のディレクトリにあるrpmファイル情報が取り込まれますので、
数によっては時間がかかると思います。
今回は1個だったのですぐに完了しました。

# createrepo -d [メタデータを作成する階層]

[root@yum-server 7]# pwd
/var/yum-repo/centos/7

[root@yum-server 7]# createrepo -d ./
Spawning worker 0 with 1 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
[root@yum-server 7]#

なおcreaterepoを行うと引数で指定したディレクトリ配下にrepodataが作成され
引数で指定したディレクトリ配下のrpm情報が取り込まれ、
以下のようにxml関連の情報が格納されます。

[root@yum-server 7]# ls -ltr
total 4
drwxr-xr-x 2 root root   45 Nov  8 19:41 rpms
drwxr-xr-x 2 root root 4096 Nov  8 19:43 repodata
[root@yum-server 7]# cd repodata/
[root@yum-server repodata]# ls -ltr
total 28
-rw-r--r-- 1 root root 1139 Nov  8 19:43 dedbecb0ef25ebe6b8646e2589dc48e9a2adb34d1d6a677eed9e100ad2cbca54-primary.xml.gz
-rw-r--r-- 1 root root  523 Nov  8 19:43 29d912df4c348eba529c6a9845827f02a8160c094ab0da8892a398fab70c27e0-filelists.xml.gz
-rw-r--r-- 1 root root  893 Nov  8 19:43 b7d1045f4692acfa996b581a68c9483103e0c4edd5b14ae96e92640aeaa68351-other.xml.gz
-rw-r--r-- 1 root root 1512 Nov  8 19:43 d9410596837121203e82f0dabbf83afc8d467d16b15d73c8436bdcfac86db0d6-other.sqlite.bz2
-rw-r--r-- 1 root root 1880 Nov  8 19:43 b80afe3ab58edef03d0043b69da54e041574ee17f3e717ef53d04926194cc67b-filelists.sqlite.bz2
-rw-r--r-- 1 root root 2620 Nov  8 19:43 dc35f95c517d1b9014fa0b21d80be4c28483bfab6dac993443ba195dd232d07a-primary.sqlite.bz2
-rw-r--r-- 1 root root 2969 Nov  8 19:43 repomd.xml
[root@yum-server repodata]#

メタデータの作成はこれで完了です。

今回のレポジトリ作成ポリシーで行くとCentOSのバージョン単位でディレクトリを作成していたと思いますので、
CentOS 6向け等もご用意される方はそちらでもcreaterepoをしてください。

各clientサーバのyum.confの設定

ローカルサーバ編

ローカルサーバで使う必要があるのか少々疑問ですが、以下の様なyumレポジトリファイルを作成することで読むこむ事ができます。

設定する
[root@yum-server yum.repos.d]# pwd
/etc/yum.repos.d
[root@yum-server yum.repos.d]#
[root@yum-server yum.repos.d]# vim local.repo
[local-repo]
enabled=0
name= local-repo of the year.
baseurl=file:///var/yum-repo/centos/$releasever
[root@yum-server yum.repos.d]#

baseurlの部分をローカルのファイルパス(メタデータが存在する階層)を指定します。
また$releaseverを使うとOSのMajorバージョンが代入されます。
おそらくCentOS-Baseレポジトリ等で使われているので何となく理解はされていると思います。
なお、細かい説明等はman yum.confで内容を確認する事ができます。
ちなみにenabled=0なので通常は読み込まれない様になってます。

レポジトリを参照してみる

yum.repos.d配下にローカル参照用の設定をしたので読み込めるか確認しましょう。

他のBaseレポジトリ等の有効フラグをファイル上で変更するのは面倒なので、
yumコマンド上でワイルドカードで今回追加したレポジトリ以外を無効にして、
作成したレポジトリの名前のみを有効にして、レポジトリ参照ができているかを確認します。

[root@yum-server 7]# yum --disablerepo=* --enablerepo=local-repo repolist
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
repo id                                                    repo name                                                              status
local-repo/7                                               local-repo of the year.                                                1
repolist: 1
[root@yum-server 7]#

無事参照と管理されているパッケージが1つであるという事が確認できました。

インストールして確認したいという方はinstallをする事もできます。

[root@yum-server 7]# yum --disablerepo=* --enablerepo=local-repo install wget
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
local-repo                                                                                                       | 2.9 kB  00:00:00
Resolving Dependencies
--> Running transaction check
---> Package wget.x86_64 0:1.14-18.el7_6.1 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

========================================================================================================================================
 Package                    Arch                         Version                                 Repository                        Size
========================================================================================================================================
Installing:
 wget                       x86_64                       1.14-18.el7_6.1                         local-repo                       547 k

Transaction Summary
========================================================================================================================================
Install  1 Package

Total download size: 547 k
Installed size: 2.0 M
Is this ok [y/d/N]:

リモートサーバ編

そしてリモート参照の場合です。
基本的にはローカル参照と同様で作成したレポジトリ参照させたいclientでレポジトリ情報を設定するだけです。

レポジトリを公開する

まずリモートで参照させるために、先ほどレポジトリを作成したサーバでレポジトリディレクトリをWeb公開させます。
apacheやnginxなどなどお好きなものでご対応ください。

冒頭の通りここの手順は省きます。
Screen Shot 2019-11-08 at 20.10.17.png

client側でyum設定をする

ローカルで設定していた時とに感じの設定です。

[root@yum-client ~]# cat /etc/yum.repos.d/local.repo
[local-repo]
enabled=0
name= local-repo of the year.
baseurl=http://[公開しているwebのアドレス]/centos/$releasever
[root@yum-client ~]#

またこちらでもレポジトリが読み込めるか確認しましょう。

[root@yum-client ~]# yum --disablerepo=* --enablerepo=local-repo repolist
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
local-repo                                                                                                       | 2.9 kB  00:00:00
local-repo/7/primary_db                                                                                          | 2.6 kB  00:00:00
リポジトリー ID                                               リポジトリー名                                                        状態
local-repo/7                                                  local-repo of the year.                                               1
repolist: 1
[root@yum-client ~]#

無事リモートから参照する事ができました!

最後にwgetをインストールしてみましょう。

[root@yum-client ~]# yum --disablerepo=* --enablerepo=local-repo install wget
読み込んだプラグイン:fastestmirror
Loading mirror speeds from cached hostfile
依存性の解決をしています
--> トランザクションの確認を実行しています。
---> パッケージ wget.x86_64 0:1.14-18.el7_6.1 を インストール
--> 依存性解決を終了しました。

依存性を解決しました

========================================================================================================================================
 Package                    アーキテクチャー             バージョン                              リポジトリー                      容量
========================================================================================================================================
インストール中:
 wget                       x86_64                       1.14-18.el7_6.1                         local-repo                       547 k

トランザクションの要約
========================================================================================================================================
インストール  1 パッケージ

総ダウンロード容量: 547 k
インストール容量: 2.0 M
Is this ok [y/d/N]: y
Downloading packages:
wget-1.14-18.el7_6.1.x86_64.rpm                                                                                  | 547 kB  00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  インストール中          : wget-1.14-18.el7_6.1.x86_64                                                                             1/1
  検証中                  : wget-1.14-18.el7_6.1.x86_64                                                                             1/1

インストール:
  wget.x86_64 0:1.14-18.el7_6.1

完了しました!
[root@yum-client ~]#

パッケージを追加してデータベースに情報を反映する

パッケージをレポジトリに追加したい場合はupdateをかける必要があります。

# createrepo --update ./
Spawning worker 0 with 1 pkgs
Spawning worker 1 with 1 pkgs
Spawning worker 2 with 1 pkgs
Spawning worker 3 with 1 pkgs
Spawning worker 4 with 1 pkgs
Spawning worker 5 with 0 pkgs
Spawning worker 6 with 0 pkgs
Spawning worker 7 with 0 pkgs
Spawning worker 8 with 0 pkgs
Spawning worker 9 with 0 pkgs
Spawning worker 10 with 0 pkgs
Spawning worker 11 with 0 pkgs
Spawning worker 12 with 0 pkgs
Spawning worker 13 with 0 pkgs
Spawning worker 14 with 0 pkgs
Spawning worker 15 with 0 pkgs
Spawning worker 16 with 0 pkgs
Spawning worker 17 with 0 pkgs
Spawning worker 18 with 0 pkgs
Spawning worker 19 with 0 pkgs
Spawning worker 20 with 0 pkgs
Spawning worker 21 with 0 pkgs
Spawning worker 22 with 0 pkgs
Spawning worker 23 with 0 pkgs
Spawning worker 24 with 0 pkgs
Spawning worker 25 with 0 pkgs
Spawning worker 26 with 0 pkgs
Spawning worker 27 with 0 pkgs
Workers Finished
Saving Primary metadata
Saving file lists metadata
Saving other metadata
Generating sqlite DBs
Sqlite DBs complete
#

さいごに

  • 独自buildを作成している方や社内で公開する必要がある方は簡単に作れるのでやってみましょう
  • メタデータが読めれば良いのでgitのレポジトリ内を参照させる事もできるようです(負荷がかかりそうですが)
5
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
5
7