CloudShellの/homeディレクトリ以下にyumパッケージをインストールしてみた

記事タイトルとURLをコピーする

はじめに

開発環境を構築する際に、EC2 を立てると運用回りで工数がとられるのであまり立てたくない、代替として CloudShell が使えるかな?と思い、検証をしていました。

その一環で CloudShell の /home ディレクトリ以下に yum でパッケージをインストールする方法を模索したので、備忘録がてらに実現できたやり方を残しておきます。

/home ディレクトリに置きたい理由としては、CloudShell の永続化ストレージは /home ディレクトリしかないためです。
※ ただし、/home ディレクトリは 120 日間 CloudShell にアクセスしないと自動削除されます。
参考: https://blog.serverworks.co.jp/reinvent2020-tryed-cloudshell-for-deployment-works

先に述べておくと、あまり推奨できるやり方ではないです。

理由① /etc/yum.repos.d/amzn2-core.repo を編集しなければいけないため
理由② インストール後の /home の容量が半分ほどになるため

個人的には、 インストールスクリプトを作成して、毎回実行する方法が推奨です。

/home ディレクトリ以下に yum パッケージをインストールする方法

以下コマンドを実行することで可能です。

① mirrolistの変更
$ sudo vi /etc/yum.repos.d/amzn2-core.repo
mirrorlist=$awsproto://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list
→mirrorlist=http://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list

② パッケージのインストール
$ sudo yum --releasever={{amzn2-coreの$releasever}} --installroot={{インストールしたいディレクトリパス}} install {{インストールしたいパッケージ}} -y
e.g. sudo yum --releasever=2 --installroot=/home/cloudshell-user install gcc -y

簡単に解説します。

① mirrolistの変更

ちゃんとした原因はわかっていないのですが、$awsproto のみ毎回正しく参照されないので、プロトコルの記述をハードコーディングします。
この時点でだいぶいやな気持ちになるのですが、お付き合いください。

$ sudo vi /etc/yum.repos.d/amzn2-core.repo
mirrorlist=$awsproto://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list
→mirrorlist=http://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list

この変更をおこなわないと以下のエラーになりました。
ちなみに /etc/yum/vars/awsproto にはちゃんとプロトコルが定義されていました。

$ sudo yum --releasever=2 --installroot=/home/cloudshell-user install gcc -y
Loaded plugins: ovl, priorities
Repository 'amzn2-core': Error parsing config: Error parsing "mirrorlist = '$awsproto://$amazonlinux.$awsregion.$awsdomain/2/$product/$target/x86_64/mirror.list'": URL must be http, ftp, file or https not ""
Repository 'amzn2-core-source': Error parsing config: Error parsing "mirrorlist = '$awsproto://$amazonlinux.$awsregion.$awsdomain/2/$product/$target/SRPMS/mirror.list'": URL must be http, ftp, file or https not ""
Repository 'amzn2-core-debuginfo': Error parsing config: Error parsing "mirrorlist = '$awsproto://$amazonlinux.$awsregion.$awsdomain/2/$product/$target/debuginfo/x86_64/mirror.list'": URL must be http, ftp, file or https not ""
There are no enabled repos.
 Run "yum repolist all" to see the repos you have.
 To enable Red Hat Subscription Management repositories:
     subscription-manager repos --enable <repo>
 To enable custom repositories:
     yum-config-manager --enable <repo>

②パッケージのインストール

$ sudo yum --releasever={{amzn2-coreの$releasever}} --installroot={{インストールしたいディレクトリパス}} install {{インストールしたいパッケージ}} -y

e.g. sudo yum --releasever=2 --installroot=/home/cloudshell-user install gcc -y

②-1 --releasever オプションについて

このオプションを使って、releaserver を指定します。--installroot オプションを使うときにこのオプションがないとエラーになりました(こちらも原因はわかってません...orz)

releasever については、redhat 公式に記載がありました。
かみ砕くと、/etc/yum.conf ファイルで定義された distroverpkg で指定されたリポジトリのリリースバージョンらしいです。

$releasever You can use this variable to reference the release version of Red Hat Enterprise Linux. Yum obtains the value of $releasever from the distroverpkg=value line in the /etc/yum.conf configuration file. If there is no such line in /etc/yum.conf, then yum infers the correct value by deriving the version number from the redhat-release-server package. The value of $releasever typically consists of the major release number and the variant of Red Hat Enterprise Linux, for example 6Client, or 6Server.
https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/6/html/deployment_guide/sec-using_yum_variables

補足すると、$releasever は、リポジトリURL の構成要素になっているみたいです。

$ less /etc/yum.repos.d/amzn2-core.repo
[amzn2-core]
name=Amazon Linux 2 core repository
mirrorlist=$awsproto://$amazonlinux.$awsregion.$awsdomain/$releasever/$product/$target/$basearch/mirror.list ←ここの $releasever で参照される
(省略)

releasever の調べ方は以下です。

$ less /etc/yum.conf
[main]
(省略)
distroverpkg=system-release //distroverpkg からリポジトリを確認する
(省略)

$ sudo yum info system-release //確認したリポジトリ名を引数に指定する
Installed Packages
Name        : system-release
Arch        : x86_64
Epoch       : 1
Version     : 2 //ここの値が releasever の値になる。
(省略)

②-2 --installroot オプション

yum パッケージを指定したしたパスにインストールします。

一応補足すると、本来は chroot した新しい Linux 環境にパッケージをインストールする場合に使うオプションらしいです。

参考: https://teratail.com/questions/130625

Tips

容量の問題

installroot で入れると必要なパッケージ以外のパッケージもインストールされるので、容量が跳ね上がります。

試しに、gcc を入れてみましたが、あっという間に残り半分になってました。(/homeディレクトリは 1GB しか使えない)

$ df -h /home
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      976M  2.6M  907M   1% /home
$ sudo yum --releasever=2 --installroot=/home/cloudshell-user install gcc -y
$ df -h /home
Filesystem      Size  Used Avail Use% Mounted on
/dev/loop0      976M  447M  462M  50% /home //残り半分しか使えない...!

インストール先に指定したフォルダの構成も何とも言えない感じになります。

$ ls /home/cloudshell-user/
bin  boot  dev  etc  home  lib  lib64  media  mnt  opt  proc  root  run  sbin  srv  sys  tmp  usr  var

パスを通す

既存の /usr/bin にコマンドがインストールされるわけではないので、パスを通す必要があります。

installroot で /home/cloudshell-user を指定した場合、/home/cloudshell-user/usr/bin にパスを通す必要があります。

パッケージの削除

以下で削除可能です。

$ sudo yum --releasever={{amzn2-coreの$releasever}} --installroot={{インストール時に指定したディレクトリパス}} remove {{削除したいパッケージ}} -y

e.g. sudo yum --releasever=2 --installroot=/home/cloudshell-user remove gcc -y

まとめ

最初にも申し上げたとおり何とも微妙な方法になってしまいました。

僕自身あまり yum に詳しくないので、もっといい方法あるよって方はコメントで教えていただけると!

ご覧いただきありがとうございました。

菅谷 歩 (記事一覧)