initial build
This commit is contained in:
commit
790062a152
8 changed files with 281 additions and 0 deletions
1
.gitignore
vendored
Normal file
1
.gitignore
vendored
Normal file
|
@ -0,0 +1 @@
|
||||||
|
/bin
|
116
.random data
Normal file
116
.random data
Normal file
|
@ -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/<device>
|
||||||
|
// 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
|
||||||
|
// }
|
37
cmd/gl.init/config.go
Normal file
37
cmd/gl.init/config.go
Normal file
|
@ -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
|
||||||
|
}
|
77
cmd/gl.init/devices.go
Normal file
77
cmd/gl.init/devices.go
Normal file
|
@ -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
|
||||||
|
}
|
32
cmd/gl.init/main.go
Normal file
32
cmd/gl.init/main.go
Normal file
|
@ -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)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
5
go.mod
Normal file
5
go.mod
Normal file
|
@ -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
|
2
go.sum
Normal file
2
go.sum
Normal file
|
@ -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=
|
11
makefile
Normal file
11
makefile
Normal file
|
@ -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
|
Loading…
Reference in a new issue