RaspberryPi上Gitlabのリポジトリ保存先変更

Gitlabのデータ保存先

Gitlabの設定情報は"/etc/gitlab/gitlab.rb"に記載されている。
その中でデータ保存先は以下のように定義されている。

  • gitlab_rails['uploads_directory']
    単一ファイルとしてアップロードされるユーザーデータの保存先
    初期値:"/var/opt/gitlab/gitlab-rails/uploads"
  • gitlab_rails['backup_path']
    バックアップデータの保存先
    初期値:"/var/opt/gitlab/backups"
  • gitlab_rails['shared_path']
    おそらく大きなファイルの保存先?共有ファイル?
    初期値:"/var/opt/gitlab/gitlab-rails/shared"
  • git_data_dirs()
    リポジトリの保存先。
    初期値:"default" => {"path" => "/var/opt/gitlab/git-data"}

データ保存先の変更準備

データ保存先の変更先

RaspberryPiで使用するmicroSDは故障しやすそうなのでUSBメモリを使用します。
ラズパイ4の安価で小型な特徴を活かして、超小型USBメモリをマウントして、データ保存先とします。
購入したUSBメモリの紹介はこちらで行っています。

USBメモリのフォーマット

標準のUSBメモリはFAT32という形式でフォーマットされています。
Gitlabの保存先として使用するためには、ext4に変換する必要があるので、その作業を行います。
USBメモリを挿してから、下記コマンドでデバイスが認識されているか確認します。

# fdisk -l

全てのデバイスの情報が出てくるので、容量等の情報から対象のUSBメモリを探します。
下記のような形でUSBメモリの情報が確認できます。今回は"/dev/sda"と"/dex/sdb"のデバイスを使用します。

...
Disk /dev/sda: 57.3 GiB, 61524148224 bytes, 120164352 sectors
Disk model:  SanDisk 3.2Gen1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0x00000000

Device     Boot Start       End   Sectors  Size Id Type
/dev/sda1          32 120164351 120164320 57.3G  c W95 FAT32 (LBA)


Disk /dev/sdb: 116.6 GiB, 125162225664 bytes, 244457472 sectors
Disk model: SanDisk 3.2 Gen1
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disklabel type: dos
Disk identifier: 0xc3072e18

Device     Boot Start       End   Sectors   Size Id Type
/dev/sdb1        7552 244457471 244449920 116.6G  7 HPFS/NTFS/exFAT
...

フォーマットを行うために必要な情報は"/dev/sd**"の部分です。この情報をコピーしておきます。
次の命令でext4へのフォーマットを行います。下記はsdbの実行結果です。間違ったデバイスをフォーマットすると取り返しがつかないので慎重に行う必要があります。

# mkfs.ext4 /dev/sdb
mke2fs 1.44.5 (15-Dec-2018)
Found a dos partition table in /dev/sdb
Proceed anyway? (y,N) y
Creating filesystem with 30557184 4k blocks and 7643136 inodes
Filesystem UUID: ********-****-****-****-************
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
        4096000, 7962624, 11239424, 20480000, 23887872

Allocating group tables: done
Writing inode tables: done
Creating journal (131072 blocks): done
Writing superblocks and filesystem accounting information: done

次にマウント先として設定するフォルダを作成、設定しておきます。

# mkdir /mnt/usb_flash1
# chomod -R git:git /mnt/usb_flash1
# mkdir /mnt/usb_flash2
# chomod -R git:git /mnt/usb_flash2

次に確認のためにUSBメモリの手動マウント処理を行います。

# mount /dev/sda /mnt/usb_flash1
# mount /dev/sdb /mnt/usb_flash2

問題無くマウントが行えているかチェックします。

# df -l
ファイルシス   1K-ブロック       使用    使用可 使用% マウント位置
/dev/root         30356460   21979156   7048484   76% /
devtmpfs           1800568          0   1800568    0% /dev
tmpfs              1965432         36   1965396    1% /dev/shm
tmpfs              1965432      16416   1949016    1% /run
tmpfs                 5120          4      5116    1% /run/lock
tmpfs              1965432          0   1965432    0% /sys/fs/cgroup
/dev/mmcblk0p1      258095      49395    208700   20% /boot
/dev/sdc1       1953511008 1724048916 229462092   89% /mnt/usb_hdd2
tmpfs               393084          0    393084    0% /run/user/109
tmpfs               393084          0    393084    0% /run/user/1001
/dev/sda          58875860      53272  55802096    1% /mnt/usb_flash1
/dev/sdb         119785240      61464 113595956    1% /mnt/usb_flash2

