やってみる

アウトプットすべく己を導くためのブログ。その試行錯誤すらたれ流す。

fdiskで複数のパーティションに分けてみた

SSDの領域を複数のパーティションに分けた。

対象環境

前回

前回SSDに1つのパーティションを設定し、ブート時にrootパーティションとして使用するように設定した。

これにより、書込先がSDカードからSSDになった。SDカードの延命ができた。

今回

240GBのSSDに対し、fdiskコマンドで複数のパーティションを作成する。

パーティションテーブルはmsdos。プライマリと拡張、2種類のパーティションを作成する。

/dev/ Size(GiB) PartitionType 用途
sda1 8 primary[1] rootパーティションに割り当てる
sda2 120 primary[2] 予備(システム拡張用、パーティション再編集用)
sda5 40 logical /homeパーティションに割り当てる
sda6 30 logical 予備(/varなど割り当てたほうがよさげなもの用)
sda7 25.6 logical NAS用(実験用)

おさらい

パーティション・テーブル

記憶装置はパーティションを設定することで、その記憶領域を利用できるようになる。

パーティション設定するには、パーティション・テーブルのデータが必要。

パーティション・テーブルには複数の種類がある。

パーティション・テーブル 概要
msdos(MBR) 一般的らしい。
GTP 2TB以上のサイズに対応。

fdiskコマンドで操作できるのはmsdosGTPには非対応。

msdos

msdosには、2種類のパーティションがある。

種類 上限数 概要
プライマリ・パーティション 4 ふつうのパーティション
拡張パーティション 1 1つの拡張パーティションの中にいくつでも論理パーティションを作成できる。

プライマリ、拡張、両パーティションあわせて4つまでしか作成できない。プライマリ3つ、拡張1つ、計4つが最大数。そして、拡張パーティションの中に論理パーティションを容量が許す限り作成できる。

パーティション

1台の記憶装置を、論理的に分離すること。

たとえば、240GBのSSDを、200GB, 40GBの2つに論理的に分ける。 パーティションを設定せねば使えない。2つ以上にわけず、1つでもいい。

マウント

マウントとは、OSが記憶装置を利用できるようにする操作のこと。

パーティションが割り当てられた領域を、指定のマウントポイント(パス)に割り当てる。 こうすることで、パスを指定すれば、その記憶装置にアクセスすることができる。

Windowsではデバイスや、そのパーティションごとに、自動でドライブレターが割り当てられる。 D:\, E:\, F:\など。それを手動で、任意のパスに設定するのがマウント。

逆に取り外す操作をアンマウントという。Windowsでいう「ハードウェアの安全な取り外し」に該当する。

fdiskコマンド

fdisk -l

