Skip to content

Instantly share code, notes, and snippets.

@hyslion
Last active July 18, 2021 16:24
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save hyslion/50d24b2686da48216a72 to your computer and use it in GitHub Desktop.
Save hyslion/50d24b2686da48216a72 to your computer and use it in GitHub Desktop.

不用 bootcamp 來安裝 Windows 7 並擁有額外分割區的方法

實踐時所用的環境

  • Mac OS X Mavericks (10.9.x)
  • Mac mini
  • GPT fdisk (0.8.10)
  • Windows 7

行前須知

  • 操作不慎可能導致資料遺失,請謹慎操作
  • bootcamp 其實幫你作的事情就是:縮小 Mac 所在的磁區(含 Recovery HD),再新加一個分割區,然後編寫 Hybrid MBR,最後再設定好開機模式以及目標磁碟,好讓 Windows 7安裝程式能用 Hybrid MBR + Legacy mode 來開機。
  • GPT fdisk 是命令列工具,得有心理準備。
  • 請事先準備好 Windows 7 安裝媒介以及 bootcamp 提供的 Windows 7 驅動程式。
  • 想要知道細節需要自己去了解 GPT (GUID Partition Table) 以及 Hybrid MBR(Master Boot Record)之間的關係,這邊只會簡單介紹。
  • MBR 先天限制最多只能有四個磁區,Hybrid MBR 不建議使用延伸磁碟區,會導致額外的問題發生,加上 Hybrid MBR 只是一個為了相容性的過度機制,所以請在安裝前規劃好用途。安裝 Windows 7 後請不要隨意更動磁碟區配置表,有可能造成損壞(除非你很清楚自己正在做什麼)
  • 雖然從 Windows 7 開始支援以 EFI + GPT 的方式來開機,但是 Mac 所用的 EFI 版本不足以讓 Windows 7 使用這種方式來開機。在蘋果公司對硬體做適當的更動以前,可能都要必須要以 Legacy mode 模擬 BIOS 的行為來開機。
  • 請使用具有管理員權限的帳號來操作以下內容。
  • 我認為這方法其他作業系統應該也行,不一定要 Windows 7。只是如果想要安裝 Linux or BSD,也許你會需要先選擇用哪種開機啟動器(boot loader)來開機,並且對其做適當的設定,詳情請參閱該系統所提供的文件。

操作步驟

  1. 安裝 GPT fdisk

  2. 打開 磁碟工具程式

    1. 過程中如果有疑問可參考下圖或是使用 Mac OS X 磁碟工具程式分割硬碟與格式化 | G. T. Wang
    2. 新增兩個新的磁區,類型隨意,標籤隨自己喜好設定。(如果想讓 Windows 能讀取 Mac 的資料,請把它也一起計算進去)
    3. 點下 套用
    4. 關閉 磁碟工具 DiskUtil
  3. 打開 終端機,並且用 mount 來確認硬碟的代號。此例為 /dev/disk0注意!你所用的代號有可能會跟我的不一樣,請確認後並做適當的更動!

  4. sudo gdisk -l /dev/disk0 可以查看目前的分割表,在操作步驟2. 以前,分割表應該看起來要像下表這樣:由一個 200MB 的 EFI 分割區、Mac 所在的分割區以及一個恢復用的分割區所組成

     $ sudo gdisk -l /dev/disk0
    
     GPT fdisk (gdisk) version 0.8.10
    
     Warning: Devices opened with shared lock will not have their
     partition table automatically reloaded!
     Partition table scan:
       MBR: protective
       BSD: not present
       APM: not present
       GPT: present
    
     Found valid GPT with protective MBR; using GPT.
     Disk /dev/disk0: 976773168 sectors, 465.8 GiB
     Logical sector size: 512 bytes
     Disk identifier (GUID): 0000070C-7DD5-0000-8966-0000B5070000
     Partition table holds up to 128 entries
     First usable sector is 34, last usable sector is 976773134
     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       975503591   465.0 GiB   AF00  Customer
        3       975503592       976773127   619.9 MiB   AB00  Recovery HD
    

    而操作步驟 2. 之後看起來是這樣的:

     $ sudo gdisk -l /dev/disk0
     GPT fdisk (gdisk) version 0.8.10
    
     Warning: Devices opened with shared lock will not have their
     partition table automatically reloaded!
     Partition table scan:
       MBR: hybrid
       BSD: not present
       APM: not present
       GPT: present
    
     Found valid GPT with hybrid MBR; using GPT.
     Disk /dev/disk0: 976773168 sectors, 465.8 GiB
     Logical sector size: 512 bytes
     Disk identifier (GUID): 0000070C-7DD5-0000-8966-0000B5070000
     Partition table holds up to 128 entries
     First usable sector is 34, last usable sector is 976773134
     Partitions will be aligned on 8-sector boundaries
     Total free space is 109 sectors (54.5 KiB)
    
     Number  Start (sector)    End (sector)  Size       Code  Name
        1              40          409639   200.0 MiB   EF00  EFI system partition
        2          409640       487956615   232.5 GiB   AF00  Customer
        3       487956616       489226151   619.9 MiB   AB00  Recovery HD
        4       489226240       732997631   116.2 GiB   0700  WINDOWS C
        5       732997632       976773119   116.2 GiB   0700  EXTERNAL
    
  5. 現在開始進入重頭戲使用 GPT fdisk 來操作了,在終端機中執行 sudo gdisk /dev/disk0gdisk 是它的執行檔檔名。在操作中,只要沒有執行寫入的指令,所有的變更都是在記憶體中進行的,如果發現操作有誤,也不知道怎麼還原的話,按下 Control + c 中止 gdisk 就行了,只要謹慎操作其實不需要太過擔心。在本節的最下方有 GPT fdisk 完整的操作過程,如果過程中感到疑惑,可以一邊參考一邊進行操作。

  6. gdisk 的互動模式中,隨時可以使用 ? 來印出說明訊息。

    1. 先用 p 來印出分割表,這和 sudo gdisk -l /dev/disk0 的執行結果是一樣的。這邊我們要先關心的是分割區在分割表中所屬的號碼(Number)。
    2. r 來切換到 recovery and transformation options。
    3. Recovery/transformation command 模式下,用 h 來建立 Hybrid MBR。會先看到警告訊息,以及提示你要選擇哪些分割區來加入 Hybrid MBR 中。
    4. 這邊輸入 4 5 來加入這兩個分割區到 Hybrid MBR 中,順序會影響使用它的系統辨認的順序,在 Windows 的習慣上,一般以第一個分割區當系統分割區。還記得 MBR 只能有 4 個分割區的限制吧?記得不要超過限制了,超過的部份會被 GPT fdisk 忽略的。如果想要讓 Windows 讀取 Mac 分割區,記得在這步驟把它加進去了。
    5. 這邊選擇不把 EFI 加到第一個,因為我們不使用 GRUB 這個開機啟動器,所以不必太在意它的相容性。值得注意的是,如果選 y,這個 EFI 分割區也含在 MBR 的限制中。
    6. 這邊會提示你想要怎麼設定每個分割區。分割區類型編號直接按下 Return 選擇預設的就好,這邊只讓 WINDOWS C 這個分割區擁有開機能力,所以在這個例子中的操作依序是 Return -> y -> Return -> n
    7. 如果沒有把 Hybrid MBR 用滿,最後會提示要不要把尾端未使用的空間也用分割區的形式標記起來,依照你的需求來決定要不要。不過,只要謹守不要隨意在系統安裝、配置完成之後還對分割表做操作,其實沒有太大的影響。所以在此例我選擇了 n
    8. 最後使用 w 來把分割表寫入磁碟中,如果擔心操作失誤,還可以用 p 來確認情形。
    9. 最後警告,按下 y 之後就沒有反悔的機會了!
  7. 插上先前準備的 Windows 7 安裝媒體。再用 mount 來確認硬體代號,在此例中,安裝光碟是分配到了 /dev/disk1

  8. 使用 bless 來設定開機模式以及目標分割區。執行 sudo bless --device /dev/disk1 --setBoot --legacy --nextonly 。**注意!你所用的代號有可能會跟我的不一樣,請確認後並做適當的更動!**這個指令的意思是:讓 /dev/disk1 成為下一次開機所使用的分割區,並使用 Legacy mode 來開機,且效力也只有下一次開機時。更詳細的資料請參閱 man bless

  9. 重開機,沒意外應該會進入 Windows 7 安裝媒介。

  10. 照正常程序安裝,重開時也許會跑進 Mac,所以在重開的當下記得按著 Option(alt) 鍵,選擇剛剛安裝好的 Windows 7 來開機。

  11. 安裝 bootcamp 所提供的驅動程式。

  12. 完成。

