Skip to content

Commit f9f07ff

Browse files
authored
perf(builder): speedup build time for macos_{arm64,amd64} (#57)
1 parent de284b1 commit f9f07ff

File tree

8 files changed

+71
-80
lines changed

8 files changed

+71
-80
lines changed

.github/workflows/build_macos-amd64.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ name: Build Intel MacOS Profile
22
on:
33
push:
44
tags:
5-
- 'v*'
5+
- 'v**'
66
branches:
77
- 'main'
88
- 'dev'
99
paths-ignore:
10-
- '*.md'
11-
- '*.txt'
10+
- '**.md'
11+
- '**.txt'
1212
pull_request:
1313
branches:
1414
- 'main'
1515
- 'dev'
1616
paths-ignore:
17-
- '*.md'
18-
- '*.txt'
17+
- '**.md'
18+
- '**.txt'
1919

2020
env:
2121
CI_ENV: IN_CI

.github/workflows/build_macos-arm64.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ name: Build ARM64 MacOS Profile
22
on:
33
push:
44
tags:
5-
- 'v*'
5+
- 'v**'
66
branches:
77
- 'main'
88
- 'dev'
99
paths-ignore:
10-
- '*.md'
11-
- '*.txt'
10+
- '**.md'
11+
- '**.txt'
1212
pull_request:
1313
branches:
1414
- 'main'
1515
- 'dev'
1616
paths-ignore:
17-
- '*.md'
18-
- '*.txt'
17+
- '**.md'
18+
- '**.txt'
1919

2020
env:
2121
CI_ENV: IN_CI

.github/workflows/build_wsl2-amd64.yaml

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,20 +2,20 @@ name: Build WSL2 Profile
22
on:
33
push:
44
tags:
5-
- 'v*'
5+
- 'v**'
66
branches:
77
- 'main'
88
- 'dev'
99
paths-ignore:
10-
- '*.md'
11-
- '*.txt'
10+
- '**.md'
11+
- '**.txt'
1212
pull_request:
1313
branches:
1414
- 'main'
1515
- 'dev'
1616
paths-ignore:
17-
- '*.md'
18-
- '*.txt'
17+
- '**.md'
18+
- '**.txt'
1919

2020
env:
2121
CI_ENV: IN_CI

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
[![Build ARM64 MacOS Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml) [![Build Intel MacOS Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-amd64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-amd64.yaml) [![Build ARM64 MacOS Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml)
1+
[![Build ARM64 MacOS Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-arm64.yaml) [![Build Intel MacOS Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-amd64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_macos-amd64.yaml) [![Build WSL2 Profile](https://github.com/ihexon/ovm-builder/actions/workflows/build_wsl2-amd64.yaml/badge.svg)](https://github.com/ihexon/ovm-builder/actions/workflows/build_wsl2-amd64.yaml)
22
# `make` commands
33
- `macos_arm64` : make ovm bootable image for macos arm64
44
- `wsl2_amd64` : make ovm wsl2 rootfs distribute

make

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ parse_profile() {
6262
if [[ "$SKIP_INSTALL_QEMU" == "true" ]]; then
6363
echo "env SKIP_INSTALL_QEMU set true, skip install qemu"
6464
else
65+
echo "install qemu for cross building"
6566
output="$output" workspace="$workspace" bash +x "${workspace}/subfunc/install_qemu.sh" || {
6667
echo "Error: Install qemu failed"
6768
exit 100

subfunc/build_proot.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@ git clone https://github.com/proot-me/proot "$proot_src" --depth 1
1717

1818
cd "$proot_src"
1919

20-
CFLAG="-Wno-error=implicit-function-declaration" make -C src loader.elf -j8
21-
CFLAG="-Wno-error=implicit-function-declaration" make -C src proot care -j8
20+
CFLAG="-Wno-error=implicit-function-declaration" make -C src loader.elf -j8 > /dev/null
21+
CFLAG="-Wno-error=implicit-function-declaration" make -C src proot care -j8 > /dev/null
2222

2323
rm -rf /usr/bin/proot
2424
rm -rf /usr/local/bin/proot

target_builder/macos_amd64

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ echo "output: ${output:?}"
2929
bootable_url="https://github.com/oomol/builded/releases/download/v1.5/alpine_uefi_bootable-x86_64.img.zst"
3030
bootable_img_zst="$output/$(basename "$bootable_url")"
3131
bootable_img="${bootable_img_zst%.*}"
32+
sudo rm -rf "$bootable_img_zst" "$bootable_img"
33+
3234
profile_name="$(basename "${target_profile:?}")"
3335
proot_args=()
34-
rootfs_dir="$output/rootfs_amd64"
36+
rootfs_dir="$output/rootfs"
3537
if [[ $BUILD_TYPE == cross ]]; then
3638
proot_args+=("--qemu=$output/qemu_bins/static_qemu/bin/qemu-x86_64")
3739
fi
40+
3841
proot_args+=("-r" "$rootfs_dir")
3942
proot_args+=("-b" "/dev:/dev")
4043
proot_args+=("-b" "/sys:/sys")
@@ -46,24 +49,36 @@ proot_args+=("-w" "/root")
4649
rootfs_part_uuid="4f1bfa20-73e8-4885-ab4b-1f9e67332f79"
4750
efi_part_uuid="F41A-23A6"
4851

52+
run_cmd_in_rootfs() {
53+
echo "Run cmd $*"
54+
sudo -E proot "${proot_args[@]}" \
55+
-0 /bin/su -c "$*"
56+
}
57+
58+
mount_image() {
59+
kpartx -av "$bootable_img"
60+
mount -m -U "$rootfs_part_uuid" "$rootfs_dir"
61+
mount -m -U "$efi_part_uuid" "$rootfs_dir/boot/efi"
62+
}
63+
4964
bootstrap_alpine() {
5065
echo "bootstrap alpine"
5166
cd "$workspace"
52-
sudo rm -rf "$bootable_img_zst" "$bootable_img"
5367

5468
mkdir -p "$output"
5569
wget -c "$bootable_url" --output-document="${bootable_img_zst}" --output-file="$output/wget_alpine_image.log"
5670

5771
cd "$output"
5872
zstd -d -f "${bootable_img_zst}"
5973
# Mapping the part into /dev/mapper/loopNpN
60-
kpartx -av "$bootable_img"
61-
mount -m -U $rootfs_part_uuid "$rootfs_dir"
62-
mount -m -U $efi_part_uuid "$rootfs_dir/boot/efi"
74+
mount_image
6375

6476
# add community repo into alpine repositories
6577
echo http://dl-cdn.alpinelinux.org/alpine/v3.20/community | tee -a "$rootfs_dir/etc/apk/repositories"
6678

79+
# change to mirror
80+
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' "$rootfs_dir/etc/apk/repositories"
81+
6782
# Install required package into rootfs
6883

6984
echo "install pkgs into rootfs"
@@ -74,8 +89,6 @@ bootstrap_alpine() {
7489
sudo -E proot "${proot_args[@]}" -0 /bin/su -c 'rm -rf /root/.ssh/ovm_id && \
7590
ssh-keygen -f /root/.ssh/ovm_id -N "" && \
7691
cat /root/.ssh/ovm_id.pub >> /root/.ssh/authorized_keys'
77-
78-
#tar -Jcvf "$output/macos_arm64_ssh.tar.xz" "$rootfs_dir/root/.ssh"
7992
sync
8093
}
8194

@@ -89,15 +102,6 @@ install_target_profile_deps() {
89102
fi
90103
}
91104

92-
clean_mapping() {
93-
echo "clean disk mapping"
94-
findmnt "$rootfs_dir" && {
95-
umount -R "$rootfs_dir"
96-
} || {
97-
echo "$rootfs_dir not mount, no need to unmount"
98-
}
99-
}
100-
101105
copy_layer() {
102106
cd "$workspace"
103107
layer_dir="$workspace/layers/$profile_name"
@@ -106,9 +110,9 @@ copy_layer() {
106110
# In MacOS the console device is hvc0
107111
# In Qemu the console device is ttyAMA0
108112
if [[ $VM_PROVIDER == "qemu" ]]; then
109-
find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0
113+
find "$layer_dir" -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0
110114
else
111-
find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0
115+
find "$layer_dir" -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0
112116
fi
113117

114118
echo "copy $layer_dir layers files"
@@ -124,33 +128,24 @@ copy_layer() {
124128
-0 /bin/su -c "echo rc_parallel=YES >> /etc/rc.conf"
125129

126130
sudo -E proot "${proot_args[@]}" \
127-
-0 /bin/su -c "touch /.first_boot"
131+
-0 /bin/su -c "echo first_boot > /.first_boot"
128132

129-
sync # must do sync !
130-
}
131-
132-
umount_rootfs() {
133-
echo "umount rootfs"
134-
umount -R "$rootfs_dir"
133+
sync
135134
}
136135

137136
pack_rootfs() {
138137
echo "pack rootfs"
138+
losetup -a
139139
cd "$output"
140-
zstd --stdout --force "$bootable_img" > "$bootable_img_zst"
140+
zstd --stdout --force "$bootable_img" >"$bootable_img_zst"
141141
sha256sum "$bootable_img_zst" | tee "$bootable_img_zst.sha256sum"
142142
cd "$workspace"
143143
}
144144

145145
echo "=== $profile_name ==="
146-
147-
clean_mapping
148146
install_target_profile_deps
149147

150148
bootstrap_alpine
151149
copy_layer
152-
umount_rootfs
153-
clean_mapping
154150

155-
# Final stage: boot raw disk and do update-grub
156151
pack_rootfs

target_builder/macos_arm64

Lines changed: 26 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,15 @@ echo "output: ${output:?}"
2929
bootable_url="https://github.com/oomol/builded/releases/download/v1.1/alpine_uefi_bootable-arm64.img.zst"
3030
bootable_img_zst="$output/$(basename "$bootable_url")"
3131
bootable_img="${bootable_img_zst%.*}"
32+
sudo rm -rf "$bootable_img_zst" "$bootable_img"
33+
3234
profile_name="$(basename "${target_profile:?}")"
3335
proot_args=()
34-
rootfs_dir="$output/rootfs_arm64"
36+
rootfs_dir="$output/rootfs"
3537
if [[ $BUILD_TYPE == cross ]]; then
3638
proot_args+=("--qemu=$output/qemu_bins/static_qemu/bin/qemu-aarch64")
3739
fi
40+
3841
proot_args+=("-r" "$rootfs_dir")
3942
proot_args+=("-b" "/dev:/dev")
4043
proot_args+=("-b" "/sys:/sys")
@@ -46,24 +49,36 @@ proot_args+=("-w" "/root")
4649
rootfs_part_uuid="d1e46927-eb5d-4fbf-986a-7e5581f37a16"
4750
efi_part_uuid="6729-82AB"
4851

52+
run_cmd_in_rootfs() {
53+
echo "Run cmd $*"
54+
sudo -E proot "${proot_args[@]}" \
55+
-0 /bin/su -c "$*"
56+
}
57+
58+
mount_image() {
59+
kpartx -av "$bootable_img"
60+
mount -m -U "$rootfs_part_uuid" "$rootfs_dir"
61+
mount -m -U "$efi_part_uuid" "$rootfs_dir/boot/efi"
62+
}
63+
4964
bootstrap_alpine() {
5065
echo "bootstrap alpine"
5166
cd "$workspace"
52-
sudo rm -rf "$bootable_img_zst" "$bootable_img"
5367

5468
mkdir -p "$output"
5569
wget -c "$bootable_url" --output-document="${bootable_img_zst}" --output-file="$output/wget_alpine_image.log"
5670

5771
cd "$output"
5872
zstd -d -f "${bootable_img_zst}"
5973
# Mapping the part into /dev/mapper/loopNpN
60-
kpartx -av "$bootable_img"
61-
mount -m -U $rootfs_part_uuid "$rootfs_dir"
62-
mount -m -U $efi_part_uuid "$rootfs_dir/boot/efi"
74+
mount_image
6375

6476
# add community repo into alpine repositories
6577
echo http://dl-cdn.alpinelinux.org/alpine/v3.20/community | tee -a "$rootfs_dir/etc/apk/repositories"
6678

79+
# change to mirror
80+
sed -i 's#https\?://dl-cdn.alpinelinux.org/alpine#https://mirrors.tuna.tsinghua.edu.cn/alpine#g' "$rootfs_dir/etc/apk/repositories"
81+
6782
# Install required package into rootfs
6883

6984
echo "install pkgs into rootfs"
@@ -74,8 +89,6 @@ bootstrap_alpine() {
7489
sudo -E proot "${proot_args[@]}" -0 /bin/su -c 'rm -rf /root/.ssh/ovm_id && \
7590
ssh-keygen -f /root/.ssh/ovm_id -N "" && \
7691
cat /root/.ssh/ovm_id.pub >> /root/.ssh/authorized_keys'
77-
78-
#tar -Jcvf "$output/macos_arm64_ssh.tar.xz" "$rootfs_dir/root/.ssh"
7992
sync
8093
}
8194

@@ -89,15 +102,6 @@ install_target_profile_deps() {
89102
fi
90103
}
91104

92-
clean_mapping() {
93-
echo "clean disk mapping"
94-
findmnt "$rootfs_dir" && {
95-
umount -R "$rootfs_dir"
96-
} || {
97-
echo "$rootfs_dir not mount, no need to unmount"
98-
}
99-
}
100-
101105
copy_layer() {
102106
cd "$workspace"
103107
layer_dir="$workspace/layers/$profile_name"
@@ -106,9 +110,9 @@ copy_layer() {
106110
# In MacOS the console device is hvc0
107111
# In Qemu the console device is ttyAMA0
108112
if [[ $VM_PROVIDER == "qemu" ]]; then
109-
find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0
113+
find "$layer_dir" -type f -exec sed -i 's/VM_CONSOLE_DEVICE/ttyAMA0/g' {} + # Replace VM_CONSOLE_DEVICE to ttyAMA0
110114
else
111-
find ./ -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0
115+
find "$layer_dir" -type f -exec sed -i 's/VM_CONSOLE_DEVICE/hvc0/g' {} + # Replace VM_CONSOLE_DEVICE to hvc0
112116
fi
113117

114118
echo "copy $layer_dir layers files"
@@ -124,33 +128,24 @@ copy_layer() {
124128
-0 /bin/su -c "echo rc_parallel=YES >> /etc/rc.conf"
125129

126130
sudo -E proot "${proot_args[@]}" \
127-
-0 /bin/su -c "touch /.first_boot"
131+
-0 /bin/su -c "echo first_boot > /.first_boot"
128132

129-
sync # must do sync !
130-
}
131-
132-
umount_rootfs() {
133-
echo "umount rootfs"
134-
umount -R "$rootfs_dir"
133+
sync
135134
}
136135

137136
pack_rootfs() {
138137
echo "pack rootfs"
138+
losetup -a
139139
cd "$output"
140-
zstd --stdout --force "$bootable_img" > "$bootable_img_zst"
140+
zstd --stdout --force "$bootable_img" >"$bootable_img_zst"
141141
sha256sum "$bootable_img_zst" | tee "$bootable_img_zst.sha256sum"
142142
cd "$workspace"
143143
}
144144

145145
echo "=== $profile_name ==="
146-
147-
clean_mapping
148146
install_target_profile_deps
149147

150148
bootstrap_alpine
151149
copy_layer
152-
umount_rootfs
153-
clean_mapping
154150

155-
# Final stage: boot raw disk and do update-grub
156151
pack_rootfs

0 commit comments

Comments
 (0)