次に自動マウント用のUUID確認を行います。フォーマットの結果にも表示されますが、改めて確認しておきます。

# blkid /dev/sda
/dev/sda: UUID="********-****-****-****-************" TYPE="ext4"
# blkid /dev/sdb
/dev/sdb: UUID="********-****-****-****-************" TYPE="ext4"

次に自動マウントの設定を行います。
2デバイス分の自動マウント設定を追加しておきます。
NTFSフォーマットのHDD等をマウントする際はオプションで所有者を指定しないと所有者がrootになってしまいましたが、ext4の場合は不要(というかそのオプションが無い)です。所有者はマウント先フォルダの所有者になります。

# vim /etc/fstab
proc            /proc           proc    defaults          0       0
...
UUID="********-****-****-****-************" /mnt/usb_flash1     ext4    defaults,nofail 0       0
UUID="********-****-****-****-************" /mnt/usb_flash2     ext4    defaults,nofail 0       0
# a swapfile is not a swap partition, no line here
#   use  dphys-swapfile swap[on|off]  for thatマウント失敗時にフリーズしないようにnofailオプションもつけておきます。

以上で設定完了です。
rebootして、自動マウントされているかチェックしておきます。
再起動後にマウントされていることを確認しておきます。

# df -l
ファイルシス   1K-ブロック       使用    使用可 使用% マウント位置
/dev/root         30356460   22114144   6913496   77% /
devtmpfs           1800568          0   1800568    0% /dev
tmpfs              1965432         36   1965396    1% /dev/shm
tmpfs              1965432      41820   1923612    3% /run
tmpfs                 5120          4      5116    1% /run/lock
tmpfs              1965432          0   1965432    0% /sys/fs/cgroup
/dev/sdb         119785240      61492 113595928    1% /mnt/usb_flash2
/dev/sda          58875860     287988  55567380    1% /mnt/usb_flash1
/dev/mmcblk0p1      258095      49395    208700   20% /boot
/dev/sdc1       1953511008 1724048916 229462092   89% /mnt/usb_hdd2
tmpfs               393084          0    393084    0% /run/user/109
tmpfs               393084          0    393084    0% /run/user/1001

 

Gitlab設定ファイルの書き替え

保存先フォルダの作成

usb_flash1の方にgitlab専用フォルダを作成し、配下にリポジトリ保存用フォルダ、Gitlab専用フォルダ、アップロード用フォルダ、UserAttachmentsフォルダを作成します。所有者はgitにしておく必要があります。

# cd /mnt/usb_flash1
# mkdir ./gitlab
# cd ./gitlab
# mkdir LargeObjectDir
# mkdir RepositoriesDir
# mkdir UserAttachments
# cd ..
# chown -R git:git ./gitlab

usb_flash2の方にもgitlab専用フォルダを作成しておきます。さらに配下にはBuckupDataフォルダを作成します。

# cd /mnt/usb_flash2
# mkdir ./gitlab
# cd ./gitlab
# mkdir ./BuckupData
# cd ..
# chown -R git:git ./gitlab

次にgitlab設定ファイルを変更します。

# vim /etc/gitlab/gitlab.rb
...
# gitlab_rails['uploads_directory'] = "/var/opt/gitlab/gitlab-rails/uploads"
gitlab_rails['uploads_directory'] = "/mnt/usb_flash1/gitlab/UserAttachments" #411行目追加
...
# gitlab_rails['backup_path'] = "/var/opt/gitlab/backups"
gitlab_rails['backup_path'] = "/mnt/usb_flash2/gitlab/BuckupData" #608行目追加
...
#658行目追加
git_data_dirs({
     "default" => {"path" => "/mnt/usb_flash1/gitlab/RepositoriesDir"},
     "storage1" => {"path" => "/mnt/usb_flash2/gitlab/RepositoriesDir"}
     })
