趁著休假整理家里閑置的設(shè)備,看到了一臺(tái)許久不用的 M2 芯片的 MacBook pro,想著或許應(yīng)該把它改造成 ARMv64 的 CI/CD 構(gòu)建機(jī),于是就有了這篇文章。

本篇文章適用于 M1、M2 全系列的設(shè)備,包括:MacBook Air、MacBook Pro、Mac Mini、Mac Studio,以及搭載 M1 芯片的 iMac。

寫在前面

MacBook Pro 原生安裝 Ubuntu 24.04 ARM 版

2020 年,開(kāi)源項(xiàng)目 Asahi Linux 成立,旨在通過(guò)逆向工程等手段,讓 Apple Silicon Mac 設(shè)備擁有開(kāi)放的操作系統(tǒng)生態(tài)。為此,項(xiàng)目組需要完成大量的硬件支持、驅(qū)動(dòng)支持和底層工具支持相關(guān)的開(kāi)發(fā)工作。

2021 年 8 月,一個(gè)對(duì) Apple 生產(chǎn)力工具搭載 M1 的設(shè)備這個(gè)項(xiàng)目的帖子在 HackNews 上火了,圍觀群眾都摩拳擦掌或各種表示贊許,只等項(xiàng)目作者們排除萬(wàn)難,推出可用的引導(dǎo)工具 m1n1,將封閉 macOS 生態(tài)中搭載了 M1/M2 芯片的高續(xù)航、高性能輕薄的 Mac 設(shè)備的大門砸開(kāi)。

MacBook Pro 原生安裝 Ubuntu 24.04 ARM 版

2022 年的 5 月下旬,AsahiLinux 發(fā)布了它第一個(gè)穩(wěn)定版本 v1.0,第一個(gè)能夠?qū)?Apple(XNU)引導(dǎo)生態(tài)與 Linux 引導(dǎo)生態(tài)連接起來(lái)的 Bootloader。M1 / M2 設(shè)備的大門再也關(guān)不嚴(yán)實(shí)了。

原項(xiàng)目作者主要維護(hù)的 Linux 發(fā)行版是 Fedora,為此還帶來(lái)了和原生 macOS 類似的多顯示器支持、HiDPI 支持,OpenGL 支持(甚至是 Vulkan),以及和 PipeWire 和 WirePlumber 合作,來(lái)讓設(shè)備擁有完整的 DSP 解決方案,讓電腦音頻更加好聽(tīng)。

不過(guò),在 2023 年,越來(lái)越多的發(fā)行版粉絲加入了 Asahi Linux 發(fā)行版泛化的過(guò)程里,目前支持的 Linux 發(fā)行版已經(jīng)有十種左右了,包括:Alpine、AOSC、CentOS、Debian、Deepin、FedoraGentoo、NixOS、Rocky、Ubuntu

MacBook Pro 原生安裝 Ubuntu 24.04 ARM 版

作為 Ubuntu 的老用戶,在試用了幾個(gè)不同的發(fā)行版后,我最終還是堅(jiān)定的選擇回了 Ubuntu(UbuntuAsahi/ubuntu-asahi),如果你對(duì) Ubuntu 感興趣,之前寫過(guò)的三十篇左右的文章,應(yīng)該會(huì)很有意思,這里就不展開(kāi)了,有興趣自取。

對(duì)了,任何操作之前,都建議你進(jìn)行重要數(shù)據(jù)的備份操作,如果你遇到了需要重裝操作系統(tǒng)或者卸載 Linux 操作系統(tǒng),可以翻閱文章中相關(guān)的章節(jié)出來(lái)。

下面,我們來(lái)聊聊怎么在 M1 / M2 芯片的設(shè)備上原生安裝、運(yùn)行 Linux。

實(shí)戰(zhàn)開(kāi)始

想要在 Mac 設(shè)備上運(yùn)行最新的 Ubuntu 24.04 操作系統(tǒng),一共分三步:準(zhǔn)備安裝程序、授權(quán)系統(tǒng)啟動(dòng)、更新操作系統(tǒng)。

準(zhǔn)備安裝程序

所有的 Asahi Linux 發(fā)行版的安裝程序準(zhǔn)備,都是固定的套路(支持自定義 installer config/data),先使用 curl 下載并執(zhí)行安裝腳本:

代碼語(yǔ)言:bash
curl -sL https://ubuntuasahi.org/install | sh

如果你對(duì)執(zhí)行的腳本不放心,可以下載腳本,進(jìn)行審查,在審查后再進(jìn)行執(zhí)行:

