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

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