@@ -16,6 +16,7 @@ import (
1616
1717 "github.com/dstackai/dstack/runner/consts"
1818 "github.com/dstackai/dstack/runner/internal/executor"
19+ linuxuser "github.com/dstackai/dstack/runner/internal/linux/user"
1920 "github.com/dstackai/dstack/runner/internal/log"
2021 "github.com/dstackai/dstack/runner/internal/runner/api"
2122 "github.com/dstackai/dstack/runner/internal/ssh"
@@ -30,7 +31,6 @@ func main() {
3031
3132func mainInner () int {
3233 var tempDir string
33- var homeDir string
3434 var httpPort int
3535 var sshPort int
3636 var sshAuthorizedKeys []string
@@ -61,13 +61,6 @@ func mainInner() int {
6161 Destination : & tempDir ,
6262 TakesFile : true ,
6363 },
64- & cli.StringFlag {
65- Name : "home-dir" ,
66- Usage : "HomeDir directory for credentials and $HOME" ,
67- Value : consts .RunnerHomeDir ,
68- Destination : & homeDir ,
69- TakesFile : true ,
70- },
7164 & cli.IntFlag {
7265 Name : "http-port" ,
7366 Usage : "Set a http port" ,
@@ -87,7 +80,7 @@ func mainInner() int {
8780 },
8881 },
8982 Action : func (ctx context.Context , cmd * cli.Command ) error {
90- return start (ctx , tempDir , homeDir , httpPort , sshPort , sshAuthorizedKeys , logLevel , Version )
83+ return start (ctx , tempDir , httpPort , sshPort , sshAuthorizedKeys , logLevel , Version )
9184 },
9285 },
9386 },
@@ -104,7 +97,7 @@ func mainInner() int {
10497 return 0
10598}
10699
107- func start (ctx context.Context , tempDir string , homeDir string , httpPort int , sshPort int , sshAuthorizedKeys []string , logLevel int , version string ) error {
100+ func start (ctx context.Context , tempDir string , httpPort int , sshPort int , sshAuthorizedKeys []string , logLevel int , version string ) error {
108101 if err := os .MkdirAll (tempDir , 0o755 ); err != nil {
109102 return fmt .Errorf ("create temp directory: %w" , err )
110103 }
@@ -114,15 +107,39 @@ func start(ctx context.Context, tempDir string, homeDir string, httpPort int, ss
114107 return fmt .Errorf ("create default log file: %w" , err )
115108 }
116109 defer func () {
117- closeErr := defaultLogFile .Close ()
118- if closeErr != nil {
119- log .Error (ctx , "Failed to close default log file" , "err" , closeErr )
110+ if err := defaultLogFile .Close (); err != nil {
111+ log .Error (ctx , "Failed to close default log file" , "err" , err )
120112 }
121113 }()
122-
123114 log .DefaultEntry .Logger .SetOutput (io .MultiWriter (os .Stdout , defaultLogFile ))
124115 log .DefaultEntry .Logger .SetLevel (logrus .Level (logLevel ))
125116
117+ currentUser , err := linuxuser .FromCurrentProcess ()
118+ if err != nil {
119+ return fmt .Errorf ("get current process user: %w" , err )
120+ }
121+ if ! currentUser .IsRoot () {
122+ return fmt .Errorf ("must be root: %s" , currentUser )
123+ }
124+ if currentUser .HomeDir == "" {
125+ log .Warning (ctx , "Current user does not have home dir, using /root as a fallback" , "user" , currentUser )
126+ currentUser .HomeDir = "/root"
127+ }
128+ // Fix the current process HOME, just in case some internals require it (e.g., they use os.UserHomeDir() or
129+ // spawn a child process which uses that variable)
130+ envHome , envHomeIsSet := os .LookupEnv ("HOME" )
131+ if envHome != currentUser .HomeDir {
132+ if ! envHomeIsSet {
133+ log .Warning (ctx , "HOME is not set, setting the value" , "home" , currentUser .HomeDir )
134+ } else {
135+ log .Warning (ctx , "HOME is incorrect, fixing the value" , "current" , envHome , "home" , currentUser .HomeDir )
136+ }
137+ if err := os .Setenv ("HOME" , currentUser .HomeDir ); err != nil {
138+ return fmt .Errorf ("set HOME: %w" , err )
139+ }
140+ }
141+ log .Trace (ctx , "Running as" , "user" , currentUser )
142+
126143 // NB: The Mkdir/Chown/Chmod code below relies on the fact that RunnerDstackDir path is _not_ nested (/dstack).
127144 // Adjust it if the path is changed to, e.g., /opt/dstack
128145 const dstackDir = consts .RunnerDstackDir
@@ -163,7 +180,7 @@ func start(ctx context.Context, tempDir string, homeDir string, httpPort int, ss
163180 }
164181 }()
165182
166- ex , err := executor .NewRunExecutor (tempDir , homeDir , dstackDir , sshd )
183+ ex , err := executor .NewRunExecutor (tempDir , dstackDir , * currentUser , sshd )
167184 if err != nil {
168185 return fmt .Errorf ("create executor: %w" , err )
169186 }
0 commit comments