R.A. Epigonos et al.

[debian] パーティション分割

システムは2つ。これらを適当に切り替えて使うけど、一部のパーティションは共有して、同じマウント位置にマウントしたい。

2システムのデュアルブート用に 100 GiB (100 * 2 30 = 107374182400 bytes) のディスクのパーティション分割を考える。システムがファイルをインストールしない場所、すなわち /home、/tmp、swap は共有する方針で。

MBR パーティション

partition size format system
device type start end id end - start + 1 (sectors) (GiB) 1st 2nd
/dev/sdz1 primary 2048 18876415 83 18874368 18874368 9.0 ext4 - /
/dev/sdz2 primary 18876416 37750783 83 18874368 18874368 9.0 ext4 / -
/dev/sdz3 primary 37750784 41945087 82 4194304 4194304 2.0 swap swap swap
/dev/sdz4 extended 41945088 209715199 5 167770112[1] 167772160 80.0 - - -
/dev/sdz5 logical 41947136 42995711 83 1048576 1048576 0.5 ext4 /tmp /tmp
/dev/sdz6 logical 42997760 209715199 83 166717440[2] 166723584 79.5 ext4 /home /home
  1. 一致しない理由は sdz1 の開始セクタが 2048 番だから (167772160 - 167770112 = 2048)
  2. 一致しない理由は sdz4 の開始セクタと sdz5 の開始セクタの間に 2048 セクタ、sdz5 の終了セクタと sdz6 の開始セクタの間に 2049 セクタあるため。
# mkswap /dev/sdz3
# swapon /dev/sdz3

swap の状態をfreeで見ると以下の様な感じ。2097148 KiB のスワップができている。確保した領域は 2GiB なので、4096 Bytes だけ小さな領域になっている。

$ free
             total       used       free     shared    buffers     cached
Mem:       1024332     326156     698176       4560      33880      89328
-/+ buffers/cache:     202948     821384
Swap:      2097148          0    2097148
0 - (2048 = 1MiB)
...
2047
2048 sdz1 (18874368 = 9GiB)
...
18876415
18876416 sdz2 (18874368 = 9GiB)
...
37750783
37750784 sdz3 (18874368 = 2GiB)
...
41945087
41945088 sdz4 (167770112 ~= 80GiB) - (2048 = 1MiB)
...
41947135
41947136 sdz5 (1048576 = 512MiB)
...
42995711
42995712 - (2048 = 1MiB)
...
42997759
42997760 sdz6 (166717440 ~= 79.5GiB)
...
209715199

dd で 100GiB のディスクイメージを作成し、fdisk でディスクサイズ確認。セクタ数は 107374182400 / 512 = 209715200 セクタ (0番から 209715199 番まで)。

# dd if=/dev/zero of=./disk.img bs=1G count=100

100+0 records in
100+0 records out
107374182400 bytes (107 GB) copied, 1915.76 s, 56.0 MB/s
# fdisk -l disk.img

Disk disk.img: 100 GiB, 107374182400 bytes, 209715200 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

sdz1 を作成。9GiB割り当てたければLast sector で+9Gと指定。

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

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

sdz2 を作成。9GiB割り当てたければLast sector で+9Gと指定。

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):
First sector (18876416-209715199, default 18876416):
Last sector, +sectors or +size{K,M,G,T,P} (18876416-209715199, default 209715199): +9G

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

sdz3 を作成。2GiB割り当てたければLast sector で+2Gと指定。パーティションタイプをswap パーティションに指定。

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

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

Command (m for help): t
Partition number (1-3, default 3):
Hex code (type L to list all codes): 82

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

sdz4 を作成。残りすべてをこの拡張パーティションに割り当てるので、First sector も Last sector もデフォルト値。

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

Selected partition 4
First sector (41945088-209715199, default 41945088):
Last sector, +sectors or +size{K,M,G,T,P} (41945088-209715199, default 209715199):

Created a new partition 4 of type 'Extended' and of size 80 GiB.

sdz5 を作成。0.5GiBは512MiBだが、+0.5Gと指定すると500MiBになるので、Last sectorは+sectorsフォーマットで指定。

Command (m for help): n
All primary partitions are in use.
Adding logical partition 5
First sector (41947136-209715199, default 41947136):
Last sector, +sectors or +size{K,M,G,T,P} (41947136-209715199, default 209715199): +1048576

