fix(host-deployer): disconnect fix and concurrence control

This commit is contained in:
wanyaoqi
2023-12-07 16:06:35 +08:00
parent 35e11967a4
commit c9f6484cd1
4 changed files with 30 additions and 16 deletions
+20 -12
View File
@@ -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 {
@@ -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)
+7 -1
View File
@@ -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 {
+1 -1
View File
@@ -301,7 +301,7 @@ func (p *SKVMGuestDiskPartition) Umount() error {
if err != nil {
log.Errorf("SKVMGuestDiskPartition unmount btrfs error %s", err)
}
}
}
if _, err := procutils.NewCommand("blockdev", "--flushbufs", p.partDev).Output(); err != nil {
log.Warningf("blockdev --flushbufs %s error: %v", p.partDev, err)
@@ -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()
}