aroz 1 год назад
Родитель
Сommit
c05c6faf55
3 измененных файлов с 55 добавлено и 44 удалено
  1. 2 1
      disk.go
  2. 32 43
      mod/disk/diskfs/diskfs.go
  3. 21 0
      mod/disk/raid/handler.go

+ 2 - 1
disk.go

@@ -161,7 +161,8 @@ func DiskServiceInit() {
 				adminRouter.HandleFunc("/system/disk/raid/format", raidManager.HandleFormatRaidDevice)
 				adminRouter.HandleFunc("/system/disk/raid/detail", raidManager.HandleLoadArrayDetail)
 
-				//adminRouter.HandleFunc("/system/disk/physical/list", raidManager.HandleListRaidDevices)
+				/* Device Management functions */
+				adminRouter.HandleFunc("/system/disk/devices/list", raidManager.HandleListUsableDevices)
 
 				/* Advance functions*/
 				adminRouter.HandleFunc("/system/disk/raid/assemble", raidManager.HandleRaidDevicesAssemble)

+ 32 - 43
mod/disk/diskfs/diskfs.go

@@ -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

+ 21 - 0
mod/disk/raid/handler.go

@@ -28,6 +28,27 @@ func (m *Manager) HandleMdadmFlushReload(w http.ResponseWriter, r *http.Request)
 	utils.SendOK(w)
 }
 
+// Handle list all the disks that is usable
+func (m *Manager) HandleListUsableDevices(w http.ResponseWriter, r *http.Request) {
+	storageDevices, err := diskfs.ListAllStorageDevices()
+	if err != nil {
+		utils.SendErrorResponse(w, err.Error())
+		return
+	}
+
+	//Filter out the block devices that are disks
+	usableDisks := []diskfs.BlockDeviceMeta{}
+	for _, device := range storageDevices.Blockdevices {
+		if device.Type == "disk" {
+			usableDisks = append(usableDisks, device)
+		}
+	}
+
+	js, _ := json.Marshal(usableDisks)
+	utils.SendJSONResponse(w, string(js))
+
+}
+
 // Handle loading the detail of a given RAID array
 func (m *Manager) HandleLoadArrayDetail(w http.ResponseWriter, r *http.Request) {
 	devName, err := utils.GetPara(r, "devName")