Skip to content

Commit

Permalink
Add device unmount logic for macOS/Linux
Browse files Browse the repository at this point in the history
  • Loading branch information
retrixe committed Nov 5, 2024
1 parent 5b157a2 commit df91191
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 3 deletions.
8 changes: 6 additions & 2 deletions app/devices_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,13 +62,17 @@ func GetDevices() ([]Device, error) {

// UnmountDevice unmounts a block device's partitons before flashing to it.
func UnmountDevice(device string) error {
// Check if device is mounted.
// Check if device exists.
stat, err := os.Stat(device)
if err != nil {
return err
} else if stat.Mode().Type()&fs.ModeDevice == 0 {
return ErrNotBlockDevice
}
// FIXME: Discover device partitions and recursively unmount them.
// Unmount all partitions of disk using `diskutil`.
_, err = exec.Command("diskutil", "unmountDisk", device).Output()
if err != nil {
return err
}
return nil
}
16 changes: 15 additions & 1 deletion app/devices_unix.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,20 @@ func UnmountDevice(device string) error {
} else if stat.Mode().Type()&fs.ModeDevice == 0 {
return ErrNotBlockDevice
}
// FIXME: Discover device partitions and recursively unmount them.
// Discover mounted device partitions.
mounts, err := exec.Command("mount").Output()
if err != nil {
return err
}
// Unmount device partitions.
for _, mount := range strings.Split(string(mounts), "\n") {
if strings.HasPrefix(mount, device) {
partition := strings.Fields(mount)[0]
err = exec.Command("umount", partition).Run()
if err != nil {
return err
}
}
}
return nil
}

0 comments on commit df91191

Please sign in to comment.