GetDevices now returns a complete description w/ disk partitions

pull/13/head
Paulo Truta 2021-06-06 17:10:08 +00:00
parent fd002987fa
commit 1ee82baf8a
1 changed files with 102 additions and 17 deletions

View File

@ -2,6 +2,7 @@ package storage
import ( import (
"bufio" "bufio"
"fmt"
"strings" "strings"
"github.com/edgebox-iot/edgeboxctl/internal/utils" "github.com/edgebox-iot/edgeboxctl/internal/utils"
@ -17,6 +18,7 @@ type Device struct {
MIN string `json:"min"` MIN string `json:"min"`
RM string `json:"rm"` RM string `json:"rm"`
RO string `json:"ro"` RO string `json:"ro"`
Partitions []Partition `json:"partitions"`
Status DeviceStatus `json:"status"` Status DeviceStatus `json:"status"`
} }
@ -26,26 +28,82 @@ type DeviceStatus struct {
Description string `json:"description"` Description string `json:"description"`
} }
// MaybeDevice : Boolean flag for validation of device existance
type MaybeDevice struct {
Device Device `json:"device"`
Valid bool `json:"valid"`
}
// Partition : Struct representing a partition / filesystem (Empty Mountpoint means it is not mounted)
type Partition struct {
ID string `json:"id"`
Size string `json:"size"`
MAJ string `json:"maj"`
MIN string `json:"min"`
RM string `json:"rm"`
RO string `json:"ro"`
Filesystem string `json:"filesystem"`
Mountpoint string `json:"mountpoint"`
}
const mainDiskID = "mmcblk0"
func GetDevice() MaybeDevice {
result := MaybeDevice{
Device: Device{},
Valid: false,
}
return result
}
// GetDevices : Returns a list of all available sotrage devices in structs filled with information // GetDevices : Returns a list of all available sotrage devices in structs filled with information
func GetDevices() []Device { func GetDevices() []Device {
var devices []Device var devices []Device
cmdArgs := []string{"--raw", "--nodeps", "--noheadings"} cmdArgs := []string{"--raw", "--noheadings"}
cmdOutput := utils.Exec("lsblk", cmdArgs) cmdOutput := utils.Exec("lsblk", cmdArgs)
cmdOutputReader := strings.NewReader(cmdOutput) cmdOutputReader := strings.NewReader(cmdOutput)
scanner := bufio.NewScanner(cmdOutputReader) scanner := bufio.NewScanner(cmdOutputReader)
scanner.Split(bufio.ScanLines) scanner.Split(bufio.ScanLines)
mainDevice := true var currentDevice Device
var currentPartitions []Partition
firstDevice := true
for scanner.Scan() { for scanner.Scan() {
// 1 Device is represented here. Extract words in order for filling a Device struct // 1 Device is represented here. Extract words in order for filling a Device struct
// Example deviceRawInfo: "mmcblk0 179:0 0 29.7G 0 disk" // Example deviceRawInfo: "mmcblk0 179:0 0 29.7G 0 disk"
deviceRawInfo := strings.Fields(scanner.Text()) deviceRawInfo := strings.Fields(scanner.Text())
majMin := strings.SplitN(deviceRawInfo[1], ":", 2) majMin := strings.SplitN(deviceRawInfo[1], ":", 2)
isDevice := true
if deviceRawInfo[5] == "part" {
isDevice = false
}
if isDevice {
// Clean up on the last device being prepared. Append all partitions found and delete the currentPartitions list afterwards.
// The first device found should not run the cleanup lines below
fmt.Println("Processing Device")
if !firstDevice {
fmt.Println("Appending finalized device info to list")
currentDevice.Partitions = currentPartitions
currentPartitions = []Partition{}
devices = append(devices, currentDevice)
} else {
fmt.Println("First device, not appending to list")
firstDevice = false
}
mainDevice := false
device := Device{ device := Device{
ID: deviceRawInfo[0], ID: deviceRawInfo[0],
Name: deviceRawInfo[0], Name: deviceRawInfo[0],
@ -53,18 +111,45 @@ func GetDevices() []Device {
MainDevice: mainDevice, MainDevice: mainDevice,
MAJ: majMin[0], MAJ: majMin[0],
MIN: majMin[1], MIN: majMin[1],
RO: deviceRawInfo[4],
RM: deviceRawInfo[2], RM: deviceRawInfo[2],
RO: deviceRawInfo[4],
Status: DeviceStatus{ID: 1, Description: "healthy"}, Status: DeviceStatus{ID: 1, Description: "healthy"},
} }
// Once the first device is found, set to false. if device.ID == mainDiskID {
if mainDevice == true { device.MainDevice = true
mainDevice = false
} }
devices = append(devices, device) currentDevice = device
} else {
fmt.Println("Processing Partition")
mountpoint := ""
if len(deviceRawInfo) > 7 {
mountpoint = deviceRawInfo[7]
} }
// It is a partition, part of the last device read.
partition := Partition{
ID: deviceRawInfo[0],
Size: deviceRawInfo[3],
MAJ: majMin[0],
MIN: majMin[1],
RM: deviceRawInfo[2],
RO: deviceRawInfo[4],
Filesystem: "",
Mountpoint: mountpoint,
}
currentPartitions = append(currentPartitions, partition)
}
}
devices = append([]Device{currentDevice}, devices...) // Prepending the first device...
return devices return devices
} }