Skip to content

Commit

Permalink
Support setup by script
Browse files Browse the repository at this point in the history
  • Loading branch information
impactaky committed Jan 27, 2024
1 parent 66f5e3d commit 87c077a
Show file tree
Hide file tree
Showing 13 changed files with 81 additions and 57 deletions.
8 changes: 3 additions & 5 deletions docker/arm64-jammy.dockerfile
Expand Up @@ -53,8 +53,6 @@ RUN mkdir -p /mimic-cross/internal/bin \
&& ln -s ../../../usr/bin/readelf /mimic-cross/internal/bin \
&& ln -s ../../../usr/sbin/chroot /mimic-cross/internal/bin

# COPY host /mimic-cross/host

# =======================================================================

FROM host-stage1 as mimic-test-host
Expand Down Expand Up @@ -92,10 +90,10 @@ RUN /mimic-cross/mimic-cross.deno/setup.sh
COPY --from=mimic-test-host /test /test
ENV MIMIC_TEST_DATA_PATH=/test

COPY mimic-cross.deno /mimic-cross.deno
WORKDIR /mimic-cross.deno
# COPY mimic-cross.deno /mimic-cross.deno
# WORKDIR /mimic-cross.deno
ENV PATH="/mimic-cross/mimic-cross/bin:$PATH"
RUN mimic-deno cache config/*.test.ts src/*.test.ts
# RUN mimic-deno cache config/*.test.ts src/*.test.ts

# =======================================================================

Expand Down
4 changes: 2 additions & 2 deletions mimic-cross.deno/apt/apt.test.ts
Expand Up @@ -6,7 +6,7 @@ import {
import { checkNeeded, getElfArch } from "../src/util.ts";
import $ from "dax/mod.ts";
import { assert, assertEquals } from "std/assert/mod.ts";
import { config } from "config/config.ts";
import { config } from "../config/config.ts";

Deno.test("deployPackages coreutils", async () => {
await deployPackages(["coreutils"]);
Expand Down Expand Up @@ -38,7 +38,7 @@ Deno.test("getIntalledPackagesFromLog not found", async () => {

Deno.test("findCommandsFromPackage(bin)", async () => {
const commands = await findCommandsFromPackage("grep");
assertEquals(commands, ["bin/grep"]);
assertEquals(commands, ["/bin/grep"]);
});

Deno.test("findCommandsFromPackage(lib)", async () => {
Expand Down
2 changes: 1 addition & 1 deletion mimic-cross.deno/apt/helper.ts
Expand Up @@ -6,7 +6,7 @@ export { deployPackageCommands } from "../apt/apt.ts";
export async function deployCli(command: string, target: PathRefLike) {
const pathRef = $.path(target);
await pathRef.writeText(`#!/bin/sh
/mimic-cross.deno/src/mimicx.ts ${command} -- $@
/usr/local/bin/mimicx ${command} -- $@
`);
await pathRef.chmod(0o755);
}
10 changes: 10 additions & 0 deletions mimic-cross.deno/apt/packages/apt.ts
@@ -0,0 +1,10 @@
import $ from "daxex/mod.ts";
import { deployCli } from "../../apt/helper.ts";
import { keepOriginalBin } from "../../src/deploy.ts";

export async function postInstall() {
const aptGetPath = await $.which("apt-get");
if (!aptGetPath) return;
await keepOriginalBin(aptGetPath);
await deployCli("apt-get", aptGetPath);
}
Empty file.
1 change: 1 addition & 0 deletions mimic-cross.deno/config/config.ts
Expand Up @@ -4,6 +4,7 @@ export let config = {
arch: "set by loadConfig()",
hostArch: "set by loadConfig()",
hostRoot: "/mimic-cross",
mimicCrossRoot: "/mimic-cross/mimic-cross.deno",
internalBin: "/mimic-cross/mimic-cross/internal/bin",
keepBin: "/mimic-cross/mimic-cross/keep/bin",
logFile: "/var/log/mimic-cross.log",
Expand Down
1 change: 0 additions & 1 deletion mimic-cross.deno/deno.json
@@ -1,7 +1,6 @@
{
"imports": {
"cliffy/": "https://deno.land/x/cliffy@v1.0.0-rc.3/",
"config/": "./config/",
"dax/": "https://deno.land/x/dax@0.36.0/",
"daxex/": "https://deno.land/x/dax_extras@2.3.2/",
"std/": "https://deno.land/std@0.210.0/"
Expand Down
68 changes: 32 additions & 36 deletions mimic-cross.deno/setup.sh
@@ -1,44 +1,40 @@
#!/bin/bash -eu
#!/bin/bash

set -eux

HOST_ARCH=$(cat /mimic-cross/mimic-cross/host_arch)
# echo PATH=\"/mimic-cross/deploy/bin:"$PATH"\" >/etc/environment

#shellcheck disable=SC2046,SC2226
function create_ld_linux_symlink() {
local host_ld_linux="$1"
local target="$2"
mkdir -p "$(dirname "${target}")"
if [[ $(realpath "${host_ld_linux}") != $(realpath "${target}") ]]; then
ln -s "$host_ld_linux" "$target"
fi
}

ln -s $(realpath /mimic-cross/usr/lib/"$HOST_ARCH"-linux-gnu /usr/lib/)
if [[ $(realpath /lib) != /usr/lib ]]; then
ln -s /mimic-cross/lib/"$HOST_ARCH"-linux-gnu /lib
fi

# TODO support another arch
mkdir -p /lib64
ln -s /mimic-cross/lib64/ld-linux-x86-64.* /lib64/
ln -s /mimic-cross/usr/aarch64-linux-gnu /usr

# random devices used in apt-key script
/mimic-cross/mimic-cross/internal/bin/chroot /mimic-cross mknod /dev/random c 1 8
/mimic-cross/mimic-cross/internal/bin/chroot /mimic-cross mknod /dev/urandom c 1 9
/mimic-cross/mimic-cross/internal/bin/chroot /mimic-cross mknod /dev/null c 1 3
/mimic-cross/mimic-cross/internal/bin/chroot /mimic-cross mknod /dev/zero c 1 5
/mimic-cross/mimic-cross/internal/bin/chroot /mimic-cross chmod 666 /dev/random /dev/urandom /dev/null /dev/zero


# mkdir -p /var/log/mimic-cross
# mkdir -p /mimic-cross/deploy/host
# mkdir -p /mimic-cross/deploy/target
# mkdir -p /mimic-cross/deploy/bin
#
# cp /mimic-cross/bin/apt-get /usr/local/bin/
# cp /mimic-cross/bin/mimic-deploy /usr/local/bin/
# cp /mimic-cross/bin/mimic-host-run /usr/local/bin/
# cp /mimic-cross/bin/mimic-dual-run /usr/local/bin/
#
# cp /host/usr/bin/qemu-aarch64-static /usr/bin/qemu-aarch64-static
#
# [[ "$(ls -A /etc/apt/sources.list.d/)" ]] && cp /etc/apt/sources.list.d/* /host/etc/apt/sources.list.d/
# cp /etc/apt/trusted.gpg.d/* /host/etc/apt/trusted.gpg.d/
#
# cp /etc/resolv.conf /host/etc/resolv.conf
# dpkg -l | awk '/gnupg/ {print $2}' | xargs -r /host/"$(command -v chroot)" /host apt-get install
# cp /host/etc/resolv.conf.orig /host/etc/resolv.conf
# #shellcheck disable=SC1091
# /mimic-cross/hostize_installed_packages.sh
find /mimic-cross -name "ld-linux-*" | while read -r host_ld_linux; do
target="${host_ld_linux#/mimic-cross}"
create_ld_linux_symlink "$host_ld_linux" "$target"
if [[ "${target}" == /usr/* ]]; then
create_ld_linux_symlink "$host_ld_linux" "${target#/usr}"
fi
done

/mimic-cross/mimic-cross/bin/mimic-deno compile \
-A -c /mimic-cross/mimic-cross.deno/deno.json \
/mimic-cross/mimic-cross.deno/src/mimicx.ts
mv ./mimicx /usr/local/bin

# 追加した行
echo '#!/bin/bash
/mimic-cross/mimic-cross/bin/mimic-deno run -A /mimic-cross/mimic-cross.deno/src/mimicx.ts $@' \
> /usr/local/bin/mimicx
chmod +x /usr/local/bin/mimicx

mimicx setup
2 changes: 1 addition & 1 deletion mimic-cross.deno/src/chroot.ts
@@ -1,5 +1,5 @@
import $ from "daxex/mod.ts";
import { config } from "config/config.ts";
import { config } from "../config/config.ts";

export const prepareChroot = (async () => {
return await $.path("/etc/resolv.conf").copyFile(
Expand Down
2 changes: 1 addition & 1 deletion mimic-cross.deno/src/deploy.test.ts
Expand Up @@ -2,7 +2,7 @@ import $ from "daxex/mod.ts";
import { mimicDeploy, readRunpath } from "./deploy.ts";
import { assert, assertEquals } from "std/assert/mod.ts";
import { checkNeeded, getElfArch } from "./util.ts";
import { config } from "config/config.ts";
import { config } from "../config/config.ts";

const testDataPath = $.path(Deno.env.get("MIMIC_TEST_DATA_PATH")!);
const deployDir = testDataPath.join("deploy");
Expand Down
2 changes: 1 addition & 1 deletion mimic-cross.deno/src/deploy.ts
@@ -1,6 +1,6 @@
import $ from "daxex/mod.ts";
import { PathRefLike } from "daxex/mod.ts";
import { config } from "config/config.ts";
import { config } from "../config/config.ts";
import { logger } from "./log.ts";
import { isElfExecutable, isInPath, parseLdconf } from "./util.ts";

Expand Down
21 changes: 12 additions & 9 deletions mimic-cross.deno/src/mimicx.ts
@@ -1,4 +1,4 @@
#!/usr/bin/env -S mimic-deno run -A --ext=ts
#!/mimic-cross/mimic-cross/bin/mimic-deno run -A --ext=ts
import { Command } from "cliffy/command/mod.ts";
import {
aptGet,
Expand All @@ -8,6 +8,7 @@ import {
} from "../apt/apt.ts";
import { logger } from "./log.ts";
import { runOnHost } from "./chroot.ts";
import { setup } from "./setup.ts";

// import $ from "daxex/mod.ts";
// $.setPrintCommand(true);
Expand Down Expand Up @@ -46,20 +47,22 @@ await new Command()
await aptGet(combinedArgs, { force: options.force });
})
.command("suggest [packageName...]", "Suggest supported package list.")
.option("-a, --all", "Target all installed packages")
.option("--show_commands", "show deploy commands")
.option("-a, --all", "Target all installed packages")
.action(async function (options, ...packageName) {
if (options.all) packageName = await getAllInstalledPackages();
for (const p of packageName) {
const commands = await findCommandsFromPackage(p);
if (commands.length > 0) {
console.log(`${p},`);
if (options.show_commands) {
for (const c of commands) {
console.log(`// ${c}`);
}
}
if (commands.length === 0) continue;
console.log(`${p},`);
if (!options.showCommands) continue;
for (const c of commands) {
console.log(`// ${c}`);
}
}
})
.command("setup", "Setup mimic-cross environment")
.action(async function () {
await setup();
})
.parse(Deno.args);
17 changes: 17 additions & 0 deletions mimic-cross.deno/src/setup.ts
@@ -0,0 +1,17 @@
import $ from "daxex/mod.ts";
import { config } from "../config/config.ts";
import { runOnHost } from "./chroot.ts";
import { deployInstalledPackages } from "../apt/apt.ts";

export async function setup() {
await Promise.all([
runOnHost("mknod /dev/random c 1 8"),
runOnHost("mknod /dev/urandom c 1 9"),
runOnHost("mknod /dev/null c 1 3"),
runOnHost("mknod /dev/zero c 1 5"),
]);
await runOnHost("chmod 666 /dev/random /dev/urandom /dev/null /dev/zero");
await $.path(`${config.logFile}`).parent()?.mkdir({ recursive: true });
await $.path(`${config.keepBin}`).mkdir({ recursive: true });
await deployInstalledPackages();
}

0 comments on commit 87c077a

Please sign in to comment.