Mountpoint for Amazon S3 の自動マウントを systemd で実現する方法

2024.04.28

Mountpoint for Amazon S3 を使って EC2 に S3 バケットを自動マウントしたいと考えていました。しかし、Mountpoint for Amazon S3 は/etc/fstab を利用した自動マウントをサポートしていません。そこで今回は、systemd のサービスを利用した自動マウントを試してみました。

Montpoint for Amazon S3 は自動マウントをサポートしていません。ここで紹介する自動マウント手法は自己責任でご利用ください。

S3 バケットの自動マウントのサポート状況

Mountpoint for Amazon S3 は/etc/fstabを利用した自動マウントをサポートしていません。ロードマップにも fstab 対応予定はありません。

Roadmap · Mountpoint for Amazon S3 - Public Roadmap

一般公開された当初から自動マウント方法については議論されており Issue が立っています。2024 年 4 月 28 日時点でもオープンのままで、クローズされていません。

Auto mount bucket to local directory e.g. on instance reboot · Issue #441 · awslabs/mountpoint-s3

私が知りうる限りの Mountpoint for Amazon S3 の自動マウント方法を以下の 2 つです。

systemd でサービス化する方法

systemd のサービスで mount-s3コマンドを利用したマウントを管理する方法です。本記事で紹介します。

crontab で mount-s3 コマンドを実行する方法

crontab の設定で起動時にmount-s3コマンドを一度実行して S3 バケットをマウントする方法です。標準的なマウント方法と変わりないから許容範囲内だろうと考えていました。

@reboot sudo mount-s3 --allow-delete --allow-other [S3バケット名] [マウントするディレクトリ名]

過去に以下のブログで紹介しています。

systemd を使って自動マウントしてみた

Mountpiont for Amazon S3 のインストールから、systemd で自動マウントのサービス作成、マウント実行までをコマンドをコピペで終わらせることを目的に作成しました。

検証環境

項目
OS Ubuntu 22.04 LTS
Mountpiont for Amazon S3 1.6.0

Mountpoint for Amazon S3 用の IAM ポリシー

EC2 からマウント対象の S3 バケットへアクセスための権限が必要です。 検証用の EC2 には以下の IAM ポリシーがアタッチされた IAM ロールを設定してあります。

{
    "Version": "2012-10-17",
    "Statement": [
        {
            "Action": [
                "s3:ListBucket"
            ],
            "Resource": [
                "arn:aws:s3:::hpc-dev-mountpoint-sample-1"
            ],
            "Effect": "Allow"
        },
        {
            "Action": [
                "s3:GetObject",
                "s3:PutObject",
                "s3:AbortMultipartUpload",
                "s3:DeleteObject"
            ],
            "Resource": [
                "arn:aws:s3:::hpc-dev-mountpoint-sample-1/*"
            ],
            "Effect": "Allow"
        }
    ]
}

インストールからサービス化まで

冒頭の変数にマウント対象の S3 バケットを指定してご利用ください。

本検証では/mnt/s3ディレクトリに S3 バケット(hpc-dev-mountpoint-sample-1)をマウントする設定です。 サービス名の末尾に英数字を追加しているのは、複数の S3 バケットをマウントするときでも同じコマンドを流しても問題ない様に意図した設計です。S3 バケットごとに個別のサービスを systemd で管理します。

BUCKET_NAME="hpc-dev-mountpoint-sample-1"
DIRECTORY="/mnt/s3"
OPTIONS="--allow-delete --allow-other"

# ----- INSTAL -----
# Install mount-s3 command for Ubuntu 22.04
sudo apt update
sudo apt install libfuse2 -y

wget -P /tmp https://s3.amazonaws.com/mountpoint-s3-release/latest/x86_64/mount-s3.deb
sudo apt-get install /tmp/mount-s3.deb -y

# ----- SET UP Systemd service -----
# Generate a distinct identifier to allow multiple mountpoint-s3 services
SERVICE_ID=$(echo -n "${BUCKET_NAME}:${DIRECTORY}" | md5sum | awk '{print $1}')
SERVICE_ID=${SERVICE_ID:0:8}
SERVICE_NAME="mountpoint-s3-${SERVICE_ID}"

# Allow other users to access mount
# Needed if --allow-root or --allow-other option is set
if ! grep -q "^user_allow_other" /etc/fuse.conf
then
    echo "user_allow_other" | sudo tee -a /etc/fuse.conf
fi

# Create mount directory
sudo mkdir -p ${DIRECTORY}
sudo chmod 777 ${DIRECTORY}

# Create service file
tee > "${SERVICE_NAME}.service" <<EOF
[Unit]
Description=Mount s3://${BUCKET_NAME} at ${DIRECTORY}
Wants=network-online.target
After=default.target
AssertPathIsDirectory=${DIRECTORY}

[Service]
Type=forking
User=ubuntu
Group=ubuntu
ExecStart=/usr/bin/mount-s3 ${OPTIONS} ${BUCKET_NAME} ${DIRECTORY}
ExecStop=/usr/bin/fusermount -u ${DIRECTORY}

[Install]
WantedBy=default.target
EOF

# Install and start the service
sudo mv "${SERVICE_NAME}.service" /etc/systemd/system/

