Macbook Air SSD換装: 試行錯誤終了(HDD実験完了・GUIDパーティションの詳細)

「Macbook Air SSD換装」の話題、第5回目です。今回は長文です。

注意: 下に書いたことも正確ではないことがわかりました。 「インターネット復元」でも、たしかにFilevault2が利用できる状態に復元パーティションを作成できますが、それはLionのクリーンインストールを行った場合のみです。 つまり、復元画面で「MacOS Xを再インストール」を選択した場合のみ、復元パーティションが作成されます。 復元画面のメニューから「Time Machineバックアップから復元」を選んだ場合は、たとえ元のシステムでFileVault2を利用していたとしても、復元時にFileVault2の実行に必要な隠しパーティションが削除されてしまいます(これはバグといってもいい問題点だと思います)。そのため、Time Machineから復元しつつFileVault2を利用するには、一旦「MacOS Xを再インストール」でLionをクリーンインストールし(ユーザ名は普段使っているのとは別の一時的な名前でインストールする)、その後「移行アシスタント」などを用いてTime Machineからデータをリストアし、一時的なユーザを削除するという手順をとる必要があります(あるいはインストーラの最後の方でTime Machineからの移行を選択する)。これについては後日まとめます。 (2012/06/15 追記)

注意: いろいろ長く書いていますが、筆者のFileVault2の実装への理解に間違いがあり、 このような面倒な手段を行わなくても、 「インターネット復元」でFilevault2が利用できる状態に復元パーティションを作成できるようです。(補足:これは正確ではありません。上に追記した「注意」をお読みください)なーんだ。 まあ、その作業を手動でやった記録として、 こちらのページは良かったら読んでみてくださいませ(トホホ)。 なお、ハードウェア的な問題については、 もしかしたら一部の人に意味があるかも知れません。(2011/11/17 追記)

Macbook Air (mid 2011, 11-inch)のSSDを換装しつつ、 Lion復元パーティションを新しいSSDに作成し、 FileVault2による暗号化を可能とするための試行錯誤まっただ中。 只今、1TBのUSBハードディスクを使って、 復元パーティションを作成するための実験を行なっています。

警告

なお、今回が一番重要な試行錯誤の部分です。 今回書いた内容の理解が不十分な場合、 想定外の事態が生じた際に対応は難しくなると思います。 ここを参考にして換装に挑戦する前に、確実に今回の内容を理解するようにしてください。

また、デバイス名(/dev/disk*)は、すべての環境で番号が同一に振られるとは限りません。 間違えた番号に対して操作を行った場合、現在利用しているパーティションだけではなく、通常の手順では復元不能な、内蔵起動ドライブの「復元パーティション」を破壊してしまう可能性もあります。

ここに記述された内容のコマンドを実行する場合は、 自分が何をしようとしているのか確実に理解した上で、 必要があれば適切なデバイス番号などに読み替えて実行するようにしてください。

作業を行う前は、かならず十分なバックアップを行い、またLion復旧ディスクアシスタントでUSBメモリから起動可能な復旧ディスクを作成しておいてください。 また、このページの最初で解説する、復元パーティションのバックアップを必ず作成し、これも安全な場所に保存しておいてください。

なお、この記事の内容を参考に操作を行い、システムに何らかの問題が生じたとしても、 筆者は一切の責任を負いません。

下準備 (gdiskのインストールと復元パーティションのバックアップ等)

さて、先ほどの試行錯誤で、GUIDパーティションテーブルを直接いじらずには、 復元パーティションの作成は出来ないだろうという結論に達したので、 腹を決めてちょっと調べ物。 OSXには、gptというコマンドがあって、 これでGUIDパーティションテーブルを扱えるらしい。

しかし、gptを使ってみようとすると、

% sudo gpt show /dev/disk1
Password:
gpt show: unable to open device '/dev/disk1': No such file or directory

なんて風にエラーが出て、うまく使えない。 ここで悩んでも仕方が無いので、 Unix系でもう一つのGUIDパーティションテーブル操作系ツールらしいgdiskを使ってみることに。 現在の最新版は0.8.1なのですが、 これはパーティションの名前付けにバグがあるようなので、 前のバージョンの0.7.2がお勧めです。 MacOS X用の0.7.2のバイナリはこちらからダウンロードできます。

ここで作業の前に、万が一の事態に備えて、復元パーティション自体のバックアップを取っておきましょう。起動ドライブが通常のdisk0であれば、復元パーティションは/dev/disk0s3に入っています。次の手順で、カレントディレクトリのrecoveryhd.binというファイルに、バックアップが取得できます。

