LoginSignup
14
13

More than 3 years have passed since last update.

fdiskコマンドでパーティション分割

Posted at

 lpic101範囲のパーティション分割の理解を深めるために、fdiskコマンドでパーティション分割をやってみることにした。

環境

最初はDockerでやろうとしたけど、Dockerコンテナ内からではディスクの操作が出来なかったので仮想マシンでやることにした。

ホストOS: Ubuntu 16.04.6 LTS
仮想マシン: vagrant 2.2.6, VirtualBox 5.1.38_Ubuntur122592
ゲストOS: CentOS7

fdiskコマンドでパーティション情報の表示

fdisk -l デバイス名

デバイス名のパーティション情報を表示する。デバイス名を省略すると「/proc/partitions」に書かれているパーティション情報が表示される。

[vagrant@localhost ~]$ sudo fdisk -l

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048    83886079    41942016   83  Linux
[vagrant@localhost ~]$ cat /proc/partitions
major minor  #blocks  name

   8        0   41943040 sda
   8        1   41942016 sda1

fdisk -s デバイス名

指定したデバイスのブロック数を表示。

[vagrant@localhost ~]$ sudo fdisk -s /dev/sda 
41943040

fdiskコマンドでパーティション操作

fdisk デバイス名

対話モードで実行。

[vagrant@localhost ~]$ sudo fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.


Command (m for help):

パーティション作成

nでパーティションの作成。ハードディスクは4つまでのパーティションに分けることができる。現在は/dev/sda1が全て使っているのでNo free sectors availableと出力されて、新しくパーティションを作成できない。
/dev/sda: 42.9 GB
/dev/sda1: (83886079 - 2048) × 512 = 42948623872 ≒ 42.9 GB

Command (m for help): n   
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 2
No free sectors available

まずパーティションを削除する。dが削除。

Command (m for help): d       
Selected partition 1
Partition 1 is deleted

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): 

パーティション作成。

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-83886079, default 2048): 2048
Last sector, +sectors or +size{K,M,G} (2048-83886079, default 83886079): +1G   
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     2099199     1048576   83  Linux

Command (m for help): 

1Gぶん割り当てられた。

拡張領域を作成

先ほどは基本領域でパーティションを作成した。次は拡張領域で作成する。拡張領域の中に論理パーティションを複数作成できる。

Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): e
Partition number (2-4, default 2): 2
First sector (2099200-83886079, default 2099200): 2099200
Last sector, +sectors or +size{K,M,G} (2099200-83886079, default 83886079): +3G
Partition 2 of type Extended and of size 3 GiB is set

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     2099199     1048576   83  Linux
/dev/sda2         2099200     8390655     3145728    5  Extended

論理パーティションの作成

作成した拡張領域に論理パーティションを作成していく。

Command (m for help): n
Partition type:
   p   primary (1 primary, 1 extended, 2 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (2101248-8390655, default 2101248): 2101248
Last sector, +sectors or +size{K,M,G} (2101248-8390655, default 8390655): +1G
Partition 5 of type Linux and of size 1 GiB is set

Command (m for help): p

Disk /dev/sda: 42.9 GB, 42949672960 bytes, 83886080 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
Disk label type: dos
Disk identifier: 0x0009ef88

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1            2048     2099199     1048576   83  Linux
/dev/sda2         2099200     8390655     3145728    5  Extended
/dev/sda5         2101248     4198399     1048576   83  Linux

作成が完了したら、wで保存。保存せずに終了するとパーティションは変更されない。

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
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)
Syncing disks.

終わりに

実際に手を動かすと覚えやすいので、これからもこの方法で知識を身に付けていこうと思います!
次回はマウント、スワップ領域の作成、シンボリックリンクをつくってみる。

14
13
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
14
13