mac 平台使用 multipass 安装 ubuntu 虚拟机
 南窗  分类:IT技术  人气:96  回帖:0  发布于1年前 收藏

1 简介

mac使用第三方虚拟机太费劲且较重;

docker 权限、目录、网络也是烦人;

好在有 multipass , 一个轻量级虚拟机, 虽然只能支持 ubuntu, 但是centos也快凉凉了,早点换ubuntu也是个不错选择;

2 安装

个人推荐用pkg装

2.1 github下载pkg安装

https://github.com/canonical/multipass/releases

网盘地址:

链接: https://pan.baidu.com/s/1ErF5P2eBkObZey1SGg33zw 提取码: emit

2.2 brew 安装

brew cask install multipass

3 使用

3.1 创建 ubuntu 容器

查看可供下载的 ubuntu 镜像

➜  ~ multipass find
Image                       Aliases           Version          Description
snapcraft:core18            18.04             20201111         Snapcraft builder for Core 18
snapcraft:core20            20.04             20210921         Snapcraft builder for Core 20
snapcraft:core22            22.04             20220426         Snapcraft builder for Core 22
18.04                       bionic            20230329         Ubuntu 18.04 LTS
20.04                       focal             20230209         Ubuntu 20.04 LTS
22.04                       jammy,lts         20230302         Ubuntu 22.04 LTS
22.10                       kinetic           20230302         Ubuntu 22.10
anbox-cloud-appliance                         latest           Anbox Cloud Appliance

下载最新版 ubuntu 镜像并运行

multipass launch --name ubuntu

自定义配置

multipass launch -n vm01 -c 4 -m 4G -d 40G

-n, --name: 名称
-c, --cpus: cpu核心数, 默认: 1
-m, --mem: 内存大小, 默认: 1G
-d, --disk: 硬盘大小, 默认: 5G

3.2 操作容器

进入容器

multipass shell 容器名称
➜  ~ multipass info vm01  
Welcome to Ubuntu 22.04.2 LTS (GNU/Linux 5.15.0-67-generic x86_64)

 * Documentation:  https://help.ubuntu.com
 * Management:     https://landscape.canonical.com
 * Support:        https://ubuntu.com/advantage

  System information as of Fri Apr 14 00:16:55 CST 2023

  System load:             0.30908203125
  Usage of /:              3.7% of 38.58GB
  Memory usage:            5%
  Swap usage:              0%
  Processes:               134
  Users logged in:         0
  IPv4 address for enp0s2: 192.168.64.3
  IPv6 address for enp0s2: fd5c:feb8:ced3:b687:dc3e:57ff:fe78:e01b


Expanded Security Maintenance for Applications is not enabled.

0 updates can be applied immediately.

Enable ESM Apps to receive additional future security updates.
See https://ubuntu.com/esm or run: sudo pro status


The list of available updates is more than a week old.
To check for new updates run: sudo apt update

To run a command as administrator (user "root"), use "sudo <command>".
See "man sudo_root" for details.

ubuntu@vm01:~$ 


# 容器外执行容器linux命令
multipass exec 容器名 mkdir /apps

查看容器信息

➜  ~ multipass info vm01                       
Name:           vm01
State:          Running
IPv4:           192.168.64.3
Release:        Ubuntu 22.04.2 LTS
Image hash:     345fbbb6ec82 (Ubuntu 22.04 LTS)
CPU(s):         4
Load:           0.85 0.23 0.08
Disk usage:     1.4GiB out of 38.6GiB
Memory usage:   164.6MiB out of 3.8GiB
Mounts:         --

3.3 主机和容器数据交互

挂载数据卷方式

multipass mount $HOME 容器名

# 或者指定容器目录
multipass mount $HOME 容器名:目录名

# 卸载数据卷
multipass umount 容器名

transfer 进行文件传输

multipass transfer 主机文件 容器名:容器目录

3.4 容器配置自动化

为了保持开发环境和线上环境一致性 同时节省部署时间 multipass给我们提供了--cloud-init选项进行容器启动初始化配置:

multipass launch --name ubuntu --cloud-init config.yaml 



# config.yaml 内容如下:
runcmd:
  - curl -sL https://deb.nodesource.com/setup_12.x | sudo -E bash -
  - sudo apt-get install -y nodejs

runcmd 可以指定容器 首次启动 时运行的命令,上面表示首次启动自动下载安装 Node.js

3.5 容器管理

1 查看所有创建容器

multipass list

2 启动和停止运行容器

# 停止ubuntu和ubuntu1
multipass stop ubuntu ubuntu1
# 启动ubuntu
multipass start ubuntu

3 对所有容器统一操作

# 启动所有容器
multipass start --all
# 停止所有容器
multipass stop --all
# 所有容器放入回收站
multipass delete --all
# 彻底删除所有容器
multipass delete --purge --all

讨论这个帖子(0)垃圾回帖将一律封号处理……