% sudo dd if=/dev/disk0s3 of=recoveryhd.bin
Password:
1269536+0 records in
1269536+0 records out
650002432 bytes transferred in 42.831785 secs (15175703 bytes/sec)
% ls -l recoveryhd.bin
-rw-r--r--  1 root  admin  650002432 11  6 19:28 recoveryhd.bin

万が一の際も、USBメモリなどにに作った起動用復元ドライブと、 上記の復元ドライブのイメージがあれば、このページに書いた内容があれば、 元の状態に起動用SSDを再設定することが可能と思われます。 念のために、USB HDDに単体で起動可能なLionのシステムを作成しておけば、 万が一の作業も楽になるでしょう。

USB外付ドライブの初期化

まず、ディスクユーティリティからUSBハードディスク全体の「消去」を行い、 「hoge」という名前の1パーティションのみにします (新パーティション名を「Macintosh HD」のような名前にすると、 現在の内蔵SSDのパーティションと区別がつかずに操作ミスを招きそうなので、全く別の名前にしました)。

ディスクユーティリティ

外付USB HDDと内蔵SSDのパーティション状態

ここからgdiskでの作業を始めます。今はUSBのディスクが/dev/disk1なので、 gdisk /dev/disk1で起動します。 なお、USBのディスクが/dev/disk2以降になることもありますので、 その場合は以下のdisk1をdisk2以降の適切な番号に読み替えてください。

gdiskは、独自のコマンドプロンプト体系を持っていて、 その中でサブコマンドを打ち込んでいく形式で用います。 パーティション情報の表示は「p」で、終了は「q」 (ヘルプは「?」で表示されます)。

% gdisk /dev/disk1
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1953262983   931.2 GiB   AF00  hoge
Command (? for help): q
%

これを見てわかることは、ディスクには2つのパーティションがあり、 1番が隠しパーティション「EFI System Partition」で、2番が「hoge」。

「EFI System Partition」は、MacOS Xでは通常は使用されていないものの、 ソフトウェアアップデートでEFIアップデートがあった場合、 EFIのイメージはこの領域に格納されることになっているようです。 そのため、この領域が正常に存在しないとEFIアップデートは不可能となってしまいます。 「hoge」はもちろんHFS+のパーティション。

一方で、既存のMacbook Air内蔵SSDの方はどうなっているかというと、次のように表示されます。こちらはgdiskの実行時にsudoが必要となります。