...
# gitlab_rails['shared_path'] = '/var/opt/gitlab/gitlab-rails/shared'
gitlab_rails['shared_path'] = '/mnt/usb_flash1/gitlab/LargeObjectDir' #669行目追加
...

後はgitlabの再構築を行うだけですが、事前にgitlabの動作を停止しておきます。

# gitlab-ctl stop
ok: down: alertmanager: 1s, normally up
ok: down: gitaly: 0s, normally up
ok: down: gitlab-exporter: 1s, normally up
ok: down: gitlab-kas: 1s, normally up, want up
ok: down: gitlab-workhorse: 0s, normally up
ok: down: logrotate: 1s, normally up
ok: down: nginx: 0s, normally up
ok: down: node-exporter: 0s, normally up
ok: down: postgres-exporter: 1s, normally up
ok: down: postgresql: 0s, normally up
ok: down: prometheus: 1s, normally up
ok: down: puma: 1s, normally up
ok: down: redis: 0s, normally up
ok: down: redis-exporter: 0s, normally up
ok: down: sidekiq: 0s, normally up

gitlabの再構築をします。エラーが出る場合はどこかの作業に間違いがある可能性があります。

# gitlab-ctl reconfigure

エラー無く終了した場合、再度gitlabを起動しておきます。

# gitlab-ctl start
ok: run: alertmanager: (pid 5972) 1s
ok: run: gitaly: (pid 5984) 0s
ok: run: gitlab-exporter: (pid 6002) 0s
ok: run: gitlab-kas: (pid 6062) 0s
ok: run: gitlab-workhorse: (pid 6069) 0s
ok: run: logrotate: (pid 6080) 1s
ok: run: nginx: (pid 6086) 0s
ok: run: node-exporter: (pid 6104) 1s
ok: run: postgres-exporter: (pid 6110) 0s
ok: run: postgresql: (pid 6123) 1s
ok: run: prometheus: (pid 6134) 0s
ok: run: puma: (pid 6145) 0s
ok: run: redis: (pid 6158) 1s
ok: run: redis-exporter: (pid 6164) 0s
ok: run: sidekiq: (pid 6173) 1s

以上で保存先フォルダ設定変更が完了しました。

動作確認

実際にVisualStudioにてリモートリポジトリ登録を行い、プッシュした後、データの保存先を確認します。
リモートリポジトリ設定等々はVisualStudio操作方法説明になるので省略します。

PWchangerのデータをプッシュした結果

ブラウザでGitlabにアクセスし、"PWchanger"のリポジトリを確認します。
一日前にデータを更新していることを確認出来ます。

サーバー上でデータの更新日を確認

次にSSH通信で、サーバー上データの更新日を確認します。
Gitlabリポジトリのデフォルト保存先は"/var/opt/gitlab/git-data/repositories"ですが、ここにはGitlab構築時に作成されたファイルしかありません。

# ls -l -a /var/opt/gitlab/git-data/repositories
合計 16
drwxr-sr-x 3 git git 4096  8月 19 16:27 +gitaly
drwxrws--- 3 git git 4096  9月  3 01:57 .
drwx------ 3 git git 4096  8月 19 16:26 ..
-rw------- 1 git git   64  8月 19 16:27 .gitaly-metadata

次に、リポジトリ保存先に設定した"/mnt/usb_flash1/gitlab/RepositoriesDir/repositories"を確認します。

# ls -l -a /mnt/usb_flash1/gitlab/RepositoriesDir/repositories
合計 20
drwxr-sr-x 3 git git 4096  9月 16 13:28 +gitaly
drwxrws--- 4 git git 4096  9月 17 01:57 .
drwx------ 3 git git 4096  9月  7 19:06 ..
-rw------- 1 git git   64  9月  7 19:22 .gitaly-metadata
drwxr-s--- 3 git git 4096  9月 16 11:27 @hashed

これでリポジトリ保存先の設定変更が正しく行われていることを確認出来ました。

 

コメントを残す

メールアドレスが公開されることはありません。 * が付いている欄は必須項目です

fourteen − 7 =