認識している記憶装置のIDを一覧表示する。IDは/dev/*のパス。

fdisk /dev/sda1

記憶装置のIDを渡して、パーティション作成の対話画面に入る。

今回は対象のSSD/dev/sda1として話を進める。

パーティション設定してみる

240GBのSSDを、以下のようにパーティション設定する。

size type
8 GiB primary[1]
120 GiB primary[2]
残り全部(95.6 GiB) extended

プライマリ・パーティション2つ、拡張パーティション1つを作成する。

拡張パーティション内には論理パーティションを3つ作成する。

size type
40 GiB logical
30 GiB logical
残り全部(25.6 GiB) logical

1つのSSDを以下の5つのパーティションに分ける。

/dev/ Size(GiB) PartitionType 用途
sda1 8 primary[1] rootパーティション
sda2 120 primary[2] 予備(システム拡張用、パーティション再編集用)
sda5 40 logical /homeパーティション
sda6 30 logical 予備(/varなど割り当てたほうがよさげなもの用)
sda7 25.6 logical NAS用(実験用)

おそらく将来、rootパーティション/homeパーティションは拡大していく。

パーティション編集開始

pi@raspberrypi:~ $ sudo fdisk /dev/sda

8GBのプライマリ・パーティション作成

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


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

120GBのプライマリ・パーティション作成

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


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

拡張パーティション作成

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


Created a new partition 3 of type 'Extended' and of size 95.6 GiB.

Last sector, ...の値を未入力(default)にすることで、残り全部を指定できる。

論理パーティション作成

拡張パーティションを作成すると、コマンドが以下のように変更される。

before

Command (m for help): n
Partition type
   p   primary (2 primary, 0 extended, 2 free)
   e   extended (container for logical partitions)

after

Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)

論理パーティション

早速、l(logical)コマンドで論理パーティションを作成する。

拡張パーティションは95.6 GiB。論理パーティションはその95.6GiBの中に作成する。

size type
40 GiB primary[1]
30 GiB primary[2]
残り全部(25.6 GiB) extended
40GBの論理パーティション作成
Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l


Adding logical partition 5
First sector (268439552-468862125, default 268439552): 
Last sector, +sectors or +size{K,M,G,T,P} (268439552-468862125, default 468862125): +40G


Created a new partition 5 of type 'Linux' and of size 40 GiB.
30GBの論理パーティション作成
Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l


Adding logical partition 6
First sector (352327680-468862125, default 352327680): 
Last sector, +sectors or +size{K,M,G,T,P} (352327680-468862125, default 468862125): +30G


Created a new partition 6 of type 'Linux' and of size 30 GiB.
残り全部(25.6GB)の論理パーティション作成
Command (m for help): n
Partition type
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l


Adding logical partition 7
First sector (415244288-468862125, default 415244288): 
Last sector, +sectors or +size{K,M,G,T,P} (415244288-468862125, default 468862125): 


Created a new partition 7 of type 'Linux' and of size 25.6 GiB.

パーティションの確認

Command (m for help): p

Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1            2048  16779263  16777216    8G 83 Linux
/dev/sda2        16779264 268437503 251658240  120G 83 Linux
/dev/sda3       268437504 468862125 200424622 95.6G  5 Extended
/dev/sda5       268439552 352325631  83886080   40G 83 Linux
/dev/sda6       352327680 415242239  62914560   30G 83 Linux
/dev/sda7       415244288 468862125  53617838 25.6G 83 Linux

パーティションの書込

変更を確定させる。

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: デバイスもしくはリソースがビジー状態です


The kernel still uses the old table. The new table will be used at the next reboot or after you run partprobe(8) or kpartx(8).

パーティションの確認

fdisk -lパーティションをリストアップする。

pi@raspberrypi:~ $ sudo fdisk -l


Disk /dev/ram0: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram1: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram2: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram3: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram4: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram5: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram6: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram7: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram8: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram9: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram10: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram11: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram12: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram13: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram14: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/ram15: 4 MiB, 4194304 bytes, 8192 sectors
Units: sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 4096 bytes
I/O size (minimum/optimal): 4096 bytes / 4096 bytes
Disk /dev/mmcblk0: 14.5 GiB, 15523119104 bytes, 30318592 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
Disklabel type: dos
Disk identifier: 0xc06b0477


Device         Boot  Start      End  Sectors  Size Id Type
/dev/mmcblk0p1        8192   137215   129024   63M  c W95 FAT32 (LBA)
/dev/mmcblk0p2      137216 30318591 30181376 14.4G 83 Linux


Disk /dev/sda: 223.6 GiB, 240057408512 bytes, 468862126 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
Disklabel type: dos
Disk identifier: 0x2063388e


Device     Boot     Start       End   Sectors  Size Id Type
/dev/sda1            2048  16779263  16777216    8G 83 Linux
/dev/sda2        16779264 268437503 251658240  120G 83 Linux
/dev/sda3       268437504 468862125 200424622 95.6G  5 Extended
/dev/sda5       268439552 352325631  83886080   40G 83 Linux
/dev/sda6       352327680 415242239  62914560   30G 83 Linux
/dev/sda7       415244288 468862125  53617838 25.6G 83 Linux

ext4でフォーマットする

以下のログはsda5, sda6, sda7のみだが、sda1, sda2に関しても同様。

まず、念のためアンマウントする。

pi@raspberrypi:~ $ umount /dev/sda5
umount: /dev/sda5: not mounted
pi@raspberrypi:~ $ umount /dev/sda6
umount: /dev/sda6: not mounted
pi@raspberrypi:~ $ umount /dev/sda7
umount: /dev/sda7: not mounted

次に、ext4でフォーマットする。データが全部消えるので注意。

pi@raspberrypi:~ $ sudo mkfs.ext4 /dev/sda5
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 10485760 4k blocks and 2621440 inodes
Filesystem UUID: ab5c4cda-6f0c-4d7e-9281-028f6cec6cc7
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000, 7962624


Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   
pi@raspberrypi:~ $ sudo mkfs.ext4 /dev/sda6
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 7864320 4k blocks and 1966080 inodes
Filesystem UUID: 2bf9a343-098f-4325-b3a8-91a50848f47f
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000


Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done   
pi@raspberrypi:~ $ sudo mkfs.ext4 /dev/sda7
mke2fs 1.42.12 (29-Aug-2014)
Creating filesystem with 6702229 4k blocks and 1676080 inodes
Filesystem UUID: e46c667d-9f7b-4928-9fc0-9253d9175958
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208, 
    4096000


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

rootパーティション割当

ラズパイのrootパーティションへ割当する方法は前回を参照。/boot/cmdline.txtroot=/dev/sda1など割当したいデバイスを設定する。

分配

今回の場合は/dev/sda1(8GiB)を割り当てる。容量が少なすぎるが、足りなくなったら拡張する。そのための/dev/sda2(120GiB)。アップデートしているとすぐに越えるかもしれない。アプリをインストールしまくったら、とても足りない。

その前に、/dev/sda5(40GiB)に/homeをマウントポイントとして割当したい。ユーザデータだけでも容量の多い領域を割り当てておけば当面は大丈夫、と思いたい。

マウント

/homemountコマンドで割り当てるということでいいのだろうか?いずれ試す。

所感

思ったようにパーティションを分けることができたっぽい。次はマウントか。