Skip to content

Commit

Permalink
fix(host,host-deployer): start deployer guest with all of disk (#21712)
Browse files Browse the repository at this point in the history
  • Loading branch information
wanyaoqi authored Dec 2, 2024
1 parent 7b96f7b commit 55ed52d
Show file tree
Hide file tree
Showing 2 changed files with 46 additions and 21 deletions.
37 changes: 23 additions & 14 deletions pkg/hostman/diskutils/qemu_kvm/driver.go
Original file line number Diff line number Diff line change
Expand Up @@ -301,15 +301,19 @@ func (d *QemuKvmDriver) connect(guestDesc *apis.GuestDesc) error {
var sshport = manager.GetSshFreePort()
defer manager.unsetPort(sshport)

//if guestDesc != nil {
// for i := range guestDesc.Disks {
// disks = append(disks, guestDesc.Disks[i].Path)
// }
//} else {
disks = append(disks, d.imageInfo.Path)
//}

err := d.qemuArchDriver.StartGuest(sshport, ncpu, memSizeMB, manager.hugepage, manager.hugepageSizeKB, d.imageInfo)
if guestDesc != nil && len(guestDesc.Disks) > 0 {
for i := range guestDesc.Disks {
disks = append(disks, guestDesc.Disks[i].Path)
}
} else {
disks = append(disks, d.imageInfo.Path)
}

err := d.qemuArchDriver.StartGuest(
sshport, ncpu, memSizeMB,
manager.hugepage, manager.hugepageSizeKB,
d.imageInfo, disks,
)
if err != nil {
return err
}
Expand Down Expand Up @@ -609,7 +613,7 @@ func (d *QemuBaseDriver) CleanGuest() {
}

func (d *QemuBaseDriver) startCmds(
sshPort, ncpu, memSizeMB int, imageInfo qemuimg.SImageInfo,
sshPort, ncpu, memSizeMB int, imageInfo qemuimg.SImageInfo, disksPath []string,
machineOpts, cdromDeviceOpts, fwOpts, socketPath, initrdPath, kernelPath string,
) string {
cmd := manager.qemuCmd
Expand Down Expand Up @@ -644,7 +648,10 @@ func (d *QemuBaseDriver) startCmds(

cmd += __("-object %s", imageInfo.SecretOptions())
}
for i, diskPath := range []string{imageInfo.Path} {
if len(disksPath) == 0 {
disksPath = []string{imageInfo.Path}
}
for i, diskPath := range disksPath {
diskDrive := __("-drive file=%s,if=none,id=drive_%d,cache=none", diskPath, i)
if imageInfo.Format != qemuimgfmt.RAW && imageInfo.Encrypted() {
diskDrive += ",encrypt.format=luks,encrypt.key-secret=sec0"
Expand All @@ -663,7 +670,7 @@ type QemuX86Driver struct {
QemuBaseDriver
}

func (d *QemuX86Driver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo) error {
func (d *QemuX86Driver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo, disksPath []string) error {
uuid := stringutils.UUID4()
socketPath := fmt.Sprintf("/opt/cloud/host-deployer/hmp_%s.socket", uuid)
d.pidPath = fmt.Sprintf("/opt/cloud/host-deployer/%s.pid", uuid)
Expand All @@ -675,6 +682,7 @@ func (d *QemuX86Driver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool,
ncpu,
memSizeMB,
imageInfo,
disksPath,
machineOpts,
cdromDeviceOpts,
"",
Expand Down Expand Up @@ -717,7 +725,7 @@ type QemuARMDriver struct {
QemuBaseDriver
}

func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo) error {
func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo, disksPath []string) error {
uuid := stringutils.UUID4()
socketPath := fmt.Sprintf("/opt/cloud/host-deployer/hmp_%s.socket", uuid)
d.pidPath = fmt.Sprintf("/opt/cloud/host-deployer/%s.pid", uuid)
Expand All @@ -736,6 +744,7 @@ func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool,
ncpu,
memSizeMB,
imageInfo,
disksPath,
machineOpts,
cdromDeviceOpts,
fwOpts,
Expand Down Expand Up @@ -775,7 +784,7 @@ func (d *QemuARMDriver) StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool,
}

type IQemuArchDriver interface {
StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo) error
StartGuest(sshPort, ncpu, memSizeMB int, hugePage bool, pageSizeKB int, imageInfo qemuimg.SImageInfo, disksPath []string) error
CleanGuest()
}

Expand Down
30 changes: 23 additions & 7 deletions pkg/hostman/guestman/qemu-kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1915,6 +1915,9 @@ func (s *SKVMGuestInstance) HandleStop(ctx context.Context, timeout int64) error
}

func (s *SKVMGuestInstance) DeployFs(ctx context.Context, userCred mcclient.TokenCredential, deployInfo *deployapi.DeployInfo) (jsonutils.JSONObject, error) {
if len(s.Desc.Disks) == 0 {
return nil, fmt.Errorf("Guest dosen't have disk ??")
}
diskInfo := deployapi.DiskInfo{}
if s.isEncrypted() {
ekey, err := s.getEncryptKey(ctx, userCred)
Expand All @@ -1925,24 +1928,37 @@ func (s *SKVMGuestInstance) DeployFs(ctx context.Context, userCred mcclient.Toke
diskInfo.EncryptPassword = ekey.Key
diskInfo.EncryptAlg = string(ekey.Alg)
}
var sysDisk storageman.IDisk
disks := s.Desc.Disks
if len(disks) > 0 {
diskPath := disks[0].Path
for i := range disks {
diskPath := disks[i].Path
// GetDiskByPath will probe disks
disk, err := storageman.GetManager().GetDiskByPath(diskPath)
if err != nil {
return nil, errors.Wrapf(err, "GetDiskByPath(%s)", diskPath)
}
diskInfo.Path = disk.GetPath()
ret, err := disk.DeployGuestFs(&diskInfo, s.Desc, deployInfo)
if i == 0 {
// sys disk
diskInfo.Path = disk.GetPath()
sysDisk = disk
}
}

ret, err := sysDisk.DeployGuestFs(&diskInfo, s.Desc, deployInfo)
for i := range disks {
diskPath := disks[i].Path
disk, e := storageman.GetManager().GetDiskByPath(diskPath)
if e != nil {
log.Errorf("failed get disk bypath %s %s", diskPath, e)
}
if utils.IsInStringArray(disk.GetType(), []string{api.STORAGE_SLVM, api.STORAGE_CLVM}) {
if errDeactive := lvmutils.LVDeactivate(diskPath); err != nil {
log.Errorf("failed deactive disk %s: %s", diskPath, errDeactive)
}
}
return ret, err
} else {
return nil, fmt.Errorf("Guest dosen't have disk ??")
}

return ret, err
}

// Delay process
Expand Down

0 comments on commit 55ed52d

Please sign in to comment.