|
@@ -2,11 +2,11 @@ package diskfs
|
|
|
|
|
|
import (
|
|
|
"bufio"
|
|
|
+ "encoding/json"
|
|
|
"errors"
|
|
|
"fmt"
|
|
|
"os"
|
|
|
"os/exec"
|
|
|
- "strconv"
|
|
|
"strings"
|
|
|
|
|
|
"imuslab.com/arozos/mod/utils"
|
|
@@ -19,12 +19,32 @@ import (
|
|
|
*/
|
|
|
|
|
|
// Storage Device meta was generated by lsblk
|
|
|
-type StorageDeviceMeta struct {
|
|
|
- Name string
|
|
|
- Size int64
|
|
|
- RO bool
|
|
|
- DevType string
|
|
|
- MountPoint string
|
|
|
+// Partitions like sdX0
|
|
|
+type PartitionMeta struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ MajMin string `json:"maj:min"`
|
|
|
+ Rm bool `json:"rm"`
|
|
|
+ Size int64 `json:"size"`
|
|
|
+ Ro bool `json:"ro"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Mountpoint string `json:"mountpoint"`
|
|
|
+}
|
|
|
+
|
|
|
+// Block device, usually disk or rom, like sdX
|
|
|
+type BlockDeviceMeta struct {
|
|
|
+ Name string `json:"name"`
|
|
|
+ MajMin string `json:"maj:min"`
|
|
|
+ Rm bool `json:"rm"`
|
|
|
+ Size int64 `json:"size"`
|
|
|
+ Ro bool `json:"ro"`
|
|
|
+ Type string `json:"type"`
|
|
|
+ Mountpoint string `json:"mountpoint"`
|
|
|
+ Children []PartitionMeta `json:"children,omitempty"`
|
|
|
+}
|
|
|
+
|
|
|
+// A collection of information for lsblk output
|
|
|
+type StorageDevicesMeta struct {
|
|
|
+ Blockdevices []BlockDeviceMeta `json:"blockdevices"`
|
|
|
}
|
|
|
|
|
|
// Check if the file format driver is installed on this host
|
|
@@ -80,48 +100,17 @@ func FormatStorageDevice(fsType string, devicePath string) error {
|
|
|
}
|
|
|
|
|
|
// List all the storage device in the system, set minSize to 0 for no filter
|
|
|
-func ListAllStorageDevices(minSize int64) ([]*StorageDeviceMeta, error) {
|
|
|
- cmd := exec.Command("sudo", "lsblk", "-b")
|
|
|
+func ListAllStorageDevices() (*StorageDevicesMeta, error) {
|
|
|
+ cmd := exec.Command("sudo", "lsblk", "-b", "--json")
|
|
|
|
|
|
output, err := cmd.CombinedOutput()
|
|
|
if err != nil {
|
|
|
return nil, fmt.Errorf("lsblk error: %v", err)
|
|
|
}
|
|
|
|
|
|
- // Split the output into lines
|
|
|
- lines := strings.Split(string(output), "\n")
|
|
|
-
|
|
|
- var devices []*StorageDeviceMeta
|
|
|
-
|
|
|
- // Parse each line to extract device information
|
|
|
- for _, line := range lines[1:] { // Skip the header line
|
|
|
- fields := strings.Fields(line)
|
|
|
- if len(fields) < 7 {
|
|
|
- continue
|
|
|
- }
|
|
|
-
|
|
|
- size, err := strconv.ParseInt(fields[3], 10, 64)
|
|
|
- if err != nil {
|
|
|
- return nil, fmt.Errorf("error parsing device size: %v", err)
|
|
|
- }
|
|
|
-
|
|
|
- ro := fields[4] == "1"
|
|
|
-
|
|
|
- device := &StorageDeviceMeta{
|
|
|
- Name: fields[0],
|
|
|
- Size: size,
|
|
|
- RO: ro,
|
|
|
- DevType: fields[5],
|
|
|
- MountPoint: fields[6],
|
|
|
- }
|
|
|
-
|
|
|
- // Filter devices based on minimum size
|
|
|
- if size >= minSize {
|
|
|
- devices = append(devices, device)
|
|
|
- }
|
|
|
- }
|
|
|
-
|
|
|
- return devices, nil
|
|
|
+ var devices StorageDevicesMeta
|
|
|
+ err = json.Unmarshal([]byte(output), &devices)
|
|
|
+ return &devices, err
|
|
|
}
|
|
|
|
|
|
// Check if a device is mounted given the path name, like /dev/sdc
|