Skip to content

Commit

Permalink
feat(region,host): add guest disk auto reset on shutdown support (#22196
Browse files Browse the repository at this point in the history
)
  • Loading branch information
wanyaoqi authored Feb 27, 2025
1 parent 3c0d842 commit bc8e922
Show file tree
Hide file tree
Showing 12 changed files with 67 additions and 4 deletions.
5 changes: 5 additions & 0 deletions cmd/climc/shell/compute/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ func init() {
AutoSnapshot string `help:"enable/disable auto snapshot of disk" choices:"enable|disable"`
DiskType string `help:"Disk type" choices:"data|volume"`
IsSsd *bool `help:"mark disk as ssd" negative:"no-is-ssd"`
AutoReset *bool `help:"Enable auto reset disk after geust shutdown"`
}
R(&DiskUpdateOptions{}, "disk-update", "Update property of a virtual disk", func(s *mcclient.ClientSession, args *DiskUpdateOptions) error {
params := jsonutils.NewDict()
Expand Down Expand Up @@ -207,6 +208,10 @@ func init() {
params.Add(jsonutils.JSONFalse, "is_ssd")
}
}
if args.AutoReset != nil {
params.Add(jsonutils.NewBool(*args.AutoReset), "auto_reset")
}

if params.Size() == 0 {
return InvalidUpdateError()
}
Expand Down
4 changes: 4 additions & 0 deletions pkg/apis/compute/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,10 @@ type DiskConfig struct {
// requried: false
Fs string `json:"fs"`

// 关机后自动重置磁盘
// required: false
AutoReset bool `json:"auto_reset"`

// 磁盘存储格式
// enum: qcow2, raw, docker, iso, vmdk, vmdkflatver1, vmdkflatver2, vmdkflat, vmdksparse, vmdksparsever1, vmdksparsever2, vmdksepsparse vhd
// requried: false
Expand Down
1 change: 1 addition & 0 deletions pkg/apis/compute/guest_disk.go
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ type GuestdiskJsonDesc struct {
Dev string `json:"dev"`
IsSSD bool `json:"is_ssd"`
NumQueues uint8 `json:"num_queues"`
AutoReset bool `json:"auto_reset"`

// esxi
ImageInfo struct {
Expand Down
2 changes: 2 additions & 0 deletions pkg/cloudcommon/cmdline/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,8 @@ func ParseDiskConfig(diskStr string, idx int) (*compute.DiskConfig, error) {
diskConfig.Mountpoint = p
} else if p == "autoextend" {
diskConfig.SizeMb = -1
} else if p == "autoreset" {
diskConfig.AutoReset = true
} else if utils.IsInStringArray(p, compute.STORAGE_TYPES) {
diskConfig.Backend = p
} else if len(p) > 0 {
Expand Down
2 changes: 2 additions & 0 deletions pkg/compute/models/disks.go
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,8 @@ type SDisk struct {

// # is persistent
Nonpersistent bool `default:"false" list:"user" json:"nonpersistent"`
// auto reset disk after guest shutdown
AutoReset bool `default:"false" list:"user" update:"user" json:"auto_reset"`

// 是否标记为SSD磁盘
IsSsd bool `nullable:"false" default:"false" list:"user" update:"user" create:"optional"`
Expand Down
1 change: 1 addition & 0 deletions pkg/compute/models/guestdisks.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,7 @@ func (self *SGuestdisk) GetDiskJsonDescAtHost(ctx context.Context, host *SHost,
}
desc.Format = disk.DiskFormat
desc.Index = self.Index
desc.AutoReset = disk.AutoReset

if len(disk.SnapshotId) > 0 {
needMerge := disk.GetMetadata(ctx, "merge_snapshot", nil)
Expand Down
1 change: 1 addition & 0 deletions pkg/compute/models/storages.go
Original file line number Diff line number Diff line change
Expand Up @@ -1441,6 +1441,7 @@ func (self *SStorage) createDisk(ctx context.Context, name string, diskConfig *a
disk.ProjectSrc = string(apis.OWNER_SOURCE_LOCAL)
disk.DomainId = ownerId.GetProjectDomainId()
disk.IsSystem = isSystem
disk.AutoReset = diskConfig.AutoReset

if self.MediumType == api.DISK_TYPE_SSD {
disk.IsSsd = true
Expand Down
28 changes: 27 additions & 1 deletion pkg/hostman/guestman/qemu-kvm.go
Original file line number Diff line number Diff line change
Expand Up @@ -1422,8 +1422,34 @@ func (s *SKVMGuestInstance) Stop() bool {
}
}

func (s *SKVMGuestInstance) getTmpDirPath() string {
hasResetDisk := false
disks, _ := s.Desc.GetArray("disks")
for _, disk := range disks {
autoReset := jsonutils.QueryBoolean(disk, "auto_reset", false)
if autoReset {
hasResetDisk = true
}
storageType, _ := disk.GetString("storage_type")
diskpath, _ := disk.GetString("path")
if autoReset && utils.IsInStringArray(storageType, api.FIEL_STORAGE) {
return filepath.Dir(diskpath)
}
}
if hasResetDisk {
return options.HostOptions.ResetDiskTmpDir
}
return ""
}

func (s *SKVMGuestInstance) scriptStart() error {
output, err := procutils.NewRemoteCommandAsFarAsPossible("bash", s.GetStartScriptPath()).Output()
tmpDir := s.getTmpDirPath()
cmd := procutils.NewRemoteCommandAsFarAsPossible("bash", s.GetStartScriptPath())
if tmpDir != "" {
envs := []string{fmt.Sprintf("TMPDIR=%s", tmpDir)}
cmd.SetEnv(envs)
}
output, err := cmd.Output()
if err != nil {
s.scriptStop()
return fmt.Errorf("Start VM Failed %s %s", output, err)
Expand Down
3 changes: 3 additions & 0 deletions pkg/hostman/guestman/qemu/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,6 +353,9 @@ func getDiskDriveOption(
if isEncrypt {
opt += ",encrypt.format=luks,encrypt.key-secret=sec0"
}
if disk.AutoReset {
opt += ",snapshot=on"
}
// #opt += ",media=disk"
return drvOpt.Drive(opt)
}
Expand Down
3 changes: 2 additions & 1 deletion pkg/hostman/options/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -197,7 +197,8 @@ type SHostOptions struct {

BinaryMemcleanPath string `help:"execute binary memclean path" default:"/opt/yunion/bin/memclean"`

MaxHotplugVCpuCount int `help:"maximal possible vCPU count that the platform kvm supports"`
MaxHotplugVCpuCount int `help:"maximal possible vCPU count that the platform kvm supports"`
ResetDiskTmpDir string `help:"auto reset disk after guest shutdown will write disk to tmpdir"`
}

var (
Expand Down
17 changes: 15 additions & 2 deletions pkg/util/procutils/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ type Cmd interface {
Wait() error
Run() error
Kill() error
SetEnv([]string)
}

type Executor interface {
Expand All @@ -63,6 +64,10 @@ func (c *defaultCmd) Kill() error {
return c.Process.Kill()
}

func (c *defaultCmd) SetEnv(envs []string) {
c.Env = append(c.Env, envs...)
}

type defaultExecutor struct{}

func (e *defaultExecutor) Command(name string, args ...string) Cmd {
Expand All @@ -88,18 +93,26 @@ func (e *defaultExecutor) GetExitStatus(err error) (int, bool) {
}
}

type remoteCmd struct {
*client.Cmd
}

func (c *remoteCmd) SetEnv(envs []string) {
c.Env = append(c.Env, envs...)
}

type remoteExecutor struct{}

func (e *remoteExecutor) Command(name string, args ...string) Cmd {
cmd := client.Command(name, args...)
remoteCmdSetEnv(cmd)
return cmd
return &remoteCmd{cmd}
}

func (e *remoteExecutor) CommandContext(ctx context.Context, name string, args ...string) Cmd {
cmd := client.CommandContext(ctx, name, args...)
remoteCmdSetEnv(cmd)
return cmd
return &remoteCmd{cmd}
}

func (e *remoteExecutor) GetExitStatus(err error) (int, bool) {
Expand Down
4 changes: 4 additions & 0 deletions pkg/util/procutils/procutils.go
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,10 @@ func (c *Command) Start() error {
return c.cmd.Start()
}

func (c *Command) SetEnv(envs []string) {
c.cmd.SetEnv(envs)
}

func (c *Command) Wait() error {
return c.cmd.Wait()
}
Expand Down

0 comments on commit bc8e922

Please sign in to comment.