赞
踩
最近有虚拟机的需求, 我第一个想到了之前就有推荐过的multipass1, 至于为什么没有选parallel desktop. 主要还是不想花太多内存在图形用户界面上, 专注于命令行会好一些, 因为我电脑的架构是arm, 就只能安装arm版本的Ubuntu, 下面汇总一下主要的安装与配置命令以及图形界面的安装(基于Microsoft远程桌面, 这样消耗的内存会少一些).
环境:
macOS 12.3.1 Apple silicon
multipass 1.9.0
安装起来比较简单, 直接
brew install --cask multipass
可选的安装:(图形界面)
brew install --cask microsoft-remote-desktop
一条命令完事:
multipass launch --name vm1 --mem 2G --disk 40G --cpus 2 impish
下面解释一下具体的选项:
$ multipass help launch Usage: multipass launch [options] [[<remote:>]<image> | <url>] Create and start a new instance. Options: -h, --help Display this help -v, --verbose Increase logging verbosity. Repeat the 'v' in the short option for more detail. Maximum verbosity is obtained with 4 (or more) v's, i.e. -vvvv. -c, --cpus <cpus> Number of CPUs to allocate. Minimum: 1, default: 1. -d, --disk <disk> Disk space to allocate. Positive integers, in bytes, or with K, M, G suffix. Minimum: 512M, default: 5G. -m, --mem <mem> Amount of memory to allocate. Positive integers, in bytes, or with K, M, G suffix. Minimum: 128M, default: 1G. -n, --name <name> Name for the instance. If it is 'primary' (the configured primary instance name), the user's home directory is mounted inside the newly launched instance, in 'Home'. --cloud-init <file> Path to a user-data cloud-init configuration, or '-' for stdin --network <spec> Add a network interface to the instance, where <spec> is in the "key=value,key=value" format, with the following keys available: name: the network to connect to (required), use the networks command for a list of possible values, or use 'bridged' to use the interface configured via `multipass set local.bridged-network`. mode: auto|manual (default: auto) mac: hardware address (default: random). You can also use a shortcut of "<name>" to mean "name=<name>". --bridged Adds one `--network bridged` network. --timeout <timeout> Maximum time, in seconds, to wait for the command to complete. Note that some background operations may continue beyond that. By default, instance startup and initialization is limited to 5 minutes each. Arguments: image Optional image to launch. If omitted, then the default Ubuntu LTS will be used. <remote> can be either ‘release’ or ‘daily‘. If <remote> is omitted, ‘release’ will be used. <image> can be a partial image hash or an Ubuntu release version, codename or alias. <url> is a custom image URL that is in http://, https://, or file:// format.
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
- 10
- 11
- 12
- 13
- 14
- 15
- 16
- 17
- 18
- 19
- 20
- 21
- 22
- 23
- 24
- 25
- 26
- 27
- 28
- 29
- 30
- 31
- 32
- 33
- 34
- 35
- 36
- 37
- 38
- 39
- 40
- 41
- 42
- 43
- 44
- 45
- 46
- 47
- 48
- 49
--name
, -n
, 指出虚拟机实例的名称, 默认为primary
--mem
, -m
, 虚拟机所用的物理内存, 默认为1GB
--disk
, -d
, 磁盘大小, 默认为5GB
--cpus
, -c
, 使用的CPU核数, 默认为1
impish
, 使用的Ubuntu版本, 采用multipass find
可以找出所有支持的版本, 最新版为22.04
.
Image Aliases Version Description
18.04 bionic 20220523 Ubuntu 18.04 LTS
20.04 focal,lts 20220505 Ubuntu 20.04 LTS
21.10 impish 20220309 Ubuntu 21.10
22.04 jammy 20220506 Ubuntu 22.04 LTS
anbox-cloud-appliance latest Anbox Cloud Appliance
charm-dev latest A development and testing environment for charmers
docker latest A Docker environment with Portainer and related tools
minikube latest minikube is local Kubernetes
这部分的内容我是参考了好多资料2才知道的, 当然也有自己的探索, 通过find
命令找出系统中的缓存镜像的位置.
配置文件, 这里指的是虚拟机的配置文件.json
, 用于在创建已经有的实例后, 还能够修改系统的内存,CPU等参数的配置文件, 由于架构不同, 以及所采用的模拟方式不同, 这里我仅列出我的电脑中可以用的配置文件的存放路径:
sudo su
cat /var/root/Library/Application\ Support/multipassd/qemu/multipassd-vm-instances.json
或者直接用:
sudo cat /var/root/Library/Application\ Support/multipassd/qemu/multipassd-vm-instances.json
这里可以先进入管理员环境, 方便找到目录, 当然直接用sudo
以及编辑器打开也可以, 我这里的配置如下:
{
"vm1": {
"deleted": false,
"disk_space": "42949672960",
"mac_addr": "52:54:00:aa:1b:c7",
// ...
"mem_size": "2147483648",
"num_cores": 2,
"ssh_username": "ubuntu",
"state": 0
}
}
有了这个文件, 就可以愉快地进行针对已创建实例的CPU核数修改等操作了, 下面参考了3.
# stop multipassd sudo launchctl unload /Library/LaunchDaemons/com.canonical.multipassd.plist # edit /var/root/Library/Application\ Support/multipassd/qemu/multipassd-vm-instances.json # you'll need sudo for that sudo vi /var/root/Library/Application\ Support/multipassd/qemu/multipassd-vm-instances.json # start multipassd again sudo launchctl load /Library/LaunchDaemons/com.canonical.multipassd.plist
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9
注意:
launchctl unload
和launchctl load
, 这两个步骤是必须的, 如果没有停掉服务直接修改配置文件的话, 之后开启虚拟机会导致配置被之前的内容覆盖, 并不会生效. 在重新加载服务之后需要等一段时间, 然后开启虚拟机, 否则会提示错误
shell failed: cannot connect to the multipass socket
Please ensure multipassd is running and '/var/run/multipass_socket' is accessible
这里我用修改内核为例, 通过htop
命令查看内核, 发现确实被修改了.
缓存镜像下载后的存放位置:
find /private/var -name *ubuntu*
/private/var/root/Library/Application Support/multipassd/qemu/vault/instances/vm1/ubuntu-22.04-server-cloudimg-arm64.img
/private/var/root/Library/Caches/multipassd/qemu/vault/images/jammy-20220506/ubuntu-22.04-server-cloudimg-arm64.img
/private/var/root/Library/Caches/multipassd/qemu/vault/images/impish-20220309/ubuntu-21.10-server-cloudimg-arm64.img
至于为什么要知道这个文件的存放位置, 主要还是因为电脑的内存不够, 一些不必要的缓存就一定要删除了, 这里列出通过find
命令找到的一些缓存目录, 令人惊讶的是, /var/root/
里面竟然还有桌面文件夹等文件夹, 像是一个新的系统, 以后再来探索这里面的东西吧.
有一些开发环境不得不用图形界面, 这里也采用了官网推荐的一个方法4, 通过迂回的方式安装并显示图形界面. 因为需要安装软件包, 这里需要先进行镜像的配置, 主要的方法就参考清华镜像了5, 这里需要注意: 如果你用的也是arm架构的话, 镜像源需要采用的是ubuntu-port
.
sudo apt update
sudo apt install ubuntu-desktop xrdp
设置密码:
sudo passwd ubuntu
然后通过brew
安装microsoft-remote-desktop
, 上面已经提到了.
通过在虚拟机中输入ip addr
来查看IP地址, 输入到远程桌面即可连接.
解决git
速度太慢的情况, 而且做开发不用代理可以说几乎是不可能的, 这里以clashx
为例配置一下在multipass
中走代理的方法, 需要在clashx
中打开允许局域网连接(allow LAN).
然后查看物理主机的IP地址, 通过ifconfig|grep inet
查看, 然后是端口, 这里默认应该就是7890
了, 在虚拟机中输入
export ALL_PROXY=socks5://10.5.46.249:7890
然后测试一下: (这里不能用ping
命令, 因为ping
采用的协议使其不能通过代理走流量)
curl -vv https://www.google.com
发现已经能成功连接了.
通过unset ALL_PROXY
取消设置代理.
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。