feat:桌面镜像制作脚本,bug修改
parent
ea39abaa9d
commit
20c98d2d5c
|
|
@ -23,7 +23,7 @@ public class DeskImageController {
|
||||||
public boolean start(@RequestParam("srcFile") String srcFile,
|
public boolean start(@RequestParam("srcFile") String srcFile,
|
||||||
@RequestParam("detFile") String detFile,
|
@RequestParam("detFile") String detFile,
|
||||||
@RequestParam("name") String name,
|
@RequestParam("name") String name,
|
||||||
@RequestParam("detFile") String type) {
|
@RequestParam("type") String type) {
|
||||||
System.out.println("制作桌面镜像");
|
System.out.println("制作桌面镜像");
|
||||||
boolean result = ImageVirtualUtils.createImage(srcFile, detFile, name, type);
|
boolean result = ImageVirtualUtils.createImage(srcFile, detFile, name, type);
|
||||||
System.out.println("执行脚本制作桌面镜像: " + (result ? "成功" : "失败"));
|
System.out.println("执行脚本制作桌面镜像: " + (result ? "成功" : "失败"));
|
||||||
|
|
@ -36,8 +36,8 @@ public class DeskImageController {
|
||||||
System.out.println("查询进度");
|
System.out.println("查询进度");
|
||||||
Map<String, Object> resultMap = ImageVirtualUtils.progress(name);
|
Map<String, Object> resultMap = ImageVirtualUtils.progress(name);
|
||||||
if(resultMap.get("process")!=null && (Double)resultMap.get("process") == 100){
|
if(resultMap.get("process")!=null && (Double)resultMap.get("process") == 100){
|
||||||
String pid = (String) resultMap.get("pid");
|
// String pid = (String) resultMap.get("pid");
|
||||||
ImageVirtualUtils.killProgress(pid);
|
// ImageVirtualUtils.killProgress(pid);
|
||||||
return 100.0;
|
return 100.0;
|
||||||
} else {
|
} else {
|
||||||
return (Double) resultMap.get("process");
|
return (Double) resultMap.get("process");
|
||||||
|
|
|
||||||
|
|
@ -15,27 +15,42 @@ import java.util.Map;
|
||||||
*/
|
*/
|
||||||
public class ImageVirtualUtils {
|
public class ImageVirtualUtils {
|
||||||
|
|
||||||
private static final String BT_SCRIPT_PATH = "/var/lib/vdi/nodejs/convert_image.sh";
|
private static final String BT_SCRIPT_PATH = "/deskTest/convert_image.sh";
|
||||||
// private static final String JSON_PATH = "/var/lib/vdi/nodejs/json/";
|
private static final String JSON_PATH = "/deskTest/logs";
|
||||||
private static final String JSON_PATH = "D:\\code\\";
|
// private static final String JSON_PATH = "D:\\code\\";
|
||||||
|
|
||||||
private static final Long WAIT_START_TIME = 8000L;
|
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 {
|
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(
|
ProcessBuilder pb = new ProcessBuilder(
|
||||||
"./",
|
"bash",
|
||||||
BT_SCRIPT_PATH,
|
BT_SCRIPT_PATH,
|
||||||
arg1,
|
srcFile,
|
||||||
arg2,
|
detFile,
|
||||||
JSON_PATH+arg3,
|
name,
|
||||||
arg4
|
type);
|
||||||
).redirectErrorStream(true);
|
// 设置工作目录
|
||||||
|
pb.directory(scriptFile.getParentFile());
|
||||||
|
pb.redirectErrorStream(true);
|
||||||
|
|
||||||
// 启动进程
|
// 启动进程
|
||||||
Process process = pb.start();
|
Process process = pb.start();
|
||||||
// 启动新线程读取输出,避免阻塞
|
// 启动新线程读取输出,避免阻塞
|
||||||
|
|
@ -46,13 +61,10 @@ public class ImageVirtualUtils {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
// 对于start命令,只要进程启动成功就返回true
|
|
||||||
|
|
||||||
// 稍微等待一下看初始输出是否有错误
|
// 稍微等待一下看初始输出是否有错误
|
||||||
Thread.sleep(WAIT_START_TIME);
|
Thread.sleep(WAIT_START_TIME);
|
||||||
return process.isAlive(); // 如果进程还在运行,认为启动成功
|
return process.isAlive(); // 如果进程还在运行,认为启动成功
|
||||||
|
|
||||||
|
|
||||||
} catch (IOException | InterruptedException e) {
|
} catch (IOException | InterruptedException e) {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
return false;
|
return false;
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue