diff --git a/pkg/hostman/diskutils/qemu_kvm/driver.go b/pkg/hostman/diskutils/qemu_kvm/driver.go index 949362fbf5d..bc8c0bbce13 100644 --- a/pkg/hostman/diskutils/qemu_kvm/driver.go +++ b/pkg/hostman/diskutils/qemu_kvm/driver.go @@ -17,9 +17,9 @@ package qemu_kvm import ( "encoding/json" "fmt" - "io" "strings" "sync" + "sync/atomic" "yunion.io/x/log" "yunion.io/x/pkg/errors" @@ -190,11 +190,19 @@ func (d *QemuKvmDriver) connect(guestDesc *apis.GuestDesc) error { } log.Infof("guest started ....") - cli, err := ssh.NewClient("localhost", sshport, "root", YUNIONOS_PASSWD, "") - if err != nil { + for i := 0; i < 3; i++ { + cli, e := ssh.NewClient("localhost", sshport, "root", YUNIONOS_PASSWD, "") + if e == nil { + d.sshClient = cli + break + } + err = e + log.Errorf("new ssh client failed: %s", err) + } + if d.sshClient == nil { return errors.Wrap(err, "new ssh client") } - d.sshClient = cli + log.Infof("guest ssh connected") out, err := d.sshRun("mount /dev/sr0 /opt") @@ -419,20 +427,15 @@ func __(v string, vs ...interface{}) string { } type QemuBaseDriver struct { - proc *procutils.Command - outb io.ReadCloser - errb io.ReadCloser - hmp *monitor.HmpMonitor hugepagePath string pidPath string + cleaned uint32 + sync.Once } func (d *QemuBaseDriver) CleanGuest() { - defer manager.Release() - if d.hmp != nil { - d.hmp.IsConnected() d.hmp.Quit(func(string) {}) d.hmp = nil } @@ -454,6 +457,11 @@ func (d *QemuBaseDriver) CleanGuest() { } d.hugepagePath = "" } + + if atomic.LoadUint32(&d.cleaned) != 1 { + manager.Release() + atomic.StoreUint32(&d.cleaned, 1) + } } type QemuX86Driver struct { @@ -565,7 +573,7 @@ func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, } else { cmd += __("-cpu max") } - cmd += __("-M virt") + cmd += __("-M virt,gic-version=max") d.pidPath = fmt.Sprintf("/tmp/%s.pid", uuid) cmd += __("-nodefaults") @@ -578,7 +586,7 @@ func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, cmd += __("-m %dM", memSizeMB) cmd += __("-initrd %s", ARM_INITRD_PATH) cmd += __("-kernel %s", ARM_KERNEL_PATH) - cmd += __("-drive if=pflash,format=raw,unit=0,file=/opt/cloud/contrib/OVMF.fd,readonly=on") + cmd += __("-drive if=pflash,format=raw,unit=0,file=/usr/share/AAVMF/AAVMF_CODE.fd,readonly=on") cmd += __("-device virtio-serial-pci") cmd += __("-netdev user,id=hostnet0,hostfwd=tcp::%d-:22", sshPort) diff --git a/pkg/hostman/diskutils/vddk.go b/pkg/hostman/diskutils/vddk.go index e77c5e68b3e..d0fc87e87bf 100644 --- a/pkg/hostman/diskutils/vddk.go +++ b/pkg/hostman/diskutils/vddk.go @@ -154,9 +154,15 @@ func (vd *VDDKDisk) Connect(*apis.GuestDesc) error { } vd.kvmDisk, err = NewKVMGuestDisk(qemuimg.SImageInfo{Path: flatFile}, vd.deployDriver, vd.readOnly) if err != nil { + vd.DisconnectBlockDevice() return errors.Wrap(err, "NewKVMGuestDisk") } - return vd.kvmDisk.Connect(nil) + err = vd.kvmDisk.Connect(nil) + if err != nil { + vd.DisconnectBlockDevice() + return errors.Wrap(err, "kvmDisk connect") + } + return nil } func (vd *VDDKDisk) Disconnect() error { diff --git a/pkg/hostman/guestfs/kvmpart/kvmpart.go b/pkg/hostman/guestfs/kvmpart/kvmpart.go index dea4c10ac69..ea62ed29b1a 100644 --- a/pkg/hostman/guestfs/kvmpart/kvmpart.go +++ b/pkg/hostman/guestfs/kvmpart/kvmpart.go @@ -303,6 +303,10 @@ func (p *SKVMGuestDiskPartition) Umount() error { } } + if _, err := procutils.NewCommand("blockdev", "--flushbufs", p.partDev).Output(); err != nil { + log.Warningf("blockdev --flushbufs %s error: %v", p.partDev, err) + } + var tries = 0 var err error var out []byte @@ -311,9 +315,6 @@ func (p *SKVMGuestDiskPartition) Umount() error { log.Infof("umount %s: %s", p.partDev, p.mountPath) out, err = procutils.NewCommand("umount", p.mountPath).Output() if err == nil { - if _, err := procutils.NewCommand("blockdev", "--flushbufs", p.partDev).Output(); err != nil { - log.Warningf("blockdev --flushbufs %s error: %v", p.partDev, err) - } if err := os.Remove(p.mountPath); err != nil { log.Warningf("remove mount path %s error: %v", p.mountPath, err) } diff --git a/pkg/hostman/hostdeployer/deployserver/deployserver.go b/pkg/hostman/hostdeployer/deployserver/deployserver.go index 9917c9770dc..cb4edab5656 100644 --- a/pkg/hostman/hostdeployer/deployserver/deployserver.go +++ b/pkg/hostman/hostdeployer/deployserver/deployserver.go @@ -94,11 +94,11 @@ func (*DeployerServer) DeployGuestFs(ctx context.Context, req *deployapi.DeployP } defer disk.Cleanup() - defer disk.Disconnect() if err := disk.Connect(req.GuestDesc); err != nil { log.Errorf("Failed to connect %s disk: %s", req.GuestDesc.Hypervisor, err) return new(deployapi.DeployGuestFsResponse), errors.Wrap(err, "Connect") } + defer disk.Disconnect() ret, err := disk.DeployGuestfs(req) if ret == nil { @@ -133,10 +133,10 @@ func (*DeployerServer) ResizeFs(ctx context.Context, req *deployapi.ResizeFsPara } defer disk.Cleanup() - defer disk.Disconnect() if err := disk.Connect(nil); err != nil { return new(deployapi.Empty), errors.Wrap(err, "disk connect failed") } + defer disk.Disconnect() return disk.ResizeFs() }