feat:桌面镜像制作脚本,bug修改

master
汤全昆 2025-09-02 16:15:37 +08:00
parent ea39abaa9d
commit 20c98d2d5c
2 changed files with 30 additions and 18 deletions

View File

@ -23,7 +23,7 @@ public class DeskImageController {
public boolean start(@RequestParam("srcFile") String srcFile,
@RequestParam("detFile") String detFile,
@RequestParam("name") String name,
@RequestParam("detFile") String type) {
@RequestParam("type") String type) {
System.out.println("制作桌面镜像");
boolean result = ImageVirtualUtils.createImage(srcFile, detFile, name, type);
System.out.println("执行脚本制作桌面镜像: " + (result ? "成功" : "失败"));
@ -36,8 +36,8 @@ public class DeskImageController {
System.out.println("查询进度");
Map<String, Object> resultMap = ImageVirtualUtils.progress(name);
if(resultMap.get("process")!=null && (Double)resultMap.get("process") == 100){
String pid = (String) resultMap.get("pid");
ImageVirtualUtils.killProgress(pid);
// String pid = (String) resultMap.get("pid");
// ImageVirtualUtils.killProgress(pid);
return 100.0;
} else {
return (Double) resultMap.get("process");

View File

@ -15,27 +15,42 @@ import java.util.Map;
*/
public class ImageVirtualUtils {
private static final String BT_SCRIPT_PATH = "/var/lib/vdi/nodejs/convert_image.sh";
// private static final String JSON_PATH = "/var/lib/vdi/nodejs/json/";
private static final String JSON_PATH = "D:\\code\\";
private static final String BT_SCRIPT_PATH = "/deskTest/convert_image.sh";
private static final String JSON_PATH = "/deskTest/logs";
// private static final String JSON_PATH = "D:\\code\\";
private static final Long WAIT_START_TIME = 8000L;
/**
* bttorrent.sh
*
* ./convert_image.sh /home/unis/jk/Win10-BIOS.qcow2 /deskTest/win10_compressed2.vhd progress2.json vhd
*/
private static boolean executeCommand(String arg1, String arg2, String arg3, String arg4) {
private static boolean executeCommand(String srcFile, String detFile, String name, String type) {
try {
// 构造命令
// 确保脚本文件存在
File scriptFile = new File(BT_SCRIPT_PATH);
if (!scriptFile.exists()) {
System.err.println("脚本文件不存在: " + BT_SCRIPT_PATH);
return false;
}
// 确保脚本有执行权限
if (!scriptFile.canExecute()) {
scriptFile.setExecutable(true);
}
// 使用bash执行脚本而不是sh
ProcessBuilder pb = new ProcessBuilder(
"./",
"bash",
BT_SCRIPT_PATH,
arg1,
arg2,
JSON_PATH+arg3,
arg4
).redirectErrorStream(true);
srcFile,
detFile,
name,
type);
// 设置工作目录
pb.directory(scriptFile.getParentFile());
pb.redirectErrorStream(true);
// 启动进程
Process process = pb.start();
// 启动新线程读取输出,避免阻塞
@ -46,13 +61,10 @@ public class ImageVirtualUtils {
e.printStackTrace();
}
}).start();
// 对于start命令只要进程启动成功就返回true
// 稍微等待一下看初始输出是否有错误
Thread.sleep(WAIT_START_TIME);
return process.isAlive(); // 如果进程还在运行,认为启动成功
} catch (IOException | InterruptedException e) {
e.printStackTrace();
return false;