MacBook m1使用Qemu搭建Ubuntu虚拟机
虽然macOS
和Linux
类似,但毕竟不同。学习Linux
,就需要一个真实的Linux
环境,思来想去,决定用qemu
装一个Ubuntu
虚拟机。
liheng@~$ sw_vers
ProductName: macOS
ProductVersion: 14.5
BuildVersion: 23F79
安装brew
https://brew.idayer.com/guide/m1/
安装qemu
brew install qemu
liheng@~/coding$ qemu-img --version
qemu-img version 9.0.1
Copyright (c) 2003-2024 Fabrice Bellard and the QEMU Project developers
下载Ubuntu镜像
https://old-releases.ubuntu.com/releases/
我选择的是Ubuntu 22.04.3 LTS (Jammy Jellyfish)
->ubuntu-22.04.3-live-server-amd64.iso
arm
系列装起来更麻烦点,所以选择了x86
。
带图形界面的desktop
系列跑起来太卡了,所以选择了server
版。
创建虚拟磁盘
qemu-img create -f qcow2 ubuntu-x86_64.img 64G
-f qcow2
制定了文件格式,我看大家创建磁盘时都这么指定格式。
ubuntu-x86_64.img
是自定义的磁盘名称。
64G
是磁盘大小。
安装操作系统
此时磁盘是空的,需要在磁盘上安装操作系统。
qemu-system-x86_64 -m 4096 -machine q35 ubuntu-x86_64.img -cdrom ubuntu-22.04.3-live-server-amd64.iso
-m 4096
指内存大小是4096M
。
-machine q35
指机器架构是q35
,一种比较新的intel架构。
ubuntu-x86_64.img
启动磁盘名,因为它现在是空的,所以没法直接从这里启动虚拟机。
-cdrom ubuntu-22.04.3-live-server-amd64.iso
指定iso
,要是虚拟机无法从磁盘启动,就先从该iso
安装操作系统到磁盘上。
接下来是装Ubuntu
系统的步骤,省略。
启动虚拟机
等操作系统安装到磁盘,后续启动虚拟机的时候就不需要iso
了。
sudo qemu-system-x86_64 -m 4096 -machine q35 ubuntu-x86_64.img -nic vmnet-bridged,ifname=en0 -smp 4
-nic vmnet-bridged,ifname=en0
指虚拟机和宿主机通过桥接网络连接(macos
可以这么创建桥接网络,其他系统不晓得)。en0
是宿主机访问互联网的网卡名称,en0
的ip
是192.168.43.157
。
liheng@~$ ifconfig en0
en0: flags=8b63<UP,BROADCAST,SMART,RUNNING,PROMISC,ALLMULTI,SIMPLEX,MULTICAST> mtu 1500
options=6460<TSO4,TSO6,CHANNEL_IO,PARTIAL_CSUM,ZEROINVERT_CSUM>
ether 18:3e:ef:d9:2d:91
inet6 fe80::103d:438b:a1c8:209e%en0 prefixlen 64 secured scopeid 0xc
inet6 240e:454:140:44d:c43:3d01:e1d6:4bf6 prefixlen 64 autoconf secured
inet6 240e:454:140:44d:40d1:e514:ff68:c1cb prefixlen 64 autoconf temporary
inet 192.168.43.157 netmask 0xffffff00 broadcast 192.168.43.255
nd6 options=201<PERFORMNUD,DAD>
media: autoselect
status: active
-smp 4
指虚拟机的cpu
是4核的。
启动成功
x86_64
架构的4
核CPU
liheng@liheng:~$ lscpu
Architecture: x86_64
CPU op-mode(s): 32-bit, 64-bit
Address sizes: 40 bits physical, 48 bits virtual
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: AuthenticAMD
Model name: QEMU Virtual CPU version 2.5+
CPU family: 15
Model: 107
Thread(s) per core: 1
Core(s) per socket: 4
Socket(s): 1
Stepping: 1
磁盘空间64G
liheng@liheng:~$ df -h
Filesystem Size Used Avail Use% Mounted on
tmpfs 392M 1016K 391M 1% /run
/dev/sda2 63G 7.6G 52G 13% /
tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs 392M 4.0K 392M 1% /run/user/1000
内存4G
liheng@liheng:~$ free -h
total used free shared buff/cache available
Mem: 3.8Gi 434Mi 2.8Gi 0.0Ki 657Mi 3.2Gi
Swap: 3.8Gi 0B 3.8Gi
操作系统
liheng@liheng:~$ uname -a
Linux liheng 5.15.0-113-generic #123-Ubuntu SMP Mon Jun 10 08:16:17 UTC 2024 x86_64 x86_64 x86_64 GNU/Linux
liheng@liheng:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
网络ip
是192.168.43.140
,能访问宿主机,也能通过宿主机访问互联网。
liheng@liheng:~$ ifconfig enp0s2
enp0s2: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1500
inet 192.168.43.140 netmask 255.255.255.0 broadcast 192.168.43.255
inet6 fe80::5054:ff:fe12:3456 prefixlen 64 scopeid 0x20<link>
inet6 240e:454:140:44d:5054:ff:fe12:3456 prefixlen 64 scopeid 0x0<global>
ether 52:54:00:12:34:56 txqueuelen 1000 (Ethernet)
RX packets 3438 bytes 712447 (712.4 KB)
RX errors 30 dropped 0 overruns 0 frame 30
TX packets 2490 bytes 1987191 (1.9 MB)
TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0
device interrupt 22 memory 0xfeb80000-feba0000
Arm64版Ubuntu
arm
版比x86
版多指定一个bios
,下载QEMU_EFI.fd
:
https://releases.linaro.org/components/kernel/uefi-linaro/16.02/release/qemu64/
下载镜像文件:
https://old-releases.ubuntu.com/releases/
Ubuntu 22.04.3 LTS (Jammy Jellyfish)
->ubuntu-22.04.3-live-server-arm64.iso
创建启动磁盘:
qemu-img create -f qcow2 ubuntu-arm64.img 64G
安装系统命令:
qemu-system-aarch64 -m 4096 -cpu cortex-a57 -smp 4 -machine virt -bios QEMU_EFI.fd -nographic -drive if=none,file=ubuntu-22.04.3-live-server-arm64.iso,id=cdrom,media=cdrom -device virtio-scsi-device -device scsi-cd,drive=cdrom -drive if=none,file=ubuntu-arm64.img,id=hd0 -device virtio-blk-device,drive=hd0
启动虚拟机命令:
sudo qemu-system-aarch64 -m 4096 -cpu cortex-a57 -smp 4 -machine virt -bios QEMU_EFI.fd -nographic -drive if=none,file=ubuntu-arm64.img,id=hd0 -device virtio-blk-device,drive=hd0 -nic vmnet-bridged,ifname=en0
启动成功
liheng@liheng:~$ lscpu
Architecture: aarch64
CPU op-mode(s): 32-bit, 64-bit
Byte Order: Little Endian
CPU(s): 4
On-line CPU(s) list: 0-3
Vendor ID: ARM
Model name: Cortex-A57
Model: 0
Thread(s) per core: 1
Core(s) per cluster: 4
Socket(s): -
Cluster(s): 1
Stepping: r1p0
BogoMIPS: 125.00
Flags: fp asimd evtstrm aes pmull sha1 sha2 crc32 cpuid
liheng@liheng:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
性能优化
MacBook m1中,使用-accel hvf
优化虚拟机性能,需要指定小内存,以arm
版为例:
sudo qemu-system-aarch64 -m 2048 -cpu cortex-a57 -smp 4 -machine virt,highmem=off -bios QEMU_EFI.fd -nographic -drive if=none,file=ubuntu-arm64.img,id=hd0 -device virtio-blk-device,drive=hd0 -nic vmnet-bridged,ifname=en0 -accel hvf
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 25岁的心里话
· 闲置电脑爆改个人服务器(超详细) #公网映射 #Vmware虚拟网络编辑器
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· 零经验选手,Compose 一天开发一款小游戏!
· 一起来玩mcp_server_sqlite,让AI帮你做增删改查!!