sudo systemctl daemon-reload
sudo systemctl enable "${SERVICE_NAME}"
sudo systemctl start "${SERVICE_NAME}"

補足: fuse.conf の設定について

AWS HPC Recipes を参考に実行コマンドを作成していました。mount-s3コマンドのオプションで--allow-otherをよく私は使っていたのですが、一般ユーザーがallow_otherを使うには/etc/fuse.confに設定追加が必要だったことを知りました。

# Allow other users to access mount
# Needed if --allow-root or --allow-other option is set
if ! grep -q "^user_allow_other" /etc/fuse.conf
then
    echo "user_allow_other" >> /etc/fuse.conf
fi

引用: AWS HPC Recipes

Ubuntu 22.04 LTS のデフォルトのfuse.confではuser_allow_otherの項目がコメントアウトされていました。この設定を追加することで、一般ユーザーが--allow-otherオプションを使用できるようになります。 肝心のfuse.confへ設定方法は既存の設定のコメントアウトを解除するより、一行追記した方がシンプルなので AWS HPC Recipes のスクリプトと同じ方法を採用しました。

今回の検証ではデフォルトの 一般ユーザー(ubuntu)がコマンドを実行することを想定しています。echoコマンドにsudoを付けても権限不足になり追記ができなかったため、sudo tee -aで追記する方法に変更しています。

if ! grep -q "^user_allow_other" /etc/fuse.conf
then
    echo "user_allow_other" | sudo tee -a /etc/fuse.conf
fi

実行結果確認

/mnt/s3ディレクトリに S3 バケット(hpc-dev-mountpoint-sample-1)をマウントされているか確認します。

S3 バケットに保存しているファイルを確認できました。

$ ls -l /mnt/s3
total 2675420
-rw-r--r-- 1 ubuntu ubuntu          0 Apr  6 04:15 YouCanLookThisFileFromS3.txt
drwxr-xr-x 2 ubuntu ubuntu          0 Apr 27 11:06 images
drwxr-xr-x 2 ubuntu ubuntu          0 Apr 27 11:06 reads
-rw-r--r-- 1 ubuntu ubuntu 2739630080 Apr 26 10:05 trinityrnaseq.v2.15.1.simg

EC2 の停止・開始しても自動マウントできるように systemd のサービスで S3 バケットのマウント管理しています。EC2 を再起動して確認してみます。

$ sudo reboot

Session terminated, killing shell... ...killed.

再起動後に改めて確認してみました。サービスは起動し、/mnt/s3ディレクトリに S3 バケットをマウントできていました。

 systemctl status mountpoint-s3-c4d7ef4c.service
● mountpoint-s3-c4d7ef4c.service - Mount s3://hpc-dev-mountpoint-sample-1 at /mnt/s3
     Loaded: loaded (/etc/systemd/system/mountpoint-s3-c4d7ef4c.service; enabled; vendor preset: enabled)
     Active: active (running) since Sat 2024-04-27 11:07:46 UTC; 2min 27s ago
    Process: 653 ExecStart=/usr/bin/mount-s3 --allow-delete --allow-other hpc-dev-mountpoint-sample-1 /mnt/s3 (code=exited, status=0/SUCCE>
   Main PID: 655 (mount-s3)
      Tasks: 9 (limit: 1068)
     Memory: 15.1M
        CPU: 115ms
     CGroup: /system.slice/mountpoint-s3-c4d7ef4c.service
             └─655 /usr/bin/mount-s3 --allow-delete --allow-other hpc-dev-mountpoint-sample-1 /mnt/s3

Apr 27 11:07:46 ip-10-0-1-136 systemd[1]: Starting Mount s3://hpc-dev-mountpoint-sample-1 at /mnt/s3...
Apr 27 11:07:46 ip-10-0-1-136 mount-s3[653]: bucket hpc-dev-mountpoint-sample-1 is mounted at /mnt/s3
Apr 27 11:07:46 ip-10-0-1-136 systemd[1]: Started Mount s3://hpc-dev-mountpoint-sample-1 at /mnt/s3.
Apr 27 11:09:18 ip-10-0-1-136 mount-s3[655]: [WARN] getxattr{req=6 ino=1 name="security.selinux"}: mountpoint_s3::fuse: getxattr failed: o>

$ ls -l /mnt/s3
total 2675420
-rw-r--r-- 1 ubuntu ubuntu          0 Apr  6 04:15 YouCanLookThisFileFromS3.txt
drwxr-xr-x 2 ubuntu ubuntu          0 Apr 27 11:07 images
drwxr-xr-x 2 ubuntu ubuntu          0 Apr 27 11:07 reads
-rw-r--r-- 1 ubuntu ubuntu 2739630080 Apr 26 10:05 trinityrnaseq.v2.15.1.simg
ubuntu@ip-10-0-1-136:~$

systemd のサービスから S3 バケットをマウントしても正常に機能していることを確認しました。

おわりに

AWS ParallelCluster のヘッドノードで自動マウントを組み込むための検証の一部でした。EC2(Ubuntu 22.02 LTS) にコマンド一発で設定済むのは需要ありそうなので紹介しました。ただし、公式には自動マウントをサポートしていないため、自己責任でご利用してください。

参考