FreeBSD zfs文件系统下跑bhyve虚拟机,使用简单且稳定高效,今天新装一个n3450的小主机(省电),此简单记录下vm-bhyve工具的使用。

1,安装

a,更改源,将pkg源改为163

root@server:~ # cat /etc/pkg/FreeBSD.conf 
# $FreeBSD$
#
# To disable this repository, instead of modifying or removing this file,
# create a /usr/local/etc/pkg/repos/FreeBSD.conf file:
#
#   mkdir -p /usr/local/etc/pkg/repos
#   echo "FreeBSD: { enabled: no }" > /usr/local/etc/pkg/repos/FreeBSD.conf
#

FreeBSD: {
#  url: "pkg+http://pkg.FreeBSD.org/${ABI}/quarterly",
  url: "pkg+http://mirrors.163.com/freebsd-pkg/${ABI}/quarterly",
  mirror_type: "srv",
  signature_type: "fingerprints",
  fingerprints: "/usr/share/keys/pkg",
  enabled: yes
}

root@server:~ # pkg update 
The package management tool is not yet installed on your system.
Do you want to fetch and install it now? [y/N]: y
Bootstrapping pkg from pkg+http://mirrors.163.com/freebsd-pkg/FreeBSD:13:amd64/quarterly, please wait...

Verifying signature with trusted certificate pkg.freebsd.org.2013102301... done
Installing pkg-1.17.5_1...
Extracting pkg-1.17.5_1: 100%
Updating FreeBSD repository catalogue...
Fetching meta.conf: 100%    163 B   0.2kB/s    00:01    
Fetching packagesite.pkg: 100%    6 MiB  50.3kB/s    02:14    
Processing entries: 100%
FreeBSD repository update completed. 31192 packages processed.
All repositories are up to date.
 

b,安装vm-bhyve

root@server:~ # pkg install -y vm-bhyve-1.4.2  grub2-bhyve-0.40_8  bhyve-rc-3  bhyve-firmware-1.0_1 bhyve+-0.1.0 
Updating FreeBSD repository catalogue...
FreeBSD repository is up to date.
All repositories are up to date.
The following 10 package(s) will be affected (of 0 checked):

New packages to be INSTALLED:
    bhyve+: 0.1.0
    bhyve-firmware: 1.0_1
    bhyve-rc: 3
    ca_root_nss: 3.76
    grub2-bhyve: 0.40_8
    libevent: 2.1.12
    tmux: 3.2a
    uefi-edk2-bhyve: g20210226,2
    uefi-edk2-bhyve-csm: 0.2_3,1
    vm-bhyve: 1.4.2

Number of packages to be installed: 10

2,配置

a,创建目录

root@server:~ # zfs create zroot/vm

root@server:~ # zfs list
NAME                 USED  AVAIL     REFER  MOUNTPOINT
zroot               1012M   456G       96K  /zroot
zroot/ROOT          1009M   456G       96K  none
zroot/ROOT/default  1009M   456G     1009M  /
zroot/tmp            888K   456G      888K  /tmp
zroot/usr            416K   456G       96K  /usr
zroot/usr/home       128K   456G      128K  /usr/home
zroot/usr/ports       96K   456G       96K  /usr/ports
zroot/usr/src         96K   456G       96K  /usr/src
zroot/var            644K   456G       96K  /var
zroot/var/audit       96K   456G       96K  /var/audit
zroot/var/crash       96K   456G       96K  /var/crash
zroot/var/log        164K   456G      164K  /var/log
zroot/var/mail        96K   456G       96K  /var/mail
zroot/var/tmp         96K   456G       96K  /var/tmp
zroot/vm              96K   456G       96K  /zroot/vm

b,配置文件

root@server:~ # echo "net.link.tap.up_on_open=1" >> /etc/sysctl.conf
root@server:~ # cat /etc/rc.conf
clear_tmp_enable="YES"
hostname="server"
ifconfig_re0="inet 192.168.1.252 netmask 255.255.255.0"
defaultrouter="192.168.1.3"
sshd_enable="YES"
ntpdate_enable="YES"
ntpd_enable="YES"
powerd_enable="YES"
# Set dumpdev to "AUTO" to enable crash dumps, "NO" to disable
dumpdev="AUTO"
zfs_enable="YES"
ntpdate_program="ntpdate"
ntpdate_flags="210.72.145.44"
vm_enable="YES"          #启动vm
vm_dir="zfs:zroot/vm"    #目录为创建的zfs目录

