update usb auto mount shell

This commit is contained in:
a624669980@163.com 2022-08-11 11:49:56 +08:00
parent 4cfd2ec2f6
commit 2f429972ad
7 changed files with 88 additions and 11 deletions

2
go.mod
View File

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

2
go.sum
View File

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

View File

@ -95,7 +95,7 @@ func main() {
return
}
go route.SocketInit(service.NotifyMsg)
go route.MonitoryUSB()
//model.Setup()
//gredis.Setup()
r := route.InitRouter()

33
pkg/utils/udev_helper.go Normal file
View File

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

View File

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

View File

@ -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")
}
}

View File

@ -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(){