代碼語(yǔ)言:bash
# 下載安裝程序
curl -sL https://ubuntuasahi.org/install > install.sh
# 審查安裝程序
cat install.sh
# 執(zhí)行安裝程序
bash install.sh

執(zhí)行命令后,不出意外,你將看到類似下面的輸出結(jié)果:

代碼語(yǔ)言:bash
Bootstrapping installer:
  Checking version...
  Version: v0.7.1
  Downloading...
  Extracting...
  Initializing...

這個(gè)安裝程序需要使用 root 權(quán)限執(zhí)行,所以,如果安裝過(guò)程和你詢問(wèn)賬號(hào)的密碼來(lái)提權(quán),需要輸入密碼。

過(guò)程中,程序會(huì)收集和展示你的系統(tǒng)信息,并展示給你:

代碼語(yǔ)言:bash
Collecting system information...
  Product name: MacBook Pro (13-inch, M2, 2022)
  SoC: Apple M2
  Device class: j493ap
  Product type: Mac14,7
  Board ID: 0x2a
  Chip ID: 0x8112
  System firmware: iBoot-10151.1.1
  Boot UUID: 4A4C1024-1024-1024-1024-49C3B4DF1024
  Boot VGID: 4A4C1024-1024-1024-1024-49C3B4DF1024
  Default boot VGID: 4A4C1024-1024-1024-1024-49C3B4DF1024
  Boot mode: macOS
  OS version: 14.0 (23A344)
  OS restore version: 23.1.344.0.0,0
  Main firmware version: 14.0 (23A344)
  No Fallback System Firmware / rOS
  SFR version: 23.1.344.0.0,0
  SystemRecovery version: 23.4.56.0.0,0 (14.3 23D56)
  Login user: soulteary

Collecting partition information...
  System disk: disk0
...

當(dāng)基礎(chǔ)信息收集完畢后,程序首先會(huì)收集和引導(dǎo)我們調(diào)整磁盤分區(qū),來(lái)騰出一些空間安裝新的 Linux 操作系統(tǒng):

代碼語(yǔ)言:bash
Collecting OS information...

Partitions in system disk (disk0):
  1: APFS [Macintosh HD] (494.38 GB, 6 volumes)
    OS: [B*] [Macintosh HD] macOS v14.0 [disk3s3s1, 4A4C1024-1024-1024-1024-49C3B4DF1024]
  2: APFS (System Recovery) (5.37 GB, 2 volumes)
    OS: [  ] recoveryOS v14.0 [Primary recoveryOS]

  [B ] = Booted OS, [R ] = Booted recovery, [? ] = Unknown
  [ *] = Default boot volume

Using OS 'Macintosh HD' (disk3s3s1) for machine authentication.

Choose what to do:
  r: Resize an existing partition to make space for a new OS
  q: Quit without doing anything
? Action (r): r

如果你沒(méi)有安裝或者提前進(jìn)行系統(tǒng)分區(qū),那么輸入 r 就能夠進(jìn)入分區(qū)調(diào)整步驟了。接下來(lái)程序會(huì)詢問(wèn)我們到底要?jiǎng)澐侄嗌倏臻g給新的操作系統(tǒng):

代碼語(yǔ)言:bash
We're going to resize this partition:
  APFS [Macintosh HD] (494.38 GB, 6 volumes)
  Total size: 494.38 GB
  Free space: 431.38 GB
  Available space: 393.38 GB
  Overhead: 0 B
  Minimum new size: 101.01 GB (20.43%)

Enter the new size for your existing partition:
  You can enter a size such as '1GB', a fraction such as '50%',
  or the word 'min' for the smallest allowable size.

  Examples:
  30%  - 30% to macOS, 70% to the new OS
  80GB - 80GB to macOS, the rest to your new OS
  min  - Shrink macOS as much as (safely) possible

? New size (50%): 30%

我個(gè)人訴求是主要使用這臺(tái)機(jī)器作為 Linux CI/CD 機(jī)器,所以我只給原始操作系統(tǒng)留了 30% 的空間,這樣既能做 PE 維護(hù)系統(tǒng)以防萬(wàn)一,真需要 macOS 運(yùn)行的軟件,也能切回去運(yùn)行。

然后,一路回車和 y (同意),用于安裝 Linux 的新分區(qū)就自動(dòng)處理好啦:

代碼語(yǔ)言:bash
Resizing will free up 346.07 GB of space.

Note: your system may appear to freeze during the resize.
This is normal, just wait until the process completes.
? Continue? (y/N): y

