sfdiskでパーティショニング

忘れてしまいそうなので備忘録。

SDカードのパーティショニングをするとき、fdiskを使うんだけどfdiskってスクリプトにするの面倒くさいよね。

シェルスクリプトで<<EOFまでとかするのってあまり好きじゃないんだよね。

ついつい、忘れがちなんだけどsfdiskを使う。

ディスクのサイズを見る

引数'-l'でデバイス情報を取得する。

$ sudo sfdisk -l /dev/sdc
Disk /dev/sdc: 14.8 GiB, 15854469120 bytes, 30965760 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: 0x0007c9c0

ディスク情報のファイルを作成する

次のようにstartとsizeにセクタ番号とセクタ数を書くことでパーティションを設定します。

label: dos
label-id: 0x0007c9c0
device: /dev/sdc
unit: sectors

/dev/sdc1 : start=        2048, size=     2097152, type=83, bootable
/dev/sdc2 : start=     2099200, size=    28866560, type=c

これをdisk.txtとする。

ディスクの書き換え

ディスク情報のファイルを次のようにsfdiskに流し込みます。

$ sudo sfdisk /dev/sdc < disk.txt
Checking that no-one is using this disk right now ... OK

Disk /dev/sdc: 14.8 GiB, 15854469120 bytes, 30965760 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: 0x0007c9c0

Old situation:

>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Script header accepted.
>>> Created a new DOS disklabel with disk identifier 0x0007c9c0.
/dev/sdc1: Created a new partition 1 of type 'Linux' and of size 1 GiB.
/dev/sdc2: Created a new partition 2 of type 'W95 FAT32 (LBA)' and of size 13.8 GiB.
/dev/sdc3: Done.

New situation:
Disklabel type: dos
Disk identifier: 0x0007c9c0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdc1  *       2048  2099199  2097152    1G 83 Linux
/dev/sdc2       2099200 30965759 28866560 13.8G  c W95 FAT32 (LBA)

The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

ディスクの確認

引数-lで出来上がったディスクを確認する。

$ sudo sfdisk -l /dev/sdc
Disk /dev/sdc: 14.8 GiB, 15854469120 bytes, 30965760 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: 0x0007c9c0

Device     Boot   Start      End  Sectors  Size Id Type
/dev/sdc1  *       2048  2099199  2097152    1G 83 Linux
/dev/sdc2       2099200 30965759 28866560 13.8G  c W95 FAT32 (LBA)

既にディスクがあるなら

バックアップの意味も兼ねて既にディスクがあるのなら引数-dでディスク情報を抜き出すことができる。

$ sudo sfdisk -d /dev/sdc
label: dos
label-id: 0x0007c9c0
device: /dev/sdc
unit: sectors

/dev/sdc1 : start=        2048, size=     2097152, type=83, bootable
/dev/sdc2 : start=     2099200, size=    28866560, type=c
write: 2019/03/14/ 22:46:48