FEMU 安装与使用

FEMU —— A QEMU-based and DRAM-backed NVMe SSD Emulator

结构

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
                      +--------------------+
| VM / Guest OS |
| |
| |
| NVMe Block Device |
+--------^^----------+
||
PCIe/NVMe
||
+------------------------------vv----------------------------+
| +---------+ +---------+ +---------+ +---------+ +------+ |
| | Blackbox| | OCSSD | | ZNS-SSD | | NoSSD | | ... | |
| +---------+ +---------+ +---------+ +---------+ +------+ |
| FEMU NVMe SSD Controller |
+------------------------------------------------------------+

环境配置

安装依赖

1
2
3
4
5
6
7
8
9
10
git clone https://github.com/vtess/femu.git
cd femu
mkdir build-femu
# Switch to the FEMU building directory
cd build-femu
# Copy femu script
cp ../femu-scripts/femu-copy-scripts.sh .
./femu-copy-scripts.sh .
# only Debian/Ubuntu based distributions supported
sudo ./pkgdep.sh

编译 FEMU

1
./femu-compile.sh

准备虚拟机镜像

下面使用官方提供的镜像:

1
2
3
4
5
6
7
8
9
10
mkdir -p ~/images
cd ~/images
wget http://people.cs.uchicago.edu/~huaicheng/femu/femu-vm.tar.xz
tar xJvf femu-vm.tar.xz
# After these steps, you will get two files: "u20s.qcow2" and "u20s.md5sum".
# You can verify the integrity of the VM image file by doing:
md5sum u20s.qcow2 > tmp.md5sum
diff tmp.md5sum u20s.md5sum
# If diff complains that the above two files differ, then the VM image file is
# corrupted. Please redo the above steps.

启动 FEMU

1
2
3
4
cd /PATH/TO/FEMU
cd build-femu
# 以黑盒模式启动
./run-blackbox.sh

The user account and guest OS of the VM:

  • username: femu
  • passwd : femu
  • Guest OS: Ubuntu 20.04.1 server, with kernel 5.4

修改 FEMU 配置

当前黑盒 SSD 参数在 hw/femu/ftl/ftl.c 中是硬编码的,请相应地更改它们并重新编译 FEMU

一些参数解释

在 PATH/TO/FEMU/build-femu/run-blackbox.sh 中:

修改以下代码设置 SSD 配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# Configurable SSD Controller layout parameters (must be power of 2)
secsz=512 # sector size in bytes
secs_per_pg=8 # number of sectors in a flash page
pgs_per_blk=256 # number of pages per flash block
blks_per_pl=256 # number of blocks per plane
pls_per_lun=1 # keep it at one, no multiplanes support
luns_per_ch=8 # number of chips per channel
nchs=8 # number of channels
ssd_size=16384 # in megabytes, if you change the above layout parameters, make sure you manually recalculate the ssd size and modify it here, please consider a default 25% overprovisioning ratio.

# Latency in nanoseconds
pg_rd_lat=56500 # page read latency
pg_wr_lat=820500 # page write latency
blk_er_lat=2000000 # block erase latency
ch_xfer_lat=0 # channel transfer time, ignored for now

# GC Threshold (1-100)
gc_thres_pcent=75
gc_thres_pcent_high=95

修改以下代码设置虚拟机配置:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
sudo ./qemu-system-x86_64 \  # 以root权限运行QEMU x86_64模拟器
-name "FEMU-BBSSD-VM" \ # 设置虚拟机名称为FEMU-BBSSD-VM
-enable-kvm \ # 启用KVM加速
-cpu host \ # 使用主机CPU特性
-smp 4 \ # 分配4个CPU核心
-m 4G \ # 分配4GB内存
-device virtio-scsi-pci,id=scsi0 \ # 添加virtio SCSI控制器
-device scsi-hd,drive=hd0 \ # 添加SCSI硬盘设备
-drive file=$OSIMGF,if=none,aio=native,cache=none,format=qcow2,id=hd0 \ # 配置虚拟机磁盘镜像
${FEMU_OPTIONS} \ # 插入之前定义的FEMU设备参数
-net user,hostfwd=tcp::8080-:22 \ # 配置用户模式网络并转发端口(8080->22)
-net nic,model=virtio \ # 添加virtio网卡
-nographic \ # 不使用图形界面
-qmp unix:./qmp-sock,server,nowait 2>&1 | tee log # 启用QMP监控并输出日志

参考链接

FEMU 官方仓库:
https://github.com/MoatLab/FEMU?tab=readme-ov-file

其他参考教程:
https://zdawng.github.io/posts/e9ca848d/

https://haslab.org/2021/05/03/femu-nvme.html


FEMU 安装与使用
http://ciliphen.github.io/FEMU-安装与使用/
作者
Felix
发布于
2025年4月18日
更新于
2025年5月7日
许可协议