hid: implemented image mount/unmount

This commit is contained in:
Artem
2024-11-04 15:31:20 +01:00
parent 083edf165a
commit df0b972ab4
7 changed files with 88 additions and 108 deletions

View File

@@ -2,16 +2,9 @@ package config
import ( import (
"fmt" "fmt"
"os"
"strings" "strings"
) )
var RootFS string
func init() {
RootFS = os.Getenv("ROOT_FS")
}
type Config struct { type Config struct {
LogLevel string `yaml:"log_level"` LogLevel string `yaml:"log_level"`
Auth bool `yaml:"auth"` Auth bool `yaml:"auth"`

View File

@@ -4,20 +4,11 @@ services:
image: rkkvm:latest image: rkkvm:latest
container_name: rkkvm container_name: rkkvm
privileged: true privileged: true
pid: host
volumes: volumes:
#- /dev/bus/usb:/dev/bus/usb
- /sys/kernel/config:/sys/kernel/config - /sys/kernel/config:/sys/kernel/config
- ./test_data:/data - ./test_data:/data
- /dev:/dev - /dev:/dev # required, if you will use Mass-Storage-Device
#devices: #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/hidg0
#- /dev/hidg1 #- /dev/hidg1
#- /dev/hidg2 #- /dev/hidg2
@@ -32,4 +23,4 @@ services:
# - EDID=1 # - EDID=1
ports: ports:
- 8888:8080 - 8888:8080
restart: always restart: unless-stopped

View File

@@ -1,10 +1,6 @@
#!/bin/sh #!/bin/sh
#set -e #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" ] && {
[ -n "$EDID_HEX" ] && echo "$EDID_HEX" > /edid.hex [ -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 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 create
/hid.sh attach /hid.sh attach
/app/kvm $@ /app/kvm $@

95
hid.sh
View File

@@ -3,7 +3,6 @@
set -e set -e
GADGET_DIR=/sys/kernel/config/usb_gadget GADGET_DIR=/sys/kernel/config/usb_gadget
GADGET_ID=g0 GADGET_ID=g0
MSD_ID=g1
create() { create() {
if [ -s "$GADGET_DIR/$GADGET_ID/UDC" ]; then if [ -s "$GADGET_DIR/$GADGET_ID/UDC" ]; then
@@ -12,14 +11,12 @@ create() {
exit 0 exit 0
fi fi
mkdir -p $GADGET_DIR/$GADGET_ID && \ mkdir -p $GADGET_DIR/$GADGET_ID && \
mkdir -p $GADGET_DIR/$MSD_ID && \
cd $GADGET_DIR/$GADGET_ID cd $GADGET_DIR/$GADGET_ID
#echo 0x3346 > idVendor echo 0x3346 > idVendor
echo 0x1b6d > idVendor echo 0x1009 > idProduct
echo 0x0104 > idProduct echo 0x0200 > bcdUSB
#echo 0x1009 > idProduct echo 0x0100 > bcdDevice
echo 0x0300 > bcdUSB
mkdir strings/0x409 mkdir strings/0x409
cat /proc/device-tree/serial-number > strings/0x409/serialnumber cat /proc/device-tree/serial-number > strings/0x409/serialnumber
echo 'rockchip' > strings/0x409/manufacturer echo 'rockchip' > strings/0x409/manufacturer
@@ -27,6 +24,7 @@ create() {
mkdir configs/c.1 mkdir configs/c.1
#echo 0xE0 > configs/c.1/bmAttributes #echo 0xE0 > configs/c.1/bmAttributes
echo 0x80 > configs/c.1/bmAttributes # only self-powered, no remote-wakeup
echo 120 > configs/c.1/MaxPower echo 120 > configs/c.1/MaxPower
mkdir configs/c.1/strings/0x409 mkdir configs/c.1/strings/0x409
echo "rk3588" > configs/c.1/strings/0x409/configuration echo "rk3588" > configs/c.1/strings/0x409/configuration
@@ -35,48 +33,36 @@ create() {
mkdir functions/hid.GS0 mkdir functions/hid.GS0
echo 1 > functions/hid.GS0/subclass echo 1 > functions/hid.GS0/subclass
echo 1 > functions/hid.GS0/protocol echo 1 > functions/hid.GS0/protocol
#echo 1 > functions/hid.GS0/no_out_endpoint
echo 6 > functions/hid.GS0/report_length 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 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 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 #echo -ne "05010906a101050719e029e715002501750195088102750895018101050719002aff00150026ff00750895068100050819012903250175019503910295059101c0" | xxd -r -ps -c 1000 > functions/hid.GS0/report_desc
ln -s functions/hid.GS0 configs/c.1
# mouse # mouse
mkdir functions/hid.GS1 mkdir functions/hid.GS1
# echo 1 > functions/hid.GS1/subclass # echo 1 > functions/hid.GS1/subclass
echo 2 > functions/hid.GS1/protocol echo 2 > functions/hid.GS1/protocol
#echo 1 > functions/hid.GS0/no_out_endpoint
echo -ne \\x34 > functions/hid.GS1/report_length 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 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 # touchpad
mkdir functions/hid.GS2 mkdir functions/hid.GS2
# echo 1 > functions/hid.GS2/subclass # echo 1 > functions/hid.GS2/subclass
echo 2 > functions/hid.GS2/protocol echo 2 > functions/hid.GS2/protocol
#echo 1 > functions/hid.GS0/no_out_endpoint
echo 6 > functions/hid.GS2/report_length 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 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 mkdir functions/mass_storage.disk0
echo 1 > functions/mass_storage.disk0/lun.0/removable
#echo 0x3346 > idVendor echo 1 > functions/mass_storage.disk0/lun.0/ro
#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
#ln -s functions/mass_storage.disk0 configs/c.1/ #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() { delete() {
@@ -92,31 +78,22 @@ delete() {
cd "$GADGET_DIR" || exit 1 cd "$GADGET_DIR" || exit 1
rm -f $GADGET_ID/configs/c.1/hid.GS0 #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.GS1
rm -f $GADGET_ID/configs/c.1/hid.GS2 #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.GS0
rmdir $GADGET_ID/functions/hid.GS1 rmdir $GADGET_ID/functions/hid.GS1
rmdir $GADGET_ID/functions/hid.GS2 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/strings/0x409
rmdir $GADGET_ID/configs/c.1 rmdir $GADGET_ID/configs/c.1
rmdir $GADGET_ID/strings/0x409 rmdir $GADGET_ID/strings/0x409
rmdir "$GADGET_ID" 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." echo "HID devices have been deleted."
} }
@@ -126,14 +103,28 @@ attach() {
exit 1 exit 1
fi 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/$GADGET_ID/UDC"
#ls /sys/class/udc/ | cat > "$GADGET_DIR/$MSD_ID/UDC"
echo "Device has been attached." echo "Device has been attached."
} }
detach() { 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/$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." echo "Device has been detached."
} }
@@ -145,26 +136,26 @@ mount_iso() {
exit 1 exit 1
fi 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." echo "Error: Device is still attached. Please detach it before mounting the ISO."
exit 1 exit 1
fi 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." echo "ISO file: $iso_file inserted."
} }
mounted_iso() { 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() { 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." echo "Error: Device is still attached. Please detach it before unmounting the ISO."
exit 1 exit 1
fi 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 if [ ! -d "$GADGET_DIR" ]; then

View File

@@ -4,7 +4,6 @@ import (
"io" "io"
"os" "os"
"os/exec" "os/exec"
"rkkvm/config"
"sync" "sync"
log "github.com/sirupsen/logrus" log "github.com/sirupsen/logrus"
@@ -26,7 +25,7 @@ type ExtProcess struct {
func Init(path string, args []string) *ExtProcess { func Init(path string, args []string) *ExtProcess {
return &ExtProcess{ return &ExtProcess{
args: args, args: args,
path: config.RootFS + path, path: path,
} }
} }

View File

@@ -3,7 +3,6 @@ package stream
import ( import (
"log" "log"
"os/exec" "os/exec"
"rkkvm/config"
"strings" "strings"
"sync" "sync"
"syscall" "syscall"
@@ -29,7 +28,7 @@ func InitPipedCmd(cmds []string) *PipedCmd {
continue continue
} }
cmdArgs := strings.Split(cmd, " ") cmdArgs := strings.Split(cmd, " ")
pipedCmds[i] = exec.Command(config.RootFS+"/"+cmdArgs[0], cmdArgs[1:]...) pipedCmds[i] = exec.Command(cmdArgs[0], cmdArgs[1:]...)
} }
pipedCmd = &PipedCmd{ pipedCmd = &PipedCmd{

View File

@@ -90,6 +90,11 @@ func mountHandler(c *gin.Context) {
return return
} }
if request.Filename == "" {
unmountHandler(c)
return
}
imageFile := filepath.Join(config.Get().ISOPath, request.Filename) imageFile := filepath.Join(config.Get().ISOPath, request.Filename)
_, err := os.Stat(imageFile) _, err := os.Stat(imageFile)
if os.IsNotExist(err) { if os.IsNotExist(err) {
@@ -103,9 +108,9 @@ func mountHandler(c *gin.Context) {
hid.GetHid().Close() hid.GetHid().Close()
cmds := []string{ cmds := []string{
config.RootFS + "/hid.sh detach", "/hid.sh detach",
config.RootFS + "/hid.sh mount_iso " + imageFile, //+ strconv.Quote(imageFile), "/hid.sh mount_iso " + imageFile, //+ strconv.Quote(imageFile),
config.RootFS + "/hid.sh attach", "/hid.sh attach",
} }
for _, cmd := range cmds { for _, cmd := range cmds {
@@ -127,25 +132,40 @@ func mountHandler(c *gin.Context) {
c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{ c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{
Msg: reqrsp.MsgSuccess, Msg: reqrsp.MsgSuccess,
}) })
os.Exit(0)
} }
func unmountHandler(c *gin.Context) { func unmountHandler(c *gin.Context) {
if output, err := exec.Command(config.RootFS + "/hid.sh unmount_iso").Output(); err != nil { hid.GetHid().Close()
c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{
Code: -2, cmds := []string{
Msg: "execute command failed: " + string(output), "/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{ c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{
Msg: reqrsp.MsgSuccess, Msg: reqrsp.MsgSuccess,
}) })
} }
func mountedHandler(c *gin.Context) { 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() output, err := cmd.Output()
if err != nil { if err != nil {
c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{ c.JSON(http.StatusInternalServerError, reqrsp.NanoKVMRsp{
@@ -154,11 +174,20 @@ func mountedHandler(c *gin.Context) {
}) })
return 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{ c.JSON(http.StatusOK, reqrsp.NanoKVMRsp{
Msg: reqrsp.MsgSuccess, Msg: reqrsp.MsgSuccess,
Data: reqrsp.FileRsp{ Data: reqrsp.FileRsp{
File: string(output), File: filepath.Base(fname),
}, },
}) })
} }