diff --git a/config/config.go b/config/config.go index a7398e7..49798e5 100644 --- a/config/config.go +++ b/config/config.go @@ -2,16 +2,9 @@ package config import ( "fmt" - "os" "strings" ) -var RootFS string - -func init() { - RootFS = os.Getenv("ROOT_FS") -} - type Config struct { LogLevel string `yaml:"log_level"` Auth bool `yaml:"auth"` diff --git a/docker-compose.yml b/docker-compose.yml index a7211b9..1966cc0 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,20 +4,11 @@ services: image: rkkvm:latest container_name: rkkvm privileged: true - pid: host volumes: - #- /dev/bus/usb:/dev/bus/usb - /sys/kernel/config:/sys/kernel/config - ./test_data:/data - - /dev:/dev + - /dev:/dev # required, if you will use Mass-Storage-Device #devices: - #- /dev/bus/usb/001/001 - #- /dev/bus/usb/002/001 - #- /dev/bus/usb/003/001 - #- /dev/bus/usb/004/001 - #- /dev/bus/usb/005/001 - #- /dev/bus/usb/005/002 - #- /dev/bus/usb/006/001 #- /dev/hidg0 #- /dev/hidg1 #- /dev/hidg2 @@ -32,4 +23,4 @@ services: # - EDID=1 ports: - 8888:8080 - restart: always \ No newline at end of file + restart: unless-stopped \ No newline at end of file diff --git a/entry.sh b/entry.sh index 52e5314..a54d433 100644 --- a/entry.sh +++ b/entry.sh @@ -1,10 +1,6 @@ #!/bin/sh #set -e -#export ROOT_FS=$(nsenter -t 1 -m docker inspect -f $'{{.GraphDriver.Data.MergedDir}}' rkkvm) -FS=$(nsenter -t 1 -m docker inspect -f $'{{.GraphDriver.Data.MergedDir}}' rkkvm) -#export LD_LIBRARY_PATH=$ROOT_FS/usr/lib:$ROOT_FS/lib -#export PATH=$ROOT_FS/bin:$ROOT_FS/sbin:$ROOT_FS/usr/bin:$ROOT_FS/usr/sbin:$PATH [ -n "$EDID" ] && { [ -n "$EDID_HEX" ] && echo "$EDID_HEX" > /edid.hex @@ -15,24 +11,6 @@ FS=$(nsenter -t 1 -m docker inspect -f $'{{.GraphDriver.Data.MergedDir}}' rkkvm) done } -#/app/ustreamer --host 0.0.0.0 --port 8888 -m BGR24 -r 1280x720 -f 30 & -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh detach -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh delete -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh create -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh attach -#nsenter -t 1 -m -u -i $ROOT_FS/app/kvm $@ -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh detach -#nsenter -t 1 -m -u -i $ROOT_FS/hid.sh delete - - -#nsenter -t 1 -m -w/ -r/ /hid.sh detach -#nsenter -t 1 -m -w/ -r/ /hid.sh delete -#nsenter -t 1 -m -w/ -r/ /hid.sh create -#nsenter -t 1 -m -w/ -r/ /hid.sh attach -#nsenter -t 1 -m -w/ -r/ /app/kvm $@ -#nsenter -t 1 -m -w/ -r/ /hid.sh detach -#nsenter -t 1 -m -w/ -r/ /hid.sh delete - /hid.sh create /hid.sh attach /app/kvm $@ \ No newline at end of file diff --git a/hid.sh b/hid.sh index a1025e3..bf66582 100755 --- a/hid.sh +++ b/hid.sh @@ -3,7 +3,6 @@ set -e GADGET_DIR=/sys/kernel/config/usb_gadget GADGET_ID=g0 -MSD_ID=g1 create() { if [ -s "$GADGET_DIR/$GADGET_ID/UDC" ]; then @@ -12,14 +11,12 @@ create() { exit 0 fi mkdir -p $GADGET_DIR/$GADGET_ID && \ - mkdir -p $GADGET_DIR/$MSD_ID && \ cd $GADGET_DIR/$GADGET_ID - #echo 0x3346 > idVendor - echo 0x1b6d > idVendor - echo 0x0104 > idProduct - #echo 0x1009 > idProduct - echo 0x0300 > bcdUSB + echo 0x3346 > idVendor + echo 0x1009 > idProduct + echo 0x0200 > bcdUSB + echo 0x0100 > bcdDevice mkdir strings/0x409 cat /proc/device-tree/serial-number > strings/0x409/serialnumber echo 'rockchip' > strings/0x409/manufacturer @@ -27,6 +24,7 @@ create() { mkdir configs/c.1 #echo 0xE0 > configs/c.1/bmAttributes + echo 0x80 > configs/c.1/bmAttributes # only self-powered, no remote-wakeup echo 120 > configs/c.1/MaxPower mkdir configs/c.1/strings/0x409 echo "rk3588" > configs/c.1/strings/0x409/configuration @@ -35,48 +33,36 @@ create() { mkdir functions/hid.GS0 echo 1 > functions/hid.GS0/subclass echo 1 > functions/hid.GS0/protocol + #echo 1 > functions/hid.GS0/no_out_endpoint echo 6 > functions/hid.GS0/report_length echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.GS0/report_desc + + # Logitech Apex keyboard #echo 8 > functions/hid.GS0/report_length - #echo -ne \\x05\\x01\\x09\\x06\\xa1\\x01\\x05\\x07\\x19\\xe0\\x29\\xe7\\x15\\x00\\x25\\x01\\x75\\x01\\x95\\x08\\x81\\x02\\x95\\x01\\x75\\x08\\x81\\x03\\x95\\x05\\x75\\x01\\x05\\x08\\x19\\x01\\x29\\x05\\x91\\x02\\x95\\x01\\x75\\x03\\x91\\x03\\x95\\x06\\x75\\x08\\x15\\x00\\x25\\x65\\x05\\x07\\x19\\x00\\x29\\x65\\x81\\x00\\xc0 > functions/hid.GS0/report_desc - ln -s functions/hid.GS0 configs/c.1 + #echo -ne "05010906a101050719e029e715002501750195088102750895018101050719002aff00150026ff00750895068100050819012903250175019503910295059101c0" | xxd -r -ps -c 1000 > functions/hid.GS0/report_desc # mouse mkdir functions/hid.GS1 # echo 1 > functions/hid.GS1/subclass echo 2 > functions/hid.GS1/protocol + #echo 1 > functions/hid.GS0/no_out_endpoint echo -ne \\x34 > functions/hid.GS1/report_length echo -ne \\x5\\x1\\x9\\x2\\xa1\\x1\\x9\\x1\\xa1\\x0\\x5\\x9\\x19\\x1\\x29\\x3\\x15\\x0\\x25\\x1\\x95\\x3\\x75\\x1\\x81\\x2\\x95\\x1\\x75\\x5\\x81\\x3\\x5\\x1\\x9\\x30\\x9\\x31\\x9\\x38\\x15\\x81\\x25\\x7f\\x75\\x8\\x95\\x3\\x81\\x6\\xc0\\xc0 > functions/hid.GS1/report_desc - ln -s functions/hid.GS1 configs/c.1 + #ln -s functions/hid.GS1 configs/c.1 # touchpad mkdir functions/hid.GS2 # echo 1 > functions/hid.GS2/subclass echo 2 > functions/hid.GS2/protocol + #echo 1 > functions/hid.GS0/no_out_endpoint echo 6 > functions/hid.GS2/report_length echo -ne \\x05\\x01\\x09\\x02\\xa1\\x01\\x09\\x01\\xa1\\x00\\x05\\x09\\x19\\x01\\x29\\x03\\x15\\x00\\x25\\x01\\x95\\x03\\x75\\x01\\x81\\x02\\x95\\x01\\x75\\x05\\x81\\x01\\x05\\x01\\x09\\x30\\x09\\x31\\x15\\x00\\x26\\xff\\x7f\\x35\\x00\\x46\\xff\\x7f\\x75\\x10\\x95\\x02\\x81\\x02\\x05\\x01\\x09\\x38\\x15\\x81\\x25\\x7f\\x35\\x00\\x45\\x00\\x75\\x08\\x95\\x01\\x81\\x06\\xc0\\xc0 > functions/hid.GS2/report_desc - ln -s functions/hid.GS2 configs/c.1 + #ln -s functions/hid.GS2 configs/c.1 - #cd $GADGET_DIR/$MSD_ID - - #echo 0x3346 > idVendor - #echo 0x1009 > idProduct - #mkdir strings/0x409 - #echo '0123456789ABCDEF' > strings/0x409/serialnumber - #echo 'rockchip' > strings/0x409/manufacturer - #echo 'rk3588_msd' > strings/0x409/product - - #mkdir configs/c.1 - #echo 0xE0 > configs/c.1/bmAttributes - #echo 250 > configs/c.1/MaxPower - #mkdir configs/c.1/strings/0x409 - #echo "rk3588_msd" > configs/c.1/strings/0x409/configuration - - # attach storage - #mkdir functions/mass_storage.disk0 + mkdir functions/mass_storage.disk0 + echo 1 > functions/mass_storage.disk0/lun.0/removable + echo 1 > functions/mass_storage.disk0/lun.0/ro #ln -s functions/mass_storage.disk0 configs/c.1/ - #echo 1 > functions/mass_storage.disk0/lun.0/removable - #echo 1 > functions/mass_storage.disk0/lun.0/ro } delete() { @@ -92,30 +78,21 @@ delete() { cd "$GADGET_DIR" || exit 1 - rm -f $GADGET_ID/configs/c.1/hid.GS0 - rm -f $GADGET_ID/configs/c.1/hid.GS1 - rm -f $GADGET_ID/configs/c.1/hid.GS2 + #rm -f $GADGET_ID/configs/c.1/hid.GS0 + #rm -f $GADGET_ID/configs/c.1/hid.GS1 + #rm -f $GADGET_ID/configs/c.1/hid.GS2 + #rm -f $GADGET_ID/configs/c.1/mass_storage.disk0 rmdir $GADGET_ID/functions/hid.GS0 rmdir $GADGET_ID/functions/hid.GS1 rmdir $GADGET_ID/functions/hid.GS2 + rmdir $GADGET_ID/functions/mass_storage.disk0 + rmdir $GADGET_ID/configs/c.1/strings/0x409 rmdir $GADGET_ID/configs/c.1 rmdir $GADGET_ID/strings/0x409 rmdir "$GADGET_ID" - - if grep -q '[^[:space:]]' "$GADGET_DIR/$MSD_ID/UDC"; then - echo "HID devices are still attached. Please detach them before deleting." - exit 1 - fi - - #rm -f $MSD_ID/configs/c.1/mass_storage.disk0 - #rmdir $MSD_ID/functions/mass_storage.disk0 - #rmdir $MSD_ID/configs/c.1/strings/0x409 - #rmdir $MSD_ID/configs/c.1 - #rmdir $MSD_ID/strings/0x409 - #rmdir "$MSD_ID" echo "HID devices have been deleted." } @@ -126,14 +103,28 @@ attach() { exit 1 fi + ln -s $GADGET_DIR/$GADGET_ID/functions/hid.GS0 $GADGET_DIR/$GADGET_ID/configs/c.1 + ln -s $GADGET_DIR/$GADGET_ID/functions/hid.GS1 $GADGET_DIR/$GADGET_ID/configs/c.1 + ln -s $GADGET_DIR/$GADGET_ID/functions/hid.GS2 $GADGET_DIR/$GADGET_ID/configs/c.1 + ln -s $GADGET_DIR/$GADGET_ID/functions/mass_storage.disk0 $GADGET_DIR/$GADGET_ID/configs/c.1 + ls /sys/class/udc/ | cat > "$GADGET_DIR/$GADGET_ID/UDC" - #ls /sys/class/udc/ | cat > "$GADGET_DIR/$MSD_ID/UDC" + echo "Device has been attached." } detach() { + # we can't just detach UDC and attach it later + # for some reason configuration on host reports about incorrect bytes in report_desc + # unlinking/linking configuration helps, but in this case every time then iso mounted + # UDC and all other devices configuration should be disconnected. echo '' > "$GADGET_DIR/$GADGET_ID/UDC" - #echo '' > "$GADGET_DIR/$MSD_ID/UDC" + + rm -f $GADGET_DIR/$GADGET_ID/configs/c.1/hid.GS0 + rm -f $GADGET_DIR/$GADGET_ID/configs/c.1/hid.GS1 + rm -f $GADGET_DIR/$GADGET_ID/configs/c.1/hid.GS2 + rm -f $GADGET_DIR/$GADGET_ID/configs/c.1/mass_storage.disk0 + echo "Device has been detached." } @@ -145,26 +136,26 @@ mount_iso() { exit 1 fi - if grep -q '[^[:space:]]' "$GADGET_DIR/$MSD_ID/UDC"; then + if grep -q '[^[:space:]]' "$GADGET_DIR/$GADGET_ID/UDC"; then echo "Error: Device is still attached. Please detach it before mounting the ISO." exit 1 fi - echo "$iso_file" > "$GADGET_DIR/$MSD_ID/functions/mass_storage.disk0/lun.0/file" + echo "$iso_file" > "$GADGET_DIR/$GADGET_ID/functions/mass_storage.disk0/lun.0/file" echo "ISO file: $iso_file inserted." } mounted_iso() { - cat "$GADGET_DIR/$MSD_ID/functions/mass_storage.disk0/lun.0/file" + cat "$GADGET_DIR/$GADGET_ID/functions/mass_storage.disk0/lun.0/file" } unmount_iso() { - if grep -q '[^[:space:]]' "$GADGET_DIR/$MSD_ID/UDC"; then + if grep -q '[^[:space:]]' "$GADGET_DIR/$GADGET_ID/UDC"; then echo "Error: Device is still attached. Please detach it before unmounting the ISO." exit 1 fi - echo > "$GADGET_DIR/$MSD_ID/functions/mass_storage.disk0/lun.0/file" + echo > "$GADGET_DIR/$GADGET_ID/functions/mass_storage.disk0/lun.0/file" } if [ ! -d "$GADGET_DIR" ]; then diff --git a/http/hw/stream/extprocess.go b/http/hw/stream/extprocess.go index 83df277..8d75a04 100644 --- a/http/hw/stream/extprocess.go +++ b/http/hw/stream/extprocess.go @@ -4,7 +4,6 @@ import ( "io" "os" "os/exec" - "rkkvm/config" "sync" log "github.com/sirupsen/logrus" @@ -26,7 +25,7 @@ type ExtProcess struct { func Init(path string, args []string) *ExtProcess { return &ExtProcess{ args: args, - path: config.RootFS + path, + path: path, } } diff --git a/http/hw/stream/pipedprocess.go b/http/hw/stream/pipedprocess.go index 3b58206..71fb1ab 100644 --- a/http/hw/stream/pipedprocess.go +++ b/http/hw/stream/pipedprocess.go @@ -3,7 +3,6 @@ package stream import ( "log" "os/exec" - "rkkvm/config" "strings" "sync" "syscall" @@ -29,7 +28,7 @@ func InitPipedCmd(cmds []string) *PipedCmd { continue } cmdArgs := strings.Split(cmd, " ") - pipedCmds[i] = exec.Command(config.RootFS+"/"+cmdArgs[0], cmdArgs[1:]...) + pipedCmds[i] = exec.Command(cmdArgs[0], cmdArgs[1:]...) } pipedCmd = &PipedCmd{ diff --git a/http/route/api.go b/http/route/api.go index c1839db..5a04e77 100644 --- a/http/route/api.go +++ b/http/route/api.go @@ -90,6 +90,11 @@ func mountHandler(c *gin.Context) { return } + if request.Filename == "" { + unmountHandler(c) + return + } + imageFile := filepath.Join(config.Get().ISOPath, request.Filename) _, err := os.Stat(imageFile) if os.IsNotExist(err) { @@ -103,9 +108,9 @@ func mountHandler(c *gin.Context) { hid.GetHid().Close() cmds := []string{ - config.RootFS + "/hid.sh detach", - config.RootFS + "/hid.sh mount_iso " + imageFile, //+ strconv.Quote(imageFile), - config.RootFS + "/hid.sh attach", + "/hid.sh detach", + "/hid.sh mount_iso " + imageFile, //+ strconv.Quote(imageFile), + "/hid.sh attach", } for _, cmd := range cmds { @@ -127,25 +132,40 @@ func mountHandler(c *gin.Context) { c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{ Msg: reqrsp.MsgSuccess, }) - - os.Exit(0) } func unmountHandler(c *gin.Context) { - if output, err := exec.Command(config.RootFS + "/hid.sh unmount_iso").Output(); err != nil { - c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{ - Code: -2, - Msg: "execute command failed: " + string(output), - }) + hid.GetHid().Close() + + cmds := []string{ + "/hid.sh detach", + "/hid.sh unmount_iso", + "/hid.sh attach", } + for _, cmd := range cmds { + log.Debugf("Executing: %s", cmd) + cc := exec.Command("sh", "-c", cmd) + cc.Stdout = os.Stdout + cc.Stderr = os.Stderr + if err := cc.Run(); err != nil { + c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{ + Code: -2, + Msg: "execute command failed: " + cmd, + }) + return + } + } + + hid.GetHid().Open() + c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{ Msg: reqrsp.MsgSuccess, }) } func mountedHandler(c *gin.Context) { - cmd := exec.Command(config.RootFS+"/hid.sh", "mounted_iso") + cmd := exec.Command("/hid.sh", "mounted_iso") output, err := cmd.Output() if err != nil { c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{ @@ -154,11 +174,20 @@ func mountedHandler(c *gin.Context) { }) return } + fname := strings.TrimSpace(string(output)) + _, err = os.Stat(fname) + if os.IsNotExist(err) { + c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{ + Code: -2, + Msg: "file not exists: " + fname, + }) + return + } c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{ Msg: reqrsp.MsgSuccess, Data: reqrsp.FileRsp{ - File: string(output), + File: filepath.Base(fname), }, }) }