root@server:~ # reboot    #重启下
 

c,复制模板

root@server:/zroot/vm # cp -R /usr/local/share/examples/vm-bhyve/* /zroot/vm/.templates/

 d,创建一个switch

root@server:/zroot/vm # vm switch create public
root@server:/zroot/vm # vm switch add public re0
root@server:/zroot/vm # ifconfig -a
re0: flags=8943<UP,BROADCAST,RUNNING,PROMISC,SIMPLEX,MULTICAST> metric 0 mtu 1500
    options=8209b<RXCSUM,TXCSUM,VLAN_MTU,VLAN_HWTAGGING,VLAN_HWCSUM,WOL_MAGIC,LINKSTATE>
    ether 00:e2:69:3c:1c:4e
    inet 192.168.1.252 netmask 0xffffff00 broadcast 192.168.1.255
    media: Ethernet autoselect (100baseTX <full-duplex>)
    status: active
    nd6 options=29<PERFORMNUD,IFDISABLED,AUTO_LINKLOCAL>
lo0: flags=8049<UP,LOOPBACK,RUNNING,MULTICAST> metric 0 mtu 16384
    options=680003<RXCSUM,TXCSUM,LINKSTATE,RXCSUM_IPV6,TXCSUM_IPV6>
    inet6 ::1 prefixlen 128
    inet6 fe80::1%lo0 prefixlen 64 scopeid 0x2
    inet 127.0.0.1 netmask 0xff000000
    groups: lo
    nd6 options=21<PERFORMNUD,AUTO_LINKLOCAL>
vm-public: flags=8843<UP,BROADCAST,RUNNING,SIMPLEX,MULTICAST> metric 0 mtu 1500
    ether ea:9d:fb:b8:91:52
    id 00:00:00:00:00:00 priority 32768 hellotime 2 fwddelay 15
    maxage 20 holdcnt 6 proto rstp maxaddr 2000 timeout 1200
    root id 00:00:00:00:00:00 priority 32768 ifcost 0 port 0
    member: re0 flags=143<LEARNING,DISCOVER,AUTOEDGE,AUTOPTP>
            ifmaxaddr 0 port 1 priority 128 path cost 200000
    groups: bridge vm-switch viid-4c918@
    nd6 options=9<PERFORMNUD,IFDISABLED>
 

以上步骤环境配置完成,即可安装虚拟及。

3,安装系统

 a,安装linux

将iso文件上传到/zroot/vm/.iso目录下,查看有哪些模板

root@server:/zroot/vm/.iso # pwd
/zroot/vm/.iso
root@server:/zroot/vm/.iso # ls
alpine-virt-3.16.0-x86_64.iso    centos7.iso            fb13.raw

root@server:/zroot/vm/.iso # ls ../.templates/
alpine.conf        centos7.conf        debian.conf        freebsd-zvol.conf    netbsd.conf        ubuntu.conf
arch.conf        config.sample        default.conf        freepbx.conf        openbsd.conf        windows.conf
centos6.conf        coreos.conf        dragonfly.conf        linux-zvol.conf        resflash.conf
 

 本处以alpine为例:

创建一个alpine,硬盘50G,名称为alpinetem

root@server:/zroot/vm/.iso # vm create -t centos7  -s 50G alpinetem

可以通过vm list获取配置或者直接查看

root@server:/zroot/vm # pwd;ls
/zroot/vm
.config        .img        .iso        .templates    alpinetem

可以通过修改alpinetem/alinetem.conf更改配置,也可以使用vm config apinetem修改配置文件。

root@server:/zroot/vm/.iso # vm config alpinetem

loader="uefi"
graphics="yes"
xhci_mouse="yes"
cpu=2
memory=2512M
network0_type="virtio-net"
network0_switch="public"
disk0_type="virtio-blk"
disk0_name="disk0.img"
uuid="474169ff-de42-11ec-a308-00e2693c1c4e"
network0_mac="58:9c:fc:0f:22:cd"
 

 通过vm config更改为2cpu 2512M memory的主机

安装alpine

root@server:/zroot/vm # vm install alpinetem alpine-virt-3.16.0-x86_64.iso
Starting alpinetem
  * found guest in /zroot/vm/alpinetem
  * booting...
root@server:/zroot/vm/.iso # vm list
NAME       DATASTORE  LOADER     CPU  MEMORY  VNC           AUTOSTART  STATE
alpinetem  default    uefi       2    2512M   0.0.0.0:5900  No         Running (5147)

通过vnc连接控制台安装即可

