From a1a00472b3070c87b68026828a259a789f0f6546 Mon Sep 17 00:00:00 2001 From: HuijingHei Date: Thu, 7 Sep 2023 10:01:10 +0800 Subject: [PATCH] qemu: add `env COSA_TEST_CDROM_UNPLUG` to test cdrom unplug The context is to hacky code to test https://github.com/openshift/os/pull/1346 According to Colin's pointer https://github.com/openshift/os/pull/1350#issuecomment-1708486101 The cdrom should be `scsi-cd`, as `ide` device can not be hotplugging. So should not pass `-cdrom` to qemu as it is `ide` by default which could not work. ``` [coreos-assembler]$ env COSA_TEST_CDROM_UNPLUG=5s cosa run qemu.qcow2 --debug -- \ -drive id=cdrom0,if=none,readonly=on,file=/srv/test.iso \ -device virtio-scsi-pci,id=scsi0 \ -device scsi-cd,bus=scsi0.0,drive=cdrom0 ... 2023-09-12T10:54:08Z platform: get cdrom id /machine/peripheral-anon/device[2] [STARTUP] '2023-09-12T10:54:13Z platform: delete cdrom ... [core@cosa-devsh ~]$ ls /dev/sr0 ls: cannot access '/dev/sr0': No such file or directory ``` --- mantle/platform/qemu.go | 37 +++++++++++++++++++++++++++++++++++++ 1 file changed, 37 insertions(+) diff --git a/mantle/platform/qemu.go b/mantle/platform/qemu.go index b07ea11861..8ac84f13d1 100644 --- a/mantle/platform/qemu.go +++ b/mantle/platform/qemu.go @@ -1910,6 +1910,43 @@ func (builder *QemuBuilder) Exec() (*QemuInstance, error) { return nil, fmt.Errorf("failed to connect over qmp to qemu instance") } + // Hacky code to test https://github.com/openshift/os/pull/1346 + if timeout, ok := os.LookupEnv("COSA_TEST_CDROM_UNPLUG"); ok { + val, err := time.ParseDuration(timeout) + if err != nil { + return nil, err + } + go func() { + devs, err := inst.listDevices() + if err != nil { + plog.Error("failed to list devices") + return + } + + var cdrom string + for _, dev := range devs.Return { + switch dev.Type { + case "child": + cdrom = filepath.Join("/machine/peripheral-anon", dev.Name) + default: + break + } + } + if cdrom == "" { + plog.Errorf("failed to get scsi-cd id") + return + } + + plog.Debugf("get cdrom id %s", cdrom) + time.Sleep(val) + if err := inst.deleteBlockDevice(cdrom); err != nil { + plog.Errorf("failed to delete block device: %s", cdrom) + return + } + plog.Info("delete cdrom") + }() + } + return &inst, nil }