在 Apple M1 平台搭建 Ubuntu 开发环境

2022.06.22
SF-Zhou

去年年底笔者更新了自己的开发设备,换成了 Apple M1 芯片的 MacBook,偶尔做开发的时候还是会因为环境问题不太方便。所以希望在这台笔记本上搭建一套 Ubuntu 的开发环境。因为可能会有内核开发相关的需求,所以没有使用 Docker,而是选择了 Ubuntu 的发行商 Canonical 开发的 Multipass。安装步骤如下:

# 1. install
brew install multipass

# 2. launch a instance
multipass launch --name primary --cpus 8 --mem 8G --disk 20G

# 3. list instances
multipass list
#> Name                    State             IPv4             Image
#> primary                 Running           192.168.64.4     Ubuntu 20.04 LTS

# 4. show info
multipass info --all
#> Name:           primary
#> State:          Running
#> IPv4:           192.168.64.4
#> Release:        Ubuntu 20.04.4 LTS
#> Image hash:     95a027336e19 (Ubuntu 20.04 LTS)
#> Load:           0.06 0.03 0.00
#> Disk usage:     1.3G out of 19.2G
#> Memory usage:   232.2M out of 7.7G
#> Mounts:         /Users/sf-zhou => Home
#>                     UID map: 501:default
#>                     GID map: 20:default

# 5. open shell
multipass shell
#> Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-120-generic aarch64)
#> 
#>  * Documentation:  https://help.ubuntu.com
#>  * Management:     https://landscape.canonical.com
#>  * Support:        https://ubuntu.com/advantage
#> 
#>   System information as of Wed Jun 22 22:50:02 CST 2022
#> 
#>   System load:             0.14
#>   Usage of /:              6.7% of 19.22GB
#>   Memory usage:            3%
#>   Swap usage:              0%
#>   Processes:               174
#>   Users logged in:         0
#>   IPv4 address for enp0s1: 192.168.64.2
#>   IPv6 address for enp0s1: fd33:cb6c:2e62:89c5:5054:ff:feb9:9b5
#> 
#> 
#> 0 updates can be applied immediately.
#> 
#> 
#> To run a command as administrator (user "root"), use "sudo <command>".
#> See "man sudo_root" for details.
#> 
#> ubuntu@test:~$ uname -a
#> Linux test 5.4.0-120-generic #136-Ubuntu SMP Fri Jun 10 13:46:10 UTC 2022 aarch64 aarch64 aarch64 GNU/Linux

可以看到在 Apple M1 平台上,Multipass 启动的虚拟机也是 ARM64 体系。启动 shell 后就可以像一般的虚拟机一样正常使用了。这里再补充一些开发可能遇到的问题。

1. ssh 登录

直接在 Mac 上使用 ssh 登录虚拟机会被拒绝:

ssh ubuntu@192.168.64.2
#> The authenticity of host '192.168.64.2 (192.168.64.2)' can't be established.
#> ED25519 key fingerprint is SHA256:UNQUAycErI2TheKOfF4zKc5HSFad+7q36Z+MCpdIA2U.
#> This key is not known by any other names
#> Are you sure you want to continue connecting (yes/no/[fingerprint])? yes
#> Warning: Permanently added '192.168.64.2' (ED25519) to the list of known hosts.
#> ubuntu@192.168.64.2: Permission denied (publickey).

这里需要手动的将自己 Mac 的 ssh 公钥加入到 Ubuntu 虚拟机的信任列表里:

# 1. in your mac
cat ~/.ssh/id_rsa.pub
#> ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDsc8Oz4jg8u2x1GiHceHTwO9PUyogZSMPZ2w1Fi9Vne8vBmv38lNTJ4BMUUYj8gtVFeoXE2TLeuOsAmrjV9hArKmwBTjMNtN7Bnwhptu+2nimYuVbXj4dfpfhErEyHBl2o51CIxeavoLeOEWpI2BBnKaT+a7SQ8G5uZtYnl2Jx7btaly1Q1uCcjSNP2ZxX5lBzfft5HT5WCMPsP+i9vrIRnUEFE9ITjSxzOcDOlPqjmnvC2MMJiR4HcpsUR36MvuGW0R3NcvdZlmF8Lna668YXFEHe4MGb2BgfS2m3YIJqPNvBLXPtdc= sf-zhou@SF-Zhous-MacBook-Pro.local

# 2. copy this
multipass shell
#> ubuntu@test:~$ echo "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABgQDsc8Oz4jg8u2x1GiHceHTwO9PUyogZSMPZ2w1Fi9Vne8vBmv38lNTJ4BMUUYj8gtVFeoXE2TLeuOsAmrjV9hArKmwBTjMNtN7Bnwhptu+2nimYuVbXj4dfpfhErEyHBl2o51CIxeavoLeOEWpI2BBnKaT+a7SQ8G5uZtYnl2Jx7btaly1Q1uCcjSNP2ZxX5lBzfft5HT5WCMPsP+i9vrIRnUEFE9ITjSxzOcDOlPqjmnvC2MMJiR4HcpsUR36MvuGW0R3NcvdZlmF8Lna668YXFEHe4MGb2BgfS2m3YIJqPNvBLXPtdc= sf-zhou@SF-Zhous-MacBook-Pro.local" >> ~/.ssh/authorized_keys
#> ubuntu@test:~$ exit

# 3. ssh again
ssh ubuntu@192.168.64.2
#> Welcome to Ubuntu 20.04.4 LTS (GNU/Linux 5.4.0-120-generic aarch64)
#> 
#>  * Documentation:  https://help.ubuntu.com
#>  * Management:     https://landscape.canonical.com
#>  * Support:        https://ubuntu.com/advantage
#> 
#>   System information as of Wed Jun 22 23:01:56 CST 2022
#> 
#>   System load:             0.0
#>   Usage of /:              6.7% of 19.22GB
#>   Memory usage:            3%
#>   Swap usage:              0%
#>   Processes:               159
#>   Users logged in:         0
#>   IPv4 address for enp0s1: 192.168.64.2
#>   IPv6 address for enp0s1: fd33:cb6c:2e62:89c5:5054:ff:feb9:9b5
#> 
#> 
#> 0 updates can be applied immediately.
#> 
#> 
#> Last login: Wed Jun 22 23:00:51 2022 from 192.168.64.1
#> To run a command as administrator (user "root"), use "sudo <command>".
#> See "man sudo_root" for details.
#> 
#> ubuntu@test:~$

然后就可以使用 VS Code 的远程开发套件进行开发了。

2. 镜像大小调整

可以使用 qemu-img 命令调整已经创建的实例的磁盘大小:

# 1. install
brew install qemu

# 2. stop instance
multipass stop primary

# 3. resize
sudo qemu-img resize "/var/root/Library/Application Support/multipassd/qemu/vault/instances/primary/ubuntu-20.04-server-cloudimg-arm64.img" +20G

# 4. start again
multipass start primary

这里也展示了实例的存储路径。实际上可以通过直接替换镜像文件的方式启动自己下载的镜像文件。

References

  1. Multipass