Skip to content

Commit

Permalink
Merge pull request #18918 from wanyaoqi/automated-cherry-pick-of-#189…
Browse files Browse the repository at this point in the history
…17-upstream-master

Automated cherry pick of #18917: Automated cherry pick of #18916: Fix/host deployer
  • Loading branch information
zexi authored Dec 7, 2023
2 parents 258dca4 + c9f6484 commit 5a2d3d3
Show file tree
Hide file tree
Showing 4 changed files with 34 additions and 19 deletions.
34 changes: 21 additions & 13 deletions pkg/hostman/diskutils/qemu_kvm/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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")
Expand Down Expand Up @@ -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
}
Expand All @@ -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 {
Expand Down Expand Up @@ -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")
Expand All @@ -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)
Expand Down
8 changes: 7 additions & 1 deletion pkg/hostman/diskutils/vddk.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
7 changes: 4 additions & 3 deletions pkg/hostman/guestfs/kvmpart/kvmpart.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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)
}
Expand Down
4 changes: 2 additions & 2 deletions pkg/hostman/hostdeployer/deployserver/deployserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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()
}
Expand Down

0 comments on commit 5a2d3d3

Please sign in to comment.