Skip to content

Commit c92c2b4

Browse files
authored
Merge pull request #471 from Dstack-TEE/fix/socket-path-for-docker-mount
fix: Move `/var/run/dstack.sock` to `/var/run/dstack/dstack.sock` directory
2 parents aca96b6 + d685e07 commit c92c2b4

13 files changed

Lines changed: 159 additions & 16 deletions

File tree

basefiles/dstack-socket.service

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2025 Phala Network <dstack@phala.network>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Proxy service that forwards connections from /var/run/dstack.sock to /var/run/dstack/dstack.sock.
6+
# Used for backward compatibility with containers that mount the socket file directly.
7+
8+
[Unit]
9+
Description=dstack socket proxy for backward compatibility
10+
Requires=dstack-socket.socket
11+
After=dstack-guest-agent.service
12+
13+
[Service]
14+
ExecStart=/usr/lib/systemd/systemd-socket-proxyd /var/run/dstack/dstack.sock
15+
Type=notify

basefiles/dstack-socket.socket

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: 2025 Phala Network <dstack@phala.network>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Backward compatibility socket for containers that mount /var/run/dstack.sock directly.
6+
# The socket is owned by systemd, so it survives service restarts without inode changes.
7+
8+
[Unit]
9+
Description=dstack backward compatibility socket (dstack.sock)
10+
11+
[Socket]
12+
ListenStream=/var/run/dstack.sock
13+
SocketMode=0777
14+
15+
[Install]
16+
WantedBy=sockets.target

basefiles/tappd-socket.service

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# SPDX-FileCopyrightText: 2025 Phala Network <dstack@phala.network>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Proxy service that forwards connections from /var/run/tappd.sock to /var/run/dstack/tappd.sock.
6+
# Used for backward compatibility with containers that mount the socket file directly.
7+
8+
[Unit]
9+
Description=tappd socket proxy for backward compatibility
10+
Requires=tappd-socket.socket
11+
After=dstack-guest-agent.service
12+
13+
[Service]
14+
ExecStart=/usr/lib/systemd/systemd-socket-proxyd /var/run/dstack/tappd.sock
15+
Type=notify

basefiles/tappd-socket.socket

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# SPDX-FileCopyrightText: 2025 Phala Network <dstack@phala.network>
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
# Backward compatibility socket for containers that mount /var/run/tappd.sock directly.
6+
# The socket is owned by systemd, so it survives service restarts without inode changes.
7+
8+
[Unit]
9+
Description=dstack backward compatibility socket (tappd.sock)
10+
11+
[Socket]
12+
ListenStream=/var/run/tappd.sock
13+
SocketMode=0777
14+
15+
[Install]
16+
WantedBy=sockets.target

dstack-types/src/lib.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -263,7 +263,14 @@ pub fn dstack_agent_address() -> String {
263263
if let Ok(address) = std::env::var("DSTACK_AGENT_ADDRESS") {
264264
return address;
265265
}
266-
"unix:/var/run/dstack.sock".into()
266+
// Try new path first, fall back to old path for backward compatibility
267+
const SOCKET_PATHS: &[&str] = &["/var/run/dstack/dstack.sock", "/var/run/dstack.sock"];
268+
for path in SOCKET_PATHS {
269+
if std::path::Path::new(path).exists() {
270+
return format!("unix:{}", path);
271+
}
272+
}
273+
format!("unix:{}", SOCKET_PATHS[0])
267274
}
268275

269276
/// Hardware/Cloud Platform

dstack-util/src/system_setup.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1440,6 +1440,7 @@ impl Stage1<'_> {
14401440
async fn setup(&self) -> Result<()> {
14411441
let _envs = self.unseal_env_vars()?;
14421442
self.link_files()?;
1443+
self.setup_socket_dir()?;
14431444
self.setup_guest_agent_config()?;
14441445
self.vmm
14451446
.notify_q("boot.progress", "setting up dstack-gateway")
@@ -1464,6 +1465,13 @@ impl Stage1<'_> {
14641465
Ok(())
14651466
}
14661467

