2
This commit is contained in:
1 parent
cce34e7960
commit
b49e096355
56 files changed
+8343
-296
No files matched your search
@@ -1,9 +1,15 @@
|
||||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'dart:ffi';
|
||||
import 'dart:io';
|
||||
import 'package:archive/archive.dart';
|
||||
import 'dart:isolate';
|
||||
import 'package:archive/archive_io.dart';
|
||||
import 'package:flutter/foundation.dart';
|
||||
import 'package:flutter/services.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'low_memory_xz_decoder.dart';
|
||||
|
||||
final RegExp _paxRecordRegexp = RegExp(r"(\d+) (\w+)=(.*)");
|
||||
|
||||
/// 安装进度回调
|
||||
/// [progress] 0.0 ~ 1.0
|
||||
@@ -19,16 +25,99 @@ class DistroInstallCancelledException implements Exception {
|
||||
String toString() => message;
|
||||
}
|
||||
|
||||
class _ExtractIsolateParams {
|
||||
final String archivePath;
|
||||
final String targetDirPath;
|
||||
final SendPort sendPort;
|
||||
|
||||
const _ExtractIsolateParams({
|
||||
required this.archivePath,
|
||||
required this.targetDirPath,
|
||||
required this.sendPort,
|
||||
});
|
||||
}
|
||||
|
||||
/// 流式 TAR 块头部结构解析器
|
||||
class _TarHeader {
|
||||
final String filename;
|
||||
final int mode;
|
||||
final int fileSize;
|
||||
final String typeFlag;
|
||||
final String? nameOfLinkedFile;
|
||||
|
||||
_TarHeader({
|
||||
required this.filename,
|
||||
required this.mode,
|
||||
required this.fileSize,
|
||||
required this.typeFlag,
|
||||
this.nameOfLinkedFile,
|
||||
});
|
||||
|
||||
static _TarHeader? read(InputStreamBase input) {
|
||||
if (input.isEOS) return null;
|
||||
final header = input.readBytes(512);
|
||||
if (header.length < 512) return null;
|
||||
final bytes = header.toUint8List();
|
||||
|
||||
var allZero = true;
|
||||
for (var i = 0; i < 512; i++) {
|
||||
if (bytes[i] != 0) {
|
||||
allZero = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
if (allZero) return null;
|
||||
|
||||
String parseString(int offset, int length) {
|
||||
var end = offset;
|
||||
final max = offset + length;
|
||||
while (end < max && bytes[end] != 0) {
|
||||
end++;
|
||||
}
|
||||
return utf8.decode(bytes.sublist(offset, end), allowMalformed: true);
|
||||
}
|
||||
|
||||
int parseInt(int offset, int length) {
|
||||
final s = parseString(offset, length).trim();
|
||||
if (s.isEmpty) return 0;
|
||||
return int.tryParse(s, radix: 8) ?? 0;
|
||||
}
|
||||
|
||||
var filename = parseString(0, 100);
|
||||
final mode = parseInt(100, 8);
|
||||
final fileSize = parseInt(124, 12);
|
||||
final typeFlag = parseString(156, 1);
|
||||
String? nameOfLinkedFile = parseString(157, 100);
|
||||
if (nameOfLinkedFile.isEmpty) nameOfLinkedFile = null;
|
||||
|
||||
final ustarIndicator = parseString(257, 6);
|
||||
if (ustarIndicator == 'ustar') {
|
||||
final filenamePrefix = parseString(345, 155);
|
||||
if (filenamePrefix.isNotEmpty) {
|
||||
filename = '$filenamePrefix/$filename';
|
||||
}
|
||||
}
|
||||
|
||||
return _TarHeader(
|
||||
filename: filename,
|
||||
mode: mode,
|
||||
fileSize: fileSize,
|
||||
typeFlag: typeFlag,
|
||||
nameOfLinkedFile: nameOfLinkedFile,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
/// Linux 发行版 Rootfs 安装与解压服务
|
||||
class DistroInstaller {
|
||||
/// 解压并安装 tar.gz 格式的 Rootfs 到目标目录
|
||||
/// 从本地文件流式解压并安装 Rootfs(低内存、防卡顿、支持 XZ/GZip/Tar)
|
||||
///
|
||||
/// [tarGzBytes] .tar.gz 压缩包字节流
|
||||
/// [archiveFile] 压缩包文件 (.tar.xz 或 .tar.gz)
|
||||
/// [targetDir] 目标解压目录 (即容器的 rootfs 路径)
|
||||
/// [onProgress] 进度回调
|
||||
/// [isCancelled] 外部取消检查函数
|
||||
static Future<void> installFromBytes({
|
||||
required Uint8List tarGzBytes,
|
||||
static Future<void> installFromFile({
|
||||
required File archiveFile,
|
||||
required Directory targetDir,
|
||||
InstallProgressCallback? onProgress,
|
||||
bool Function()? isCancelled,
|
||||
@@ -37,88 +126,30 @@ class DistroInstaller {
|
||||
throw DistroInstallCancelledException();
|
||||
}
|
||||
|
||||
onProgress?.call(0.05, '正在校验与解析系统镜像...');
|
||||
if (!archiveFile.existsSync()) {
|
||||
throw ArgumentError('压缩包文件不存在: ${archiveFile.path}');
|
||||
}
|
||||
|
||||
onProgress?.call(0.05, '正在校验与准备解压目录...');
|
||||
|
||||
if (!targetDir.existsSync()) {
|
||||
targetDir.createSync(recursive: true);
|
||||
}
|
||||
|
||||
try {
|
||||
// 1. 在后台解压 Gzip 与 Tar(避免 UI 线程卡顿)
|
||||
onProgress?.call(0.15, '正在解压内核与文件系统结构...');
|
||||
final archive = await compute(_decodeTarGz, tarGzBytes);
|
||||
// 全平台统一使用低内存流式 Isolate 解压引擎(稳定、确定性进度、低内存、零外部依赖)
|
||||
await _extractWithDartStreamIsolate(
|
||||
archiveFile: archiveFile,
|
||||
targetDir: targetDir,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
|
||||
if (isCancelled?.call() == true) {
|
||||
throw DistroInstallCancelledException();
|
||||
}
|
||||
|
||||
// 2. 写入文件与目录
|
||||
//
|
||||
// 权限位必须逐条按 tar 头恢复:dart:io 没有 chmod,而解压本身不会保留
|
||||
// 可执行位,所以恢复方式是按 mode 分组批量调用 chmod(避免逐文件 fork)。
|
||||
// 之前这里用 `chmod -R 755` 一把梭,会把 /etc 下的配置文件也变成可执行。
|
||||
final total = archive.length;
|
||||
int processed = 0;
|
||||
final Map<int, List<String>> byMode = <int, List<String>>{};
|
||||
|
||||
for (final file in archive) {
|
||||
if (isCancelled?.call() == true) {
|
||||
throw DistroInstallCancelledException();
|
||||
}
|
||||
|
||||
processed++;
|
||||
final filename = file.name;
|
||||
// 避免路径穿越漏洞 (Directory Traversal)
|
||||
if (filename.contains('..')) continue;
|
||||
|
||||
final outPath = p.join(targetDir.path, filename);
|
||||
|
||||
if (file.isFile) {
|
||||
final parentDir = Directory(p.dirname(outPath));
|
||||
if (!parentDir.existsSync()) {
|
||||
parentDir.createSync(recursive: true);
|
||||
}
|
||||
|
||||
// 处理符号链接与常规文件
|
||||
if (file.isSymbolicLink) {
|
||||
try {
|
||||
final link = Link(outPath);
|
||||
if (link.existsSync()) link.deleteSync();
|
||||
link.createSync(file.nameOfLinkedFile);
|
||||
} catch (_) {
|
||||
// Windows 或受限环境降级为写入目标路径文本或忽略
|
||||
}
|
||||
} else {
|
||||
final content = file.content as List<int>;
|
||||
File(outPath).writeAsBytesSync(content, flush: false);
|
||||
}
|
||||
} else {
|
||||
Directory(outPath).createSync(recursive: true);
|
||||
}
|
||||
|
||||
// 记录 tar 头里的权限位(仅低 12 位有效位)。
|
||||
// 符号链接必须跳过:chmod 会跟随链接,把目标文件的权限改坏
|
||||
// (tar 里 symlink 条目的 mode 固定为 0777)。
|
||||
final mode = file.mode & 0xFFF;
|
||||
if (mode != 0 && !file.isSymbolicLink) {
|
||||
byMode.putIfAbsent(mode, () => <String>[]).add(outPath);
|
||||
}
|
||||
|
||||
if (processed % 50 == 0 || processed == total) {
|
||||
final percent = 0.20 + (processed / total) * 0.70; // 20% ~ 90%
|
||||
onProgress?.call(percent, '正在释放系统文件 ($processed/$total)...');
|
||||
}
|
||||
}
|
||||
|
||||
if (!Platform.isWindows) {
|
||||
await _restoreModes(byMode);
|
||||
}
|
||||
|
||||
if (isCancelled?.call() == true) {
|
||||
throw DistroInstallCancelledException();
|
||||
}
|
||||
|
||||
// 3. 执行系统配置与网络补丁
|
||||
// 阶段 3:执行系统配置与网络补丁
|
||||
onProgress?.call(0.92, '正在配置网络 DNS 与系统环境...');
|
||||
await postInstallConfigure(targetDir);
|
||||
|
||||
@@ -134,12 +165,334 @@ class DistroInstaller {
|
||||
}
|
||||
}
|
||||
|
||||
/// 后台 Isolate 解压 tar.gz
|
||||
static Archive _decodeTarGz(Uint8List bytes) {
|
||||
final tarData = GZipDecoder().decodeBytes(bytes);
|
||||
return TarDecoder().decodeBytes(tarData);
|
||||
/// 解压并安装内存字节流格式的 Rootfs 到目标目录(兼容已有内置 asset 资源包接口)
|
||||
static Future<void> installFromBytes({
|
||||
required Uint8List tarGzBytes,
|
||||
required Directory targetDir,
|
||||
InstallProgressCallback? onProgress,
|
||||
bool Function()? isCancelled,
|
||||
}) async {
|
||||
final tempDir = Directory.systemTemp.createTempSync('distro_temp_');
|
||||
try {
|
||||
final tempFile = File(p.join(tempDir.path, 'archive.bin'));
|
||||
await tempFile.writeAsBytes(tarGzBytes, flush: true);
|
||||
await installFromFile(
|
||||
archiveFile: tempFile,
|
||||
targetDir: targetDir,
|
||||
onProgress: onProgress,
|
||||
isCancelled: isCancelled,
|
||||
);
|
||||
} finally {
|
||||
if (tempDir.existsSync()) {
|
||||
try {
|
||||
tempDir.deleteSync(recursive: true);
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 在独立后台 Isolate 中执行流式解压与逐文件直接落盘(避免生成几十万对象引发 OOM)
|
||||
static Future<void> _extractWithDartStreamIsolate({
|
||||
required File archiveFile,
|
||||
required Directory targetDir,
|
||||
InstallProgressCallback? onProgress,
|
||||
bool Function()? isCancelled,
|
||||
}) async {
|
||||
final receivePort = ReceivePort();
|
||||
final exitPort = ReceivePort();
|
||||
final completer = Completer<Map<int, List<String>>>();
|
||||
Isolate? isolate;
|
||||
Timer? cancelTimer;
|
||||
|
||||
try {
|
||||
isolate = await Isolate.spawn<_ExtractIsolateParams>(
|
||||
_extractIsolateEntry,
|
||||
_ExtractIsolateParams(
|
||||
archivePath: archiveFile.path,
|
||||
targetDirPath: targetDir.path,
|
||||
sendPort: receivePort.sendPort,
|
||||
),
|
||||
);
|
||||
|
||||
isolate.addOnExitListener(exitPort.sendPort);
|
||||
exitPort.listen((_) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.completeError(Exception('解压引擎意外退出'));
|
||||
}
|
||||
});
|
||||
|
||||
cancelTimer = Timer.periodic(const Duration(milliseconds: 100), (_) {
|
||||
if (isCancelled?.call() == true) {
|
||||
isolate?.kill(priority: Isolate.immediate);
|
||||
if (!completer.isCompleted) {
|
||||
completer.completeError(DistroInstallCancelledException());
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
receivePort.listen((message) {
|
||||
if (message is List) {
|
||||
final type = message[0] as int;
|
||||
if (type == 0) {
|
||||
final prog = message[1] as double;
|
||||
final msg = message[2] as String;
|
||||
onProgress?.call(prog, msg);
|
||||
} else if (type == 1) {
|
||||
final byMode = (message[1] as Map).cast<int, List<String>>();
|
||||
if (!completer.isCompleted) {
|
||||
completer.complete(byMode);
|
||||
}
|
||||
} else if (type == 3) {
|
||||
if (!completer.isCompleted) {
|
||||
completer.completeError(Exception(message[1].toString()));
|
||||
}
|
||||
}
|
||||
}
|
||||
});
|
||||
|
||||
final byMode = await completer.future;
|
||||
|
||||
if (!Platform.isWindows) {
|
||||
await _restoreModes(byMode);
|
||||
}
|
||||
} finally {
|
||||
cancelTimer?.cancel();
|
||||
receivePort.close();
|
||||
exitPort.close();
|
||||
isolate?.kill(priority: Isolate.immediate);
|
||||
}
|
||||
}
|
||||
|
||||
/// Isolate 入口:低内存流式解压与逐文件直接落盘
|
||||
static Future<void> _extractIsolateEntry(_ExtractIsolateParams params) async {
|
||||
File? tempTarFile;
|
||||
try {
|
||||
params.sendPort.send([0, 0.10, '正在读取并解析系统压缩包...']);
|
||||
final archiveFile = File(params.archivePath);
|
||||
if (!archiveFile.existsSync()) {
|
||||
params.sendPort.send([3, '压缩包文件不存在: ${params.archivePath}']);
|
||||
return;
|
||||
}
|
||||
|
||||
// 1. 检查头部魔数判断文件格式
|
||||
final raf = archiveFile.openSync(mode: FileMode.read);
|
||||
final magic = raf.readSync(6);
|
||||
raf.closeSync();
|
||||
|
||||
final isXz = magic.length >= 6 &&
|
||||
magic[0] == 0xFD &&
|
||||
magic[1] == 0x37 &&
|
||||
magic[2] == 0x7A &&
|
||||
magic[3] == 0x58 &&
|
||||
magic[4] == 0x5A &&
|
||||
magic[5] == 0x00;
|
||||
|
||||
final isGz = magic.length >= 2 && magic[0] == 0x1F && magic[1] == 0x8B;
|
||||
|
||||
final targetDir = Directory(params.targetDirPath);
|
||||
final tempDir = targetDir.parent.existsSync()
|
||||
? targetDir.parent
|
||||
: Directory.systemTemp;
|
||||
|
||||
File tarToExtract;
|
||||
if (isXz) {
|
||||
params.sendPort.send([0, 0.12, '正在流式解压内核与文件系统结构 (XZ)...']);
|
||||
tempTarFile = File(p.join(
|
||||
tempDir.path,
|
||||
'temp_distro_extract_${DateTime.now().microsecondsSinceEpoch}.tar',
|
||||
));
|
||||
if (tempTarFile.existsSync()) {
|
||||
try {
|
||||
tempTarFile.deleteSync();
|
||||
} catch (_) {}
|
||||
}
|
||||
final tarSink = tempTarFile.openWrite();
|
||||
final inputStream = InputFileStream(archiveFile.path);
|
||||
final archiveSize = archiveFile.lengthSync();
|
||||
|
||||
try {
|
||||
final decoder = LowMemoryXZDecoder();
|
||||
decoder.decodeToSink(
|
||||
inputStream,
|
||||
tarSink,
|
||||
onProgress: (decompressed, compPos) {
|
||||
final frac = archiveSize > 0
|
||||
? (compPos / archiveSize).clamp(0.0, 1.0)
|
||||
: 0.0;
|
||||
final prog = 0.12 + frac * 0.38; // 0.12 ~ 0.50
|
||||
final mb = (decompressed / (1024 * 1024)).toStringAsFixed(1);
|
||||
params.sendPort.send([0, prog, '正在解压系统文件 ($mb MB)...']);
|
||||
},
|
||||
);
|
||||
} finally {
|
||||
await tarSink.flush();
|
||||
await tarSink.close();
|
||||
await inputStream.close();
|
||||
}
|
||||
tarToExtract = tempTarFile;
|
||||
} else if (isGz) {
|
||||
params.sendPort.send([0, 0.12, '正在流式解压内核与文件系统结构 (GZip)...']);
|
||||
tempTarFile = File(p.join(
|
||||
tempDir.path,
|
||||
'temp_distro_extract_${DateTime.now().microsecondsSinceEpoch}.tar',
|
||||
));
|
||||
if (tempTarFile.existsSync()) {
|
||||
try {
|
||||
tempTarFile.deleteSync();
|
||||
} catch (_) {}
|
||||
}
|
||||
final tarSink = tempTarFile.openWrite();
|
||||
try {
|
||||
await archiveFile.openRead().transform(gzip.decoder).pipe(tarSink);
|
||||
} finally {
|
||||
await tarSink.close();
|
||||
}
|
||||
tarToExtract = tempTarFile;
|
||||
} else {
|
||||
tarToExtract = archiveFile;
|
||||
}
|
||||
|
||||
// 2. 流式释放 TAR 文件到目标根目录
|
||||
params.sendPort.send([0, 0.50, '正在释放系统文件...']);
|
||||
final byMode = await _streamExtractTarFile(
|
||||
tarFile: tarToExtract,
|
||||
targetDirPath: params.targetDirPath,
|
||||
sendPort: params.sendPort,
|
||||
progressStart: 0.50,
|
||||
progressEnd: 0.90,
|
||||
);
|
||||
|
||||
params.sendPort.send([1, byMode]);
|
||||
} catch (e) {
|
||||
params.sendPort.send([3, e.toString()]);
|
||||
} finally {
|
||||
if (tempTarFile != null && tempTarFile.existsSync()) {
|
||||
try {
|
||||
tempTarFile.deleteSync();
|
||||
} catch (_) {}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// 流式逐条解析 TAR 文件并直接落盘
|
||||
static Future<Map<int, List<String>>> _streamExtractTarFile({
|
||||
required File tarFile,
|
||||
required String targetDirPath,
|
||||
required SendPort sendPort,
|
||||
required double progressStart,
|
||||
required double progressEnd,
|
||||
}) async {
|
||||
final input = InputFileStream(tarFile.path);
|
||||
final byMode = <int, List<String>>{};
|
||||
final totalLen = tarFile.lengthSync();
|
||||
String? nextName;
|
||||
String? nextLinkName;
|
||||
int processed = 0;
|
||||
|
||||
try {
|
||||
while (!input.isEOS) {
|
||||
final th = _TarHeader.read(input);
|
||||
if (th == null) break;
|
||||
|
||||
if (th.filename == '././@LongLink') {
|
||||
final contentBytes = input.readBytes(th.fileSize).toUint8List();
|
||||
final remainder = th.fileSize % 512;
|
||||
if (remainder != 0) input.skip(512 - remainder);
|
||||
final text = utf8.decode(contentBytes, allowMalformed: true).split('\x00').first;
|
||||
if (th.typeFlag == 'K') {
|
||||
nextLinkName = text;
|
||||
} else {
|
||||
nextName = text;
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
if (th.typeFlag == 'g' || th.typeFlag == 'x') {
|
||||
// PAX Header
|
||||
try {
|
||||
final paxBytes = input.readBytes(th.fileSize).toUint8List();
|
||||
final remainder = th.fileSize % 512;
|
||||
if (remainder != 0) input.skip(512 - remainder);
|
||||
final headerContent = utf8.decode(paxBytes, allowMalformed: true);
|
||||
for (final line in headerContent.split('\n')) {
|
||||
final match = _paxRecordRegexp.firstMatch(line);
|
||||
if (match != null) {
|
||||
final k = match.group(2);
|
||||
final v = match.group(3);
|
||||
if (k == 'path' && v != null) nextName = v;
|
||||
if (k == 'linkpath' && v != null) nextLinkName = v;
|
||||
}
|
||||
}
|
||||
} catch (_) {}
|
||||
continue;
|
||||
}
|
||||
|
||||
final filename = nextName ?? th.filename;
|
||||
nextName = null;
|
||||
final linkName = nextLinkName ?? th.nameOfLinkedFile;
|
||||
nextLinkName = null;
|
||||
|
||||
if (filename.contains('..')) {
|
||||
if (th.fileSize > 0) {
|
||||
input.skip(th.fileSize);
|
||||
final remainder = th.fileSize % 512;
|
||||
if (remainder != 0) input.skip(512 - remainder);
|
||||
}
|
||||
continue;
|
||||
}
|
||||
|
||||
final outPath = p.join(targetDirPath, filename);
|
||||
|
||||
if (th.typeFlag == '5' || th.filename.endsWith('/')) {
|
||||
final d = Directory(outPath);
|
||||
if (!d.existsSync()) d.createSync(recursive: true);
|
||||
} else if ((th.typeFlag == '2' || th.typeFlag == '1') && linkName != null) {
|
||||
try {
|
||||
final parent = Directory(p.dirname(outPath));
|
||||
if (!parent.existsSync()) parent.createSync(recursive: true);
|
||||
final link = Link(outPath);
|
||||
if (link.existsSync()) link.deleteSync();
|
||||
link.createSync(linkName);
|
||||
} catch (_) {}
|
||||
} else {
|
||||
final parent = Directory(p.dirname(outPath));
|
||||
if (!parent.existsSync()) parent.createSync(recursive: true);
|
||||
|
||||
final f = File(outPath);
|
||||
final sink = f.openWrite();
|
||||
var remaining = th.fileSize;
|
||||
while (remaining > 0) {
|
||||
final chunk = remaining < 65536 ? remaining : 65536;
|
||||
sink.add(input.readBytes(chunk).toUint8List());
|
||||
remaining -= chunk;
|
||||
}
|
||||
await sink.flush();
|
||||
await sink.close();
|
||||
|
||||
final remainder = th.fileSize % 512;
|
||||
if (remainder != 0) input.skip(512 - remainder);
|
||||
}
|
||||
|
||||
final mode = th.mode & 0xFFF;
|
||||
if (mode != 0 && th.typeFlag != '2') {
|
||||
byMode.putIfAbsent(mode, () => <String>[]).add(outPath);
|
||||
}
|
||||
|
||||
processed++;
|
||||
if (processed % 150 == 0) {
|
||||
final fraction = totalLen > 0 ? (input.position / totalLen).clamp(0.0, 1.0) : 0.0;
|
||||
final prog = progressStart + fraction * (progressEnd - progressStart);
|
||||
sendPort.send([0, prog, '正在释放系统文件 ($processed)...']);
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
await input.close();
|
||||
}
|
||||
|
||||
return byMode;
|
||||
}
|
||||
|
||||
|
||||
/// 按 tar 头恢复权限位(dart:io 无 chmod,按 mode 分组批量调用,避免逐文件 fork)
|
||||
static Future<void> _restoreModes(Map<int, List<String>> byMode) async {
|
||||
for (final entry in byMode.entries) {
|
||||
@@ -197,34 +550,171 @@ class DistroInstaller {
|
||||
debugPrint('[DistroInstaller] 写入 hosts 失败: $e');
|
||||
}
|
||||
|
||||
// 3. 配置 Alpine apk 软件源镜像(使用阿里云高速稳定镜像)
|
||||
// 3. 配置软件源镜像(按发行版自动适配,国内高速镜像)
|
||||
final apkDir = Directory(p.join(etcDir.path, 'apk'));
|
||||
if (apkDir.existsSync()) {
|
||||
// 3.1 Alpine apk 软件源镜像(使用阿里云高速镜像,自适应主版本号)
|
||||
var alpineVer = 'v3.20';
|
||||
final alpineRel = File(p.join(etcDir.path, 'alpine-release'));
|
||||
if (alpineRel.existsSync()) {
|
||||
try {
|
||||
final relStr = alpineRel.readAsStringSync().trim();
|
||||
final match = RegExp(r'^(\d+\.\d+)').firstMatch(relStr);
|
||||
if (match != null) {
|
||||
alpineVer = 'v${match.group(1)}';
|
||||
}
|
||||
} catch (_) {}
|
||||
}
|
||||
final repoFile = File(p.join(apkDir.path, 'repositories'));
|
||||
try {
|
||||
repoFile.writeAsStringSync(
|
||||
'https://mirrors.aliyun.com/alpine/v3.20/main\n'
|
||||
'https://mirrors.aliyun.com/alpine/v3.20/community\n',
|
||||
'https://mirrors.aliyun.com/alpine/$alpineVer/main\n'
|
||||
'https://mirrors.aliyun.com/alpine/$alpineVer/community\n',
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 写入 repositories 失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 4. 确保 /workspace 挂载点目录存在
|
||||
// 4. 自动部署根证书(解决最小化 Linux 发行版缺少 ca-certificates 导致 https 证书验证失败)
|
||||
await ensureCaCertificates(rootfsDir);
|
||||
|
||||
final aptDir = Directory(p.join(etcDir.path, 'apt'));
|
||||
if (aptDir.existsSync()) {
|
||||
final sourcesD = Directory(p.join(aptDir.path, 'sources.list.d'));
|
||||
final ubuntuSources = File(p.join(sourcesD.path, 'ubuntu.sources'));
|
||||
final isUbuntu = ubuntuSources.existsSync() ||
|
||||
(File(p.join(etcDir.path, 'os-release')).existsSync() &&
|
||||
File(p.join(etcDir.path, 'os-release')).readAsStringSync().toLowerCase().contains('ubuntu'));
|
||||
|
||||
if (isUbuntu) {
|
||||
// 3.2 Ubuntu deb822 源配置(清华大学高速镜像)
|
||||
if (!sourcesD.existsSync()) sourcesD.createSync(recursive: true);
|
||||
try {
|
||||
final isArm = (Abi.current() == Abi.androidArm64 || Abi.current() == Abi.linuxArm64);
|
||||
final mirrorUri = isArm
|
||||
? 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu-ports/'
|
||||
: 'https://mirrors.tuna.tsinghua.edu.cn/ubuntu/';
|
||||
ubuntuSources.writeAsStringSync(
|
||||
'Types: deb\n'
|
||||
'URIs: $mirrorUri\n'
|
||||
'Suites: noble noble-updates noble-backports noble-security\n'
|
||||
'Components: main universe restricted multiverse\n'
|
||||
'Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg\n',
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 写入 ubuntu.sources 失败: $e');
|
||||
}
|
||||
} else {
|
||||
// 3.3 Debian 源配置(清华大学高速镜像)
|
||||
final debianSources = File(p.join(sourcesD.path, 'debian.sources'));
|
||||
final sourcesList = File(p.join(aptDir.path, 'sources.list'));
|
||||
try {
|
||||
sourcesList.writeAsStringSync(
|
||||
'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm main contrib non-free non-free-firmware\n'
|
||||
'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-updates main contrib non-free non-free-firmware\n'
|
||||
'deb https://mirrors.tuna.tsinghua.edu.cn/debian/ bookworm-backports main contrib non-free non-free-firmware\n'
|
||||
'deb https://mirrors.tuna.tsinghua.edu.cn/debian-security bookworm-security main contrib non-free non-free-firmware\n',
|
||||
);
|
||||
if (debianSources.existsSync()) {
|
||||
debianSources.writeAsStringSync(
|
||||
'Types: deb\n'
|
||||
'URIs: https://mirrors.tuna.tsinghua.edu.cn/debian/\n'
|
||||
'Suites: bookworm bookworm-updates bookworm-backports\n'
|
||||
'Components: main contrib non-free non-free-firmware\n'
|
||||
'Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg\n'
|
||||
'\n'
|
||||
'Types: deb\n'
|
||||
'URIs: https://mirrors.tuna.tsinghua.edu.cn/debian-security\n'
|
||||
'Suites: bookworm-security\n'
|
||||
'Components: main contrib non-free non-free-firmware\n'
|
||||
'Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg\n',
|
||||
);
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 写入 debian sources 失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 3.4 Arch Linux pacman 源配置(清华大学高速镜像)
|
||||
final pacmanDir = Directory(p.join(etcDir.path, 'pacman.d'));
|
||||
if (pacmanDir.existsSync()) {
|
||||
final mirrorlist = File(p.join(pacmanDir.path, 'mirrorlist'));
|
||||
try {
|
||||
mirrorlist.writeAsStringSync(
|
||||
'Server = https://mirrors.tuna.tsinghua.edu.cn/archlinux/\$repo/os/\$arch\n'
|
||||
'Server = https://mirrors.aliyun.com/archlinux/\$repo/os/\$arch\n',
|
||||
);
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 写入 Arch mirrorlist 失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
// 3.5 Fedora yum/dnf 源配置(清华大学高速镜像)
|
||||
final yumDir = Directory(p.join(etcDir.path, 'yum.repos.d'));
|
||||
if (yumDir.existsSync()) {
|
||||
final fedoraRepo = File(p.join(yumDir.path, 'fedora.repo'));
|
||||
if (fedoraRepo.existsSync()) {
|
||||
try {
|
||||
var content = fedoraRepo.readAsStringSync();
|
||||
content = content.replaceAll('metalink=', '#metalink=');
|
||||
content = content.replaceAll(
|
||||
'#baseurl=http://download.example/pub/fedora/linux/',
|
||||
'baseurl=https://mirrors.tuna.tsinghua.edu.cn/fedora/',
|
||||
);
|
||||
fedoraRepo.writeAsStringSync(content);
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 写入 Fedora repo 失败: $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// 5. 确保 /workspace 挂载点目录存在
|
||||
final workspaceDir = Directory(p.join(rootfsDir.path, 'workspace'));
|
||||
if (!workspaceDir.existsSync()) {
|
||||
workspaceDir.createSync(recursive: true);
|
||||
}
|
||||
|
||||
// 5. 确保 /tmp 目录存在
|
||||
// 6. 确保 /tmp 目录存在
|
||||
final tmpDir = Directory(p.join(rootfsDir.path, 'tmp'));
|
||||
if (!tmpDir.existsSync()) {
|
||||
tmpDir.createSync(recursive: true);
|
||||
}
|
||||
|
||||
// 6. 部署容器修复 Shim(libfix_seccomp.so)
|
||||
// 7. 部署容器修复 Shim(libfix_seccomp.so)
|
||||
await ensureSeccompShim(rootfsDir);
|
||||
|
||||
// 8. 自动配置 Java 运行时动态链接路径
|
||||
await ensureJavaConfiguration(rootfsDir);
|
||||
}
|
||||
|
||||
/// 自动部署标准 SSL CA 证书链(让 APT / Curl / Git 默认支持 HTTPS)
|
||||
static Future<void> ensureCaCertificates(Directory rootfsDir) async {
|
||||
try {
|
||||
final certsDir = Directory(p.join(rootfsDir.path, 'etc', 'ssl', 'certs'));
|
||||
if (!certsDir.existsSync()) {
|
||||
certsDir.createSync(recursive: true);
|
||||
}
|
||||
final targetCert = File(p.join(certsDir.path, 'ca-certificates.crt'));
|
||||
if (!targetCert.existsSync() || targetCert.lengthSync() == 0) {
|
||||
final byteData = await rootBundle.load('assets/certs/ca-certificates.crt');
|
||||
final bytes = byteData.buffer.asUint8List(byteData.offsetInBytes, byteData.lengthInBytes);
|
||||
await targetCert.writeAsBytes(bytes, flush: true);
|
||||
|
||||
// 建立标准符号链接以兼容 OpenSSL/GnuTLS 默认寻找路径
|
||||
final etcSslDir = Directory(p.join(rootfsDir.path, 'etc', 'ssl'));
|
||||
final certPem = Link(p.join(etcSslDir.path, 'cert.pem'));
|
||||
if (!certPem.existsSync()) {
|
||||
try {
|
||||
certPem.createSync('certs/ca-certificates.crt');
|
||||
} catch (_) {}
|
||||
}
|
||||
debugPrint('[DistroInstaller] 成功注入根证书链: ${targetCert.path}');
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 注入根证书链异常 (非阻塞): $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 部署容器修复 Shim(libfix_seccomp.so)
|
||||
@@ -237,8 +727,10 @@ class DistroInstaller {
|
||||
/// 传统 syscall 返回 ENOSYS,而 musl 在有这些调用的架构上优先用它们。
|
||||
static Future<void> ensureSeccompShim(Directory rootfsDir) async {
|
||||
final libDir = Directory(p.join(rootfsDir.path, 'lib'));
|
||||
if (!libDir.existsSync()) {
|
||||
libDir.createSync(recursive: true);
|
||||
if (!libDir.existsSync() && !FileSystemEntity.isLinkSync(libDir.path)) {
|
||||
try {
|
||||
libDir.createSync(recursive: true);
|
||||
} catch (_) {}
|
||||
}
|
||||
final targetShim = File(p.join(libDir.path, 'libfix_seccomp.so'));
|
||||
|
||||
@@ -272,4 +764,44 @@ class DistroInstaller {
|
||||
debugPrint('[DistroInstaller] 部署 SECCOMP Shim 失败: $e');
|
||||
}
|
||||
}
|
||||
|
||||
/// 自动探测并适配 OpenJDK 运行环境(解决 Debian/Ubuntu 等发行版下 /usr/bin/java
|
||||
/// 符号链接调用时找不到 libjli.so / libjvm.so 的问题)
|
||||
static Future<void> ensureJavaConfiguration(Directory rootfsDir) async {
|
||||
try {
|
||||
final jvmDir = Directory(p.join(rootfsDir.path, 'usr', 'lib', 'jvm'));
|
||||
if (!jvmDir.existsSync()) return;
|
||||
|
||||
final javaConfLines = <String>[];
|
||||
final jvmEntries = jvmDir.listSync();
|
||||
for (final entry in jvmEntries) {
|
||||
if (entry is Directory) {
|
||||
final guestJvmBase = '/usr/lib/jvm/${p.basename(entry.path)}';
|
||||
final libDir = Directory(p.join(entry.path, 'lib'));
|
||||
if (libDir.existsSync()) {
|
||||
javaConfLines.add('$guestJvmBase/lib');
|
||||
final serverDir = Directory(p.join(libDir.path, 'server'));
|
||||
if (serverDir.existsSync()) {
|
||||
javaConfLines.add('$guestJvmBase/lib/server');
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (javaConfLines.isNotEmpty) {
|
||||
final ldConfD = Directory(p.join(rootfsDir.path, 'etc', 'ld.so.conf.d'));
|
||||
if (!ldConfD.existsSync()) {
|
||||
ldConfD.createSync(recursive: true);
|
||||
}
|
||||
final javaConfFile = File(p.join(ldConfD.path, 'java.conf'));
|
||||
final newContent = '${javaConfLines.join('\n')}\n';
|
||||
if (!javaConfFile.existsSync() || javaConfFile.readAsStringSync() != newContent) {
|
||||
javaConfFile.writeAsStringSync(newContent);
|
||||
debugPrint('[DistroInstaller] 自动配置 /etc/ld.so.conf.d/java.conf');
|
||||
}
|
||||
}
|
||||
} catch (e) {
|
||||
debugPrint('[DistroInstaller] 配置 Java ld.so 失败 (非阻塞): $e');
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user