完整的操作過程像是這個樣子的:

    $ sudo gdisk /dev/disk0
    GPT fdisk (gdisk) version 0.8.10

    Warning: Devices opened with shared lock will not have their
    partition table automatically reloaded!
    Partition table scan:
      MBR: hybrid
      BSD: not present
      APM: not present
      GPT: present

    Found valid GPT with hybrid MBR; using GPT.

    Command (? for help): ?
    b back up GPT data to a file
    c change a partition's name
    d delete a partition
    i show detailed information on a partition
    l list known partition types
    n add a new partition
    o create a new empty GUID partition table (GPT)
    p print the partition table
    q quit without saving changes
    r recovery and transformation options (experts only)
    s sort partitions
    t change a partition's type code
    v verify disk
    w write table to disk and exit
    x extra functionality (experts only)
    ? print this menu

    Command (? for help): r

    Recovery/transformation command (? for help): ?
    b use backup GPT header (rebuilding main)
    c load backup partition table from disk (rebuilding main)
    d use main GPT header (rebuilding backup)
    e load main partition table from disk (rebuilding backup)
    f load MBR and build fresh GPT from it
    g convert GPT into MBR and exit
    h make hybrid MBR
    i show detailed information on a partition
    l load partition data from a backup file
    m return to main menu
    o print protective MBR data
    p print the partition table
    q quit without saving changes
    t transform BSD disklabel partition
    v verify disk
    w write table to disk and exit
    x extra functionality (experts only)
    ? print this menu

    Recovery/transformation command (? for help): h

    WARNING! Hybrid MBRs are flaky and dangerous! If you decide not to use one,
    just hit the Enter key at the below prompt and your MBR partition table will
    be untouched.

    Type from one to three GPT partition numbers, separated by spaces, to be
    added to the hybrid MBR, in sequence: 4 5 
    Place EFI GPT (0xEE) partition first in MBR (good for GRUB)? (Y/N): n

    Creating entry for GPT partition #4 (MBR partition #1)
    Enter an MBR hex code (default 07): 
    Set the bootable flag? (Y/N): y

    Creating entry for GPT partition #5 (MBR partition #2)
    Enter an MBR hex code (default 07): 
    Set the bootable flag? (Y/N): n

    Unused partition space(s) found. Use one to protect more partitions? (Y/N): n

    Recovery/transformation 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) to /dev/disk0.
    Warning: Devices opened with shared lock will not have their
    partition table automatically reloaded!
    Warning: The kernel may continue to use old or deleted partitions.
    You should reboot or remove the drive.
    The operation has completed successfully.

    $ sudo bless --device /dev/disk1 --setBoot --nextonly --legacy

一些也許能派得上用場的東西

  • Mac 開機瞬間按著鍵盤的 x 可以強制進入 Mac OS X。
  • 開機時按 Option 的時機不是很好抓,有時候要多嘗試幾次,或者乾脆在 Mac 的 系統設定 內的指定目標磁碟來完事(剛好 bless 也可以作到)。
  • GPT fdisk 是很強大的工具,其功能遠不止於此,還可以備份 GPT 等。值得花時間好好學習。
  • GPT fdisk 也可以完成步驟 2. 除了調整 HFS+ 容量以外的工作。
  • exFAT 是 Mac OS X 和 Windows 7 都支援讀寫的格式。如果想要兩個系統共用一個分割區,也許這是個好主意。
  • GPT fdisk Tutorial 是官方的教學,裡面有很多有用的知識。
  • Mac 本身也有 diskutil 這工具可以操作分割區,如果你對寫腳本有興趣,這會派上用場的。步驟 2. 其實應該也可以用 diskutil 來完成。
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment