Skip to content

Commit

Permalink
[shim] Unmount volumes when run exits (#1937)
Browse files Browse the repository at this point in the history
Fixes: #1936
  • Loading branch information
un-def authored Nov 1, 2024
1 parent 39ead95 commit b8674ca
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 2 deletions.
39 changes: 37 additions & 2 deletions runner/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,5 +74,40 @@ The `local` backend will submit the run to the locally started shim and runner.
...
Shown 3 of 4041 offers, $56.6266 max
Continue? [y/n]:
```
Continue? [y/n]:
```

## Dependencies (WIP)

These are nonexhaustive lists of external dependencies (executables, libraries) of the `dstack-*` binaries.

**TODO**: inspect codebase, add missing dependencies.

### `dstack-shim`

#### Libraries

* libc
* ...

#### Executables

* `mount`
* `umount`
* `mountpoint`
* `lsblk`
* `mkfs.ext4`
* ...

Debian/Ubuntu packages: `mount` (`mount`, `umount`), `util-linux` (`mountpoint`, `lsblk`), `e2fsprogs` (`mkfs.ext4`)

### `dstack-runner`

#### Libraries

* libc
* ...

#### Executables

* ...
31 changes: 31 additions & 0 deletions runner/internal/shim/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,10 @@ func (d *DockerRunner) Run(ctx context.Context, cfg TaskConfig) error {
}

log.Println("Preparing volumes")
// defer unmountVolumes() before calling prepareVolumes(), as the latter
// may fail when some volumes are already mounted; if the volume is not mounted,
// unmountVolumes() simply skips it
defer func() { _ = unmountVolumes(cfg) }()
err = prepareVolumes(cfg)
if err != nil {
d.state = Pending
Expand Down Expand Up @@ -274,6 +278,33 @@ func prepareVolumes(taskConfig TaskConfig) error {
return nil
}

func unmountVolumes(taskConfig TaskConfig) error {
if len(taskConfig.Volumes) == 0 {
return nil
}
log.Println("Unmounting volumes...")
var failed []string
for _, volume := range taskConfig.Volumes {
mountPoint := getVolumeMountPoint(volume.Name)
cmd := exec.Command("mountpoint", mountPoint)
if output, err := cmd.CombinedOutput(); err != nil {
log.Printf("Skipping %s: %s", mountPoint, output)
continue
}
cmd = exec.Command("umount", "-qf", mountPoint)
if output, err := cmd.CombinedOutput(); err != nil {
log.Printf("Failed to unmount %s: %s", mountPoint, output)
failed = append(failed, mountPoint)
} else {
log.Printf("Unmounted: %s\n", mountPoint)
}
}
if len(failed) > 0 {
return fmt.Errorf("Failed to unmount volume(s): %v", failed)
}
return nil
}

func formatAndMountVolume(volume VolumeInfo) error {
backend, err := getBackend(volume.Backend)
if err != nil {
Expand Down

0 comments on commit b8674ca

Please sign in to comment.