-
Notifications
You must be signed in to change notification settings - Fork 48
/
Copy pathbuild-archlinux.sh
executable file
·164 lines (134 loc) · 5.45 KB
/
build-archlinux.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
#!/bin/bash
# requirements: sudo jq sfdisk u-boot-tools qemu bimfmt_misc parted bsdtar
# require armbian kernel and u-boot
# run following command to init pacman keyring:
# pacman-key --init
# pacman-key --populate archlinuxarm
[ "$EUID" != "0" ] && echo "please run as root" && exit 1
armbian_mount_point="/mnt/armbian_rootfs"
rootfs_mount_point="/mnt/arch_rootfs"
tmpdir="tmp"
output="output"
origin="latest"
target="beikeyun-$(date +%Y-%m-%d)"
rootsize=1500
ROOTOFFSET=32768
qemu_static="./tools/qemu/qemu-aarch64-static"
func_umount_armbian() {
umount $armbian_mount_point
}
func_mount_armbian() {
local img=$1
local mount_point=$2
[ ! -f "$img" ] && echo "img file not found!" && return 1
start=$(sfdisk -J $img | jq .partitiontable.partitions[0].start)
offset=$((start * 512))
mkdir -p $mount_point
mount -o loop,offset=$offset $1 $mount_point
}
func_generate() {
local armbian_img=$1
local dtb=$2
local rootfs=$3
local img_new=${4:-archlinux.img}
[ ! -f "$dtb" ] && echo "dtb file not found!" && return 1
[ ! -f "$rootfs" ] && echo "archlinux rootfs file not found!" && return 1
[ ! -f "$armbian_img" ] && echo "armbian img file not found!" && return 1
# create ext4 rootfs img
mkdir -p ${tmpdir}
echo "create ext4 rootfs, size: ${rootsize}M"
dd if=/dev/zero bs=1M status=none count=$rootsize of=$tmpdir/rootfs.img
mkfs.ext4 -q -m 2 $tmpdir/rootfs.img
# mount rootfs
mkdir -p $rootfs_mount_point
mount -o loop $tmpdir/rootfs.img $rootfs_mount_point
# extract archlinux rootfs
echo "extract archlinux rootfs($rootfs) to $rootfs_mount_point"
bsdtar -xpf $rootfs -C $rootfs_mount_point
# chroot to archlinux rootfs
echo "configure binfmt to chroot"
modprobe binfmt_misc
if [ -e /proc/sys/fs/binfmt_misc/register ]; then
echo -1 > /proc/sys/fs/binfmt_misc/status
echo ":arm64:M::\x7fELF\x02\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\xb7\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-aarch64-static:OC" > /proc/sys/fs/binfmt_misc/register
echo "copy $qemu_static to $rootfs_mount_point/usr/bin/"
cp $qemu_static $rootfs_mount_point/usr/bin/qemu-aarch64-static
else
echo "Could not configure binfmt for qemu!" && exit 1
fi
cp ./tools/archlinux/init.sh $rootfs_mount_point/init.sh
echo "chroot to archlinux rootfs"
LANG=C LC_ALL=C chroot $rootfs_mount_point /init.sh
rm -f $rootfs_mount_point/init.sh
[ -n "$qemu" ] && rm -f $rootfs_mount_point/$qemu || rm -f $rootfs_mount_point/usr/bin/qemu-aarch64-static
# mount armbian rootfs
func_mount_armbian $armbian_img $armbian_mount_point
# get /boot /lib/modules /lib/firmware
echo "copy /boot,/lib/modules,/lib/firmware from armbian"
rm -rf $rootfs_mount_point/boot && cp -rf $armbian_mount_point/boot $rootfs_mount_point/boot
rm -rf $rootfs_mount_point/lib/modules && cp -rf $armbian_mount_point/lib/modules $rootfs_mount_point/lib/modules
rm -rf $rootfs_mount_point/lib/firmware && cp -rf $armbian_mount_point/lib/firmware $rootfs_mount_point/lib/firmware
#umount armbian rootfs
func_umount_armbian
# patch /boot
echo "patch /boot"
cp -f $dtb $rootfs_mount_point/boot/
sed -i '/^verbosity/cverbosity=7' $rootfs_mount_point/boot/armbianEnv.txt
sed -i '/^rootdev/crootdev=\/dev\/mmcblk0p1' $rootfs_mount_point/boot/armbianEnv.txt
if [ -z "`grep fdtfile $rootfs_mount_point/boot/armbianEnv.txt`" ]; then
echo "fdtfile=$(basename $dtb)" >> $rootfs_mount_point/boot/armbianEnv.txt
fi
if [ -z "`grep extraargs $rootfs_mount_point/boot/armbianEnv.txt`" ]; then
echo "extraargs=rw console=ttyFIQ0,1500000 audit=0" >> $rootfs_mount_point/boot/armbianEnv.txt
fi
sed -i 's#${prefix}dtb/${fdtfile}#${prefix}/${fdtfile}#' $rootfs_mount_point/boot/boot.cmd
mkimage -C none -T script -d $rootfs_mount_point/boot/boot.cmd $rootfs_mount_point/boot/boot.scr
# add resize script
echo "add resize mmc script"
cp ./tools/archlinux/resizemmc.service $rootfs_mount_point/lib/systemd/system/
cp ./tools/archlinux/resizemmc.sh $rootfs_mount_point/sbin/
mkdir -p $rootfs_mount_point/etc/systemd/system/basic.target.wants
ln -sf /lib/systemd/system/resizemmc.service $rootfs_mount_point/etc/systemd/system/basic.target.wants/resizemmc.service
touch $rootfs_mount_point/root/.need_resize
# generate img
umount $rootfs_mount_point
echo "copy boot header from armbian img"
dd if=$armbian_img bs=512 count=$ROOTOFFSET status=none of=${output}/${img_new}
cat $tmpdir/rootfs.img >> ${output}/${img_new}
parted -s ${output}/${img_new} -- mklabel msdos
parted -s ${output}/${img_new} -- mkpart primary ext4 ${ROOTOFFSET}s -1s
sync
rm -f $tmpdir/rootfs.img
}
func_release() {
local dlpkg=$1
local dtb=$2
local rootfs=$3
[ ! -f "$dlpkg" ] && echo "dlpkg not found!" && return 1
rm -rf ${tmpdir}
echo "Extract 7zpkg and checksum..."
7z x -y -o${tmpdir} $dlpkg >/dev/null && cd ${tmpdir} && sha256sum -c sha256sum.sha && cd - > /dev/null || exit 1
armbian_img="$(ls ${tmpdir}/*.img)"
echo "archlinux rootfs: $rootfs"
echo "armbian image file: $armbian_img"
echo "dtb file: $dtb"
imgname_new="`basename $rootfs | sed "s/${origin}/${target}/" | sed 's/.tar.gz$/.img/'`"
func_generate $armbian_img $dtb $rootfs $imgname_new
echo "new image file: $imgname_new"
if [ -n "$TRAVIS_TAG" ]; then
xz -f -T0 -v ${output}/${imgname_new}
fi
rm -rf ${tmpdir}
}
case "$1" in
generate)
func_generate "$2" "$3" "$4"
;;
release)
func_release "$2" "$3" "$4"
;;
*)
echo "Usage: $0 { generate [armbian-img] [dtb] [rootfs] | release [armbian-7zpkg] [dtb] [rootfs] }"
exit 1
;;
esac