1468+
/// Setup socket directory for dstack-guest-agent.
1469+
fn setup_socket_dir(&self) -> Result<()> {
1470+
info!("Setting up socket directory");
1471+
fs::create_dir_all("/var/run/dstack").context("Failed to create socket directory")?;
1472+
Ok(())
1473+
}
1474+
14671475
fn setup_guest_agent_config(&self) -> Result<()> {
14681476
info!("Setting up guest agent config");
14691477
let data_disks = ["/".as_ref() as &Path, self.args.mount_point.as_ref()];

guest-agent/dstack.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ enabled = false
2121
attestation_file = "attestation.bin"
2222

2323
[internal-v0]
24-
address = "unix:/var/run/tappd.sock"
24+
address = "unix:/var/run/dstack/tappd.sock"
2525
reuse = true
2626

2727
[internal]
28-
address = "unix:/var/run/dstack.sock"
28+
address = "unix:/var/run/dstack/dstack.sock"
2929
reuse = true
3030

3131
[external]

sdk/go/dstack/client.go

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -229,8 +229,9 @@ func NewDstackClient(opts ...DstackClientOption) *DstackClient {
229229

230230
// Returns the appropriate endpoint based on environment and input. If the
231231
// endpoint is empty, it will use the simulator endpoint if it is set in the
232-
// environment through DSTACK_SIMULATOR_ENDPOINT. Otherwise, it will use the
233-
// default endpoint at /var/run/dstack.sock.
232+
// environment through DSTACK_SIMULATOR_ENDPOINT. Otherwise, it will try
233+
// /var/run/dstack/dstack.sock first, falling back to /var/run/dstack.sock
234+
// for backward compatibility.
234235
func (c *DstackClient) getEndpoint() string {
235236
if c.endpoint != "" {
236237
return c.endpoint
@@ -239,7 +240,18 @@ func (c *DstackClient) getEndpoint() string {
239240
c.logger.Info("using simulator endpoint", "endpoint", simEndpoint)
240241
return simEndpoint
241242
}
242-
return "/var/run/dstack.sock"
243+
// Try new path first, fall back to old path for backward compatibility
244+
socketPaths := []string{
245+
"/var/run/dstack/dstack.sock",
246+
"/var/run/dstack.sock",
247+
}
248+
for _, path := range socketPaths {
249+
if _, err := os.Stat(path); err == nil {
250+
return path
251+
}
252+
}
253+
// Default to new path even if not exists (will fail with clear error)
254+
return socketPaths[0]
243255
}
244256

245257
// Sends an RPC request to the dstack service.

sdk/go/tappd/client.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,8 @@ func WithLogger(logger *slog.Logger) TappdClientOption {
185185
// Creates a new TappdClient instance based on the provided endpoint.
186186
// If the endpoint is empty, it will use the simulator endpoint if it is
187187
// set in the environment through DSTACK_SIMULATOR_ENDPOINT. Otherwise, it
188-
// will use the default endpoint at /var/run/tappd.sock.
188+
// will try /var/run/dstack/tappd.sock first, falling back to /var/run/tappd.sock
189+
// for backward compatibility.
189190
func NewTappdClient(opts ...TappdClientOption) *TappdClient {
190191
client := &TappdClient{
191192
endpoint: "",
@@ -218,8 +219,9 @@ func NewTappdClient(opts ...TappdClientOption) *TappdClient {
218219

219220
// Returns the appropriate endpoint based on environment and input. If the
220221
// endpoint is empty, it will use the simulator endpoint if it is set in the
221-
// environment through DSTACK_SIMULATOR_ENDPOINT. Otherwise, it will use the
222-
// default endpoint at /var/run/tappd.sock.
222+
// environment through DSTACK_SIMULATOR_ENDPOINT. Otherwise, it will try
223+
// /var/run/dstack/tappd.sock first, falling back to /var/run/tappd.sock
224+
// for backward compatibility.
223225
func (c *TappdClient) getEndpoint() string {
224226
if c.endpoint != "" {
225227
return c.endpoint
@@ -228,7 +230,17 @@ func (c *TappdClient) getEndpoint() string {
228230
c.logger.Info("using simulator endpoint", "endpoint", simEndpoint)
229231
return simEndpoint
230232
}
231-
return "/var/run/tappd.sock"
233+
// Try new path first, fall back to old path for backward compatibility
234+
socketPaths := []string{
235+
"/var/run/dstack/tappd.sock",
236+
"/var/run/tappd.sock",
237+
}
238+
for _, path := range socketPaths {
239+
if _, err := os.Stat(path); err == nil {
240+
return path
241+
}
242+
}
243+
return socketPaths[0]
232244
}
233245

234246
// Sends an RPC request to the Tappd service.

sdk/js/src/index.ts

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,12 @@ export class DstackClient<T extends TcbInfo = TcbInfoV05x> {
179179
console.warn(`Using simulator endpoint: ${process.env.DSTACK_SIMULATOR_ENDPOINT}`)
180180
endpoint = process.env.DSTACK_SIMULATOR_ENDPOINT
181181
} else {
182-
endpoint = '/var/run/dstack.sock'
182+
// Try new path first, fall back to old path for backward compatibility
183+
const socketPaths = [
184+
'/var/run/dstack/dstack.sock',
185+
'/var/run/dstack.sock',
186+
]
187+
endpoint = socketPaths.find(p => fs.existsSync(p)) ?? socketPaths[0]
183188
}
184189
}
185190
if (endpoint.startsWith('/') && !fs.existsSync(endpoint)) {
@@ -402,7 +407,12 @@ export class TappdClient extends DstackClient<TcbInfoV03x> {
402407
console.warn(`Using tappd endpoint: ${process.env.TAPPD_SIMULATOR_ENDPOINT}`)
403408
endpoint = process.env.TAPPD_SIMULATOR_ENDPOINT
404409
} else {
405-
endpoint = '/var/run/tappd.sock'
410+
// Try new path first, fall back to old path for backward compatibility
411+
const socketPaths = [
412+
'/var/run/dstack/tappd.sock',
413+
'/var/run/tappd.sock',
414+
]
415+
endpoint = socketPaths.find(p => fs.existsSync(p)) ?? socketPaths[0]
406416
}
407417
}
408418
console.warn('TappdClient is deprecated, please use DstackClient instead')

0 commit comments

Comments
 (0)