-
Notifications
You must be signed in to change notification settings - Fork 0
/
mkimage.sh
executable file
·163 lines (126 loc) · 4.83 KB
/
mkimage.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#!/bin/bash
set -xeuo pipefail
export ROOTFSDIR=reflash_rootfs
sudo rm -rf "${ROOTFSDIR}"
mkdir -p "${ROOTFSDIR}"
sudo debootstrap --arch=arm64 --foreign --variant=minbase bookworm "${ROOTFSDIR}"/initrd
if [ ! -f rootfs_files/debs/linux-image-legacy-sunxi64_23.08.0-trunk_arm64__5.15.127.deb ]; then
wget -q -P rootfs_files/debs/ http://feeds.iagent.no/debian/pool/main/linux-image-legacy-sunxi64_23.08.0-trunk_arm64__5.15.127.deb
fi
sudo cp rootfs_files/debs/* "${ROOTFSDIR}"/initrd
sudo bash -c "echo recore > ${ROOTFSDIR}/initrd/etc/hostname"
sudo chroot "${ROOTFSDIR}"/initrd /bin/bash <<ENDOFDEB
export DEBIAN_FRONTEND="noninteractive"
export TERM=xterm-color
/debootstrap/debootstrap --second-stage
export LC_ALL=C
dpkg -i linux-image-legacy-sunxi64_23.08.0-trunk_arm64__5.15.127.deb
apt install -y systemd-resolved systemd openssh-server udev kmod fdisk parted ca-certificates xz-utils pv systemd-timesyncd wget wpasupplicant sudo policykit-1 iproute2 --no-install-recommends --no-install-suggests
systemctl enable systemd-networkd
ln -s /lib/systemd/systemd /init
useradd debian -d /home/debian -G tty,dialout -m -s /bin/bash -e -1
echo "debian ALL=(ALL) NOPASSWD: ALL" > /etc/sudoers.d/debian
# Set default passwords
echo 'debian:temppwd' | chpasswd
echo 'root:temppwd' | chpasswd
echo "g_serial" >> /etc/modules
echo "ttyGS0" >> /etc/securetty
systemctl enable serial-getty@ttyGS0.service
# Clean up
rm ./*.deb
rm -rf /var/lib/apt/lists/
rm -rf /var/cache/
rm -rf /usr/share/locale/
find /usr/share/doc -depth -type f -print0 ! -name copyright | xargs -0 rm
find /usr/share/doc -empty -print0 | xargs -0 rmdir
rm -rf /usr/share/man/* /usr/share/groff/* /usr/share/info/*
rm -rf /usr/share/lintian/* /usr/share/linda/* /var/cache/man/*
ENDOFDEB
cat <<EOF > "${ROOTFSDIR}"/initrd/etc/systemd/network/20-wired.network
[Match]
Name=eth0
[Network]
DHCP=yes
MulticastDNS=yes
[Link]
Multicast=yes
EOF
cat <<EOF > "${ROOTFSDIR}"/initrd/etc/systemd/network/30-wireless.network
[Match]
Name=wlan0
[Network]
Address=192.168.50.1/24
DHCPServer=yes
LinkLocalAddressing=yes
MulticastDNS=yes
EOF
cat <<'EOF' > "${ROOTFSDIR}"/initrd/etc/udev/rules.d/20-wifi.rules
ACTION=="add", SUBSYSTEM=="net", KERNEL=="wlan0", ENV{SYSTEMD_WANTS}+="wpa_supplicant@wlan0.service"
EOF
cat <<EOF > "${ROOTFSDIR}"/initrd/etc/wpa_supplicant/wpa_supplicant-wlan0.conf
ctrl_interface=DIR=/var/run/wpa_supplicant GROUP=netdev
update_config=1
ap_scan=1
network={
priority=0
ssid="Recore"
mode=2
key_mgmt=WPA-PSK
psk="12345678"
frequency=2462
}
EOF
cat <<EOF >"${ROOTFSDIR}"/initrd/etc/systemd/system/reflash.service
[Unit]
Description=Refactor flashing server
After=network.target
Conflicts=getty@tty1.service
Before=getty.target
[Service]
ExecStart=/usr/local/bin/reflash
[Install]
WantedBy=multi-user.target
EOF
systemctl enable reflash --root="${ROOTFSDIR}"/initrd
# Install app
sudo mkdir -p "${ROOTFSDIR}"/initrd/usr/local/bin
sudo cp reflash/reflash "${ROOTFSDIR}"/initrd/usr/local/bin/
sudo mkdir -p "${ROOTFSDIR}"/initrd/usr/local/share/fonts
sudo cp reflash/Roboto-Light.ttf "${ROOTFSDIR}"/initrd/usr/local/share/fonts/
sudo mkdir -p "${ROOTFSDIR}"/initrd/var/www/html/reflash
sudo cp -r client/dist "${ROOTFSDIR}"/initrd/var/www/html/reflash
sudo cp bin/* "${ROOTFSDIR}"/initrd/usr/local/bin
sudo mkdir -p "${ROOTFSDIR}"/initrd/mnt/usb
sudo cp reflash-version "$ROOTFSDIR"/initrd/etc/
NAME="reflash-"$(cat reflash-version | tr -d '\n')
# Move the boot folder outside the rootfs
sudo rm -rf "${ROOTFSDIR}"/boot
sudo mv "${ROOTFSDIR}"/initrd/boot/ "${ROOTFSDIR}"
# Compile and copy extra files
sudo cp rootfs_files/boot/* "${ROOTFSDIR}"/boot
mkimage -C none -A arm -T script -d "${ROOTFSDIR}"/boot/boot.cmd "${ROOTFSDIR}"/boot/boot.scr
# Copy recore dtb and fixup
mkdir -p "${ROOTFSDIR}"/boot/dtb/allwinner/overlay
sudo cp rootfs_files/dtb/*.dtb "${ROOTFSDIR}"/boot/dtb/allwinner/
sudo cp rootfs_files/dtb/*.scr "${ROOTFSDIR}"/boot/dtb/allwinner/overlay/
# Crate initramfs
sudo bash -c "cd '${ROOTFSDIR}/initrd' && find . | cpio -ov --format=newc | gzip -9 >'../initrd.img.gz'" >/dev/null 2>&1
mkimage -A arm -T ramdisk -C gzip -n uInitrd -d "${ROOTFSDIR}"/initrd.img.gz "${ROOTFSDIR}"/uInitrd
sudo cp "${ROOTFSDIR}"/uInitrd "${ROOTFSDIR}"/boot
# Create new empty image
sudo rm -rf "${ROOTFSDIR}"/reflash.img
truncate -s 250M "${ROOTFSDIR}"/reflash.img
LOOPDEV=$(sudo losetup -f)
sudo losetup -P "${LOOPDEV}" "${ROOTFSDIR}"/reflash.img
printf "g\nn\n\n\n\nw\n" | sudo fdisk "${LOOPDEV}"
sudo mkfs.ext4 -E nodiscard "${LOOPDEV}"p1
mkdir -p "${ROOTFSDIR}"/image
sudo mount "${LOOPDEV}"p1 "${ROOTFSDIR}"/image
# Copy files to new image
sudo cp -r "${ROOTFSDIR}"/boot/* "${ROOTFSDIR}"/image
# Unmount and compress image
sudo umount "${ROOTFSDIR}"/image
sudo losetup -d "${LOOPDEV}"
xz -f -T 0 -k -z "${ROOTFSDIR}"/reflash.img
mv "${ROOTFSDIR}"/reflash.img.xz /output/${NAME}.img.xz
sudo rm -rf "${ROOTFSDIR}"