Ubuntu 20.04 で HDD, SSD を増設

HDD や SSD をフォーマットし、既に稼働している Ubuntu 20.04 に増設して使用します。Ubuntu 20.04 が稼働していればよいため、Linux PC だけではなく、Raspberry Pi でも同様の手順で増設することができます。また、接続方法が USB であっても SATA であっても同様の手順で増設することができます。

ここでは、HDD を増設した手順を記していきますが、SSD でも同様の手順で増設することができます。

ディスクの情報確認

まず、HDD を接続し、fdisk を実行し、HDD の情報を表示します。私の環境では、HDD は /dev/sdc でした。デバイス名の最後のアルファベットは、b や c や d など、個々の環境によって異なります。

$ sudo fdisk -l

...

Disk /dev/sdc: 149.5 GiB, 160041885696 bytes, 312581808 sectors
Disk model: Ext. HDD        
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/sdc1        2048 312581807 312579760 149.1G 83 Linux

パーティションの作成

既存のパーティションを削除し、新しいパーティションを作成します。sdX の X は個々の環境で異なるアルファベット小文字です。間違えると重要なデータが消えることになるため、細心の注意を払います。

$ sudo fdisk /dev/sdX

fdisk の Welcome が表示されます。

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


Command (m for help): 

m を入力すると、ずらっとオプションが表示されます。

Command (m for help): m

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

既存のパーティションを削除し,新規にパーティションを作成した実行結果です。

  • d で delete a partition
  • n で add a new partition
  • w で write table to disk and exit

という流れになります。

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

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

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

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

ディスクのフォーマット

再度、fdisk を実行してみます。私の環境では、HDD のデバイス名は /dev/sdc1 でした。

$ sudo fdisk -l

...

Disk /dev/sdc: 149.5 GiB, 160041885696 bytes, 312581808 sectors
Disk model: Ext. HDD        
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/sdc1        2048 312581807 312579760 149.1G 83 Linux

HDD を ext4 でフォーマットします。sdX の X は個々の環境で異なるアルファベット小文字です。間違えると重要なデータが消えることになるため、細心の注意を払います。

$ sudo mkfs.ext4 /dev/sdX1

mke2fs 1.45.5 (07-Jan-2020)
Creating filesystem with 39072470 4k blocks and 9773056 inodes
Filesystem UUID: 34f15ff7-bc8e-42fa-a848-48db6cbf94af
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 (262144 blocks): done
Writing superblocks and filesystem accounting information: done 

ディスクのマウント

フォーマットされた HDD を使用できるようにマウントします。

HDD をマウントするためのディレクトリを作成し、そこにマウントします。

sudo mkdir /mnt/hdd
sudo mount /dev/sdX1 /mnt/hdd

HDD にアクセスし、ファイルを作成してみます。

cd /mnt/hdd
sudo touch test.txt

OS 起動時の自動マウント

mount コマンドでマウントしただけでは、OS を最起動した場合に再度 mount コマンドでマウントさせる必要があります。そのため、OS 起動時に HDD が自動でマウントされるようにします。

HDD の UUID を確認します。下記のように UUID が表示されます。

$ sudo blkid /dev/sdX1

/dev/sdc1: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx" TYPE="ext4"

vi 等で /etc/fstab を開きます。

sudo vi /etc/fstab

HDD の情報を追記します。各項目の間はタブで区切ります。

UUID=xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx       /mnt/hdd        ext4    defaults        0       0

OS を再起動します。

sudo reboot

OS の最起動後、増設した HDD が自動でマウントされていることを確認します。

その他

パーティションを再作成する前、フォーマットする前に macOS で使用していたディスクを Ubuntu 20.04 でパーティション再作成する場合のメモです。

fdisk で情報を確認すると HFS / HFS+ と表示されます。

$ sudo fdisk -l

...

Disk /dev/sdc: 149.5 GiB, 160041885696 bytes, 312581808 sectors
Disk model: Ext. HDD        
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/sdc1           2 312581807 312581806 149.1G af HFS / HFS+

このディスクに対してパーティションを再作成して書き込み (fdisk の w コマンド) を行おうとすると、下記のようなエラーが表示されることがあります。

その場合は、いったん OS を再起動した後、fdisk の w コマンドを実行します。

Command (m for help): d
Selected partition 1
Partition 1 has been deleted.

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

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-312581807, default 2048): 2048
Last sector, +/-sectors or +/-size{K,M,G,T,P} (2048-312581807, default 312581807): 

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

Command (m for help): w
The partition table has been altered.
Failed to remove partition 1 from system: Device or resource busy
Failed to add partition 1 to system: Device or resource busy

The kernel still uses the old partitions. The new table will be used at the next reboot. 
Syncing disks.