-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmkarm-image.sh
executable file
·166 lines (150 loc) · 3.87 KB
/
mkarm-image.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
#!/bin/bash
# Copyright (C) 2021 <Jerry Vonau>
SIZE=$1
SOURCE=$2
IMAGE=$3
MP=/tmp/mkarm-mp
cache_name=""
rm -rf "${MP}"
mkdir "${MP}"
echo "$1 $2 $3"
echo "Preparing $1 GB image named $3 from $2"
#COMMON
mk_image(){
dd if=/dev/zero of="${IMAGE}" bs=1 count=0 seek="${SIZE}G" status=progress
losetup -f "${IMAGE}"
sleep 2
LOOP=$(losetup | grep "${IMAGE}" | awk -F " " '{ print $1 }')
echo "loop is $LOOP"
sleep 2
}
case "${SOURCE}" in
*.xz)
mk_image
xzcat -c "$SOURCE" | dd bs=4M of="${LOOP}" status=progress
sync
losetup -d "${LOOP}"
cache_name=$(basename "${SOURCE}" | sed s/.xz//)
;;
*.zip)
mk_image
unzip -p "${SOURCE}" | dd bs=4M of="${LOOP}" status=progress
sync
losetup -d "${LOOP}"
cache_name=$(basename "${SOURCE}" | sed s/.zip//)
;;
*.gz)
mk_image
gunzip -c if="${SOURCE}" | dd bs=4M of="${LOOP}" status=progress
sync
losetup -d "${LOOP}"
cache_name=$(basename "${SOURCE}" | sed s/.gz//)
;;
*.img)
mk_image
dd bs=4M if="${SOURCE}" of="${LOOP}" status=progress
sync
losetup -d "${LOOP}"
cache_name=$(basename "${SOURCE}" | sed s/.img//)
;;
*)
echo "unknown extention"
exit 1
;;
esac
sleep 2
losetup -P -f "${IMAGE}"
LOOP=$(losetup | grep "${IMAGE}" | awk -F " " '{ print $1 }')
echo "growpart $LOOP"
growpart "${LOOP}" 2
sleep 2
sync
echo "resize $LOOP"
e2fsck -f "${LOOP}p2"
sync
resize2fs "${LOOP}p2"
sync
losetup -d "${LOOP}"
sleep 2
losetup -P -f "${IMAGE}"
LOOP=$(losetup | grep "${IMAGE}" | awk -F " " '{ print $1 }')
echo "working loop is $LOOP"
e2fsck -f "${LOOP}p2"
sync
mount "${LOOP}p2" "${MP}"
if [ -d "${MP}"/boot/firmware ]; then
mount "${LOOP}p1" "${MP}"/boot/firmware
else
mount "${LOOP}p1" "${MP}"/boot
fi
mount --bind /sys "${MP}"/sys
mount -t proc proc "${MP}"/proc
mount --bind /dev "${MP}"/dev
mount --bind /dev/pts "${MP}"/dev/pts
mv "${MP}"/etc/resolv.conf "${MP}"/etc/resolv.conf.hold
touch "${MP}"/etc/resolv.conf
mount --bind /etc/resolv.conf "${MP}"/etc/resolv.conf
if [ $(uname -m) = "x86_64" ];then
cp /usr/bin/qemu-arm-static "${MP}"/usr/bin/
fi
#/usr/share/doc/apt/examples/configure-index varies by distro
# ubuntu apt::keep-downloaded-packages "<BOOL>";
# debian Binary::apt::APT::Keep-Downloaded-Packages "<BOOL>";
# handle this by OS in runme.sh
if [ -d apt_cache ]; then
use_cache=1
mkdir -p apt_cache/"${cache_name}"
mount --bind apt_cache/"${cache_name}" "${MP}"/var/cache/apt/archives
# cat << EOF > "${MP}"/etc/apt/apt.conf.d/99-keep
#Binary::apt::APT::Keep-Downloaded-Packages 1;
#EOF
fi
# target of bind mounted files must exist
if [ -f sources/en.zip ]; then
touch "${MP}"/tmp/en.zip
mount --bind sources/en.zip "${MP}"/tmp/en.zip
fi
if [ -f sources/local_vars.yml ]; then
mkdir -p "${MP}"/etc/iiab/install-flags
cp sources/local_vars.yml "${MP}"/etc/iiab/
fi
cp -f runme.sh "${MP}"/runme.sh
chmod 0755 "${MP}"/runme.sh
echo "ENTERING CHROOT"
#echo "exit to leave"
#chroot "${MP}"
chroot "${MP}" /runme.sh
echo "out of chroot"
rm "${MP}"/runme.sh
# undo bind mounted files
if [ -f "${MP}"/tmp/en.zip ]; then
umount "${MP}"/tmp/en.zip
fi
umount "${MP}"/etc/resolv.conf
rm "${MP}"/etc/resolv.conf
mv "${MP}"/etc/resolv.conf.hold "${MP}"/etc/resolv.conf
#if eval "$(mount | grep "${cache_name}")"; then
if [ "${use_cache}" = "1" ]; then
echo "cache name ${cache_name}"
rm "${MP}"/etc/apt/apt.conf.d/01-keep
umount "${MP}"/var/cache/apt/archives
fi
if [ -f "${MP}"/usr/bin/qemu-arm-static ]; then
rm "${MP}"/usr/bin/qemu-arm-static
fi
# clean trash from iiab-refresh-wiki-docs
rm -rf "${MP}"/tmp/*
# end cleanup
sync
if [ -d "${MP}"/boot/firmware ]; then
umount "${MP}"/boot/firmware
else
umount "${MP}"/boot
fi
umount "${MP}"/dev/pts
umount "${MP}"/dev
umount "${MP}"/sys
umount "${MP}"/proc
umount "${MP}"
losetup -d "${LOOP}"
echo "$IMAGE created"