created: 2022-10-14T02:31:36.611Z

Linuxで買ってきたHDDにパーティション切ってフォーマットしてマウントする

こちらが前回にやった似ている作業。

サーバ用途じゃない外付けディスクを引っ張り出して、ラズパイで smbd とか dlna とかに使っていたら早々にディスクが不安定になり、けっこう古いし多分壊れてきてるので新調する。

NAS用途のHDDと、そのクレードルを買ってきて設定することにした。

おさらい

HDDまわりの用語と関係を整理しておく。

  • ディスクデバイスの中にパーティションを設定する
  • パーティションフォーマットしてファイルシステムを設定する
  • 設定されたファイルシステムマウントする

デバイスの確認

差し込んだばかりのディスクを fdisk で確認する。

$ sudo fdisk -l | grep -A10 TiB
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: 004-2E4164
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes

ちなみにまだマウントはされてないので mount コマンドにはこのデバイスは出てこない。

$ mount -v | grep -c sda
0

ディスクデバイスにパーティションの作成

まだディスクにはパーティションがないので作る。

$ sudo fdisk /dev/sda

Welcome to fdisk (util-linux 2.36.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table.
Created a new DOS disklabel with disk identifier 0xd7a538a9.

p で現状を確認。

> Command (m for help): p
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: 004-2E4164
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: 0xd7a538a9

(デフォルトのパーティションがあるのかと思って) d コマンドを試したら、まだ1つもないですよとの返事。

> Command (m for help): d
No partition is defined yet!

n コマンドで1つ新規作成する。

凝ったことをするわけでもないのでデフォルトの指示にしたがっておく。

> 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-3907029167, default 2048):
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-3907029167, default 3907029167):

Created a new partition 1 of type 'Linux' and of size 1.8 TiB.

n コマンドで設定ができたらそれを w コマンドでディスクに書き込む。

> Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

ディスクにパーティションが設定できたか確認。

期待通り /dev/sda1 というパーティションができている。

$ sudo fdisk -l | grep -A20 TiB
Disk /dev/sda: 1.82 TiB, 2000398934016 bytes, 3907029168 sectors
Disk model: 004-2E4164
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: 0xd7a538a9

Device     Boot Start        End    Sectors  Size Id Type
/dev/sda1        2048 3907029167 3907027120  1.8T 83 Linux

パーティションをフォーマットしてファイルシステムを設定

主に Linux で使うので ext4 でフォーマットすることにする。

lsblk で確認するとまだフォーマットされていない(ファイルシステムが設定されていない)ことがわかる

$ lsblk -f
NAME        FSTYPE FSVER LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1

mkfs をたたく。これは完了まで何秒か時間がかかった。

$ sudo mkfs -t ext4 /dev/sda1
mke2fs 1.46.2 (28-Feb-2021)
Creating filesystem with 488378390 4k blocks and 122101760 inodes
Filesystem UUID: 4f5a6452-5754-41a3-abb5-5682aed23730
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 (262144 blocks): done
Writing superblocks and filesystem accounting information: done

再度 lsblk を叩いて無事にパーティションができたことを確認。

$ lsblk -f
NAME        FSTYPE FSVER LABEL  UUID                                 FSAVAIL FSUSE% MOUNTPOINT
sda
└─sda1      ext4   1.0          4f5a6452-5754-41a3-abb5-5682aed23730

マウント

マウントはよくやるので簡単。

$ sudo mount /dev/sda1 /mnt/testmnt
$ df -Th | grep sda1
/dev/sda1      ext4      1.8T   32K  1.7T   1% /mnt/testmnt

動作確認。

$ echo "test" | sudo tee -a /mnt/testmnt/test.txt
$ cat /mnt/testmnt/test.txt
test

umount。

$ sudo umount /mnt/testmnt
$ ll /mnt/testmnt/
total 8.0K
drwxr-xr-x 2 root root 4.0K Oct 14 11:21 .
drwxr-xr-x 4 root root 4.0K Oct 14 11:21 ..
# ↑umountしたので期待通りtext.txtがなくなっている

参考にした記事

お金のむこうに人がいる 元ゴールドマン・サックス金利トレーダーが書いた 予備知識のいらない経済新入門
[ad] お金のむこうに人がいる 元ゴールドマン・サックス金利トレーダーが書いた 予備知識のいらない経済新入門
田内学 (単行本(ソフトカバー))