2023/10/06
手持ちの古い HD(hard disk)を整理していると、Vista がインストールされた 2.5 インチ SATA HD に出会った。format して別の用途に使ってもよいのだが、ちょいと不思議に思うことがあって、調べてみることにした。手頃な教材として。僕は実例を見ないと解った気にならないから...
GPT については、まだ解らぬこといっぱい... 信頼できる文献が欲しい。
以下ではパーティション情報を調べているが、パーティションに関する操作は最も危険な操作であり、間違えるとコンピュータの全データが吹っ飛ぶ。パーティションの変更が発生する可能性がある操作は必ず不要なハードディスクに対して行うこと。
After inserting the disk to maia (linux)
ubu$ ls -l /dev/sd* brw-rw---- 1 root disk 8, 0 8月 23 18:30 /dev/sda brw-rw---- 1 root disk 8, 1 8月 23 18:30 /dev/sda1 brw-rw---- 1 root disk 8, 2 8月 23 18:30 /dev/sda2 brw-rw---- 1 root disk 8, 3 8月 23 18:30 /dev/sda3 brw-rw---- 1 root disk 8, 16 9月 27 07:25 /dev/sdb brw-rw---- 1 root disk 8, 17 9月 27 07:25 /dev/sdb1 brw-rw---- 1 root disk 8, 18 9月 27 07:25 /dev/sdb2 brw-rw---- 1 root disk 8, 21 9月 27 07:25 /dev/sdb5 ubu$
exec
sudo fdisk /dev/sdb
Command (m for help): p Disk /dev/sdb: 111.79 GiB, 120033041920 bytes, 234439535 sectors Disk model: 106X 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: 0x9247f3e2 Device Boot Start End Sectors Size Id Type /dev/sdb1 * 2048 116566015 116563968 55.6G 7 HPFS/NTFS/exFAT /dev/sdb2 116567640 234436544 117868905 56.2G 5 Extended /dev/sdb5 228765663 234436544 5670882 2.7G 82 Linux swap / Solaris
/dev/sdb1
は HPFS/NTFS/exFAT
のどれかでフォーマットされていると思われる。どれなのかは、これからは分からない。拡張パーティションの中に Linux の swap パーティションが含まれている。Vista をインストールする前に、このディスクは Linux で使われていのではないかと推測するが、昔のことですっかり忘れている。
Looking the disk on MacOS
mbook$ ls /dev/disk* /dev/disk0 /dev/disk0s2 /dev/disk1 /dev/disk2s1 /dev/disk0s1 /dev/disk0s3 /dev/disk2 /dev/disk2s5 mbook$ mbook$ diskutil list ... /dev/disk2 (external, physical): #: TYPE NAME SIZE IDENTIFIER 0: FDisk_partition_scheme *120.0 GB disk2 1: Windows_NTFS 59.7 GB disk2s1 2: Linux_Swap 2.9 GB disk2s5 mbook$NB: disk2s2 is not here.
/dev/disk2
(これは Linux の /dev/sdb2
に相当する)に関してもっと詳しい情報を提供している。これは NTFS
なのだと。
mbook$ diskutil info /dev/disk2s1 Device Identifier: disk2s1 Device Node: /dev/disk2s1 Whole: No Part of Whole: disk2 Volume Name: Mounted: Yes Mount Point: /Volumes/Untitled Partition Type: Windows_NTFS File System Personality: NTFS Type (Bundle): ntfs Name (User Visible): Windows NT File System (NTFS) OS Can Be Installed: No Media Type: Generic Protocol: USB SMART Status: Not Supported Volume UUID: 47E04978-EF4E-4277-BC05-0AE7280FDBE2 Disk Size: 59.7 GB (59680751616 Bytes) (exactly 116563968 512-Byte-Units) Device Block Size: 512 Bytes Volume Total Space: 59.7 GB (59680747520 Bytes) (exactly 116563960 512-Byte-Units) Volume Used Space: 26.3 GB (26331676672 Bytes) (exactly 51429056 512-Byte-Units) (44.1%) Volume Available Space: 33.3 GB (33349070848 Bytes) (exactly 65134904 512-Byte-Units) (55.9%) Allocation Block Size: 4096 Bytes Read-Only Media: No Read-Only Volume: Yes Device Location: External Removable Media: Fixed mbook$
NTFS の利用状態まで報告している。
mbook automatically mount the disk.
the contents is seen in /Volumes/Untitled
mbook$ ls -l /Volumes/Untitled total 9002256 drwxr-xr-x@ 1 arisawa staff 0 7 8 2011 $Recycle.Bin -rwxr-xr-x 1 arisawa staff 8192 7 8 2011 BOOTSECT.BAK drwxr-xr-x@ 1 arisawa staff 4096 7 11 2011 Boot drwxr-xr-x@ 1 arisawa staff 0 11 2 2006 Documents and Settings -rwxr-xr-x 1 arisawa staff 244 7 9 2011 Install.log drwxr-xr-x 1 arisawa staff 0 7 9 2011 Intel drwxr-xr-x@ 1 arisawa staff 0 7 8 2011 MSOCache drwxr-xr-x 1 arisawa staff 12288 7 29 2011 Program Files drwxr-xr-x@ 1 arisawa staff 4096 7 29 2011 ProgramData -rwxr-xr-x 1 arisawa staff 1841 7 9 2011 RHDSetup.log drwxr-xr-x@ 1 arisawa staff 12288 8 2 2011 System Volume Information drwxr-xr-x 1 arisawa staff 4096 7 8 2011 Users drwxr-xr-x 1 arisawa staff 20480 8 2 2011 Windows -rwxr-xr-x 1 arisawa staff 24 9 19 2006 autoexec.bat -rwxr-xr-x@ 1 arisawa staff 443912 7 11 2011 bootmgr -rwxr-xr-x 2 arisawa staff 10 9 19 2006 config.sys -rwxr-xr-x@ 1 arisawa staff 2147483648 1 3 2022 hiberfil.sys -rwxr-xr-x@ 1 arisawa staff 2461212672 1 3 2022 pagefile.sys -rwxr-xr-x 1 arisawa staff 218 1 3 2022 service.log mbook$NB: mbook can mount NTFS in this disk
hebe# ls /dev/sd* /dev/sdE0/9fat /dev/sdE0/ctl ... /dev/sdE1/raw /dev/sdU76a40/ctl /dev/sdU76a40/data /dev/sdU76a40/linuxswap /dev/sdU76a40/ntfs /dev/sdU76a40/raw /dev/sdctl hebe# ls -l /dev/sdU76a40 --rw-rw-r-- M 1062929 arisawa arisawa 0 Jan 1 1970 /dev/sdU76a40/ctl --rw-r----- M 1062929 arisawa arisawa 120033041920 Jan 1 1970 /dev/sdU76a40/data --rw-r----- M 1062929 arisawa arisawa 2903491584 Jan 1 1970 /dev/sdU76a40/linuxswap --rw-r----- M 1062929 arisawa arisawa 59680751616 Jan 1 1970 /dev/sdU76a40/ntfs --rw-r----- M 1062929 arisawa arisawa 0 Jan 1 1970 /dev/sdU76a40/raw hebe# disk/fdisk /dev/sdU76a40/data cylinder = 8225280 bytes * p1 0 7256 (7256 cylinders, 55.58 GB) HPFS empty 7256 14240 (6984 cylinders, 53.50 GB) s2 14240 14593 (353 cylinders, 2.70 GB) LINUXSWAP >>> q hebe#
Plan9(9front) では、デバイスファイルを普通のファイルとして見せている。すなわち
/dev/sdU76a40/data
/dev/sdU76a40/raw
は sata HD を制御するために存在するのであろう。
IBM-PC 互換機
パソコンの記憶容量が指数関数的に増えていく
FD(フロッピーディスク)
HD(ハードディスク)
BIOS
起動ドライブ
起動ドライブの最初のセクター(sector)の先頭は 8086 命令*
読み書きの最小単位
シリンダー(cylinder)数
1シリンダーに含まれるセクター数
HD 毎に異なる
統一的に扱うために
HD を Byte 列と考えて
論理ブロック(logical block) (単にセクターとも言われる)
512B
を単位にアドレス(0,1,2,...)やサイズを扱う。
MBR は各 HD の
LBA 0
size 1
[1]: Day 2: ブートローダ(MBR)
https://ja.tech.jar.jp/ac/2018/day02.html
HD を区分けして使う
各 HD の MBR に区分け情報
図1: MBR
枠で囲った 64B(=16✕4 B) -- from addr 0x1be
to 0x1fd
.
ここに4つの partition の情報が含まれている。続く 2B (55 AA
) は boot signature と呼ばれている。後に説明する PBR と EBR にも同じ signature が含まれている。
first partition info:
80 20 21 00 07 FE FF FF 00 08 00 00 00 A0 F2 06
00 FE FF FF 05 FE FF FF 58 AE F2 06 69 89 06 07
00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00
07 -- hpfs 05 -- extended
00 08 00 00 58 AE F2 06
00 00 08 00 --> 0x800 06 F2 AE 58 --> 0x6f2ae58
0x100000 0xde55cb000
size 情報は各々
00 A0 F2 06 69 89 06 07
0x06f2a000*512 B --> 59680751616 B 0x07068969*512 B --> 60348879360 B
first partition の first sector の byte アドレスを計算すると 0x0100000
である。
パーティションの最初のセクターは PBR (Partition Boot Record) から始まる。
この例では最初のパーティションの PBR は次のとおりである。
0100000: EB 52 90 4E 54 46 53 20 20 20 20 00 02 08 00 00 0100010: 00 00 00 00 00 F8 00 00 3F 00 FF 00 00 08 00 00 0100020: 00 00 00 00 80 00 80 00 FF 9F F2 06 00 00 00 00 0100030: 00 00 0C 00 00 00 00 00 FF 29 6F 00 00 00 00 00 0100040: F6 00 00 00 01 00 00 00 C5 99 AC 9A A8 AC 9A 42 0100050: 00 00 00 00 FA 33 C0 8E D0 BC 00 7C FB 68 C0 07 0100060: 1F 1E 68 66 00 CB 88 16 0E 00 66 81 3E 03 00 4E 0100070: 54 46 53 75 15 B4 41 BB AA 55 CD 13 72 0C 81 FB 0100080: 55 AA 75 06 F7 C1 01 00 75 03 E9 D2 00 1E 83 EC 0100090: 18 68 1A 00 B4 48 8A 16 0E 00 8B F4 16 1F CD 13 01000a0: 9F 83 C4 18 9E 58 1F 72 E1 3B 06 0B 00 75 DB A3 01000b0: 0F 00 C1 2E 0F 00 04 1E 5A 33 DB B9 00 20 2B C8 01000c0: 66 FF 06 11 00 03 16 0F 00 8E C2 FF 06 16 00 E8 01000d0: 40 00 2B C8 77 EF B8 00 BB CD 1A 66 23 C0 75 2D 01000e0: 66 81 FB 54 43 50 41 75 24 81 F9 02 01 72 1E 16 01000f0: 68 07 BB 16 68 70 0E 16 68 09 00 66 53 66 53 66 0100100: 55 16 16 16 68 B8 01 66 61 0E 07 CD 1A E9 6A 01 0100110: 90 90 66 60 1E 06 66 A1 11 00 66 03 06 1C 00 1E 0100120: 66 68 00 00 00 00 66 50 06 53 68 01 00 68 10 00 0100130: B4 42 8A 16 0E 00 16 1F 8B F4 CD 13 66 59 5B 5A 0100140: 66 59 66 59 1F 0F 82 16 00 66 FF 06 11 00 03 16 0100150: 0F 00 8E C2 FF 0E 16 00 75 BC 07 1F 66 61 C3 A0 0100160: F8 01 E8 08 00 A0 FB 01 E8 02 00 EB FE B4 01 8B 0100170: F0 AC 3C 00 74 09 B4 0E BB 07 00 CD 10 EB F2 C3 0100180: 0D 0A 41 20 64 69 73 6B 20 72 65 61 64 20 65 72 0100190: 72 6F 72 20 6F 63 63 75 72 72 65 64 00 0D 0A 42 01001a0: 4F 4F 54 4D 47 52 20 69 73 20 6D 69 73 73 69 6E 01001b0: 67 00 0D 0A 42 4F 4F 54 4D 47 52 20 69 73 20 63 01001c0: 6F 6D 70 72 65 73 73 65 64 00 0D 0A 50 72 65 73 01001d0: 73 20 43 74 72 6C 2B 41 6C 74 2B 44 65 6C 20 74 01001e0: 6F 20 72 65 73 74 61 72 74 0D 0A 00 00 00 00 00 01001f0: 00 00 00 00 00 00 00 00 80 9D B2 CA 00 00 55 AAnote "4E 54 64 53" --> NTFS
他方(すでに述べたように) second partition の first sector の byte アドレスを計算すると 0xde55cb000
であった。
de55cb000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00何も書かれていないように見えるが、512B を表示すると
de55cb000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 ... de55cb190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FE de55cb1c0: FF FF 82 FE FF FF 87 01 B0 06 E2 87 56 00 00 00 de55cb1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AAこれが拡張 partition における EBR (Extended Boot Record) の内容である。
00
)00 FE FF FF 82 FE FF FF 87 01 B0 06 E2 87 56 00
0x82
であり、これは linux swap partition を意味する。我々は拡張パーティションの中に埋め込まれたパーティションの例を見たのである。
図2: 拡張パーティションと論理パーティション
拡張パーティションは高々1個。
基本パーティション(primary partition)と拡張パーティションを合わせて4個まで。
最後。(多分1)
拡張パーティションの中には多数の仮想的なパーティション(論理パーティション)を作ることができる。
もっとも Windows 以外の OS では、拡張パーティションに頼らずとも自前で同様なことを実現していた。
図3: 拡張パーティションを利用した swp パーティションの追加
論理パーティション(Logical partition)はどのような仕組みで実現されているのか?
MBR とよく似た構造を持った EBR が論理パーティションを結びつけている。
図4: EBR によって多数の論理パーティションが追加できる
詳しくは文献[4]を見よ。
[2]: https://ja.wikipedia.org/wiki/マスターブートレコード
[3]: マスターブートレコード(MBR)とは?定義と使用方法
https://jp.minitool.com/lib/mbr-master-boot-record.html
[4]: Extended boot record
https://en.wikipedia.org/wiki/Extended_boot_record
パーティション絡みの問題は用語が混乱する嫌いがある。
Plan9 の fdisk のマニュアルでは「拡張パーティション」のことを secondary partition と言っている。
「パーティション」を FreeBSD では slice と言っていた([6])。しかし現在は partition と言っている([7])。"partition" と言うネーミングは悪くない。役割を簡明に語っているから。
Linux のマニュアル([8])には次の語が出てくる。
「論理パーティション」は Windows の世界では「論理ドライブ」と言うらしい([9])。
「論理〇〇」について
この「論理」は「仮想的な」の意味合いで使われている。つまり「〇〇」のように振る舞うが、実際には〇〇ではない、の意味合いである。
パーティションをさらに分割したとの意味合いで語るのであれば subpartition とでも言うべきであろう。
subpartition は FreeBSD も Plan9 も旧くから活用している。OS 独自の partition 形式である。
「拡張」の英語
Plan9 -- extant ([10])
FreeBSD -- ?
Linux -- extended ([5])
Windows -- extended
[6]: FreeBSD man page fdisk(8)
[7]: FreeBSD man page gpart(8)
[8]: Linux man page fdisk(8)
[9]: ベーシックおよびダイナミックディスク
https://learn.microsoft.com/ja-jp/windows/win32/fileio/basic-and-dynamic-disks
[10]: Plan9 man page prep(8)
mbr を調べる Lua で書かれた toy program:
http:mbr.lua
unix 環境では(lua がインストールされていれば)修正なしで動くはず。調べるだけで書き込みは行っていない。
Plan9 環境では、最初の行を
#!/bin/lua
次は Ubuntu での実行例。
ubu$ sudo ./mbr.lua /dev/sdb 0 mbr 512 0000000: 33 C0 8E D0 BC 00 7C 8E C0 8E D8 BE 00 7C BF 00 0000010: 06 B9 00 02 FC F3 A4 50 68 1C 06 CB FB B9 04 00 0000020: BD BE 07 80 7E 00 00 7C 0B 0F 85 10 01 83 C5 10 0000030: E2 F1 CD 18 88 56 00 55 C6 46 11 05 C6 46 10 00 0000040: B4 41 BB AA 55 CD 13 5D 72 0F 81 FB 55 AA 75 09 0000050: F7 C1 01 00 74 03 FE 46 10 66 60 80 7E 10 00 74 0000060: 26 66 68 00 00 00 00 66 FF 76 08 68 00 00 68 00 0000070: 7C 68 01 00 68 10 00 B4 42 8A 56 00 8B F4 CD 13 0000080: 9F 83 C4 10 9E EB 14 B8 01 02 BB 00 7C 8A 56 00 0000090: 8A 76 01 8A 4E 02 8A 6E 03 CD 13 66 61 73 1E FE 00000a0: 4E 11 0F 85 0C 00 80 7E 00 80 0F 84 8A 00 B2 80 00000b0: EB 82 55 32 E4 8A 56 00 CD 13 5D EB 9C 81 3E FE 00000c0: 7D 55 AA 75 6E FF 76 00 E8 8A 00 0F 85 15 00 B0 00000d0: D1 E6 64 E8 7F 00 B0 DF E6 60 E8 78 00 B0 FF E6 00000e0: 64 E8 71 00 B8 00 BB CD 1A 66 23 C0 75 3B 66 81 00000f0: FB 54 43 50 41 75 32 81 F9 02 01 72 2C 66 68 07 0000100: BB 00 00 66 68 00 02 00 00 66 68 08 00 00 00 66 0000110: 53 66 53 66 55 66 68 00 00 00 00 66 68 00 7C 00 0000120: 00 66 61 68 00 00 07 CD 1A 5A 32 F6 EA 00 7C 00 0000130: 00 CD 18 A0 B7 07 EB 08 A0 B6 07 EB 03 A0 B5 07 0000140: 32 E4 05 00 07 8B F0 AC 3C 00 74 FC BB 07 00 B4 0000150: 0E CD 10 EB F2 2B C9 E4 64 EB 00 24 02 E0 F8 24 0000160: 02 C3 49 6E 76 61 6C 69 64 20 70 61 72 74 69 74 0000170: 69 6F 6E 20 74 61 62 6C 65 00 45 72 72 6F 72 20 0000180: 6C 6F 61 64 69 6E 67 20 6F 70 65 72 61 74 69 6E 0000190: 67 20 73 79 73 74 65 6D 00 4D 69 73 73 69 6E 67 00001a0: 20 6F 70 65 72 61 74 69 6E 67 20 73 79 73 74 65 00001b0: 6D 00 00 00 00 62 7A 99 E2 F3 47 92 00 00 80 20 00001c0: 21 00 07 FE FF FF 00 08 00 00 00 A0 F2 06 00 FE 00001d0: FF FF 05 FE FF FF 58 AE F2 06 69 89 06 07 00 00 00001e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00001f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA info partition 0 0000000: 80 20 21 00 07 FE FF FF 00 08 00 00 00 A0 F2 06 active type: 07 hpfs xlba 00 08 00 00 lba 0000800 xsize 00 A0 F2 06 size 116563968 end addr 6f2a800 0000800 (lba) 0100000 (Ba) fdump 1048576 32 0100000: EB 52 90 4E 54 46 53 20 20 20 20 00 02 08 00 00 0100010: 00 00 00 00 00 F8 00 00 3F 00 FF 00 00 08 00 00 0100020: 00 00 00 00 80 00 80 00 FF 9F F2 06 00 00 00 00 0100030: 00 00 0C 00 00 00 00 00 FF 29 6F 00 00 00 00 00 0100040: F6 00 00 00 01 00 00 00 C5 99 AC 9A A8 AC 9A 42 0100050: 00 00 00 00 FA 33 C0 8E D0 BC 00 7C FB 68 C0 07 0100060: 1F 1E 68 66 00 CB 88 16 0E 00 66 81 3E 03 00 4E 0100070: 54 46 53 75 15 B4 41 BB AA 55 CD 13 72 0C 81 FB 0100080: 55 AA 75 06 F7 C1 01 00 75 03 E9 D2 00 1E 83 EC 0100090: 18 68 1A 00 B4 48 8A 16 0E 00 8B F4 16 1F CD 13 01000a0: 9F 83 C4 18 9E 58 1F 72 E1 3B 06 0B 00 75 DB A3 01000b0: 0F 00 C1 2E 0F 00 04 1E 5A 33 DB B9 00 20 2B C8 01000c0: 66 FF 06 11 00 03 16 0F 00 8E C2 FF 06 16 00 E8 01000d0: 40 00 2B C8 77 EF B8 00 BB CD 1A 66 23 C0 75 2D 01000e0: 66 81 FB 54 43 50 41 75 24 81 F9 02 01 72 1E 16 01000f0: 68 07 BB 16 68 70 0E 16 68 09 00 66 53 66 53 66 0100100: 55 16 16 16 68 B8 01 66 61 0E 07 CD 1A E9 6A 01 0100110: 90 90 66 60 1E 06 66 A1 11 00 66 03 06 1C 00 1E 0100120: 66 68 00 00 00 00 66 50 06 53 68 01 00 68 10 00 0100130: B4 42 8A 16 0E 00 16 1F 8B F4 CD 13 66 59 5B 5A 0100140: 66 59 66 59 1F 0F 82 16 00 66 FF 06 11 00 03 16 0100150: 0F 00 8E C2 FF 0E 16 00 75 BC 07 1F 66 61 C3 A0 0100160: F8 01 E8 08 00 A0 FB 01 E8 02 00 EB FE B4 01 8B 0100170: F0 AC 3C 00 74 09 B4 0E BB 07 00 CD 10 EB F2 C3 0100180: 0D 0A 41 20 64 69 73 6B 20 72 65 61 64 20 65 72 0100190: 72 6F 72 20 6F 63 63 75 72 72 65 64 00 0D 0A 42 01001a0: 4F 4F 54 4D 47 52 20 69 73 20 6D 69 73 73 69 6E 01001b0: 67 00 0D 0A 42 4F 4F 54 4D 47 52 20 69 73 20 63 01001c0: 6F 6D 70 72 65 73 73 65 64 00 0D 0A 50 72 65 73 01001d0: 73 20 43 74 72 6C 2B 41 6C 74 2B 44 65 6C 20 74 01001e0: 6F 20 72 65 73 74 61 72 74 0D 0A 00 00 00 00 00 01001f0: 00 00 00 00 00 00 00 00 80 9D B2 CA 00 00 55 AA info partition 1 0000000: 00 FE FF FF 05 FE FF FF 58 AE F2 06 69 89 06 07 active type: 05 extended xlba 58 AE F2 06 lba 6f2ae58 xsize 69 89 06 07 size 117868905 end addr df937c1 6f2ae58 (lba) de55cb000 (Ba) fdump 59682631680 32 de55cb000: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb010: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb020: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb030: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb040: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb050: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb060: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb070: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb080: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb090: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0c0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb0f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb100: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb110: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb120: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb130: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb140: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb150: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb160: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb170: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb180: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb190: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1a0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1b0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 FE de55cb1c0: FF FF 82 FE FF FF 87 01 B0 06 E2 87 56 00 00 00 de55cb1d0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1e0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 de55cb1f0: 00 00 00 00 00 00 00 00 00 00 00 00 00 00 55 AA info partition 2 info partition 3 ubu$
僕は今の所、これで間に合っているが、partition type に関してはもっとサポートが必要だろうと思われる。必要なデータは文献[2]に載っているので追加すればよい。mbr.lua
は小さなプログラムだから、追加場所は簡単に見つかるはずである。
同じことは Python でどうだろうか? と考えてちょっと調べたら、Python は file open のときに text file と binary file を区別している。Lua は区別しない。なぜ区別が必要なのか?
もうかなり記憶が薄れているが... 昔々、MS-DOS ができた頃、MS-DOS では text file と binary ファイルの区別が必要であった。その頃はファイルはセクター単位で管理されていて、ファイルの文字数も然り。
プログラムコードであれば、後ろに無意味なデータがついていても問題はない。しかしテキストデータでは困る。そこでテキストのデータは ctlr-Z のコード(0x1a
) で終わるルールにしていた。
やがて MS-DOS は前身の CP/M から一歩進んでファイルに含まれている文字数の情報を持つようになった。この情報に基づいてファイルを読み取れば 0x1a
は要らないはずある。あれから50年、このルールがまだ使われているとも思えない。
当時のルールが未だ生きている場面が行の定義である。「行」と言うからにはテキストを想定している。unix の「行」は LF
コード1文字で終わるが、MS-DOS は (従って Windows も) CR
LF
の 2文字で終わる。行の読み取り関数は行末記号に応じた処理をしなくてはならない。つまり行の読み取り関数の問題なのである。このことを open mode と絡めると複雑になってくる。行読み取り関数は open の mode を参照しなくても、この程度の違いは処理できる。
Python の open/seek の仕様を眺めていると、旧い C の仕様に縛られていると感じる。対して Lua は無くてもよいものは大胆に捨てている。その方が扱いやすい。