Started APFS operation
Aligning shrink delta to 346,068,963,328 bytes and targeting a new container size of 148,315,832,320 bytes
Determined the minimum size for the APFS Container to be 62,226,694,144 bytes
Resizing APFS Container designated by APFS Container Reference disk3
The specific APFS Physical Store being resized is disk0s2
[ - 0%..10%.............................................. ] 

Verifying allocated space
The container /dev/disk0s2 appears to be OK
Storage system check exit code is 0
Shrinking APFS Physical Store disk0s2 from 494,384,795,648 to 148,315,832,320 bytes
Shrinking APFS data structures
Shrinking partition
Modifying partition map
Finished APFS operation

Resize complete. Press enter to continue.

當(dāng)我們的分區(qū)調(diào)整完畢之后,能夠看到程序收集的磁盤信息得到了更新,我們可以執(zhí)行的動(dòng)作除了“調(diào)整分區(qū)”和“退出安裝”之外,多了一條“安裝操作系統(tǒng)到空閑區(qū)域”:

代碼語(yǔ)言:bash
Collecting partition information...
  System disk: disk0

Collecting OS information...

Partitions in system disk (disk0):
  1: APFS [Macintosh HD] (148.32 GB, 6 volumes)
    OS: [B*] [Macintosh HD] macOS v14.0 [disk3s3s1, 4A4C1024-1024-1024-1024-49C3B4DF1024]
  2: (free space: 346.07 GB)
  3: APFS (System Recovery) (5.37 GB, 2 volumes)
    OS: [  ] recoveryOS v14.0 [Primary recoveryOS]

  [B ] = Booted OS, [R ] = Booted recovery, [? ] = Unknown
  [ *] = Default boot volume

Using OS 'Macintosh HD' (disk3s3s1) for machine authentication.

Choose what to do:
  f: Install an OS into free space
  r: Resize an existing partition to make space for a new OS
  q: Quit without doing anything
? Action (f): 

直接回車后,來(lái)到操作系統(tǒng)選擇列表,類似下面這樣。選擇我們想要安裝的系統(tǒng),輸入對(duì)應(yīng)的數(shù)字序號(hào),就能夠開(kāi)始安裝啦:

代碼語(yǔ)言:bash
Choose an OS to install:
  1: Ubuntu Desktop 23.10
  2: Ubuntu Desktop 23.04
  3: Ubuntu Desktop 22.04 LTS
  4: Ubuntu Server Minimal
  5: UEFI environment only (m1n1 + U-Boot + ESP)

我這里推薦安裝距離 24.04 最近的 23.10,硬件驅(qū)動(dòng)支持的更好。

過(guò)程中,依舊是詢問(wèn)我們的用戶密碼,來(lái)獲得授權(quán),應(yīng)給盡給即可。

代碼語(yǔ)言:bash
To continue the installation, you will need to enter your macOS
admin credentials.

Password for soulteary: 

Setting the new OS as the default boot volume...

Installation successful!

Install information:
  APFS VGID: A42B1024-1024-1024-1024-E99F0C481024
  EFI PARTUUID: aebe1024-1024-1024-1024-42bf967a1024

To be able to boot your new OS, you will need to complete one more step.
Please read the following instructions carefully. Failure to do so
will leave your new installation in an unbootable state.

Press enter to continue

到這里,安裝就完畢啦,作者比較小心,特別寫了一段 “防呆” 文檔提醒:

代碼語(yǔ)言:bash
When the system shuts down, follow these steps:

1. Wait 25 seconds for the system to fully shut down.
2. Press and hold down the power button to power on the system.
   * It is important that the system be fully powered off before this step,
     and that you press and hold down the button once, not multiple times.
     This is required to put the machine into the right mode.
3. Release it once you see 'Loading startup options...' or a spinner.
4. Wait for the volume list to appear.
5. Choose 'macbian'.
6. You will briefly see a 'macOS Recovery' dialog.
   * If you are asked to 'Select a volume to recover',
     then choose your normal macOS volume and click Next.
     You may need to authenticate yourself with your macOS credentials.
7. Once the 'Asahi Linux installer' screen appears, follow the prompts.

If you end up in a bootloop or get a message telling you that macOS needs to
be reinstalled, that means you didn't follow the steps above properly.
Fully shut down your system without doing anything, and try again.
If in trouble, hold down the power button to boot, select macOS, run
this installer again, and choose the 'p' option to retry the process.

Press enter to shut down the system.

簡(jiǎn)單來(lái)說(shuō),和準(zhǔn)備過(guò)程相關(guān)的有下面的提醒:

