Technology Engineering

178inaba の技術ブログ

【Raspberry Pi】バックアップ用のHDDをセットアップする。

inabaです。

GitLabのバックアップ用にとHDDを買っていました。
そのHDDのセットアップをします。

また、Swap領域も今までのHDDのファイルから新HDDのパーティションに変更します。

Swap領域の削除

今まで使っていたSwapファイル及びSwap起動サービスを削除します。

# Swap起動サービスの停止
$ sudo /etc/init.d/dphys-swapfile stop

# Swap起動サービスパッケージの削除
$ sudo apt-get purge dphys-swapfile 
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following package was automatically installed and is no longer required:
  dc
Use 'apt-get autoremove' to remove it.
The following packages will be REMOVED:
  dphys-swapfile*
0 upgraded, 0 newly installed, 1 to remove and 5 not upgraded.
After this operation, 85.0 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 98423 files and directories currently installed.)
Removing dphys-swapfile (20100506-1) ...
Purging configuration files for dphys-swapfile (20100506-1) ...
Processing triggers for man-db (2.7.0.2-5) ...

# 不要になったと示されたdcパッケージの削除
$ sudo apt-get purge dc
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following packages will be REMOVED:
  dc*
0 upgraded, 0 newly installed, 1 to remove and 5 not upgraded.
After this operation, 111 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 98408 files and directories currently installed.)
Removing dc (1.06.95-9) ...
Purging configuration files for dc (1.06.95-9) ...
Processing triggers for install-info (5.2.0.dfsg.1-6) ...
Processing triggers for man-db (2.7.0.2-5) ...

# Swapファイルの削除
$ sudo rm -rf /var/swap 

フォーマット

フォーマットは以前Raspberry PiにHDDを追加した時とだいたい同じです。

# 対象ディスクの確認
$ sudo fdisk -l
# ...
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

# フォーマット
$ sudo fdisk /dev/sdb

# Swap領域用のパーティション(16GiB)を切る。
Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-1953525167, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-1953525167, default 1953525167): +16G # +16Gでサイズ指定(16GiB)のパーティションが切れる。

Created a new partition 1 of type 'Linux' and of size 16 GiB.

Command (m for help): p # パーティションができたことを確認。
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
# ...

Device     Boot Start      End  Sectors Size Id Type
/dev/sdb1        2048 33556479 33554432  16G 83 Linux

# バックアップ用のパーティションを切る
Command (m for help): n
Partition type
   p   primary (1 primary, 0 extended, 3 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (2-4, default 2): 
First sector (33556480-1953525167, default 33556480): 
Last sector, +sectors or +size{K,M,G,T,P} (33556480-1953525167, default 1953525167): # デフォルトで最大のパーティションが作れるのでEnterを押すだけで良い。

Created a new partition 2 of type 'Linux' and of size 915.5 GiB.

Command (m for help): p # パーティションができたことを確認。
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
# ...

Device     Boot    Start        End    Sectors   Size Id Type
/dev/sdb1           2048   33556479   33554432    16G 83 Linux
/dev/sdb2       33556480 1953525167 1919968688 915.5G 83 Linux

Command (m for help): t # Swap領域に使うパーティションのパーティションタイプIDを82(Linux swap)に設定。
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'.

Command (m for help): p # パーティションタイプIDが変更された事を確認。
Disk /dev/sdb: 931.5 GiB, 1000204886016 bytes, 1953525168 sectors
# ...

Device     Boot    Start        End    Sectors   Size Id Type
/dev/sdb1           2048   33556479   33554432    16G 82 Linux swap / Solaris
/dev/sdb2       33556480 1953525167 1919968688 915.5G 83 Linux

Command (m for help): w # 保存して終了。

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

Swap領域作成&Swap開始

# Swap領域作成
$ sudo mkswap -c /dev/sdb1 
0 bad pages
Setting up swapspace version 1, size = 16777212 KiB
no label, UUID=50945b0a-3c30-4de5-a95d-223bb0f6415a

# Swap開始
$ sudo swapon /dev/sdb1

# 再起動しても自動でSwap開始するように設定
$ sudo nano /etc/fstab
$ cat /etc/fstab 
# ...
/dev/sdb1       swap            swap    defaults          0       0

バックアップ領域をマウント

# バックアップ領域のファイルシステムをext4にする。
$ sudo mkfs.ext4 /dev/sdb2
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 239996086 4k blocks and 60006400 inodes
Filesystem UUID: 50945b0a-3c30-4de5-a95d-223bb0f6415a
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968, 
    102400000, 214990848

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

# 念のためディスクチェック
$ sudo e2fsck -f /dev/sdb2
e2fsck 1.42.12 (29-Aug-2014)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb2: 11/60006400 files (0.0% non-contiguous), 3817299/239996086 blocks

# マウントポイントの作成
$ mkdir backups

# マウント
$ sudo mount -t ext4 -o defaults /dev/sdb2 /mnt/backups

# 確認
$ df -kh
Filesystem      Size  Used Avail Use% Mounted on
# ...
/dev/sdb2       902G   72M  856G   1% /mnt/backups

# 不要ファイル削除(後述)
$ sudo rm -rf  /mnt/backups/lost+found

# マウント永続化
$ sudo nano /etc/fstab 
$ cat /etc/fstab 
# ...
/dev/sdb2       /mnt/backups    ext4    defaults          0       0

マウントしたディスクにlost+foundというディレクトリがありますが、これはe2fsckコマンドでできたディレクトリのようです。
削除してもかまわないようです。

ja.stackoverflow.com

今日はここまで。