参考
实验
建立下面的PCI总线拓扑,然后来模拟scsi磁盘的添加和删除
- PCI总线拓扑
root@ubuntu-vm:~# lspci -tv -[0000:00]-+-00.0 Intel Corporation 440FX - 82441FX PMC [Natoma] +-01.0 Intel Corporation 82371SB PIIX3 ISA [Natoma/Triton II] +-01.1 Intel Corporation 82371SB PIIX3 IDE [Natoma/Triton II] +-01.3 Intel Corporation 82371AB/EB/MB PIIX4 ACPI +-02.0 Device 1234:1111 +-03.0 Red Hat, Inc. Virtio filesystem +-04.0 Red Hat, Inc. Virtio network device +-05.0 Red Hat, Inc. Virtio block device +-06.0 Red Hat, Inc. Virtio block device \-07.0-[01]--+-03.0 Red Hat, Inc. Virtio SCSI \-04.0 Red Hat, Inc. Virtio SCSI
- 实验用的块设备
root@ubuntu-vm:~# ls /sys/block/ -l lrwxrwxrwx 1 root root 0 Sep 1 16:37 sda -> ../devices/pci0000:00/0000:00:07.0/0000:01:03.0/virtio4/host0/target0:0:0/0:0:0:0/block/sda lrwxrwxrwx 1 root root 0 Sep 1 16:34 sdb -> ../devices/pci0000:00/0000:00:07.0/0000:01:04.0/virtio5/host1/target1:0:0/1:0:0:0/block/sdb
步骤
启动虚拟机的命令
qemu-system-x86_64 \ -m 2G \ -smp 4 \ --kernel "./linux-6.4/arch/x86_64/boot/bzImage" \ --append "noinitrd rootfstype=ext4 root=/dev/vda rw console=ttyS0" \ --nographic \ -fsdev local,security_model=passthrough,id=fsdev0,path=./share \ -device virtio-9p-pci,id=fs0,fsdev=fsdev0,mount_tag=hostshare \ -netdev user,id=eth0,hostfwd=tcp::8765-:22 -device virtio-net-pci,netdev=eth0 \ -drive if=none,file=./slave2.qcow2,format=qcow2,id=hd0 \ -device virtio-blk-pci,drive=hd0 \ -drive if=none,file=./test_disk.qcow2,format=qcow2,id=hd1 \ -device virtio-blk-pci,drive=hd1,num-queues=2 \ --enable-kvm \ \ -device pci-bridge,id=bridge0,chassis_nr=2 \ -device virtio-scsi-pci,id=scsi0,bus=bridge0,addr=0x3 \ -device scsi-hd,drive=hd2,bus=scsi0.0 \ -drive if=none,file=./test_disk2.qcow2,format=qcow2,id=hd2 \ -device virtio-scsi-pci,id=scsi1,bus=bridge0,addr=0x4 \ -device scsi-hd,drive=hd3,bus=scsi1.0 \ -drive if=none,file=./test_disk3.qcow2,format=qcow2,id=hd3 \
查看scsi磁盘
root@ubuntu-vm:~# lsscsi -v [0:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sda dir: /sys/bus/scsi/devices/0:0:0:0 [/sys/devices/pci0000:00/0000:00:07.0/0000:01:03.0/virtio4/host0/target0:0:0/0:0:0:0] [1:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sdb dir: /sys/bus/scsi/devices/1:0:0:0 [/sys/devices/pci0000:00/0000:00:07.0/0000:01:04.0/virtio5/host1/target1:0:0/1:0:0:0]
剔除和增加
方法1
- 剔除
# echo 1 > /sys/block/sda/device/delete # echo 1 > /sys/bus/pci/devices/0000\:00\:07.0/0000\:01\:03.0/remove
此时只剩下sdb:
# lsscsi [1:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sdb
- 添加
# echo 1 > /sys/bus/pci/devices/0000\:00\:07.0/rescan
可以看到,sda又被添加回来:
# lsscsi [0:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sda [1:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sdb
方法2
- 剔除
# echo 1 > /sys/block/sda/device/delete
此时只剩下sdb:
# lsscsi [1:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sdb
- 添加
# echo 1 > /sys/class/scsi_host/host0/scan
可以看到,sda又被添加回来:
# lsscsi [0:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sda [1:0:0:0] disk QEMU QEMU HARDDISK 2.5+ /dev/sdb BASH 复制 全屏
完。