關(guān)機(jī)稍等一些時(shí)間,等待系統(tǒng)完全關(guān)閉。再次開(kāi)機(jī)時(shí),長(zhǎng)按“電源按鈕”,而不是按多次按鈕,直到看到“加載啟動(dòng)選項(xiàng)…”時(shí)再松開(kāi)。

MacBook Pro 原生安裝 Ubuntu 24.04 ARM 版

然后,在啟動(dòng)列表中選擇你的 Linux 操作系統(tǒng)。

到這里為止,準(zhǔn)備和安裝工作就結(jié)束啦。

授權(quán)系統(tǒng)啟動(dòng)

當(dāng)我們?cè)趩?dòng)列表選擇了我們的 Linux 操作系統(tǒng)后,首次啟動(dòng)系統(tǒng),我們需要完成“系統(tǒng)啟動(dòng)切換”的授權(quán)。

如果看到了 “macOS 恢復(fù)”對(duì)話框,我們需要 “選擇要恢復(fù)的卷”,點(diǎn)擊下一步,選擇我們安裝了 Linux 的分區(qū),然后進(jìn)行身份驗(yàn)證對(duì)其授權(quán)。

在界面彈出的終端窗口中,和上文一樣,該輸入 Mac 設(shè)備的用戶名和密碼的時(shí)候,小心輸入,并回車確認(rèn),幾輪下來(lái)后,點(diǎn)擊回車,設(shè)備重啟,授權(quán)系統(tǒng)啟動(dòng)的工作就結(jié)束啦。

更新操作系統(tǒng)

一般情況,如果上面的工作都順利完成,那么我們將自動(dòng)的進(jìn)入 Linux 環(huán)境。

默認(rèn)的賬號(hào)和密碼都是 ubuntu,輸入密碼,將自動(dòng)進(jìn)入 Ubuntu 環(huán)境。

因?yàn)槟J(rèn)安裝的操作系統(tǒng)版本是 Ubuntu 23.10,想要得到最新的 Ubuntu 24.04 LTS 版本,我們需要做一些工作。

默認(rèn)的 Ubuntu 軟件源,可能訪問(wèn)數(shù)據(jù)會(huì)比較慢,可以根據(jù)自己的具體情況來(lái)做一個(gè)軟件源替換,比如替換為清華源:

代碼語(yǔ)言:bash
sed -i 's/ports.ubuntu.com/mirrors.tuna.tsinghua.edu.cn/g' /etc/apt/sources.list

然后,執(zhí)行系統(tǒng)所有軟件包的更新:

代碼語(yǔ)言:bash
apt update && apt upgrade -y

當(dāng)軟件都更新完畢之后,我們執(zhí)行 do-release-upgrade 開(kāi)始 Linux 發(fā)行版更新即可:

代碼語(yǔ)言:bash
do-release-upgrade
# 或者
do-release-upgrade -d

如果過(guò)程中遇到問(wèn)題,可以參考《服務(wù)端升級(jí) Ubuntu 20.04 LTS 記錄》中的方法,來(lái)排查和解決。

如果你在過(guò)程中遇到了類似 "The following packages have been kept back:" Why and how do I solve it? 的問(wèn)題,我們可以使用下面的命令來(lái)解決問(wèn)題:

代碼語(yǔ)言:bash
apt-get --with-new-pkgs upgrade -y

執(zhí)行命令,然后等待程序完成更新即可。

如果你有系統(tǒng)安裝潔癖,可以順手執(zhí)行一個(gè)清理:

代碼語(yǔ)言:bash
apt-get autoremove -y

重啟操作系統(tǒng),再次進(jìn)入系統(tǒng),我們就得到了 Ubuntu 24.04 版本的全新操作系統(tǒng)。

其他

替你們踩過(guò)一些坑啦,膽大心細(xì)的朋友不妨看完。

重新安裝 macOS 或清理已安裝的 Linux

想要“卸載”已安裝的 Linux 非常簡(jiǎn)單,只需要在開(kāi)機(jī)的時(shí)候長(zhǎng)按“電源鍵”,然后選擇啟動(dòng)機(jī)器的 macOS 系統(tǒng),進(jìn)入操作系統(tǒng)中,選擇系統(tǒng)自帶的“磁盤工具”,選擇分區(qū),然后將前三個(gè)分區(qū)之外“刪除掉”(減號(hào)按鈕),保存修改即可。

