diff --git a/go.mod b/go.mod index 550dbc5..1c95686 100644 --- a/go.mod +++ b/go.mod @@ -38,6 +38,7 @@ require ( github.com/jinzhu/copier v0.3.2 github.com/json-iterator/go v1.1.11 // indirect github.com/klauspost/compress v1.13.6 // indirect + github.com/kr/pretty v0.2.1 github.com/kr/text v0.2.0 // indirect github.com/leodido/go-urn v1.2.1 // indirect github.com/lucas-clemente/quic-go v0.25.0 @@ -49,6 +50,7 @@ require ( github.com/opencontainers/image-spec v1.0.2 // indirect github.com/opencontainers/selinux v1.8.5 // indirect github.com/patrickmn/go-cache v2.1.0+incompatible + github.com/pilebones/go-udev v0.9.0 github.com/pkg/errors v0.9.1 github.com/prometheus/procfs v0.7.3 // indirect github.com/robfig/cron v1.2.0 diff --git a/go.sum b/go.sum index b472d34..2b007fd 100644 --- a/go.sum +++ b/go.sum @@ -706,6 +706,8 @@ github.com/pelletier/go-toml v1.8.1/go.mod h1:T2/BmBdy8dvIRq1a/8aqjN41wvWlN4lrap github.com/peterbourgon/diskv v2.0.1+incompatible/go.mod h1:uqqh8zWWbv1HBMNONnaR/tNboyR3/BZd58JJSHlUSCU= github.com/pierrec/lz4/v4 v4.1.2 h1:qvY3YFXRQE/XB8MlLzJH7mSzBs74eA2gg52YTk6jUPM= github.com/pierrec/lz4/v4 v4.1.2/go.mod h1:gZWDp/Ze/IJXGXf23ltt2EXimqmTUXEy0GFuRQyBid4= +github.com/pilebones/go-udev v0.9.0 h1:N1uEO/SxUwtIctc0WLU0t69JeBxIYEYnj8lT/Nabl9Q= +github.com/pilebones/go-udev v0.9.0/go.mod h1:T2eI2tUSK0hA2WS5QLjXJUfQkluZQu+18Cqvem3CaXI= github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1-0.20171018195549-f15c970de5b7/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= diff --git a/main.go b/main.go index e1ec179..0aa2a70 100644 --- a/main.go +++ b/main.go @@ -95,7 +95,7 @@ func main() { return } go route.SocketInit(service.NotifyMsg) - + go route.MonitoryUSB() //model.Setup() //gredis.Setup() r := route.InitRouter() diff --git a/pkg/utils/udev_helper.go b/pkg/utils/udev_helper.go new file mode 100644 index 0000000..701a6de --- /dev/null +++ b/pkg/utils/udev_helper.go @@ -0,0 +1,33 @@ +/* + * @Author: LinkLeong link@icewhale.org + * @Date: 2022-08-10 16:06:12 + * @LastEditors: LinkLeong + * @LastEditTime: 2022-08-10 16:11:37 + * @FilePath: /CasaOS/pkg/utils/udev_helper.go + * @Description: + * @Website: https://www.casaos.io + * Copyright (c) 2022 by icewhale, All Rights Reserved. + */ +package utils + +// func getOptionnalMatcher() (matcher netlink.Matcher, err error) { +// if filePath == nil || *filePath == "" { +// return nil, nil +// } + +// stream, err := ioutil.ReadFile(*filePath) +// if err != nil { +// return nil, err +// } + +// if stream == nil { +// return nil, fmt.Errorf("Empty, no rules provided in \"%s\", err: %w", *filePath, err) +// } + +// var rules netlink.RuleDefinitions +// if err := json.Unmarshal(stream, &rules); err != nil { +// return nil, fmt.Errorf("Wrong rule syntax, err: %w", err) +// } + +// return &rules, nil +// } diff --git a/route/periodical.go b/route/periodical.go index c3bab59..651a7f5 100644 --- a/route/periodical.go +++ b/route/periodical.go @@ -2,7 +2,7 @@ * @Author: LinkLeong link@icewhale.com * @Date: 2022-07-01 15:11:36 * @LastEditors: LinkLeong - * @LastEditTime: 2022-08-03 14:49:15 + * @LastEditTime: 2022-08-10 17:19:27 * @FilePath: /CasaOS/route/periodical.go * @Description: * @Website: https://www.casaos.io @@ -11,14 +11,20 @@ package route import ( + "os" + "os/signal" "reflect" "strconv" "strings" + "syscall" "time" "unsafe" "github.com/IceWhaleTech/CasaOS/model" + "github.com/IceWhaleTech/CasaOS/pkg/utils/loger" "github.com/IceWhaleTech/CasaOS/service" + "github.com/pilebones/go-udev/netlink" + "go.uber.org/zap" ) func SendNetINfoBySocket() { @@ -257,3 +263,38 @@ func SendAllHardwareStatusBySocket() { service.MyService.Notify().SendAllHardwareStatusBySocket(summary, usb, memInfo, cpuData, newNet) } +func MonitoryUSB() { + var matcher netlink.Matcher + + conn := new(netlink.UEventConn) + if err := conn.Connect(netlink.UdevEvent); err != nil { + loger.Error("udev err", zap.Any("Unable to connect to Netlink Kobject UEvent socket", err)) + } + defer conn.Close() + + queue := make(chan netlink.UEvent) + errors := make(chan error) + quit := conn.Monitor(queue, errors, matcher) + + signals := make(chan os.Signal, 1) + signal.Notify(signals, syscall.SIGINT, syscall.SIGTERM, syscall.SIGQUIT) + go func() { + <-signals + close(quit) + os.Exit(0) + }() + + for { + select { + case uevent := <-queue: + if uevent.Env["DEVTYPE"] == "disk" { + time.Sleep(time.Microsecond * 500) + SendUSBBySocket() + continue + } + case err := <-errors: + loger.Error("udev err", zap.Any("err", err)) + } + } + +} diff --git a/service/system.go b/service/system.go index 58dfd13..1bb622f 100644 --- a/service/system.go +++ b/service/system.go @@ -233,9 +233,9 @@ func (s *systemService) GetTimeZone() string { func (s *systemService) ExecUSBAutoMountShell(state string) { if state == "False" { - command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Remove_File") + command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Stop_Auto") } else { - command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Move_File") + command2.OnlyExec("source " + config.AppInfo.ShellPath + "/helper.sh ;USB_Start_Auto") } } diff --git a/shell/helper.sh b/shell/helper.sh index 2a7545e..62471ca 100644 --- a/shell/helper.sh +++ b/shell/helper.sh @@ -330,17 +330,16 @@ TarFolder() { du -sh /DATA } -USB_Move_File() { +USB_Start_Auto() { ((EUID)) && sudo_cmd="sudo" - $sudo_cmd cp -rf /casaOS/server/shell/11-usb-mount.rules /etc/udev/rules.d/ - $sudo_cmd chmod +x /casaOS/server/shell/usb-mount.sh - $sudo_cmd cp -rf /casaOS/server/shell/usb-mount@.service /etc/systemd/system/ + $sudo_cmd systemctl enable devmon@devmon + $sudo_cmd} systemctl start devmon@devmon } -USB_Remove_File() { +USB_Stop_Auto() { ((EUID)) && sudo_cmd="sudo" - $sudo_cmd rm -fr /etc/udev/rules.d/11-usb-mount.rules - $sudo_cmd rm -fr /etc/systemd/system/usb-mount@.service + $sudo_cmd} systemctl stop devmon@devmon + $sudo_cmd systemctl disable devmon@devmon } GetDeviceTree(){