RHEL8にs3をマウントしてみました。

#はじめに

先日RHEL8にs3をマウントして使いたいという要望があったので、
パッケージの導入～設定～動作確認をやってみました。

#目次

1.前提条件
2.追加ソフトウェア
3.s3バケットの用意
4.パッケージ導入
5./etc/fstab設定
6.s3マウントと確認
7.RHEL8を再起動し、再起動後のマウント状態を確認
8.参考

#1.前提条件

* ここでまとめた情報は、AWS上のRHEL8.4 20210825のamiをベースにしています。
* SELINUXは無効化してあります。
* AmazonS3FullAccessポリシーをアタッチしたロールを作成し、作成したロールをサーバロールとしてアタッチしています。
* 使用リージョンはap-northeast-1です。
* s3をマウントする際のユーザは、ec2-userとします。

#2.追加パッケージ・ソフトウェア

##2-1 OS標準の追加パッケージは、以下の通りです。
* curl
* langpack-ja
* jq 
* unzip 

##2-2 追加ソフトウェアとして、以下の通りです。
* AWS CLI Version2を導入して、aws configureを実施済みです。
* Amazon ssm agentを導入して、自動起動を設定済みです。

#3.s3バケットの用意

##3-1 テスト用にtest-s3fs-20210914バケットを作成します。

```
$ aws s3 mb s3://test-s3fs-20210914
make_bucket: test-s3fs-20210914
$ aws s3 ls s3://test-s3fs-20210914
```

##3-2 バケットにテスト用ファイルを配置します。

```
$ date > s3fs-test.txt
$ cat s3fs-test.txt
Tue Sep 14 10:59:24 UTC 2021
$ aws s3 cp s3fs-test.txt s3://test-s3fs-20210914
upload: ./s3fs-test.txt to s3://test-s3fs-20210914/s3fs-test.txt
$ aws s3 ls s3://test-s3fs-20210914
2021-09-14 10:59:26         29 s3fs-test.txt
```

#4.パッケージ導入

##4-1 s3fs-fuseをパッケージを導入するため、EPELパッケージを導入し、EPELリポジトリを有効化します。

```
$ sudo dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-8.noarch.rpm
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

Last metadata expiration check: 0:13:55 ago on Tue 14 Sep 2021 10:53:00 AM UTC.
epel-release-latest-8.noarch.rpm                                                                     24 kB/s |  23 kB     00:00
Dependencies resolved.
====================================================================================================================================
 Package                          Architecture               Version                         Repository                        Size
====================================================================================================================================
Installing:
 epel-release                     noarch                     8-13.el8                        @commandline                      23 k

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

Total size: 23 k
Installed size: 35 k
Downloading Packages:
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                            1/1
  Installing       : epel-release-8-13.el8.noarch                                                                               1/1
  Running scriptlet: epel-release-8-13.el8.noarch                                                                               1/1
  Verifying        : epel-release-8-13.el8.noarch                                                                               1/1
Installed products updated.

Installed:
  epel-release-8-13.el8.noarch

Complete!
$ sudo dnf config-manager --set-enabled codeready-builder-for-rhel-8-rhui-rpms
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
```

##4-2 s3fs-fuseをパッケージを導入します。

