Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 4 additions & 3 deletions runner/internal/shim/backends/aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package backends

import (
"bytes"
"context"
"fmt"
"os"
"os/exec"
Expand All @@ -23,10 +24,10 @@ func NewAWSBackend() *AWSBackend {
// * Red Hat and CentOS: may increment trailing letters in some versions – not supported.
// * Other legacy systems: /dev/sda => /dev/sda.
// More: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/device_naming.html
func (e *AWSBackend) GetRealDeviceName(volumeID, deviceName string) (string, error) {
func (e *AWSBackend) GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error) {
// Run the lsblk command to get block device information
// On AWS, SERIAL contains volume id.
cmd := exec.Command("lsblk", "-o", "NAME,SERIAL")
cmd := exec.CommandContext(ctx, "lsblk", "-o", "NAME,SERIAL")
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
Expand Down Expand Up @@ -67,7 +68,7 @@ func (e *AWSBackend) GetRealDeviceName(volumeID, deviceName string) (string, err
}

// Run lsblk again to check for partitions on the base device
cmd = exec.Command("lsblk", "-ln", "-o", "NAME", baseDevice)
cmd = exec.CommandContext(ctx, "lsblk", "-ln", "-o", "NAME", baseDevice)
out.Reset()
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
Expand Down
4 changes: 3 additions & 1 deletion runner/internal/shim/backends/base.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package backends

import "context"

type Backend interface {
// GetRealDeviceName returns the real device name for the given volume ID and virtual device name.
GetRealDeviceName(volumeID, deviceName string) (string, error)
GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error)
}
3 changes: 2 additions & 1 deletion runner/internal/shim/backends/gcp.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package backends

import (
"context"
"fmt"
"os"
"path/filepath"
Expand All @@ -15,7 +16,7 @@ func NewGCPBackend() *GCPBackend {
}

// GetRealDeviceName resolves device names according to https://cloud.google.com/compute/docs/disks/disk-symlinks
func (e *GCPBackend) GetRealDeviceName(volumeID, deviceName string) (string, error) {
func (e *GCPBackend) GetRealDeviceName(ctx context.Context, volumeID, deviceName string) (string, error) {
// Try resolving first partition or external volumes
realDeviceName, err := os.Readlink(fmt.Sprintf("/dev/disk/by-id/google-%s-part1", deviceName))
if err != nil {
Expand Down
12 changes: 6 additions & 6 deletions runner/internal/shim/docker.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,12 +535,12 @@ func unmountVolumes(ctx context.Context, taskConfig TaskConfig) error {
var failed []string
for _, volume := range taskConfig.Volumes {
mountPoint := getVolumeMountPoint(volume.Name)
cmd := exec.Command("mountpoint", mountPoint)
cmd := exec.CommandContext(ctx, "mountpoint", mountPoint)
if output, err := cmd.CombinedOutput(); err != nil {
log.Info(ctx, "skipping", "mountpoint", mountPoint, "output", output)
continue
}
cmd = exec.Command("umount", "-qf", mountPoint)
cmd = exec.CommandContext(context.TODO(), "umount", "-qf", mountPoint)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So what's the benefit of CommandContext(context.TODO()?

if output, err := cmd.CombinedOutput(); err != nil {
log.Error(ctx, "failed to unmount", "mountpoint", mountPoint, "output", output)
failed = append(failed, mountPoint)
Expand All @@ -559,7 +559,7 @@ func formatAndMountVolume(ctx context.Context, volume VolumeInfo) error {
if err != nil {
return tracerr.Wrap(err)
}
deviceName, err := backend.GetRealDeviceName(volume.VolumeId, volume.DeviceName)
deviceName, err := backend.GetRealDeviceName(ctx, volume.VolumeId, volume.DeviceName)
if err != nil {
return tracerr.Wrap(err)
}
Expand Down Expand Up @@ -618,7 +618,7 @@ func prepareInstanceMountPoints(taskConfig TaskConfig) error {
// Returns true if the file system is created.
func initFileSystem(ctx context.Context, deviceName string, errorIfNotExists bool) (bool, error) {
// Run the lsblk command to get filesystem type
cmd := exec.Command("lsblk", "-no", "FSTYPE", deviceName)
cmd := exec.CommandContext(ctx, "lsblk", "-no", "FSTYPE", deviceName)
var out bytes.Buffer
cmd.Stdout = &out
if err := cmd.Run(); err != nil {
Expand All @@ -636,7 +636,7 @@ func initFileSystem(ctx context.Context, deviceName string, errorIfNotExists boo
}

log.Debug(ctx, "formatting disk with ext4 filesystem...", "device", deviceName)
cmd = exec.Command("mkfs.ext4", "-F", deviceName)
cmd = exec.CommandContext(ctx, "mkfs.ext4", "-F", deviceName)
if output, err := cmd.CombinedOutput(); err != nil {
return false, fmt.Errorf("failed to format disk: %w, output: %s", err, string(output))
}
Expand All @@ -655,7 +655,7 @@ func mountDisk(ctx context.Context, deviceName, mountPoint string, fsRootPerms o

// Mount the disk to the mount point
log.Debug(ctx, "mounting disk...", "device", deviceName, "mountpoint", mountPoint)
cmd := exec.Command("mount", deviceName, mountPoint)
cmd := exec.CommandContext(ctx, "mount", deviceName, mountPoint)
if output, err := cmd.CombinedOutput(); err != nil {
return fmt.Errorf("failed to mount disk: %w, output: %s", err, string(output))
}
Expand Down