如果你在安裝過(guò)程中,出現(xiàn)了意外情況,或者重復(fù)安裝將系統(tǒng)分區(qū)弄亂了,或者在“卸載”的時(shí)候,馬大哈的操作錯(cuò)誤,導(dǎo)致你需要進(jìn)行重裝,一般情況,我們只需要重啟 macOS 操作系統(tǒng),進(jìn)入恢復(fù)模式,將 WiFi 連上跟著引導(dǎo)重新安裝即可,如果出現(xiàn)磁盤分區(qū)不可調(diào)整,那么在安裝完畢系統(tǒng)后,再進(jìn)行安裝即可。

如果你是效率黨,或者正巧想快速的重新安裝 macOS 到你的設(shè)備上,可以采用自制 macOS USB 安裝盤的方法,官方有兩篇文檔應(yīng)該會(huì)對(duì)你有幫助《如何下載和安裝 macOS》、《創(chuàng)建可引導(dǎo)的 macOS 安裝器》。

簡(jiǎn)單來(lái)說(shuō),完成你想要的、機(jī)器能夠安裝的 macOS 操作系統(tǒng),然后“刻盤”安裝即可:

代碼語(yǔ)言:bash
sudo /Applications/Install macOS Sonoma.app/Contents/Resources/createinstallmedia --volume /Volumes/MyVolume

執(zhí)行完畢,大概會(huì)看到類似下面的輸出內(nèi)容:

代碼語(yǔ)言:bash
To continue we need to erase the volume at /Volumes/MyVolume.
If you wish to continue type (Y) then press return: y
Erasing disk: 0%... 10%... 20%... 30%... 100%
Copying essential files...
Copying the macOS RecoveryOS...
Making disk bootable...
Copying to disk: 0%... 10%... 20%... 30%... 40%... 50%... 60%... 70%... 100%
Install media now available at "/Volumes/Install macOS Sonoma"

需要注意的是,我們使用的方案,要求進(jìn)行安裝的原始 macOS 操作系統(tǒng)環(huán)境版本在 13.0 以上,也就是說(shuō),只支持 Sonoma、Ventura 兩個(gè)版本或之后的版本(13.x~14.x)。

最近下載 macOS 鏡像似乎會(huì)因?yàn)楣俜?CDN 的不穩(wěn)定導(dǎo)致下載總是中斷失敗,我們可以通過(guò)三方下載工具進(jìn)行下載下面的鏈接。

代碼語(yǔ)言:bash
# macOS Sonoma 安裝包
https://swcdn.apple.com/content/downloads/04/13/052-77516-A_4P7VY083DT/83qy3989rsnylxagdmim3owwjdtc33zfe4/InstallAssistant.pkg

關(guān)于項(xiàng)目還在完善的部分

MacBook Pro 原生安裝 Ubuntu 24.04 ARM 版

如果你仔細(xì)翻看項(xiàng)目對(duì)于設(shè)備硬件的支持度,你會(huì)發(fā)現(xiàn),除了 USB-C 外接顯示器,雷電接口、麥克風(fēng)、TouchID 幾個(gè)部分外,項(xiàng)目已經(jīng)支持了所有主要的設(shè)備。

如果你不需要這臺(tái)設(shè)備外接輸出畫(huà)面、不需要借用它的雷電接口來(lái)做傳輸、不需要用它來(lái)開(kāi)視頻會(huì)議,以及不是頻繁的使用 TouchID 來(lái)解鎖設(shè)備,像我一樣,用它來(lái)跑編譯構(gòu)建,或許這個(gè)操作系統(tǒng)還是蠻適合你的。

但是如果你有任意上面的需求,或者這臺(tái)設(shè)備不是閑置的筆記本、臺(tái)式機(jī),我個(gè)人是不太建議你這樣折騰的,畢竟折騰的本質(zhì)是為了不折騰。

不過(guò),你也可以許愿作者們更快的支持你需要的功能,保持圍觀這個(gè)文檔獲取項(xiàng)目完善進(jìn)度?;蛘呒尤胨麄儯黄饋?lái)完善設(shè)備最后幾個(gè)硬件功能的支持。

最后

這篇文章就寫到這里,后續(xù)相關(guān)的文章,展開(kāi)聊聊它的使用經(jīng)驗(yàn)和細(xì)節(jié)吧。

 

聲明:本站所有文章,如無(wú)特殊說(shuō)明或標(biāo)注,均為本站原創(chuàng)發(fā)布。任何個(gè)人或組織,在未征得本站同意時(shí),禁止復(fù)制、盜用、采集、發(fā)布本站內(nèi)容到任何網(wǎng)站、書(shū)籍等各類媒體平臺(tái)。如若本站內(nèi)容侵犯了原著者的合法權(quán)益,可聯(lián)系我們進(jìn)行處理。