/
image-gen.ts
116 lines (101 loc) · 4.61 KB
/
image-gen.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
import * as dotenv from 'dotenv';
dotenv.config();
import fs from "fs";
import path from "path";
import { OUTPUT_DIR, TARGET_DEVICE, BUILD_DIR, FILES_DIR, ACCEPTABLE_ANDROID_DEVICES } from '../../src/helpers/consts';
import exec from "../../src/helpers/exec";
let PROLINUX_VARIANT = process.env.PROLINUX_VARIANT;
let PROLINUX_CHANNEL = process.env.PROLINUX_CHANNEL;
let loopDevice = "";
const mountPMOSImage = (targetDevice: string) => {
loopDevice = exec("sudo losetup -f", false).toString().trim();
fs.writeFileSync(path.join(BUILD_DIR, "loop_device.txt"), loopDevice);
console.log("image-gen loopDevice: " + loopDevice);
let loopExtraArgs = "";
let rootfs_image_sector_size = ACCEPTABLE_ANDROID_DEVICES.find((d) => d.name === targetDevice)?.rootfs_image_sector_size;
if (rootfs_image_sector_size) {
loopExtraArgs += "-b " + rootfs_image_sector_size;
}
exec(`
sudo losetup -f -P ${BUILD_DIR}/img-staging/${targetDevice}.img ${loopExtraArgs}
losetup -l
sudo partprobe ${loopDevice}
sudo udevadm trigger
echo "Waiting for devices to settle..."
sleep 5
`);
};
const buildTargetStandardPMOSDeviceImage = (targetDevice: string) => {
console.log(`Building ${targetDevice} image`);
let adevice = ACCEPTABLE_ANDROID_DEVICES.find((d) => d.name === targetDevice)
// Format and place files into rootfs
exec(`
sudo mkfs.ext4 /dev/disk/by-label/pmOS_root -F -L pmOS_root
sudo mount /dev/disk/by-label/pmOS_root ${BUILD_DIR}/pmos_root_mnt
sudo rsync -ah --progress ${OUTPUT_DIR}/prolinux-root-${PROLINUX_VARIANT}-${PROLINUX_CHANNEL}.squish ${BUILD_DIR}/pmos_root_mnt/prolinux_a.squish
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/squishroot
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/persistroot
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/workdir
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/oroot
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/data
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/data/home
sudo cp -r ${FILES_DIR}/layout/home/* ${BUILD_DIR}/pmos_root_mnt/data/home/
sudo chown -R 1000:1000 ${BUILD_DIR}/pmos_root_mnt/data/home/user
sudo cp -v ${FILES_DIR}/prolinux.toml ${BUILD_DIR}/pmos_root_mnt/data/prolinux.toml
# TODO: the current situation on android is that kexec is broken
if [ "${adevice?.should_disable_kexec}" = true ]; then
sudo sh -c "echo >> ${BUILD_DIR}/pmos_root_mnt/data/prolinux.toml"
echo "pl2.disable_kexec = true" | sudo tee -a ${BUILD_DIR}/pmos_root_mnt/data/prolinux.toml
fi
# pmos init checks this to see if root was mounted
sudo mkdir -pv ${BUILD_DIR}/pmos_root_mnt/usr
sudo umount /dev/disk/by-label/pmOS_root
sync
sudo ./scripts/unmount.sh || true
sleep 4
`);
exec(`
sudo cp -v ${BUILD_DIR}/img-staging/${targetDevice}.img ${OUTPUT_DIR}
`);
if(adevice) {
// Convert to Android sparse image
exec(`
sudo mv ${OUTPUT_DIR}/${targetDevice}.img ${OUTPUT_DIR}/${targetDevice}-raw.img
sudo img2simg ${OUTPUT_DIR}/${targetDevice}-raw.img ${OUTPUT_DIR}/${targetDevice}.img ${adevice.rootfs_image_sector_size ?? ""}
sudo rm -rf ${OUTPUT_DIR}/${targetDevice}-raw.img
`);
// Insert initramfs into boot.img
// also blacklists nt36xxx during stage 1 to fix poco touchscreen issue
exec(`pushd .
cd ${OUTPUT_DIR}/${targetDevice}
mkdir -pv work
cd work
sudo abootimg -x ../boot.img
sudo sed -i "s/bootsize.*/bootsize = 16900000/g" bootimg.cfg
sudo sed -i "s/cmdline.*/cmdline = console=tty0 PMOS_NO_OUTPUT_REDIRECT/g" bootimg.cfg
echo "- Packing new boot image..."
sudo abootimg --create boot.img -f bootimg.cfg -k zImage -r ../initramfs
sleep 1
sudo mv -v boot.img ../boot.img
popd`);
}
};
export function main() {
exec("sudo ./scripts/unmount.sh || true");
if (!TARGET_DEVICE) {
console.log("TARGET_DEVICE is not set");
process.exit(1);
}
let device;
let kernel;
[device, kernel] = TARGET_DEVICE.split(":");
let targetDeviceFolder = path.join(OUTPUT_DIR, device);
if (!fs.existsSync(targetDeviceFolder)) {
console.log(`${targetDeviceFolder} does not exist`);
process.exit(1);
}
mountPMOSImage(device);
buildTargetStandardPMOSDeviceImage(device);
exec(`sudo losetup -d ${loopDevice} || true`);
}
main();