-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathbuild_locally.sh
executable file
·145 lines (127 loc) · 4.44 KB
/
build_locally.sh
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
#!/usr/bin/env bash
: <<COMMENTBLOCK
title :build_localy.sh
description :
author :Valeriu Stinca
email :[email protected]
date :20230916
version :0.1
notes :
=========================
COMMENTBLOCK
# Display ASCII banner
echo "ICAgICAgIHwgICAgICAgICAgICAgICAgfCAgICAgICAgICAgICAgICBfKSAgICAgICAgICAgICAg
ICAgICAgICAgICAgICAgICAgICAKICBfX3wgIF9ffCAgIF9ffCAgX2AgfCAgX198ICAgXyBcICAg
X2AgfCAgfCAgIF9ffCAgIF8gIC8gICBfIFwgICBfXyBcICAgIF8gXCAKXF9fIFwgIHwgICAgfCAg
ICAoICAgfCAgfCAgICAgX18vICAoICAgfCAgfCAgKCAgICAgICAgLyAgICggICB8ICB8ICAgfCAg
IF9fLyAKX19fXy8gXF9ffCBffCAgIFxfXyxffCBcX198IFxfX198IFxfXywgfCBffCBcX19ffCAg
IF9fX3wgXF9fXy8gIF98ICBffCBcX19ffCAKICAgICAgICAgICAgICAgICAgICAgICAgICAgICAg
ICAgIHxfX18vICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAgICAK" | base64 -d
#
# 1. Configuration Variables
#
INSTALL_REQUIREMENTS=false
LOOP_IMAGE="archlinux-aarch64-rpi.img"
LOOP_IMAGE_SIZE="4G"
RPI_MODEL=5
ARM_VERSION=aarch64
DEFAULT_LOCALE="en_US.UTF-8"
TIMEZONE="Europe/Paris"
KEYMAP="us-acentos"
SSH_PUB_KEY="ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIKMidTQ6KGfZtonNKd1HtNPPDiPtzEmlg5yOduvmZzTA valerius laptop"
PACKAGES="base-devel dosfstools git mkinitcpio-utils neovim nftables openssh python qrencode rsync tailscale uboot-tools unzip zerotier-one zsh"
#
# 2. Working Directory Setup
#
echo "Setting up working directory..."
WORKDIR_BASE="/tmp/archlinux-rpi-aarch64/$(date +%Y%m%d%H%M%S)"
LOOP_IMAGE_PATH="$WORKDIR_BASE/$LOOP_IMAGE"
ARCH_AARCH64_IMG_URL="http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz"
ARCH_AARCH64_IMG_URL_MD5="http://os.archlinuxarm.org/os/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz.md5"
#
# 3. System Dependencies
#
echo "Checking and installing dependencies..."
if [ "$INSTALL_REQUIREMENTS" = true ]; then
pacman -Syu --noconfirm
pacman -S --noconfirm qemu-user-static-binfmt qemu-user-static dosfstools wget libarchive arch-install-scripts
fi
#
# 4. Preparation Steps
#
echo "Creating work directory and image file..."
# Create and set permissions for work directory
mkdir -p "$WORKDIR_BASE"
chown -R $USER:$USER "$WORKDIR_BASE"
# Create base image file
fallocate -l "$LOOP_IMAGE_SIZE" "$LOOP_IMAGE_PATH"
#
# 5. Download and Verify Arch Linux Image
#
echo "Downloading and verifying Arch Linux image..."
wget -q "$ARCH_AARCH64_IMG_URL" -O "${WORKDIR_BASE}/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz"
wget -q "$ARCH_AARCH64_IMG_URL_MD5" -O "${WORKDIR_BASE}/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz.md5"
md5sum -c "${WORKDIR_BASE}/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz.md5"
#
# 6. Disk Setup and Partitioning
#
echo "Setting up disk partitions..."
# Setup loop device
losetup -fP "$LOOP_IMAGE_PATH"
LOOP_DEVICE=$(losetup -j "$LOOP_IMAGE_PATH" | cut -d: -f1)
# Create partitions
parted --script "$LOOP_DEVICE" mklabel msdos
parted --script "$LOOP_DEVICE" mkpart primary fat32 1MiB 257MiB
parted --script "$LOOP_DEVICE" mkpart primary ext4 257MiB 100%
parted --script "$LOOP_DEVICE" set 1 boot on
parted --script "$LOOP_DEVICE" print
#
# 7. Filesystem Creation and Mounting
#
echo "Creating and mounting filesystems..."
# Format partitions
mkfs.vfat -F32 "${LOOP_DEVICE}p1" -n PI-BOOT
mkfs.ext4 -q -E lazy_itable_init=0,lazy_journal_init=0 -F "${LOOP_DEVICE}p2" -L PI-ROOT
# Mount partitions
mkdir -p "${WORKDIR_BASE}/root"
mount "${LOOP_DEVICE}p2" "$WORKDIR_BASE/root"
mkdir -p "${WORKDIR_BASE}/root/boot"
mount "${LOOP_DEVICE}p1" "$WORKDIR_BASE/root/boot"
#
# 8. System Installation
#
echo "Installing base system..."
# Extract base system
bsdtar -xpf "${WORKDIR_BASE}/ArchLinuxARM-rpi-${ARM_VERSION}-latest.tar.gz" -C "$WORKDIR_BASE/root"
sync
# Prepare chroot environment
systemctl start systemd-binfmt
mount --bind "$WORKDIR_BASE/root" "$WORKDIR_BASE/root"
mount --bind "$WORKDIR_BASE/root/boot" "$WORKDIR_BASE/root/boot"
#
# 9. System Configuration
#
echo "Starting system configuration..."
./build_archlinux_rpi_aarch64_img.sh \
"$WORKDIR_BASE" \
"$DEFAULT_LOCALE" \
"$KEYMAP" \
"$TIMEZONE" \
"$PACKAGES" \
"$RPI_MODEL" \
"$RPI_HOSTNAME" \
"$SSH_PUB_KEY"
#
# 10. Cleanup and Unmount
#
echo "Cleaning up..."
# Unmount all filesystems
umount -R ${WORKDIR_BASE}/root/boot
umount -R ${WORKDIR_BASE}/root
sync
# Detach loop device
losetup -d $LOOP_DEVICE
echo "Build completed successfully. Image is available at: $LOOP_IMAGE_PATH"
# Optional: Calculate SHA256 of the final image
sha256sum "$LOOP_IMAGE_PATH" > "${LOOP_IMAGE_PATH}.sha256"
echo "SHA256 checksum saved to: ${LOOP_IMAGE_PATH}.sha256"