ほっしーの技術ネタ備忘録

技術ネタの備忘録です。基本的に私が忘れないためのものです。他の人の役にも立つといいなぁ。

MBR 形式のディスクのデータ

MBR の構造

MBR は HDD の第0セクタにあります。

# dd if=/dev/ad0 of=tmp.bin iseek=0 bs=512 count=1
# hexdump -Cv tmp.bin
00000000  fc 31 c0 8e c0 8e d8 8e  d0 bc 00 7c be 1a 7c bf
00000010  1a 06 b9 e6 01 f3 a4 e9  00 8a 31 f6 bb be 07 b1
<中略>
00000190  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90
000001a0  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90
000001b0  90 90 90 90 90 90 90 90  90 90 90 90 90 80 80 01
000001c0  01 00 a5 0f ff ff 3f 00  00 00 71 fd ff 18 00 ff
000001d0  ff ff a5 0f ff ff b0 fd  ff 18 80 62 38 21 00 00
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa

ブートローダは /boot にも同じものが置いてあります。

# hexdump -Cv mbr
00000000  fc 31 c0 8e c0 8e d8 8e  d0 bc 00 7c be 1a 7c bf
00000010  1a 06 b9 e6 01 f3 a4 e9  00 8a 31 f6 bb be 07 b1
<中略>
00000190  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90
000001a0  90 90 90 90 90 90 90 90  90 90 90 90 90 90 90 90
000001b0  90 90 90 90 90 90 90 90  90 90 90 90 90 80 00 00
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001f0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 55 aa

ただし、パーティションテーブルは空っぽです。

BSD スライスの構造

第0セクタ

ここには boot1 が置いてあります。

# dd if=/dev/ad0s1 of=tmp.bin iseek=0 bs=512 count=1
# hexdump -Cv tmp.bin
00000000  eb 3c 00 00 00 00 00 00  00 00 00 00 02 00 00 00
00000010  00 00 00 00 00 00 00 00  12 00 02 00 00 00 00 00
<中略>
00000190  46 0a d0 e3 00 5e 05 28  46 02 77 88 c3 52 65 61
000001a0  64 00 42 6f 6f 74 00 20  65 72 72 6f 72 0d 0a 00
000001b0  80 90 90 90 90 90 90 90  90 90 90 90 90 90 00 00
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 00
000001f0  01 00 a5 fe ff ff 00 00  00 00 50 c3 00 00 55 aa

バックアップはこちら。

# hexdump -Cv /boot/boot1
00000000  eb 3c 00 00 00 00 00 00  00 00 00 00 02 00 00 00
00000010  00 00 00 00 00 00 00 00  12 00 02 00 00 00 00 00
<中略>
00000190  46 0a d0 e3 00 5e 05 28  46 02 77 88 c3 52 65 61
000001a0  64 00 42 6f 6f 74 00 20  65 72 72 6f 72 0d 0a 00
000001b0  80 90 90 90 90 90 90 90  90 90 90 90 90 90 00 00
000001c0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001d0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
000001e0  00 00 00 00 00 00 00 00  00 00 00 00 00 00 80 00
000001f0  01 00 a5 fe ff ff 00 00  00 00 50 c3 00 00 55 aa

何バイトか異なります。なぜ?

第1セクタ以降

ここはパーティションテーブルと boot2 があります。

# dd if=/dev/ad0s1 of=tmp.bin iseek=1 bs=512 count=1
# hexdump -Cv tmp.bin
00000000  57 45 56 82 00 00 00 00  61 6d 6e 65 73 69 61 63
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000020  00 00 00 00 00 00 00 00  00 02 00 00 3f 00 00 00
00000030  10 00 00 00 64 59 06 00  f0 03 00 00 71 fd ff 18
00000040  00 00 00 00 00 00 00 00  10 0e 01 00 00 00 00 00
00000050  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000060  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000070  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000080  00 00 00 00 57 45 56 82  08 79 08 00 00 20 00 00
00000090  00 00 00 00 00 00 60 00  4f 00 00 00 00 00 00 00
000000a0  07 00 00 00 00 00 20 00  4f 00 60 00 00 00 00 00
000000b0  01 00 00 00 71 fd ff 18  3f 00 00 00 00 00 00 00
000000c0  00 00 00 00 00 00 a0 00  4f 00 80 00 00 00 00 00
000000d0  07 00 00 00 00 00 a0 00  4f 00 20 01 00 00 00 00
000000e0  07 00 00 00 00 00 80 02  4f 00 c0 01 00 00 00 00
000000f0  07 00 00 00 00 00 40 01  4f 00 00 08 00 00 00 00
00000100  07 00 00 00 00 00 00 0f  4f 00 40 09 00 00 00 00
00000110  07 00 00 00 eb 0e 42 54  58 01 02 80 f6 0f 90 06
00000120  00 20 00 00 fa 31 c0 8e  d0 bc 00 18 8e c0 8e d8
00000130  66 6a 02 66 9d bf 00 5e  b9 00 19 f3 ab bb 29 95
<省略>

その他の情報は src/sys/geom/geom_bsd_enc.c にパーサが書いてあります。

こちらのバックアップは /boot/boot2

# hexdump -Cv /boot/boot2
00000000  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000010  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
<中略>
00000100  00 00 00 00 00 00 00 00  00 00 00 00 00 00 00 00
00000110  00 00 00 00 eb 0e 42 54  58 01 02 80 f6 0f 90 06
00000120  00 20 00 00 fa 31 c0 8e  d0 bc 00 18 8e c0 8e d8
00000130  66 6a 02 66 9d bf 00 5e  b9 00 19 f3 ab bb 29 95

テーブルが空っぽなのも他と同様。
/boot/boot2 は 8kB 弱あるので、16セクタほど続いている計算になります。

UFS2 の先頭

src/sys/ufs/ffs/fs.h によると、スーパーブロックの場所は

#define	SBLOCK_UFS2	65536

で、構造体は

struct fs {
	<中略>
	int32_t	fs_magic;	/* magic number */
};

と最後にマジックナンバーがあります。構造体のサイズは

CTASSERT(sizeof(struct fs) == 1376);

で、マジックナンバー

#define	FS_UFS2_MAGIC	0x19540119	/* UFS2 fast filesystem magic number */

とのこと。


実際にダンプを見てみると

# dd if=/dev/ad0s1a of=tmp.bin iseek=128 bs=512 count=3
# hexdump -C tmp.bin
<中略>
00000550  00 00 00 00 00 00 00 00  00 00 00 00 19 01 54 19
<省略>

なるほど確かに。