```
$ sudo dnf install -y s3fs-fuse
Updating Subscription Management repositories.
Unable to read consumer identity

This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.

Extra Packages for Enterprise Linux Modular 8 - x86_64                                              537 kB/s | 939 kB     00:01
Extra Packages for Enterprise Linux 8 - x86_64                                                      7.3 MB/s |  10 MB     00:01
Red Hat Enterprise Linux 8 for x86_64 - AppStream from RHUI (RPMs)                                   24 kB/s | 2.8 kB     00:00
Red Hat Enterprise Linux 8 for x86_64 - BaseOS from RHUI (RPMs)                                      20 kB/s | 2.4 kB     00:00
Red Hat CodeReady Linux Builder for RHEL 8 x86_64 (RPMs) from RHUI                                   13 MB/s | 5.5 MB     00:00
Dependencies resolved.
====================================================================================================================================
 Package                      Architecture            Version                        Repository                                Size
====================================================================================================================================
Installing:
 s3fs-fuse                    x86_64                  1.90-1.el8                     epel                                     347 k
Installing dependencies:
 fuse                         x86_64                  2.9.7-12.el8                   rhel-8-baseos-rhui-rpms                   83 k
 fuse-common                  x86_64                  3.2.1-12.el8                   rhel-8-baseos-rhui-rpms                   21 k
 mailcap                      noarch                  2.1.48-3.el8                   rhel-8-baseos-rhui-rpms                   39 k

Transaction Summary
====================================================================================================================================
Install  4 Packages

Total download size: 489 k
Installed size: 1.1 M
Downloading Packages:
(1/4): s3fs-fuse-1.90-1.el8.x86_64.rpm                                                              7.3 MB/s | 347 kB     00:00
(2/4): fuse-common-3.2.1-12.el8.x86_64.rpm                                                          200 kB/s |  21 kB     00:00
(3/4): mailcap-2.1.48-3.el8.noarch.rpm                                                              356 kB/s |  39 kB     00:00
(4/4): fuse-2.9.7-12.el8.x86_64.rpm                                                                 790 kB/s |  83 kB     00:00
------------------------------------------------------------------------------------------------------------------------------------
Total                                                                                               695 kB/s | 489 kB     00:00
warning: /var/cache/dnf/epel-fafd94c310c51e1e/packages/s3fs-fuse-1.90-1.el8.x86_64.rpm: Header V4 RSA/SHA256 Signature, key ID 2f86d6a1: NOKEY
Extra Packages for Enterprise Linux 8 - x86_64                                                      1.6 MB/s | 1.6 kB     00:00
Importing GPG key 0x2F86D6A1:
 Userid     : "Fedora EPEL (8) <epel@fedoraproject.org>"
 Fingerprint: 94E2 79EB 8D8F 25B2 1810 ADF1 21EA 45AB 2F86 D6A1
 From       : /etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
Key imported successfully
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                                                                            1/1
  Installing       : mailcap-2.1.48-3.el8.noarch                                                                                1/4
  Installing       : fuse-common-3.2.1-12.el8.x86_64                                                                            2/4
  Installing       : fuse-2.9.7-12.el8.x86_64                                                                                   3/4
  Installing       : s3fs-fuse-1.90-1.el8.x86_64                                                                                4/4
  Running scriptlet: s3fs-fuse-1.90-1.el8.x86_64                                                                                4/4
  Verifying        : s3fs-fuse-1.90-1.el8.x86_64                                                                                1/4
  Verifying        : fuse-common-3.2.1-12.el8.x86_64                                                                            2/4
  Verifying        : mailcap-2.1.48-3.el8.noarch                                                                                3/4
  Verifying        : fuse-2.9.7-12.el8.x86_64                                                                                   4/4
Installed products updated.

Installed:
  fuse-2.9.7-12.el8.x86_64     fuse-common-3.2.1-12.el8.x86_64     mailcap-2.1.48-3.el8.noarch     s3fs-fuse-1.90-1.el8.x86_64

Complete!
```

#5./etc/fstab設定

ec2-userの権限で、/mediaにtest-s3fs-20210914をマウント設定するを記載します。

```:/etc/fstab
test-s3fs-20210914 /media fuse.s3fs _netdev,rw,allow_other,uid=1000,gid=1000,iam_role=auto,endpoint=ap-northeast-1 0 0
```

以下の編集コマンドを実行して、/etc/fstabを編集し、その結果を確認します。

