From 6e66a129d65a8d2cbc05f4e41820554fde8c7728 Mon Sep 17 00:00:00 2001 From: wanyaoqi Date: Mon, 16 Nov 2020 20:44:06 +0800 Subject: [PATCH] host-deployer: customize user cloudroot home dir --- pkg/baremetal/service/service.go | 2 +- pkg/esxi/service/esxi_agent_service.go | 2 +- pkg/hostman/guestfs/fsdriver/drivers.go | 10 ++++++---- pkg/hostman/guestfs/fsdriver/interface.go | 2 +- pkg/hostman/guestfs/fsdriver/linux.go | 7 ++++--- pkg/hostman/guestfs/localfs.go | 11 +++++++++-- pkg/hostman/guestfs/sshpart/sshpart.go | 5 ++++- pkg/hostman/hostdeployer/deployserver/deployserver.go | 2 +- pkg/hostman/hostdeployer/deployserver/options.go | 1 + 9 files changed, 28 insertions(+), 14 deletions(-) diff --git a/pkg/baremetal/service/service.go b/pkg/baremetal/service/service.go index 4e922b8736..57d5890e77 100644 --- a/pkg/baremetal/service/service.go +++ b/pkg/baremetal/service/service.go @@ -64,7 +64,7 @@ func (s *BaremetalService) StartService() { log.Infof("auth complete") }) - fsdriver.Init(nil) + fsdriver.Init(nil, "") app := app_common.InitApp(&o.Options.BaseOptions, false) common_options.StartOptionManager(&o.Options, o.Options.ConfigSyncPeriodSeconds, api.SERVICE_TYPE, api.SERVICE_VERSION, o.OnOptionsChange) diff --git a/pkg/esxi/service/esxi_agent_service.go b/pkg/esxi/service/esxi_agent_service.go index 9a600080db..fbddfa7b9d 100644 --- a/pkg/esxi/service/esxi_agent_service.go +++ b/pkg/esxi/service/esxi_agent_service.go @@ -69,7 +69,7 @@ func (s *SExsiAgentService) StartService() { log.Infof("auth complete") }) - fsdriver.Init(nil) + fsdriver.Init(nil, "") deployclient.Init(options.Options.DeployServerSocketPath) hostutils.InitWorkerManagerWithCount(options.Options.HostDelayTaskWorkerCount) diff --git a/pkg/hostman/guestfs/fsdriver/drivers.go b/pkg/hostman/guestfs/fsdriver/drivers.go index 4904226b8d..44d76eac56 100644 --- a/pkg/hostman/guestfs/fsdriver/drivers.go +++ b/pkg/hostman/guestfs/fsdriver/drivers.go @@ -25,16 +25,17 @@ import ( type newRootFsDriverFunc func(part IDiskPartition) IRootFsDriver var ( - privatePrefixes []string - rootfsDrivers = make([]newRootFsDriverFunc, 0) - hostCpuArch string + privatePrefixes []string + rootfsDrivers = make([]newRootFsDriverFunc, 0) + hostCpuArch string + cloudrootDirectory string ) func GetRootfsDrivers() []newRootFsDriverFunc { return rootfsDrivers } -func Init(initPrivatePrefixes []string) error { +func Init(initPrivatePrefixes []string, cloudrootDir string) error { if len(initPrivatePrefixes) > 0 { privatePrefixes = make([]string, len(initPrivatePrefixes)) copy(privatePrefixes, initPrivatePrefixes) @@ -55,5 +56,6 @@ func Init(initPrivatePrefixes []string) error { return errors.Wrap(err, "get cpu architecture") } hostCpuArch = strings.TrimSpace(string(cpuArch)) + cloudrootDirectory = cloudrootDir return nil } diff --git a/pkg/hostman/guestfs/fsdriver/interface.go b/pkg/hostman/guestfs/fsdriver/interface.go index 31b4709fd4..cef687ae98 100644 --- a/pkg/hostman/guestfs/fsdriver/interface.go +++ b/pkg/hostman/guestfs/fsdriver/interface.go @@ -34,7 +34,7 @@ type IDiskPartition interface { Exists(sPath string, caseInsensitive bool) bool Chown(sPath string, uid, gid int, caseInsensitive bool) error Chmod(sPath string, mode uint32, caseInsensitive bool) error - UserAdd(user string, caseInsensitive bool) error + UserAdd(user, homeDir string, caseInsensitive bool) error Stat(sPath string, caseInsensitive bool) os.FileInfo Symlink(src, dst string, caseInsensitive bool) error diff --git a/pkg/hostman/guestfs/fsdriver/linux.go b/pkg/hostman/guestfs/fsdriver/linux.go index 1fcd3b05c0..e11c94dcd0 100644 --- a/pkg/hostman/guestfs/fsdriver/linux.go +++ b/pkg/hostman/guestfs/fsdriver/linux.go @@ -94,7 +94,7 @@ func (l *sLinuxRootFs) DeployHosts(rootFs IDiskPartition, hostname, domain strin func (l *sLinuxRootFs) GetLoginAccount(rootFs IDiskPartition, sUser string, defaultRootUser bool, windowsDefaultAdminUser bool) (string, error) { if len(sUser) > 0 { - if err := rootFs.UserAdd(sUser, false); err != nil && !strings.Contains(err.Error(), "already exists") { + if err := rootFs.UserAdd(sUser, "", false); err != nil && !strings.Contains(err.Error(), "already exists") { return "", fmt.Errorf("UserAdd %s: %v", sUser, err) } if err := l.EnableUserSudo(rootFs, sUser); err != nil { @@ -154,10 +154,11 @@ func (l *sLinuxRootFs) DeployYunionroot(rootFs IDiskPartition, pubkeys *deployap l.DisableCloudinit(rootFs) } var yunionroot = YUNIONROOT_USER - if err := rootFs.UserAdd(yunionroot, false); err != nil && !strings.Contains(err.Error(), "already exists") { + rootdir := path.Join(cloudrootDirectory, yunionroot) + if err := rootFs.UserAdd(yunionroot, cloudrootDirectory, false); err != nil && !strings.Contains(err.Error(), "already exists") { log.Errorf("UserAdd %s: %v", yunionroot, err) } - err := DeployAuthorizedKeys(rootFs, path.Join("/home", yunionroot), pubkeys, true) + err := DeployAuthorizedKeys(rootFs, rootdir, pubkeys, true) if err != nil { return fmt.Errorf("DeployAuthorizedKeys: %v", err) } diff --git a/pkg/hostman/guestfs/localfs.go b/pkg/hostman/guestfs/localfs.go index 0ad552d1a3..10f5930031 100644 --- a/pkg/hostman/guestfs/localfs.go +++ b/pkg/hostman/guestfs/localfs.go @@ -222,8 +222,15 @@ func (f *SLocalGuestFS) Chmod(sPath string, mode uint32, caseInsensitive bool) e return nil } -func (f *SLocalGuestFS) UserAdd(user string, caseInsensitive bool) error { - output, err := procutils.NewCommand("chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user).Output() +func (f *SLocalGuestFS) UserAdd(user, homeDir string, caseInsensitive bool) error { + if err := f.Mkdir(homeDir, 0755, false); err != nil { + return errors.Wrap(err, "Mkdir") + } + cmd := []string{"chroot", f.mountPath, "useradd", "-m", "-s", "/bin/bash", user} + if len(homeDir) > 0 { + cmd = append(cmd, "-d", path.Join(homeDir, user)) + } + output, err := procutils.NewCommand(cmd[0], cmd[1:]...).Output() if err != nil { log.Errorf("Useradd fail: %s, %s", err, output) return fmt.Errorf("%s", output) diff --git a/pkg/hostman/guestfs/sshpart/sshpart.go b/pkg/hostman/guestfs/sshpart/sshpart.go index 369e58acd0..673a2993dc 100644 --- a/pkg/hostman/guestfs/sshpart/sshpart.go +++ b/pkg/hostman/guestfs/sshpart/sshpart.go @@ -390,8 +390,11 @@ func (p *SSHPartition) Remove(sPath string, caseInsensitive bool) { } } -func (p *SSHPartition) UserAdd(user string, caseInsensitive bool) error { +func (p *SSHPartition) UserAdd(user, homeDir string, caseInsensitive bool) error { cmd := fmt.Sprintf("/usr/sbin/chroot %s /usr/sbin/useradd -m -s /bin/bash %s", p.mountPath, user) + if len(homeDir) > 0 { + cmd += fmt.Sprintf(" -d %s", path.Join(homeDir, user)) + } _, err := p.term.Run(cmd) return err } diff --git a/pkg/hostman/hostdeployer/deployserver/deployserver.go b/pkg/hostman/hostdeployer/deployserver/deployserver.go index 18fc164a16..e9c48ed73f 100644 --- a/pkg/hostman/hostdeployer/deployserver/deployserver.go +++ b/pkg/hostman/hostdeployer/deployserver/deployserver.go @@ -382,7 +382,7 @@ func (s *SDeployService) InitService() { if err := s.PrepareEnv(); err != nil { log.Fatalln(err) } - if err := fsdriver.Init(DeployOption.PrivatePrefixes); err != nil { + if err := fsdriver.Init(DeployOption.PrivatePrefixes, DeployOption.CloudrootDir); err != nil { log.Fatalln(err) } s.O = &DeployOption.BaseOptions diff --git a/pkg/hostman/hostdeployer/deployserver/options.go b/pkg/hostman/hostdeployer/deployserver/options.go index 595fb7b14f..0a909a7079 100644 --- a/pkg/hostman/hostdeployer/deployserver/options.go +++ b/pkg/hostman/hostdeployer/deployserver/options.go @@ -24,6 +24,7 @@ type SDeployOptions struct { ChntpwPath string `help:"path to chntpw tool" default:"/usr/local/bin/chntpw.static"` EnableRemoteExecutor bool `help:"Enable remote executor" default:"false"` ExecSocketPath string `help:"Exec socket paht" default:"/var/run/exec.sock"` + CloudrootDir string `help:"User cloudroot home dir" default:"/opt"` } var DeployOption SDeployOptions