lee@rock-3a:~/S02$ vncviewer 192.168.1.252:5900

TigerVNC Viewer 64-bit v1.9.0
Built on: 2020-09-29 18:21
Copyright (C) 1999-2018 TigerVNC Team and many others (see README.rst)
See http://www.tigervnc.org for information on TigerVNC.

Sat May 28 12:55:03 2022
 DecodeManager: Detected 4 CPU core(s)
 DecodeManager: Creating 4 decoder thread(s)
 CConn:       connected to host 192.168.1.252 port 5900
 CConnection: Server supports RFB protocol version 3.8
 CConnection: Using RFB protocol version 3.8
 CConnection: Choosing security type None(1)
 CConn:       Using pixel format depth 24 (32bpp) little-endian rgb888
 CConn:       Using Tight encoding

4,其他应用

a,clone

将安装的系统做为模板,进行克隆

root@server:/zroot/vm/.iso # vm list
NAME       DATASTORE  LOADER     CPU  MEMORY  VNC  AUTOSTART  STATE
alpinetem  default    uefi       2    2512M   -    No         Stopped
fb13       default    bhyveload  1    256M    -    No         Stopped
root@server:/zroot/vm/.iso # vm clone alpinetem alpine
root@server:/zroot/vm/.iso # vm list
NAME       DATASTORE  LOADER     CPU  MEMORY  VNC  AUTOSTART  STATE
alpine     default    uefi       2    2512M   -    No         Stopped
alpinetem  default    uefi       2    2512M   -    No         Stopped
fb13       default    bhyveload  1    256M    -    No         Stopped
root@server:/zroot/vm/.iso # 

b,snapshot

想给当前虚拟机做一个快照,可以使用snapshot

开机

root@server:/zroot/vm # vm start alpine
Starting alpine
  * found guest in /zroot/vm/alpine
  * booting...
 

 在主机中创建文件并关机

lee@rock-3a:~/S02$ ssh lee@192.168.1.94                #登录到主机
The authenticity of host '192.168.1.94 (192.168.1.94)' can't be established.
ECDSA key fingerprint is SHA256:Gzmb0PVSqutWMgIVU6fzccnFJ+GQ422Lk9QmB7H/mIM.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added '192.168.1.94' (ECDSA) to the list of known hosts.
lee@192.168.1.94's password: 
Welcome to Alpine!

localhost:~$ echo "This is a test file">1.txt
localhost:~$ ls
1.txt
localhost:~$ echo "This is a test file">1.txt
localhost:~$ ls
1.txt
localhost:~# halt                                                           #关机
localhost:~# Connection to 192.168.1.94 closed by remote host.
Connection to 192.168.1.94 closed.
 

 创建snapshot

root@server:/zroot/vm # vm snapshot alpine alpine@20001554
root@server:/zroot/vm # zfs list -t snapshot
NAME                                  USED  AVAIL     REFER  MOUNTPOINT
zroot/vm/alpine@2022-05-28-15:54:46     0B      -      249M  -
zroot/vm/alpinetem@e17efe49             0B      -      249M  -

开机并删除1.txt 

root@server:/zroot/vm # vm start alpine
Starting alpine
  * found guest in /zroot/vm/alpine
  * booting...
root@server:/zroot/vm # ssh lee@192.168.1.94
localhost:~$ ls
1.txt
localhost:~$ rm 1.txt 
localhost:~$ su -
Password: 
localhost:~# halt
localhost:~# Connection to 192.168.1.94 closed by remote host.
Connection to 192.168.1.94 closed.
 

回滚

 root@server:/zroot/vm # vm rollback alpine@2022-05-28-15:54:46

开机验证 

root@server:/zroot/vm # ssh lee@192.168.1.94

localhost:~$ ls
1.txt
localhost:~$ cat 1.txt 
This is a test file
 

5,开机启动

开机启动直接加入rc.conf即可,例如:

root@power:~ # cat  /etc/rc.conf
 

hostname="power"

vm_enable="YES"
vm_dir="zfs:zroot/vm"
ntpdate_enable="YES"
ntpdate_program="ntpdate"
ntpdate_flags="210.72.145.44"
vm_list="win10 alpine"   #开机自启列表,以空格隔开
vm_delay="10"               #启动间隔
 

后记:

vm-bhyve是一个脚本,更多信息可以参看man vm和vm的官方github    https://github.com/churchers/vm-bhyve
一个方便操作vm的工具,可以让我们更容易使用vm。 

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