This repository has been archived by the owner on Oct 1, 2023. It is now read-only.
-
-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathBuildImage.sh
executable file
·1330 lines (1097 loc) · 41.4 KB
/
BuildImage.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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
#
# This script will download and update the ODROID M1 20.04 image to 22.04
#
# More information available at:
# https://jamesachambers.com/legendary-odroid-m1-ubuntu-images/
# https://github.com/TheRemote/Legendary-ODROID-M1
# Dependencies - build-essential guestfs-tools kpartx
# CONFIGURATION
IMAGE_VERSION="v1.4"
SOURCE_RELEASE="20.04"
DEST_RELEASE="22.04.1"
TARGET_IMG="legendary-ubuntu-${DEST_RELEASE}-server-odroidm1-${IMAGE_VERSION}.img"
TARGET_IMGXZ="legendary-ubuntu-${DEST_RELEASE}-server-odroidm1-${IMAGE_VERSION}.tar.xz"
DESKTOP_IMG="legendary-ubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.img"
DESKTOP_IMGXZ="legendary-ubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.tar.xz"
MATE_DESKTOP_IMG="legendary-ubuntu-${DEST_RELEASE}-mate-desktop-odroidm1-${IMAGE_VERSION}.img"
MATE_DESKTOP_IMGXZ="legendary-ubuntu-${DEST_RELEASE}-mate-desktop-odroidm1-${IMAGE_VERSION}.tar.xz"
KUBUNTU_DESKTOP_IMG="legendary-kubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.img"
KUBUNTU_DESKTOP_IMGXZ="legendary-kubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.tar.xz"
XUBUNTU_DESKTOP_IMG="legendary-xubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.img"
XUBUNTU_DESKTOP_IMGXZ="legendary-xubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.tar.xz"
LUBUNTU_DESKTOP_IMG="legendary-lubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.img"
LUBUNTU_DESKTOP_IMGXZ="legendary-lubuntu-${DEST_RELEASE}-desktop-odroidm1-${IMAGE_VERSION}.tar.xz"
SOURCE_IMG="ubuntu-${SOURCE_RELEASE}-server-odroidm1-20220531.img"
SOURCE_IMGXZ="ubuntu-${SOURCE_RELEASE}-server-odroidm1-20220531.img.xz"
UPDATED_IMG="ubuntu-${DEST_RELEASE}-server-odroidm1.img"
UPDATED_DESKTOP_IMG="ubuntu-${DEST_RELEASE}-desktop-odroidm1.img"
UPDATED_MATE_IMG="ubuntu-${DEST_RELEASE}-mate-desktop-odroidm1.img"
UPDATED_XUBUNTU_IMG="xubuntu-${DEST_RELEASE}-desktop-odroidm1.img"
UPDATED_KUBUNTU_IMG="kubuntu-${DEST_RELEASE}-desktop-odroidm1.img"
UPDATED_LUBUNTU_IMG="lubuntu-${DEST_RELEASE}-desktop-odroidm1.img"
export SLEEP_SHORT="0.2"
export SLEEP_LONG="1"
export MOUNT_IMG=""
export KERNEL_VERSION=""
# FUNCTIONS
function PrepareIMG() {
while mountpoint -q /mnt/boot && ! sudo umount /mnt/boot; do
echo "/mnt/boot still mounted -- unmounting"
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/proc && ! sudo umount /mnt/proc; do
echo "/mnt/proc still mounted -- unmounting"
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/sys && ! sudo umount /mnt/sys; do
echo "/mnt/sys still mounted -- unmounting"
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/tmp && ! sudo umount -f /mnt/tmp; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/null && ! sudo umount -f /mnt/dev/null; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/random && ! sudo umount -f /mnt/dev/random; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/urandom && ! sudo umount -f /mnt/dev/urandom; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/pts && ! sudo umount -f /mnt/dev/pts; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt && ! sudo umount /mnt; do
echo "/mnt still mounted -- unmounting"
sync
sync
sleep "${SLEEP_SHORT}"
done
MountCheck=$(sudo losetup --list | grep "(deleted)" | awk 'NR==1{ print $1 }')
while [ -n "$MountCheck" ]; do
echo "Leftover image $MountCheck found -- removing"
# sudo rm -rf $MountCheck
sudo kpartx -dv "${MountCheck%p1}"
sleep "${SLEEP_SHORT}"
sudo losetup -D
sleep "${SLEEP_SHORT}"
MountCheck=$(sudo losetup --list | grep "(deleted)" | awk 'NR==1{ print $1 }')
done
MountCheck=$(sudo losetup --list | grep "legendary-" | awk 'NR==1{ print $1 }')
while [ -n "$MountCheck" ]; do
echo "Leftover legendary-image $MountCheck found -- removing"
# sudo rm -rf $MountCheck
sudo kpartx -dv "${MountCheck%p1}"
sleep "${SLEEP_SHORT}"
sudo losetup -D
sleep "${SLEEP_SHORT}"
MountCheck=$(sudo losetup --list | grep "legendary-" | awk 'NR==1{ print $1 }')
done
}
function MountIMG() {
if [ -n "${MOUNT_IMG}" ]; then
echo "An image is already mounted on ${MOUNT_IMG}"
return 1
fi
if [ ! -e "${1}" ]; then
echo "Image ${1} does not exist!"
return 1
fi
echo "Mounting image ${1}"
MountCheck=$(sudo kpartx -avs "${1}")
echo "$MountCheck"
export MOUNT_IMG=$(echo "$MountCheck" | awk 'NR==1{ print $3 }')
export MOUNT_IMG="${MOUNT_IMG%p1}"
if [ -n "${MOUNT_IMG}" ]; then
sync
sync
sleep "${SLEEP_SHORT}"
echo "Mounted ${1} on loop ${MOUNT_IMG}"
else
echo "Unable to mount ${1}: ${MOUNT_IMG} Check - $MountCheck"
export MOUNT_IMG=""
fi
sync
sync
sleep "${SLEEP_SHORT}"
}
function MountIMGPartitions() {
echo "Mounting partitions"
# Mount the rootfs on /mnt (/)
sudo mount "/dev/mapper/${1}p2" /mnt
# Mount the bootfs on /mnt/boot (/boot)
sudo mount "/dev/mapper/${1}p1" /mnt/boot
sudo mount --bind /tmp "/mnt/tmp" &&
sudo mount --bind /dev/null "/mnt/dev/null" &&
sudo mount --bind /dev/pts "/mnt/dev/pts" &&
sudo mount --bind /dev/random "/mnt/dev/random" &&
sudo mount --bind /dev/urandom "/mnt/dev/urandom" &&
sync
sync
sleep "${SLEEP_SHORT}"
}
function UnmountIMGPartitions() {
sync
sync
# Unmount boot and root partitions
echo "Unmounting partitions ..."
while mountpoint -q /mnt/boot && ! sudo umount -f /mnt/boot; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/proc && ! sudo umount /mnt/proc; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/sys && ! sudo umount /mnt/sys; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/tmp && ! sudo umount -f /mnt/tmp; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/null && ! sudo umount -f /mnt/dev/null; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/random && ! sudo umount -f /mnt/dev/random; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/urandom && ! sudo umount -f /mnt/dev/urandom; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt/dev/pts && ! sudo umount -f /mnt/dev/pts; do
sync
sync
sleep "${SLEEP_SHORT}"
done
while mountpoint -q /mnt && ! sudo umount -f /mnt; do
sync
sync
sleep "${SLEEP_SHORT}"
done
sync
sync
}
function UnmountIMG() {
# Unmount image and save changes
sync
sync
# Check if image is mounted first
MountCheck=$(sudo losetup --list | grep "${1}")
if [ ! -n "$MountCheck" ]; then
echo "Unable to unmount $1 (not in losetup --list)"
UnmountIMGPartitions
export MOUNT_IMG=""
return
fi
echo "Unmounting $1"
UnmountIMGPartitions
sudo kpartx -dvs "/dev/${MOUNT_IMG}"
sync
sync
sleep "${SLEEP_LONG}"
sudo losetup -D
MountCheck=$(sudo losetup --list | grep "legendary-" | awk 'NR==1{ print $1 }')
while [ -n "$MountCheck" ]; do
echo "Leftover legendary-image $MountCheck found -- removing"
# sudo rm -rf $MountCheck
sudo kpartx -dv "${MountCheck%p1}"
sleep "${SLEEP_SHORT}"
sudo losetup -D
sleep "${SLEEP_SHORT}"
MountCheck=$(sudo losetup --list | grep "legendary-" | awk 'NR==1{ print $1 }')
done
# Wait for loop to disappear from list before continuing
WaitLoops=0
while [ -n "$(sudo losetup --list | grep ${1})" ]; do
WaitLoops=$((WaitLoops + 1))
if ((WaitLoops > 100)); then
# Remove all mappings
echo "Stuck -- can use sudo dmsetup remove_all to remove all mappings"
#sudo dmsetup remove_all
elif ((WaitLoops > 50)); then
echo "Exceeded maximum wait time -- trying to force close"
sudo kpartx -dvs "/dev/${MOUNT_IMG}"
sudo losetup -D
fi
sync
sync
sleep "${SLEEP_SHORT}"
done
sudo chown -R "$LOGNAME" .
export MOUNT_IMG=""
}
function CompactIMG() {
echo "Compacting IMG file ${1}"
sudo rm -rf "${1}.2"
sudo virt-sparsify "${1}" "${1}.2"
sync
sync
sleep "${SLEEP_SHORT}"
sudo rm -rf "${1}"
mv "${1}.2" "${1}"
sync
sync
sleep "${SLEEP_SHORT}"
}
function CleanIMG() {
echo "Cleaning IMG file (after)"
# Clear apt cache
sudo rm -rf /mnt/var/lib/apt/lists/ports* /mnt/var/lib/apt/lists/*InRelease /mnt/var/lib/apt/lists/*-en /mnt/var/lib/apt/lists/*Packages
# Clear Python cache
sudo find /mnt -regex '^.*\(__pycache__\|\.py[co]\)$' -delete
# Remove any crash files generated
sudo rm -rf /mnt/var/crash/*
sudo rm -rf /mnt/root/*
# Remove machine ID so all clones don't have the same one
sudo rm -rf /mnt/etc/machine-id
sudo touch /mnt/etc/machine-id
# Trim
sudo fstrim -v /mnt
sudo fstrim -v /mnt/boot
sync
sync
sleep "${SLEEP_LONG}"
}
function ShrinkIMG() {
MountIMG "$1"
tune2fs_output=$(sudo tune2fs -l "/dev/mapper/${MOUNT_IMG}p2")
currentsize=$(echo "$tune2fs_output" | grep '^Block count:' | tr -d ' ' | cut -d ':' -f 2)
blocksize=$(echo "$tune2fs_output" | grep '^Block size:' | tr -d ' ' | cut -d ':' -f 2)
minsize=$(sudo resize2fs -P "/dev/mapper/${MOUNT_IMG}p2" | tr -d ' ' | cut -d ':' -f 2)
extra_space=$(($currentsize - $minsize))
beforesize=$(ls -lh "$1" | cut -d ' ' -f 5)
echo "Before: $beforesize, Extra_Space: $extra_space, Blocksize: $blocksize"
parted_output=$(sudo parted -ms "$1" unit B print | tail -n 1)
partnum=$(echo "$parted_output" | cut -d ':' -f 1)
partstart=$(echo "$parted_output" | cut -d ':' -f 2 | tr -d 'B')
# Add 10000 blocks of free space
minsize=$(($minsize + 10000))
echo "parted_output: $parted_output, partnum: $partnum, partstart: $partstart, minsize: $minsize"
sudo resize2fs -fp "/dev/mapper/${MOUNT_IMG}p2" $minsize
UnmountIMG "$1"
# Set new partition size
partnewsize=$(($minsize * $blocksize))
newpartend=$(($partstart + $partnewsize))
echo "partnewsize: $partnewsize, newpartend: $newpartend"
if ! sudo parted -s -a minimal "$1" rm "$partnum"; then
rc=$?
echo "parted failed: $rc"
return
fi
if ! sudo parted -s "$1" unit B mkpart primary "$partstart" "$newpartend"; then
rc=$?
echo "parted failed: $rc"
return
fi
# Truncate the file
if ! endresult=$(sudo parted -ms "$1" unit B print free); then
rc=$?
echo "parted failed: $rc"
return
fi
endresult=$(tail -1 <<<"$endresult" | cut -d ':' -f 2 | tr -d 'B')
if ! sudo truncate -s "$endresult" "$1"; then
rc=$?
echo "truncate failed: $rc"
return
fi
MountIMG "$1"
# Run e2fsck
echo "Running fsck"
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "$1"
MountIMG "$1"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
# Run resize2fs
echo "Running resize2fs"
sudo resize2fs -p "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "$1"
CompactIMG "$1"
}
function CreateUpdatedIMG() {
if [ ! -f "${UPDATED_IMG}" ]; then
echo "Creating updated image ..."
# Get ODROID M1 Ubuntu source image
if [ ! -f "$SOURCE_IMGXZ" ]; then
echo "Retrieving Ubuntu $SOURCE_RELEASE source image ..."
wget "https://dn.odroid.com/RK3568/ODROID-M1/Ubuntu/${SOURCE_IMGXZ}"
fi
# Extract and compact our source image from the xz if the source image isn't present
if [ ! -f "${SOURCE_IMG}" ]; then
echo "Extracting Ubuntu $SOURCE_RELEASE source image ..."
xzcat --threads=0 "$SOURCE_IMGXZ" >"${SOURCE_IMG}"
fi
cp -vf "${SOURCE_IMG}" "${UPDATED_IMG}"
# Expands the target image to help us not run out of space and encounter errors
echo "Expanding target image free space ..."
truncate -s +1209715200 "${UPDATED_IMG}"
sync
sync
# Mount image
MountIMG "${UPDATED_IMG}"
# Run fdisk
# Get the starting offset of the root partition
PART_START=$(sudo parted "/dev/${MOUNT_IMG}" -ms unit s p | grep ":ext4" | cut -f 2 -d: | sed 's/[^0-9]//g')
# Perform fdisk to correct the partition table
sudo fdisk "/dev/${MOUNT_IMG}" <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
UnmountIMG "${UPDATED_IMG}"
MountIMG "${UPDATED_IMG}"
# Run e2fsck
echo "Running e2fsck"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_IMG}"
MountIMG "${UPDATED_IMG}"
# Run resize2fs
echo "Running resize2fs"
sudo resize2fs -p "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_IMG}"
# Compact image after our file operations
CompactIMG "${UPDATED_IMG}"
MountIMG "${UPDATED_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
sync
sync
sleep "${SLEEP_SHORT}"
# Prepare chroot
sudo cp -f /usr/bin/qemu-aarch64-static /mnt/usr/bin
# Copy resolv.conf from local host so we have networking in our chroot
echo "Copying resolv.conf"
sudo mkdir -p /mnt/run/systemd/resolve
sudo touch /mnt/run/systemd/resolve/stub-resolv.conf
sudo cat /run/systemd/resolve/stub-resolv.conf | sudo tee /mnt/run/systemd/resolve/stub-resolv.conf >/dev/null
# Update apt sources for Jammy
echo "Updating apt sources"
sed -i 's/focal/jammy/g' /mnt/etc/apt/sources.list
sed -i 's/focal/jammy/g' /mnt/etc/apt/sources.list.d/ppa-linuxfactory-or-kr.list
# Remove flash-kernel hooks
echo "Removing flash-kernel hooks"
sudo mv /mnt/etc/kernel/postinst.d/zz-flash-kernel /mnt/etc/zz-flash-kernel-postinst
sudo mv /mnt/etc/kernel/postrm.d/zz-flash-kernel /mnt/etc/zz-flash-kernel
sudo mv /mnt/etc/initramfs/post-update.d//flash-kernel /mnt/flash-kernel
sudo chroot /mnt /bin/bash <<EOF
# Update and install packages
DEBIAN_FRONTEND=noninteractive apt update && DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade -y && DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install netplan.io network-manager linux-support-6.0.0-odroid linux-image-6.0.0-odroid-arm64 usbutils ethtool ufw macchanger man-db locales ifupdown nano libubootenv-tool git bc curl unzip -y
EOF
sudo chroot /mnt /bin/bash <<EOF
# Purge old 4.x kernels
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" purge linux-image-4.* -y
EOF
sudo chroot /mnt /bin/bash <<EOF
# Clean up after ourselves and clean out package cache to keep the image small
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" autoremove -y
EOF
# Restore flash-kernel hooks
echo "Restoring flash-kernel hooks"
sudo mv /mnt/etc/zz-flash-kernel-postinst /mnt/etc/kernel/postrm.d/zz-flash-kernel
sudo mv /mnt/etc/zz-flash-kernel /mnt/etc/kernel/postinst.d/zz-flash-kernel
sudo mv /mnt/flash-kernel /mnt/etc/initramfs/post-update.d//flash-kernel
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${UPDATED_IMG}"
CompactIMG "${UPDATED_IMG}"
echo "Update image completed"
fi
}
function ModifyFilesystem() {
# Enable NPU overlay helper script
#sudo cp enable-npu.sh /mnt/home/odroid/enable-npu.sh
#sudo chmod +x /mnt/home/odroid/enable-npu.sh
#sudo cp disable-npu.sh /mnt/home/odroid/disable-npu.sh
#sudo chmod +x /mnt/home/odroid/disable-npu.sh
# Resize rootfs helper script
sudo cp -f resize_rootfs.sh /mnt/usr/sbin/
sudo chmod +x /mnt/usr/sbin/resize_rootfs.sh
cat >"/mnt/lib/systemd/system/resize-rootfs.service" <<EOF
[Unit]
Description=Resize root filesystem to fit available disk space
After=systemd-remount-fs.service
[Service]
Type=oneshot
ExecStart=-/usr/sbin/resize_rootfs.sh
ExecStartPost=/bin/systemctl disable resize-rootfs.service
[Install]
WantedBy=basic.target
EOF
# Remove flash-kernel hooks
echo "Removing flash-kernel hooks"
sudo mv /mnt/etc/kernel/postinst.d/zz-flash-kernel /mnt/etc/zz-flash-kernel-postinst
sudo mv /mnt/etc/kernel/postrm.d/zz-flash-kernel /mnt/etc/zz-flash-kernel
sudo mv /mnt/etc/initramfs/post-update.d//flash-kernel /mnt/flash-kernel
# Copy new boot.scr
sudo cp boot.scr /mnt/boot/boot.scr
# Create forcefsck file for first startup
sudo touch /mnt/forcefsck
# Prepare chroot
sudo cp -f /usr/bin/qemu-aarch64-static /mnt/usr/bin
# Fix dtbs and image path
initrdpath=$(ls /mnt/boot | grep -m 1 'initrd.img-6.0')
vmlinuzpath=$(ls /mnt/boot | grep -m 1 'vmlinuz-6.0')
olddir=$(pwd)
kvers=$(ls /mnt/boot | grep -m 1 'initrd.img-6.0' | cut -d- -f2-)
mkdir -p "/mnt/boot/dtbs/$kvers/rockchip/overlays/odroidm1"
cp -rfv /mnt/usr/lib/linux-image-$kvers/* "/mnt/boot/dtbs/$kvers"
cd /mnt/boot
ln -sfv "$initrdpath" initrd.img
ln -sfv "$vmlinuzpath" vmlinuz
ln -sfv "dtbs/$kvers/rockchip/rk3568-odroid-m1.dtb" dtb
ln -sfv "dtbs/$kvers/rockchip/rk3568-odroid-m1.dtb" "dtb-$kvers"
cd "dtbs/$kvers"
ln -sfv rockchip/rk3568-odroid-m1.dtb rk3568-odroid-m1.dtb
cd "$olddir"
# Enter Ubuntu image chroot
echo "Entering chroot of IMG file"
sudo chroot /mnt /bin/bash <<EOF
# Enable SSH
systemctl enable ssh
# Enable resize_rootfs oneshot service
systemctl enable resize-rootfs.service
# Change machine name from "server"
echo "odroidm1" > /etc/hostname
sed -i 's/server/odroidm1/g' /etc/hosts
# Fix permissions
chown -R odroid /home/odroid
EOF
echo "The chroot container has exited"
# First startup helper script
sudo cp -f first_startup.sh /mnt/usr/sbin/
sudo chmod +x /mnt/usr/sbin/first_startup.sh
cat >"/mnt/lib/systemd/system/first_startup.service" <<EOF
[Unit]
Description=First startup service to configure ODROID image
After=networking.service
[Service]
Type=oneshot
ExecStart=-/usr/sbin/first_startup.sh
ExecStartPost=/bin/systemctl disable first_startup.service
[Install]
WantedBy=multi-user.target
EOF
sudo chroot /mnt /bin/bash <<EOF
systemctl enable first_startup.service
EOF
# Restore flash-kernel hooks
echo "Restoring flash-kernel hooks"
sudo mv /mnt/etc/zz-flash-kernel-postinst /mnt/etc/kernel/postrm.d/zz-flash-kernel
sudo mv /mnt/etc/zz-flash-kernel /mnt/etc/kernel/postinst.d/zz-flash-kernel
sudo mv /mnt/flash-kernel /mnt/etc/initramfs/post-update.d//flash-kernel
}
function ModifyDesktopFilesystem() {
echo ""
}
function CreateServerIMG() {
# Create target image from Ubuntu source image
echo "Creating target image ..."
if [ -f "${TARGET_IMG}" ]; then
sudo rm -rf "${TARGET_IMG}"
fi
cp -vf "${UPDATED_IMG}" "${TARGET_IMG}"
MountIMG "${TARGET_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Modify filesystem
ModifyFilesystem
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${TARGET_IMG}"
CompactIMG "${TARGET_IMG}"
}
function CreateUpdatedMateIMG() {
# Build mate-desktop image
if [ ! -f "${UPDATED_MATE_IMG}" ]; then
echo "Creating updated mate-desktop image ..."
cp -vf "${UPDATED_IMG}" "${UPDATED_MATE_IMG}"
# Expands the target image by approximately 2GB to help us not run out of space and encounter errors
echo "Expanding desktop image free space ..."
truncate -s +6009715200 "${UPDATED_MATE_IMG}"
sync
sync
MountIMG "${UPDATED_MATE_IMG}"
# Run fdisk
# Get the starting offset of the root partition
PART_START=$(sudo parted "/dev/${MOUNT_IMG}" -ms unit s p | grep ":ext4" | cut -f 2 -d: | sed 's/[^0-9]//g')
# Perform fdisk to correct the partition table
sudo fdisk "/dev/${MOUNT_IMG}" <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
UnmountIMG "${UPDATED_MATE_IMG}"
MountIMG "${UPDATED_MATE_IMG}"
# Run e2fsck
echo "Running e2fsck"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_MATE_IMG}"
MountIMG "${UPDATED_MATE_IMG}"
# Run resize2fs
echo "Running resize2fs"
sudo resize2fs -p "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_MATE_IMG}"
# Compact image after our file operations
CompactIMG "${UPDATED_MATE_IMG}"
MountIMG "${UPDATED_MATE_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Remove flash-kernel hooks
echo "Removing flash-kernel hooks"
sudo mv /mnt/etc/kernel/postinst.d/zz-flash-kernel /mnt/etc/zz-flash-kernel-postinst
sudo mv /mnt/etc/kernel/postrm.d/zz-flash-kernel /mnt/etc/zz-flash-kernel
sudo mv /mnt/etc/initramfs/post-update.d//flash-kernel /mnt/flash-kernel
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update && DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install ubuntu-mate-desktop -y
EOF
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade -y
EOF
# Restore flash-kernel hooks
echo "Restoring flash-kernel hooks"
sudo mv /mnt/etc/zz-flash-kernel-postinst /mnt/etc/kernel/postrm.d/zz-flash-kernel
sudo mv /mnt/etc/zz-flash-kernel /mnt/etc/kernel/postinst.d/zz-flash-kernel
sudo mv /mnt/flash-kernel /mnt/etc/initramfs/post-update.d//flash-kernel
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${UPDATED_MATE_IMG}"
CompactIMG "${UPDATED_MATE_IMG}"
fi
}
function CreateMateIMG() {
# Build mate-desktop image
echo "Creating mate-desktop image ..."
if [ -f "${MATE_DESKTOP_IMG}" ]; then
sudo rm -rf "${MATE_DESKTOP_IMG}"
fi
cp -vf "${UPDATED_MATE_IMG}" "${MATE_DESKTOP_IMG}"
# Mount image
MountIMG "${MATE_DESKTOP_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Modify filesystem
ModifyFilesystem
# Modify desktop filesystem
ModifyDesktopFilesystem
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${MATE_DESKTOP_IMG}"
CompactIMG "${MATE_DESKTOP_IMG}"
}
function CreateUpdatedDesktopIMG() {
# Build ubuntu-desktop image
if [ ! -f "${UPDATED_DESKTOP_IMG}" ]; then
echo "Creating ubuntu-desktop image ..."
cp -vf "${UPDATED_IMG}" "${UPDATED_DESKTOP_IMG}"
# Expands the target image by approximately 2GB to help us not run out of space and encounter errors
echo "Expanding desktop image free space ..."
truncate -s +6009715200 "${UPDATED_DESKTOP_IMG}"
sync
sync
MountIMG "${UPDATED_DESKTOP_IMG}"
# Run fdisk
# Get the starting offset of the root partition
PART_START=$(sudo parted "/dev/${MOUNT_IMG}" -ms unit s p | grep ":ext4" | cut -f 2 -d: | sed 's/[^0-9]//g')
# Perform fdisk to correct the partition table
sudo fdisk "/dev/${MOUNT_IMG}" <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
UnmountIMG "${UPDATED_DESKTOP_IMG}"
MountIMG "${UPDATED_DESKTOP_IMG}"
# Run e2fsck
echo "Running e2fsck"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_DESKTOP_IMG}"
MountIMG "${UPDATED_DESKTOP_IMG}"
# Run resize2fs
echo "Running resize2fs"
sudo resize2fs -p "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_DESKTOP_IMG}"
# Compact image after our file operations
CompactIMG "${UPDATED_DESKTOP_IMG}"
MountIMG "${UPDATED_DESKTOP_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Remove flash-kernel hooks
echo "Removing flash-kernel hooks"
sudo mv /mnt/etc/kernel/postinst.d/zz-flash-kernel /mnt/etc/zz-flash-kernel-postinst
sudo mv /mnt/etc/kernel/postrm.d/zz-flash-kernel /mnt/etc/zz-flash-kernel
sudo mv /mnt/etc/initramfs/post-update.d//flash-kernel /mnt/flash-kernel
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt update && DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install ubuntu-desktop -y
EOF
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" autoremove -y
EOF
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade -y
EOF
# Restore flash-kernel hooks
echo "Restoring flash-kernel hooks"
sudo mv /mnt/etc/zz-flash-kernel-postinst /mnt/etc/kernel/postrm.d/zz-flash-kernel
sudo mv /mnt/etc/zz-flash-kernel /mnt/etc/kernel/postinst.d/zz-flash-kernel
sudo mv /mnt/flash-kernel /mnt/etc/initramfs/post-update.d//flash-kernel
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${UPDATED_DESKTOP_IMG}"
CompactIMG "${UPDATED_DESKTOP_IMG}"
fi
}
function CreateDesktopIMG() {
# Build ubuntu-desktop image
echo "Creating ubuntu-desktop image ..."
if [ -f "${DESKTOP_IMG}" ]; then
sudo rm -rf "${DESKTOP_IMG}"
fi
cp -vf "${UPDATED_DESKTOP_IMG}" "${DESKTOP_IMG}"
MountIMG "${DESKTOP_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Modify filesystem
ModifyFilesystem
# Modify desktop filesystem
ModifyDesktopFilesystem
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${DESKTOP_IMG}"
CompactIMG "${DESKTOP_IMG}"
}
function CreateUpdatedXubuntuIMG() {
# Build mate-desktop image
if [ ! -f "${UPDATED_XUBUNTU_IMG}" ]; then
echo "Creating updated xubuntu-desktop image ..."
cp -vf "${UPDATED_IMG}" "${UPDATED_XUBUNTU_IMG}"
# Expands the target image by approximately 2GB to help us not run out of space and encounter errors
echo "Expanding desktop image free space ..."
truncate -s +6009715200 "${UPDATED_XUBUNTU_IMG}"
sync
sync
MountIMG "${UPDATED_XUBUNTU_IMG}"
# Run fdisk
# Get the starting offset of the root partition
PART_START=$(sudo parted "/dev/${MOUNT_IMG}" -ms unit s p | grep ":ext4" | cut -f 2 -d: | sed 's/[^0-9]//g')
# Perform fdisk to correct the partition table
sudo fdisk "/dev/${MOUNT_IMG}" <<EOF
p
d
2
n
p
2
$PART_START
p
w
EOF
UnmountIMG "${UPDATED_XUBUNTU_IMG}"
MountIMG "${UPDATED_XUBUNTU_IMG}"
# Run e2fsck
echo "Running e2fsck"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_XUBUNTU_IMG}"
MountIMG "${UPDATED_XUBUNTU_IMG}"
# Run resize2fs
echo "Running resize2fs"
sudo resize2fs -p "/dev/mapper/${MOUNT_IMG}p2"
sync
sync
sleep "${SLEEP_SHORT}"
UnmountIMG "${UPDATED_XUBUNTU_IMG}"
# Compact image after our file operations
CompactIMG "${UPDATED_XUBUNTU_IMG}"
MountIMG "${UPDATED_XUBUNTU_IMG}"
MountIMGPartitions "${MOUNT_IMG}"
# Remove flash-kernel hooks
echo "Removing flash-kernel hooks"
sudo mv /mnt/etc/kernel/postinst.d/zz-flash-kernel /mnt/etc/zz-flash-kernel-postinst
sudo mv /mnt/etc/kernel/postrm.d/zz-flash-kernel /mnt/etc/zz-flash-kernel
sudo mv /mnt/etc/initramfs/post-update.d//flash-kernel /mnt/flash-kernel
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" update && DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" install xubuntu-desktop -y
EOF
sudo chroot /mnt /bin/bash <<EOF
DEBIAN_FRONTEND=noninteractive apt -o Dpkg::Options::="--force-confdef" -o Dpkg::Options::="--force-confold" full-upgrade -y
EOF
# Restore flash-kernel hooks
echo "Restoring flash-kernel hooks"
sudo mv /mnt/etc/zz-flash-kernel-postinst /mnt/etc/kernel/postrm.d/zz-flash-kernel
sudo mv /mnt/etc/zz-flash-kernel /mnt/etc/kernel/postinst.d/zz-flash-kernel
sudo mv /mnt/flash-kernel /mnt/etc/initramfs/post-update.d//flash-kernel
# Run the after clean function
CleanIMG
# Run fsck on image then unmount and remount
UnmountIMGPartitions
sudo fsck.ext4 -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo e2fsck -yfv "/dev/mapper/${MOUNT_IMG}p2"
sudo fsck.ext2 -yfv "/dev/mapper/${MOUNT_IMG}p1"
UnmountIMG "${UPDATED_XUBUNTU_IMG}"
CompactIMG "${UPDATED_XUBUNTU_IMG}"
fi
}