```
$ sudo vi /etc/fstab
$ cat /etc/fstab

#
# /etc/fstab
# Created by anaconda on Wed Aug 25 16:03:47 2021
#
# Accessible filesystems, by reference, are maintained under '/dev/disk/'.
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info.
#
# After editing this file, run 'systemctl daemon-reload' to update systemd
# units generated from this file.
#
UUID=2f154232-e9be-41b4-b29e-1cd05281cf52 / xfs     defaults        0 0
test-s3fs-20210914 /media fuse.s3fs _netdev,rw,allow_other,uid=1000,gid=1000,iam_role=auto,endpoint=ap-northeast-1 0 0
```

#6.s3マウントと確認
##6-1 s3をマウントします。

```
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G  8.4M  1.8G   1% /run
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/nvme0n1p2   30G   10G   21G  34% /
tmpfs           368M     0  368M   0% /run/user/1000
$ sudo mount -a
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G  8.4M  1.8G   1% /run
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/nvme0n1p2   30G   10G   21G  34% /
tmpfs           368M     0  368M   0% /run/user/1000
s3fs             16E     0   16E   0% /media
```

##6-2 s3へのファイルの読み込み・書き込み・削除確認をします。

```
$ cd /media
$ pwd
/media
$ ls -la
total 1
drwxrwxrwx.  1 ec2-user ec2-user   0 Jan  1  1970 .
dr-xr-xr-x. 18 root     root     252 Sep 14 10:53 ..
-rw-r-----.  1 ec2-user ec2-user  29 Sep 14 10:59 s3fs-test.txt
$ cat s3fs-test.txt
Tue Sep 14 10:59:24 UTC 2021
$ date > test-write.txt
$ ls -la
total 2
drwxrwxrwx.  1 ec2-user ec2-user   0 Jan  1  1970 .
dr-xr-xr-x. 18 root     root     252 Sep 14 10:53 ..
-rw-r-----.  1 ec2-user ec2-user  29 Sep 14 10:59 s3fs-test.txt
-rw-rw-r--.  1 ec2-user ec2-user  29 Sep 14 11:49 test-write.txt
$ cat test-write.txt
Tue Sep 14 11:49:05 UTC 2021
$ rm test-write.txt
$ ls -la
total 1
drwxrwxrwx.  1 ec2-user ec2-user   0 Jan  1  1970 .
dr-xr-xr-x. 18 root     root     252 Sep 14 10:53 ..
-rw-r-----.  1 ec2-user ec2-user  29 Sep 14 10:59 s3fs-test.txt
```

#7.RHEL8を再起動し、再起動後のマウント状態を確認

##7-1 RHELを再起動します。

```
$ sudo shutdown -r now 
```

##7-2 RHELに接続して。

```
$ df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.8G     0  1.8G   0% /dev
tmpfs           1.8G     0  1.8G   0% /dev/shm
tmpfs           1.8G  8.4M  1.8G   1% /run
tmpfs           1.8G     0  1.8G   0% /sys/fs/cgroup
/dev/nvme0n1p2   30G   10G   21G  34% /
s3fs             16E     0   16E   0% /media
tmpfs           368M     0  368M   0% /run/user/1000
$ ls -la /media
total 1
drwxrwxrwx   1 ec2-user ec2-user   0 Jan  1  1970 .
dr-xr-xr-x. 18 root     root     272 Sep 14 11:54 ..
-rw-r-----   1 ec2-user ec2-user  29 Sep 14 10:59 s3fs-test.txt
```

#8.参考

[AWS CLI で S3バケットを作成してみる](https://qiita.com/leomaro7/items/12563b5364702afa321c)
[s3fs-fuse/README.md](https://github.com/s3fs-fuse/s3fs-fuse/blob/master/README.md)
[s3fsでAmazon S3をLinuxサーバにマウントする](https://qiita.com/latin1/items/aade223be36a87cbe538)
[Amazon Linux2でIAMロールを使ったs3fsの設定をやってみた](https://dev.classmethod.jp/articles/setting-s3fs-with-al2/)