分类目录归档:TrueNAS

在TrueNAS中给虚拟机直通磁盘

参考:https://www.truenas.com/community/threads/using-cli-to-passthrough-hdd-to-a-vm.98549/post-679949

刚给NAS加了几块硬盘,在导数据的时候不知道为啥,重启了好几次之后,旧磁盘的整个池都没法导入了,一导入就整个机器卡死重启,重启之后卡在ix-etc服务,没法进入系统。

不得已,只能在TrueNAS里面开个虚拟机,把磁盘直通给虚拟机,重新导入池然后scrub一次试试捞数据。但是TrueNAS的虚拟机功能,只能说有,和好用是一点边都沾不上。

幸好,在truenas的论坛上有人po出了操作方法,这里做个翻译和记录。

首先,需要使用root登录到Truenas。

然后,使用这个命令来查找所有磁盘:

find /dev/disk/by-id/ -type l|xargs -I{} ls -l {}|grep -v -E '[0-9]$' |sort -k11|cut -d' ' -f9,10,11,12

以下是输出结果的例子

root@datacenter[~]# find /dev/disk/by-id/ -type l|xargs -I{} ls -l {}|grep -v -E '[0-9]$' |sort -k11|cut -d' ' -f9,10,11,12 | sort
/dev/disk/by-id/ata-KINGSTON_SA400M8120G_50026B7685357785 -> ../../sdn
/dev/disk/by-id/ata-ST16000NM001G-2KK103_ZL2KB41G -> ../../sdb
/dev/disk/by-id/ata-ST16000NM001G-2KK103_ZL2KG36H -> ../../sdd
/dev/disk/by-id/ata-ST16000NM001G-2KK103_ZL2KG8BD -> ../../sda
/dev/disk/by-id/ata-ST16000NM001G-2KK103_ZL2KGSRT -> ../../sdc
/dev/disk/by-id/ata-Samsung_SSD_850_EVO_500GB_S21JNSAG158509B -> ../../sdj
/dev/disk/by-id/ata-Samsung_SSD_850_EVO_500GB_S2RBNX0H937583L -> ../../sdk
/dev/disk/by-id/ata-WDC_WD10JFCX-68N6GN0_WD-WX11A15ET12E -> ../../sdm
/dev/disk/by-id/ata-WDC_WD10JFCX-68N6GN0_WD-WX11A15ETRZ9 -> ../../sdl
/dev/disk/by-id/ata-WDC_WD40EFRX-68N32N0_WD-WCC7K1DFDE54 -> ../../sdi
/dev/disk/by-id/ata-WDC_WD40EFRX-68WT0N0_WD-WCC4E0135118 -> ../../sde
/dev/disk/by-id/ata-WDC_WD40EFRX-68WT0N0_WD-WCC4E0185240 -> ../../sdh
/dev/disk/by-id/ata-WDC_WD40EFRX-68WT0N0_WD-WCC4E0243702 -> ../../sdg
/dev/disk/by-id/ata-WDC_WD40EFRX-68WT0N0_WD-WCC4ENSDRE27 -> ../../sdf
/dev/disk/by-id/wwn-0x5000c500dbd815d2 -> ../../sdb
/dev/disk/by-id/wwn-0x5000c500dbe1b5b7 -> ../../sdd
/dev/disk/by-id/wwn-0x5000c500dbe40981 -> ../../sdc
/dev/disk/by-id/wwn-0x5000c500dbe46e89 -> ../../sda
/dev/disk/by-id/wwn-0x50014ee209112b98 -> ../../sde
/dev/disk/by-id/wwn-0x50014ee20b01a0fc -> ../../sdf
/dev/disk/by-id/wwn-0x50014ee210a8b4bc -> ../../sdi
/dev/disk/by-id/wwn-0x50014ee25e74959c -> ../../sdg
/dev/disk/by-id/wwn-0x50014ee2b3bbed86 -> ../../sdh
/dev/disk/by-id/wwn-0x50014ee6b01bee55 -> ../../sdl
/dev/disk/by-id/wwn-0x50014ee6b01bfbaa -> ../../sdm
/dev/disk/by-id/wwn-0x5002538d414b3774 -> ../../sdk
/dev/disk/by-id/wwn-0x5002538da015f254 -> ../../sdj
/dev/disk/by-id/wwn-0x50026b7685357785 -> ../../sdn

其中/dev/disk/by-id/wwn-0x5002538da015f254就是一个磁盘目标(sdj)。

然后,创建一个alias命令,方便virsh连接到TrueNAS的qemu。

alias virsh='virsh -c "qemu+unix:///system?socket=/run/truenas_libvirt/libvirt-sock" $1'

然后用virsh list --all来列出所有vm,注意TrueNAS的在命令行里面的虚拟机名称会有一个编号在前头。

root@datacenter[~]# virsh list --all
 Id   Name      State
-------------------------
 7    1_mover   running

知道虚拟机名称之后,就可以将设备直通给虚拟机了,命令如下

virsh attach-disk <VM Name> <Disk ID> <Target Name>

举个例子

virsh attach-disk 1_mover /dev/disk/by-id/wwn-0x5002538da015f254 vdj

然后就能在虚拟机里面看到这个直通的磁盘了。

要注意的是,这个直通是临时的,关闭虚拟机会失去这个直通,需要重新配置。重启则不需要重新配置。