Created a new partition 5 of type 'Linux' and of size 512 MiB.

sdz6 を作成。sdz6 は残りすべてなので、First sectorもLast sectorもデフォルト値で。

Command (m for help): n
All primary partitions are in use.
Adding logical partition 6
First sector (42997760-209715199, default 42997760):
Last sector, +sectors or +size{K,M,G,T,P} (42997760-209715199, default 209715199):

Created a new partition 6 of type 'Linux' and of size 79.5 GiB.

最終的に出来上がった分割は以下。

Command (m for help): p
Disk disk.img: 100 GiB, 107374182400 bytes, 209715200 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: **********

Device     Boot    Start       End   Sectors  Size Id Type
disk.img1           2048  18876415  18874368    9G 83 Linux
disk.img2       18876416  37750783  18874368    9G 83 Linux
disk.img3       37750784  41945087   4194304    2G 82 Linux swap / Solaris
disk.img4       41945088 209715199 167770112   80G  5 Extended
disk.img5       41947136  42995711   1048576  512M 83 Linux
disk.img6       42997760 209715199 166717440 79.5G 83 Linux

GPT パーティション

fdisk は GPT パーティションを取り扱うことができない。fdisk の GPT パーティション対応版が gdisk。fdisk と gdisk はほぼ操作感は変わらない。また、fdisk と同様にデフォルトの値がそれなりに適当。gpt パーティションを取り扱えるプログラムには parted があるけど、parted よりも gdisk を使ったほうが、アラインメントなどの観点からデフォルトの値が適切。

LBA sector size usage
(positive) (negative) (index) (sectors) (B)
0 -209715200 0 1 512B Protective MBR
1 -209715199 1 1 512B Primary GPT Header
2 -209715198 2 32 16KiB Primary partition table
...
33 -209715167 33
34 -209715166 34 2014 1007KiB -
...
2047 -209713153 2047
2048 -209713152 2048 18874368 9GiB /dev/sdz1
...
18876415 -190838785 18876415
18876416 -190838784 18876416 18874368 9GiB /dev/sdz2
...
37750783 -171964417 37750783
37750784 -171964416 37750784 4194304 2GiB /dev/sdz3
...
41945087 -167770113 41945087
41945088 -167770112 41945088 1048576 512MiB /dev/sdz4
...
42993663 -166721537 42993663
42993664 -166721536 42993664 166721503 ~ 79.5GiB /dev/sdz5
...
209715166 -34 209715166
209715167 -33 209715167 32 16KiB Secondary partition table
...
209715198 -2 209715198
209715199 -1 209715199 1 512B Secondary GPT Header

最終的に出来たパーティションは拡張パーティションがないので MBR パーティションよりもシンプル。また、パーティションを分割する数が増えると無駄な領域が増えないという点も MBR パーティションより有効。古いブートローダを使うと起動できない問題とかある様子。起動ディスクをgptパーティションで分割するのはやめておこうと思う。

dd で 100GiB のディスクイメージを作成し、gdisk でディスクサイズ確認。セクタ数は 107374182400 / 512 = 209715200 セクタ (0番から 209715199 番まで)。しかしながら、gpt パーティションとして予約済みの領域 0 番 から 33 番 (最初から 34 セクタ)、209715165 番から 209715199 番 (最後から 34 セクタ) にたいしてパーティションを作ることはできない。

# dd if=/dev/zero of=./disk.img bs=1G count=100
100+0 records in
100+0 records out
107374182400 bytes (107 GB) copied, 1923.36 s, 55.8 MB/s
# gdisk -l disk.img
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.
Disk disk.img: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): ********-****-****-****-************
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 209715133 sectors (100.0 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name

インタラクティブにパーティションを作成するために、gdiskを起動。

# gdisk disk.img
GPT fdisk (gdisk) version 0.8.10

Partition table scan:
  MBR: not present
  BSD: not present
  APM: not present
  GPT: not present

Creating new GPT entries.

sdz1 を作成。9GiB割り当てたければLast sector で+9Gと指定。

Command (? for help): n
Partition number (1-128, default 1):
First sector (34-209715166, default = 2048) or {+-}size{KMGTP}:
Last sector (2048-209715166, default = 209715166) or {+-}size{KMGTP}: +9G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

sdz2 を作成。9GiB割り当てたければLast sector で+9Gと指定。

Command (? for help): n
Partition number (2-128, default 2):
First sector (34-209715166, default = 18876416) or {+-}size{KMGTP}:
Last sector (18876416-209715166, default = 209715166) or {+-}size{KMGTP}: +9G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

sdz3 を作成。2GiB割り当てたければLast sector で+2Gと指定。パーティションタイプをswap パーティションに指定。

Command (? for help): n
Partition number (3-128, default 3):
First sector (34-209715166, default = 37750784) or {+-}size{KMGTP}:
Last sector (37750784-209715166, default = 209715166) or {+-}size{KMGTP}: +2G
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300): 8200
Changed type of partition to 'Linux swap'