% sudo gdisk /dev/disk0
Password:
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk0: 490234752 sectors, 233.8 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 490234718
Partitions will be aligned on 8-sector boundaries
Total free space is 13 sectors (6.5 KiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI system partition
   2          409640       488965175   233.0 GiB   AF00  Macintosh HD
   3       488965176       490234711   619.9 MiB   AB00  Recovery HD

Command (? for help): q
%

1番はタイプEF00番(EFI System Partition)は200MiBでUSB HDDと同じ、 2番パーティションは「Macintosh HD」としてタイプAF00番(Apple Filesystem)の通常のHFS+のファイルシステム、 そして3番に新しいタイプAB00番(Apple Boot)の「Recovery HD」という619.9MiBのパーティションがある。

作成するパーティション情報の計算

双方のデータから、1TB HDDに新しく作るべきパーティションの位置とサイズを考えると次のようになります。要するに、現在2番パーティションにある「hoge」の931.2GiBの領域の末尾の位置に、619.9MiBの「Recovery HD」パーティションを3番として作成し、その分2番パーティションを切り詰めれば良いのでしょう。細かく計算すると次のようになります。

  • 1番のEFI System Partitionはそのまま放置して良い。

  • 3番のRecovery HDは、サイズは現在の内蔵SSDのレイアウトから490234711+1-488965176を計算して、1269536ブロック。

    1TB HDDの現在の2番パーティションが409640番ブロックから1953262983番ブロックなので、ここから3番パーティションの開始ブロックを計算すると、1953262983+1-1269536で、1951993448番ブロックとなる。

    終了位置は現在の2番パーティションの終了位置である1953262983番ブロック。

  • 2番のファイルシステムは、開始位置は変わらず409640番ブロック。

    終了ブロックは3番パーティションの開始ブロック1951993448番の1つ前の1951993447番ブロック。

まとめると、結果は次のようになります。

番号タイプ開始ブロック終了ブロック名称
1EF0040409,639EFI System Partition
2AF00409,6401,951,993,447hoge
3AB001,951,993,4481,953,262,983Recovery HD

パーティション情報の書き込み

そこで、このデータを実際に1TBディスクに書きこんでみましょう。 1番パーティションは触らずに、2番パーティションを削除、2番と3番パーティションを作成、という手順で作成します。まずは、「hoge」がマウントされていた場合は、 当然Finderから取り出しボタンをクリックして、アンマウントしておきましょう。

Finderからhogeをアンマウント

gdiskでの作業手順は次のようになります。 まずはパーティションの状態を確認します。 ちなみに、途中でCtrl-Cなどでgdiskを停止した場合は、 もう一度最初からやり直しになるので注意。

% gdisk /dev/disk1
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1953262983   931.2 GiB   AF00  hoge

2番パーティションを削除します。削除コマンドは「d」です。 これによって、1番のEFI System Partitionのみが残ります。

Command (? for help): d
Partition number (1-2): 2

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 1953115501 sectors (931.3 GiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition

新しい2番パーティションを作成します。作成コマンドは「n」です。 開始、終了ブロックの番号は、先ほどの計算どおりに入力します。 タイプの入力の際は「L」(大文字)で指定可能なタイプのリストを表示できます。 今回はApple HFS/HFS+でよいので、af00を入力します。

Command (? for help): n
Partition number (2-128, default 2): 2
First sector (34-1953525134, default = 409640) or {+-}size{KMGTP}: 409640
Last sector (409640-1953525134, default = 1953525134) or {+-}size{KMGTP}: 1951993447
Current type is 'Apple HFS/HFS+'
Hex code or GUID (L to show codes, Enter = AF00): L
0700 Microsoft basic data  0c01 Microsoft reserved    2700 Windows RE          
4200 Windows LDM data      4201 Windows LDM metadata  7501 IBM GPFS            
7f00 ChromeOS kernel       7f01 ChromeOS root         7f02 ChromeOS reserved   
8200 Linux swap            8300 Linux filesystem      8301 Linux reserved      
8e00 Linux LVM             a500 FreeBSD disklabel     a501 FreeBSD boot        
a502 FreeBSD swap          a503 FreeBSD UFS           a504 FreeBSD ZFS         
a505 FreeBSD Vinum/RAID    a800 Apple UFS             a901 NetBSD swap         
a902 NetBSD FFS            a903 NetBSD LFS            a904 NetBSD concatenated 
a905 NetBSD encrypted      a906 NetBSD RAID           ab00 Apple boot          
af00 Apple HFS/HFS+        af01 Apple RAID            af02 Apple RAID offline  
af03 Apple label           af04 AppleTV recovery      be00 Solaris boot        
bf00 Solaris root          bf01 Solaris /usr & Mac Z  bf02 Solaris swap        
bf03 Solaris backup        bf04 Solaris /var          bf05 Solaris /home       
bf06 Solaris alternate se  bf07 Solaris Reserved 1    bf08 Solaris Reserved 2  
bf09 Solaris Reserved 3    bf0a Solaris Reserved 4    bf0b Solaris Reserved 5  
c001 HP-UX data            c002 HP-UX service         ef00 EFI System          
ef01 MBR partition scheme  ef02 BIOS boot partition   fd00 Linux RAID  

Hex code or GUID (L to show codes, Enter = af00): af00
Changed type of partition to 'Apple HFS/HFS+'

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 1531693 sectors (747.9 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  Apple HFS/HFS+

そして3番パーティションを作成します。今度はタイプはab00(Apple boot)です。 これが復元パーティションとなります。

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-1953525134, default = 1951993448) or {+-}size{KMGTP}: 1951993448
Last sector (1951993448-1953525134, default = 1953525134) or {+-}size{KMGTP}: 1953262983
Current type is 'Apple HFS/HFS+'
Hex code or GUID (L to show codes, Enter = AF00): ab00
Changed type of partition to 'Apple boot'
Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  Apple HFS/HFS+
   3      1951993448      1953262983   619.9 MiB   AB00  

そして、2番と3番パーティションの名前を設定しましょう。 名前の設定は「c」コマンドです。

Command (? for help): c
Partition number (1-3): 2
Enter name: hoge

Command (? for help): c
Partition number (1-3): 3
Enter name: Recovery HD

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge
   3      1951993448      1953262983   619.9 MiB   AB00  Recovery HD

最後に、「w」コマンドでパーティション情報を書き込みます。

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT).
Warning: The kernel may continue to use old or deleted partitions.
You should reboot or remove the drive.
The operation has completed successfully.

Lionがまた新しく出来たhogeパーティションをマウントしてしまいますが、 すぐにFinderから取り出しボタンで再度アンマウントしておきましょう。 そして、もう一度gdiskコマンドで、確実にパーティションが書きこまれていることを確認します。

% gdisk /dev/disk1
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge
   3      1951993448      1953262983   619.9 MiB   AB00  Recovery HD

Command (? for help): q

ちなみに、最後の128MiBの空き領域は、何に使うのかわからないけれども、 一応そのまま残しておいた方がよいと私のゴーストが囁いています。

コマンドラインでのフォーマットとその後始末

フォーマットもコマンドラインから行いましょう。 これはMacOS Xのdiskutilコマンドを用いて行います。 まずはパーティションの確認を行います。

% diskutil list /dev/disk1
/dev/disk1
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *1.0 TB     disk1
   1:                        EFI                         209.7 MB   disk1s1
   2:                  Apple_HFS hoge                    999.2 GB   disk1s2
   3:                 Apple_Boot                         650.0 MB   disk1s3

そしてパーティション2番をHFS+で再フォーマットします。

% diskutil eraseVolume HFS+ hoge /dev/disk1s2
Started erase on disk1s2 hoge
Unmounting disk
Erasing
Initialized /dev/rdisk1s2 as a 931 GB HFS Plus volume
Mounting disk
Finished erase on disk1s2 hoge

ところが、ここでもう一回gdiskでパーティションテーブルを確認すると…。

% gdisk /dev/disk1
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 1531693 sectors (747.9 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge

Command (? for help): 

なんと3番パーティションが消えてしまいました。 とは言っても、2番パーティションの位置もサイズも変わっていないようなので、 もう一度3番パーティションを作り直します。 どうやら、diskutil eraseVolumeは、Apple bootパーティションを消去してしまうようです。

Command (? for help): n
Partition number (3-128, default 3): 3
First sector (34-1953525134, default = 1951993448) or {+-}size{KMGTP}: 1951993448
Last sector (1951993448-1953525134, default = 1953525134) or {+-}size{KMGTP}: 1953262983
Current type is 'Apple HFS/HFS+'
Hex code or GUID (L to show codes, Enter = AF00): ab00
Changed type of partition to 'Apple boot'
Bogus UTF-16 name found in GPTPart::SetName()! Name not changed!

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge
   3      1951993448      1953262983   619.9 MiB   AB00  

Command (? for help): c
Partition number (1-3): 3
Enter name: Recovery HD

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge
   3      1951993448      1953262983   619.9 MiB   AB00  Recovery HD

Command (? for help): w

Final checks complete. About to write GPT data. THIS WILL OVERWRITE EXISTING
PARTITIONS!!

Do you want to proceed? (Y/N): y
OK; writing new GUID partition table (GPT).
Warning: The kernel may continue to use old or deleted partitions.
You should reboot or remove the drive.
The operation has completed successfully.
%

復元パーティションの内容をコピー

この状態で、やっと準備は出来ました。 いよいよ、復元パーティションの内容を内蔵SSDからコピーしましょう。 もちろん、ifとして先ほど取ったバックアップファイルrecoveryhd.binを利用することも可能です。

% sudo dd if=/dev/disk0s3 of=/dev/disk1s3
Password:
1269536+0 records in
1269536+0 records out
650002432 bytes transferred in 45.074774 secs (14420537 bytes/sec)

再度Finderからhogeをアンマウントし、USBのディスクを抜き、もう一度挿してパーティションテーブルを確認しましょう。

% gdisk /dev/disk1
GPT fdisk (gdisk) version 0.7.2

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

Found valid GPT with protective MBR; using GPT.

Command (? for help): p
Disk /dev/disk1: 1953525168 sectors, 931.5 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 1953525134
Partitions will be aligned on 8-sector boundaries
Total free space is 262157 sectors (128.0 MiB)

Number  Start (sector)    End (sector)  Size       Code  Name
   1              40          409639   200.0 MiB   EF00  EFI System Partition
   2          409640      1951993447   930.6 GiB   AF00  hoge
   3      1951993448      1953262983   619.9 MiB   AB00  Recovery HD

Command (? for help): q
%

ドライブの内容をコピー

あとは、テストのために再度、Carbon Copy Clonerを使って、 現在の内蔵SSDのパーティション2の内容を、外付けUSBドライブのパーティション2(hoge)にクローンします。

carbon copy cloner

時間がかかるのでごはんでも食べましょう。この話題はさらに続きます