diff --git a/pkg/hostman/guestfs/fsdriver/windows.go b/pkg/hostman/guestfs/fsdriver/windows.go index 2a0841130f..55e3275f18 100644 --- a/pkg/hostman/guestfs/fsdriver/windows.go +++ b/pkg/hostman/guestfs/fsdriver/windows.go @@ -1,7 +1,6 @@ package fsdriver import ( - "crypto/md5" "fmt" "math/rand" "path" @@ -17,6 +16,7 @@ import ( "yunion.io/x/onecloud/pkg/util/fileutils2" "yunion.io/x/onecloud/pkg/util/netutils2" "yunion.io/x/onecloud/pkg/util/seclib2" + "yunion.io/x/onecloud/pkg/util/stringutils2" "yunion.io/x/onecloud/pkg/util/version" "yunion.io/x/onecloud/pkg/util/winutils" "yunion.io/x/pkg/utils" @@ -156,7 +156,6 @@ func (w *SWindowsRootFs) putGuestScriptContents(spath, content string) error { } content = strings.Join(contentArr, "\r\n") - return w.rootFs.FilePutContents(spath, content, false, true) } @@ -370,7 +369,7 @@ func (w *SWindowsRootFs) deployPublicKeyByGuest(uname, passwd string) bool { }, "\r\n") w.prependGuestBootScript(bootScript) logPath := w.guestDebugLogPath - chksum := md5.Sum([]byte(passwd + logPath[(len(logPath)-10):])) + chksum := stringutils2.GetMD5Hash(passwd + logPath[(len(logPath)-10):]) chgpwdScript := strings.Join([]string{ w.MakeGuestDebugCmd("change password step 1"), @@ -415,8 +414,8 @@ func (w *SWindowsRootFs) deploySetupCompleteScripts(uname, passwd string) bool { {"IncludeRecommendedUpdates", "REG_DWORD", "0"}, {"EnableFeaturedSoftware", "REG_DWORD", "1"}, } { - cmds = append(cmds, `REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /v %s /t %s /d %s /f`, - v[0], v[1], v[2]) + cmds = append(cmds, fmt.Sprintf(`REG ADD "HKLM\SOFTWARE\Microsoft\Windows\CurrentVersion\WindowsUpdate\Auto Update" /v %s /t %s /d %s /f`, + v[0], v[1], v[2])) } cmds = append(cmds, "Net start wuauserv") cmds = append(cmds, "wuauclt /detectnow") diff --git a/pkg/hostman/host_services.go b/pkg/hostman/host_services.go index d7fc185540..a14fd1b72a 100644 --- a/pkg/hostman/host_services.go +++ b/pkg/hostman/host_services.go @@ -85,8 +85,13 @@ func (host *SHostService) StartService() { go func() { defer func() { if r := recover(); r != nil { - log.Errorln(r) + if !host.isExiting { + log.Fatalf("%s", r) + } else { + log.Errorln(r) + } } + }() cloudcommon.ServeForever(app, &options.HostOptions.CommonOptions) }() diff --git a/pkg/hostman/hostinfo/hostbridge/hostbridge.go b/pkg/hostman/hostinfo/hostbridge/hostbridge.go index 91bedb0e0f..a2aded7584 100644 --- a/pkg/hostman/hostinfo/hostbridge/hostbridge.go +++ b/pkg/hostman/hostinfo/hostbridge/hostbridge.go @@ -239,6 +239,11 @@ func (o *SOVSBridgeDriver) Setup() error { } } + if o.inter != nil && !utils.IsInStringArray(o.inter.String(), o.Interfaces()) { + if err := o.SetupInterface(); err != nil { + return err + } + } if len(o.bridge.Addr) == 0 { if len(o.ip) > 0 { if err := o.SetupAddresses(o.inter.Mask); err != nil { @@ -264,6 +269,17 @@ func (o *SOVSBridgeDriver) Setup() error { return o.BringupInterface() } +func (o *SOVSBridgeDriver) SetupInterface() error { + if o.inter != nil && !utils.IsInStringArray(o.inter.String(), o.Interfaces()) { + output, err := procutils.NewCommand("ovs-vsctl", "--", "--may-exist", + "add-port", o.bridge.String(), o.inter.String()).Run() + if err != nil { + return fmt.Errorf("Failed to add interface %s", output) + } + } + return nil +} + func (o *SOVSBridgeDriver) SetupBridgeDev() error { if !o.Exists() { _, err := procutils.NewCommand("ovs-vsctl", "--", "--may-exist", "add-br", o.bridge.String()).Run() diff --git a/pkg/hostman/storageman/disklocal.go b/pkg/hostman/storageman/disklocal.go index b0665e3ce9..4fab39f418 100644 --- a/pkg/hostman/storageman/disklocal.go +++ b/pkg/hostman/storageman/disklocal.go @@ -174,6 +174,21 @@ func (d *SLocalDisk) CreateFromImageFuse(ctx context.Context, url string) error } func (d *SLocalDisk) CreateFromTemplate(ctx context.Context, imageId, format string, size int64) (jsonutils.JSONObject, error) { + ret, err := d.createFromTemplate(ctx, imageId, format) + if err != nil { + return nil, err + } + retSize, _ := ret.Int("disk_size") + log.Infof("REQSIZE: %d, RETSIZE: %d", size, retSize) + if size > retSize { + params := jsonutils.NewDict() + params.Set("size", jsonutils.NewInt(size)) + return d.Resize(ctx, params) + } + return ret, nil +} + +func (d *SLocalDisk) createFromTemplate(ctx context.Context, imageId, format string) (jsonutils.JSONObject, error) { var imageCacheManager = storageManager.LocalStorageImagecacheManager imageCache := imageCacheManager.AcquireImage(ctx, imageId, d.GetZone(), "", "") if imageCache != nil { @@ -193,7 +208,7 @@ func (d *SLocalDisk) CreateFromTemplate(ctx context.Context, imageId, format str log.Errorln(err) return nil, err } - if err := newImg.CreateQcow2(int(size), false, cacheImagePath); err != nil { + if err := newImg.CreateQcow2(0, false, cacheImagePath); err != nil { log.Errorln(err) return nil, fmt.Errorf("Fail to create disk %s", d.Id) } diff --git a/pkg/hostman/storageman/storagebase.go b/pkg/hostman/storageman/storagebase.go index 6bd4522592..d96f601ac2 100644 --- a/pkg/hostman/storageman/storagebase.go +++ b/pkg/hostman/storageman/storagebase.go @@ -168,10 +168,13 @@ func (s *SBaseStorage) CreateDiskByDiskinfo(ctx context.Context, params interfac switch { case createParams.DiskInfo.Contains("snapshot"): + log.Infof("CreateDiskFromSnpashot %s", createParams) return s.CreateDiskFromSnpashot(ctx, disk, createParams) case createParams.DiskInfo.Contains("image_id"): + log.Infof("CreateDiskFromTemplate %s", createParams) return s.CreateDiskFromTemplate(ctx, disk, createParams) case createParams.DiskInfo.Contains("size"): + log.Infof("CreateRawDisk %s", createParams) return s.CreateRawDisk(ctx, disk, createParams) default: return nil, fmt.Errorf("Not fount") diff --git a/pkg/hostman/storageman/storagehelper.go b/pkg/hostman/storageman/storagehelper.go index 1bef35d379..7ebc464c01 100644 --- a/pkg/hostman/storageman/storagehelper.go +++ b/pkg/hostman/storageman/storagehelper.go @@ -1,6 +1,10 @@ package storageman -import "yunion.io/x/jsonutils" +import ( + "fmt" + + "yunion.io/x/jsonutils" +) type SDiskCreateByDiskinfo struct { DiskId string @@ -10,6 +14,10 @@ type SDiskCreateByDiskinfo struct { Storage IStorage } +func (i *SDiskCreateByDiskinfo) String() string { + return fmt.Sprintf("disk_id: %s, disk_info: %s", i.DiskId, i.DiskInfo) +} + type SDiskReset struct { SnapshotId string OutOfChain bool diff --git a/pkg/util/fileutils2/fileutils.go b/pkg/util/fileutils2/fileutils.go index 841e6d86eb..04bdd938af 100644 --- a/pkg/util/fileutils2/fileutils.go +++ b/pkg/util/fileutils2/fileutils.go @@ -12,6 +12,7 @@ import ( "regexp" "strconv" "strings" + "syscall" "golang.org/x/sys/unix" @@ -446,7 +447,14 @@ func ResizeDiskFs(diskPath string, sizeMb int) error { log.Infof("gdisk: %s %s", stdoutPut, stderrOutPut) if err = proc.Wait(); err != nil { log.Errorln(err) - return err + if exiterr, ok := err.(*exec.ExitError); ok { + ws := exiterr.Sys().(syscall.WaitStatus) + if ws.ExitStatus() != 1 { + return err + } + } else { + return err + } } } if len(parts) > 0 && (label == "gpt" || diff --git a/pkg/util/qemuimg/qemuimg.go b/pkg/util/qemuimg/qemuimg.go index ccfdb30c0a..3623c1e066 100644 --- a/pkg/util/qemuimg/qemuimg.go +++ b/pkg/util/qemuimg/qemuimg.go @@ -366,6 +366,7 @@ func (img *SQemuImage) create(sizeMB int, format TImageFormat, options []string) if sizeMB > 0 { args = append(args, fmt.Sprintf("%dM", sizeMB)) } + log.Debugf("%s %s", qemutils.GetQemuImg(), args) cmd := exec.Command(qemutils.GetQemuImg(), args...) err := cmd.Run() if err != nil { diff --git a/pkg/util/stringutils2/stringutils.go b/pkg/util/stringutils2/stringutils.go index db4a3f3384..6d75e2b8b3 100644 --- a/pkg/util/stringutils2/stringutils.go +++ b/pkg/util/stringutils2/stringutils.go @@ -1,10 +1,18 @@ package stringutils2 import ( + "crypto/md5" + "encoding/hex" "fmt" "strings" ) +func GetMD5Hash(text string) string { + hasher := md5.New() + hasher.Write([]byte(text)) + return hex.EncodeToString(hasher.Sum(nil)) +} + func EscapeString(str string, pairs [][]string) string { if len(pairs) == 0 { pairs = [][]string{ diff --git a/pkg/util/winutils/winutils.go b/pkg/util/winutils/winutils.go index 0e5d306426..b6da6b74e2 100644 --- a/pkg/util/winutils/winutils.go +++ b/pkg/util/winutils/winutils.go @@ -232,7 +232,7 @@ func (w *SWinRegTool) showRegistry(spath string, keySeg []string, verb string) ( if err := proc.Start(); err != nil { return nil, err } - keypath := strings.Join(keySeg, `\\`) + keypath := strings.Join(keySeg, "\\") io.WriteString(stdin, fmt.Sprintf("%s %s\n", verb, keypath)) io.WriteString(stdin, "q\n") stdoutPut, err := ioutil.ReadAll(outb) @@ -257,7 +257,7 @@ func (w *SWinRegTool) showRegistry(spath string, keySeg []string, verb string) ( } func (w *SWinRegTool) getRegistry(spath string, keySeg []string) string { - keyPath := strings.Join(keySeg, `\\`) + keyPath := strings.Join(keySeg, "\\") lines, err := w.showRegistry(spath, keySeg, "cat") if err != nil { log.Errorln(err) @@ -371,14 +371,14 @@ func (w *SWinRegTool) cmdRegistry(spath string, ops []string, retcode int) bool } func (w *SWinRegTool) setRegistry(spath string, keySeg []string, value string) bool { - keyPath := strings.Join(keySeg, `\\`) + keyPath := strings.Join(keySeg, "\\") return w.cmdRegistry(spath, []string{fmt.Sprintf("ed %s", keyPath), value}, 0) } func (w *SWinRegTool) mkdir(spath string, keySeg []string) bool { return w.cmdRegistry(spath, []string{ - fmt.Sprintf("cd %s", strings.Join(keySeg[:len(keySeg)-1], `\\`)), + fmt.Sprintf("cd %s", strings.Join(keySeg[:len(keySeg)-1], "\\")), fmt.Sprintf("nk %s", keySeg[len(keySeg)-1]), }, 2) } @@ -444,7 +444,7 @@ func (w *SWinRegTool) newValue(spath string, keySeg []string, regtype, val strin } cmds := []string{ - fmt.Sprintf("cd %s", strings.Join(keySeg[:len(keySeg)-1], `\\`)), + fmt.Sprintf("cd %s", strings.Join(keySeg[:len(keySeg)-1], "\\")), fmt.Sprintf("nv %x %s", idx, keySeg[len(keySeg)-1]), fmt.Sprintf("ed %s", keySeg[len(keySeg)-1]), }