gpt パーティションを使えば 4 つ以上のプライマリパーティションを作れる。このため、次に作成するのは /tmp 用の sdz4 を作成。0.5GiBは512MiBだが、+0.5Gと指定すると500MiBになるので、Last sectorは+512Mと指定。

Command (? for help): n
Partition number (4-128, default 4):
First sector (34-209715166, default = 41945088) or {+-}size{KMGTP}:
Last sector (41945088-209715166, default = 209715166) or {+-}size{KMGTP}: +512M
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

sdz5 を作成。sdz5 は残りすべてなので、First sectorもLast sectorもデフォルト値で。ディスクの最終セクタ番号は 209715199 だが、gpt パーティションに適切な最終値がデフォルトで選択されている事がわかる。

Command (? for help): n
Partition number (5-128, default 5):
First sector (34-209715166, default = 42993664) or {+-}size{KMGTP}:
Last sector (42993664-209715166, default = 209715166) or {+-}size{KMGTP}:
Current type is 'Linux filesystem'
Hex code or GUID (L to show codes, Enter = 8300):
Changed type of partition to 'Linux filesystem'

最終的に出来上がった分割は以下。

Command (? for help): p
Disk disk.img: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): ********-****-****-****-************
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        18876415   9.0 GiB     8300  Linux filesystem
   2        18876416        37750783   9.0 GiB     8300  Linux filesystem
   3        37750784        41945087   2.0 GiB     8200  Linux swap
   4        41945088        42993663   512.0 MiB   8300  Linux filesystem
   5        42993664       209715166   79.5 GiB    8300  Linux filesystem

GPT パーティション作成の自動化

自動化するにはgdiskのスクリプト版であるsgdiskを使う。上と同じパーティションが作成されたことが確認できる。

$ sgdisk \
	--zap-all \
	./disk.img
Creating new GPT entries.
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
GPT data structures destroyed! You may now partition the disk using fdisk or
other utilities.
$ sgdisk \
	--clear \
	--new='1::+9G' --typecode='1:8300' \
	--new='2::+9G' --typecode='2:8300' \
	--new='3::+2G' --typecode='3:8200' \
	--new='4::+512M' --typecode='4:8300' \
	--new='5::' --typecode='5:8300' \
	--print \
	./disk.img
Creating new GPT entries.
Disk ./disk.img: 209715200 sectors, 100.0 GiB
Logical sector size: 512 bytes
Disk identifier (GUID): ********-****-****-****-************
Partition table holds up to 128 entries
First usable sector is 34, last usable sector is 209715166
Partitions will be aligned on 2048-sector boundaries
Total free space is 2014 sectors (1007.0 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1            2048        18876415   9.0 GiB     8300
   2        18876416        37750783   9.0 GiB     8300
   3        37750784        41945087   2.0 GiB     8200
   4        41945088        42993663   512.0 MiB   8300
   5        42993664       209715166   79.5 GiB    8300
Warning: The kernel is still using the old partition table.
The new table will be used at the next reboot or after you
run partprobe(8) or kpartx(8)
The operation has completed successfully.
$ sgdisk \
	--verify \
	./disk.img

No problems found. 2014 free sectors (1007.0 KiB) available in 1
segments, the largest of which is 2014 (1007.0 KiB) in size.

ソーシャルブックマーク

  1. はてなブックマーク
  2. Google Bookmarks
  3. del.icio.us

ChangeLog

  1. Posted: 2008-03-05T20:13:32+09:00
  2. Modified: 2008-03-05T20:13:32+09:00
  3. Generated: 2023-08-27T23:09:16+09:00