forked from hanwckf/build-beikeyun
-
Notifications
You must be signed in to change notification settings - Fork 0
/
build-alpine.sh
executable file
·171 lines (141 loc) · 5.59 KB
/
build-alpine.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
#!/bin/bash
# requirements: sudo jq sfdisk u-boot-tools qemu bimfmt_misc parted
# require armbian kernel and u-boot
[ "$EUID" != "0" ] && echo "please run as root" && exit 1
armbian_mount_point="/mnt/armbian_rootfs"
rootfs_mount_point="/mnt/alpine_rootfs"
tmpdir="tmp"
output="output"
origin="minirootfs"
target="beikeyun"
rootsize=700
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:-alpine.img}
[ ! -f "$dtb" ] && echo "dtb file not found!" && return 1
[ ! -f "$rootfs" ] && echo "alpine 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 alpine rootfs
echo "extract alpine rootfs($rootfs) to $rootfs_mount_point"
tar xpf $rootfs -C $rootfs_mount_point
# 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"
cp -rf $armbian_mount_point/boot $rootfs_mount_point/boot
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" >> $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
# change mirrors
if [ -z "$TRAVIS" ]; then
sed -i 's#http://dl-cdn.alpinelinux.org#https://mirrors.tuna.tsinghua.edu.cn#' $rootfs_mount_point/etc/apk/repositories
echo "nameserver 119.29.29.29" > $rootfs_mount_point/etc/resolv.conf
else
echo "nameserver 8.8.8.8" > $rootfs_mount_point/etc/resolv.conf
fi
# chroot to alpine 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/alpine/init.sh $rootfs_mount_point/init.sh
echo "chroot to alpine rootfs"
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
if [ -z "$TRAVIS" ]; then
sed -i 's/https/http/' $rootfs_mount_point/etc/apk/repositories
else
sed -i 's#http://dl-cdn.alpinelinux.org#http://mirrors.tuna.tsinghua.edu.cn#' $rootfs_mount_point/etc/apk/repositories
fi
# add resize script
cp ./tools/alpine/resizemmc.sh $rootfs_mount_point/sbin/resizemmc.sh
cp ./tools/alpine/resizemmc $rootfs_mount_point/etc/init.d/resizemmc
ln -sf /etc/init.d/resizemmc $rootfs_mount_point/etc/runlevels/default/resizemmc
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 "alpine 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