From 790062a1525349d7fe94bffcd3434ece0329a461 Mon Sep 17 00:00:00 2001 From: Merith Date: Tue, 6 May 2025 11:21:11 -0700 Subject: [PATCH] initial build --- .gitignore | 1 + .random data | 116 +++++++++++++++++++++++++++++++++++++++++ cmd/gl.init/config.go | 37 +++++++++++++ cmd/gl.init/devices.go | 77 +++++++++++++++++++++++++++ cmd/gl.init/main.go | 32 ++++++++++++ go.mod | 5 ++ go.sum | 2 + makefile | 11 ++++ 8 files changed, 281 insertions(+) create mode 100644 .gitignore create mode 100644 .random data create mode 100644 cmd/gl.init/config.go create mode 100644 cmd/gl.init/devices.go create mode 100644 cmd/gl.init/main.go create mode 100644 go.mod create mode 100644 go.sum create mode 100644 makefile diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..7447f89 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/bin \ No newline at end of file diff --git a/.random data b/.random data new file mode 100644 index 0000000..a0a55d5 --- /dev/null +++ b/.random data @@ -0,0 +1,116 @@ +// package main + +// import ( +// "fmt" +// "os" +// "os/exec" +// ) + +// func main() { +// devices, err := listStorageDevices() +// if err != nil { +// fmt.Println("Error:", err) +// return +// } + +// fmt.Println("Connected storage devices:") +// var storageDevices []string +// for _, device := range devices { +// // filter out non-disk devices +// if device[:2] != "sd" { +// continue +// } +// // show only ones that end with a number +// if device[len(device)-1] < '0' || device[len(device)-1] > '9' { +// continue +// } +// fmt.Println(device) +// storageDevices = append(storageDevices, device) +// } + +// if len(storageDevices) == 0 { +// fmt.Println("No storage devices found.") +// return +// } + +// // if there are storage devices, check if they are mounted +// for _, device := range storageDevices { +// mounted, err := isMounted(device) +// if err != nil { +// fmt.Println("Error:", err) +// return +// } + +// // if not mounted, mount it to /mnt/init/ +// if !mounted { +// err = mountDevice(device) +// if err != nil { +// fmt.Println("Error:", err) +// return +// } +// fmt.Println("Device", device, "mounted to /mnt/init/"+device) +// } +// } +// } + +// func mountDevice(device string) error { +// // ensure the mount point exists +// err := os.MkdirAll("/mnt/init/"+device, 0755) +// if err != nil { +// return err +// } + +// // Use `mount` command to mount the device +// cmd := exec.Command("mount", "/dev/"+device, "/mnt/init/"+device) +// err = cmd.Run() +// if err != nil { +// return err +// } +// return nil +// } + +// func isMounted(device string) (bool, error) { +// file, err := os.Open("/proc/mounts") +// if err != nil { +// return false, err +// } +// defer file.Close() + +// var devicePath string +// for { +// var path, dev string +// _, err := fmt.Fscanf(file, "%s %s\n", &path, &dev) +// if err != nil { +// break +// } +// if dev == "/dev/"+device { +// devicePath = path +// break +// } +// } + +// return devicePath != "", nil + +// } + +// func listStorageDevices() ([]string, error) { +// dir, err := os.Open("/dev") +// if err != nil { +// return nil, err +// } +// defer dir.Close() + +// files, err := dir.Readdir(-1) +// if err != nil { +// return nil, err +// } + +// devices := make([]string, 0) +// for _, file := range files { +// if file.Mode()&os.ModeDevice != 0 { +// devices = append(devices, file.Name()) +// } +// } + +// return devices, nil +// } diff --git a/cmd/gl.init/config.go b/cmd/gl.init/config.go new file mode 100644 index 0000000..ed89792 --- /dev/null +++ b/cmd/gl.init/config.go @@ -0,0 +1,37 @@ +package main + +import ( + "encoding/json" + "io/ioutil" +) + +var config ConfigData + +type ConfigData struct { + Servicefiles string `json:"servicefiles"` + Automount []struct { + PartitionUUID string `json:"partition-uuid"` + Mountpoint string `json:"mountpoint"` + } `json:"automount"` +} + +func loadConfig() { + // Load JSON config from /etc/gl.userinit.json + configFile := "userinit.json" + + // Read the JSON file + data, err := ioutil.ReadFile(configFile) + if err != nil { + // Handle error + return + } + + // Unmarshal JSON data into the config struct + err = json.Unmarshal(data, &config) + if err != nil { + // Handle error + return + } + + // Config loaded successfully +} diff --git a/cmd/gl.init/devices.go b/cmd/gl.init/devices.go new file mode 100644 index 0000000..7fbae4a --- /dev/null +++ b/cmd/gl.init/devices.go @@ -0,0 +1,77 @@ +package main + +import ( + "fmt" + "os/exec" + "runtime" + "strings" +) + +type Device struct { + Name string + MajMin string + Rm string + Size string + Ro string + Type string + MountPoint string +} + +func getDevices() string { + if runtime.GOOS == "windows" { + return sampleOutput + } + cmd := exec.Command("lsblk", "-pP") + // store the output + output, err := cmd.Output() + if err != nil { + panic(err) + } + // convert the byte buffer to a string + outputString := string(output) + return outputString +} + +func parseDevices(output string) []Device { + // split the output string by new line + lines := strings.Split(output, "\n") + if len(lines) == 0 { + return nil + } + // create a slice of devices + devices := make([]Device, 0) + // iterate over the lines + for _, line := range lines { + // skip empty lines + if line == "" { + continue + } + line = strings.ReplaceAll(line, "\"", "") // remove the quotes + line = strings.ReplaceAll(line, "NAME=", "") + line = strings.ReplaceAll(line, "MAJ:MIN=", "") + line = strings.ReplaceAll(line, "RM=", "") + line = strings.ReplaceAll(line, "SIZE=", "") + line = strings.ReplaceAll(line, "RO=", "") + line = strings.ReplaceAll(line, "TYPE=", "") + line = strings.ReplaceAll(line, "MOUNTPOINT=", "") + fmt.Println(line) + + // split the line by space + parts := strings.Split(line, " ") + + // create a new device + device := Device{ + Name: parts[0], + MajMin: parts[1], + Rm: parts[2], + Size: parts[3], + Ro: parts[4], + Type: parts[5], + MountPoint: parts[6], + } + // append the device to the slice + devices = append(devices, device) + } + // return the slice of devices + return devices +} diff --git a/cmd/gl.init/main.go b/cmd/gl.init/main.go new file mode 100644 index 0000000..b4dde12 --- /dev/null +++ b/cmd/gl.init/main.go @@ -0,0 +1,32 @@ +package main + +import ( + "fmt" +) + +// `lsblk -pP` returns a string with the following format: +var sampleOutput = `NAME="/dev/sda" MAJ:MIN="8:0" RM="1" SIZE="57.7G" RO="0" TYPE="disk" MOUNTPOINT="" +NAME="/dev/sda1" MAJ:MIN="8:1" RM="1" SIZE="57.6G" RO="0" TYPE="part" MOUNTPOINT="/mnt/init/sda1" +NAME="/dev/sda2" MAJ:MIN="8:2" RM="1" SIZE="32M" RO="0" TYPE="part" MOUNTPOINT="/mnt/init/sda2" +NAME="/dev/mtdblock0" MAJ:MIN="31:0" RM="0" SIZE="128K" RO="1" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock1" MAJ:MIN="31:1" RM="0" SIZE="384K" RO="1" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock2" MAJ:MIN="31:2" RM="0" SIZE="128K" RO="1" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock3" MAJ:MIN="31:3" RM="0" SIZE="128K" RO="1" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock4" MAJ:MIN="31:4" RM="0" SIZE="125.3M" RO="0" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock5" MAJ:MIN="31:5" RM="0" SIZE="4M" RO="0" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock6" MAJ:MIN="31:6" RM="0" SIZE="121.3M" RO="0" TYPE="disk" MOUNTPOINT="" +NAME="/dev/mtdblock7" MAJ:MIN="31:7" RM="0" SIZE="256K" RO="0" TYPE="disk" MOUNTPOINT="" +NAME="/dev/ubiblock0_0" MAJ:MIN="254:0" RM="0" SIZE="13.9M" RO="0" TYPE="disk" MOUNTPOINT="/rom" +` + +func main() { + loadConfig() + + devices := getDevices() + parsedDevices := parseDevices(devices) + for _, device := range parsedDevices { + // if device starts with /dev/sd and ends with a number, it's a disk + fmt.Println("Name: " + device.Name) + + } +} diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..cb5e9fb --- /dev/null +++ b/go.mod @@ -0,0 +1,5 @@ +module git.merith.xyz/packages/gl.init + +go 1.21.3 + +require github.com/deepakjois/gousbdrivedetector v0.0.0-20220514003247-ea439de1c459 // indirect diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..1719e2a --- /dev/null +++ b/go.sum @@ -0,0 +1,2 @@ +github.com/deepakjois/gousbdrivedetector v0.0.0-20220514003247-ea439de1c459 h1:gZcBabrda6G8OBWOLr2hRCwtOVr2z2oD8GHlv9wpMSE= +github.com/deepakjois/gousbdrivedetector v0.0.0-20220514003247-ea439de1c459/go.mod h1:zBq85v4Jn+9KU3tkyNs7IKzmyP/KQcitMLzLkwhxfSk= diff --git a/makefile b/makefile new file mode 100644 index 0000000..ea9e25d --- /dev/null +++ b/makefile @@ -0,0 +1,11 @@ +GOOS=linux +GOARCH=mipsle + + +default: + go run ./cmd/gl.init/ + +target: + GOOS=$(GOOS) GOARCH=$(GOARCH) go build -o ./bin/gl.init ./cmd/gl.init/ +push: target + scp ./bin/gl.init root@192.168.8.1:/tmp \ No newline at end of file