forked from SolidRun/imx8mp_build
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdeboot.sh
executable file
·295 lines (252 loc) · 7.55 KB
/
deboot.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
#!/bin/bash
#
# deboot.sh
# script to build Ubuntu rootfs (for arm64, armhf, powerpc, ppc64el)
#
# Copyright 2017 knotdevel
# Released under the MIT license
# http://opensource.org/licenses/mit-license.php
#
#
# Prerequisite:
#
# 1. Setup Ubuntu 16.04 64bit desktop machine connected to the Internet
# 2. Upgrade the software
# $ sudo apt update
# $ sudo apt upgrade
# 3. Install below packages
# $ sudo apt install debootstrap qemu-user-static
# 4. Download this script
# $ git clone https://gist.github.com/knotdevel/bc5b8d547edd374bf0017fe16bf751bc deboot
# $ cd deboot
#
#
# Usage:
#
# $ ./deboot.sh
#
#
# Configuration:
# If you want to change the configuration, create a file 'config' with below contents
# in the same directory.
#
# # example of the config
# TARGET_ARCH=arm64
# DISTRO_MIRROR='http://jp.ports.ubuntu.com/ubuntu-ports'
# DISTRO_CODE=xenial
# USERNAME=myusername
# PASSWORD=complex!password#123
# TZ_AREA=Asia
# TZ_CITY=Tokyo
# LOCALE_LANG=ja_JP.UTF-8
# ADD_LIST='git build-essential ubuntu-desktop'
#
#
# Output:
#
# apt cache file : apt-cache-xenial-arm64_<date>_<time>.tgz
# rootfs cache file : rfs-cache-xenial-arm64_<date>_<time>.tgz
# final rootfs : rootfs-xenial-arm64_<date>_<time>.tgz
#
#
set -e
export LC_ALL=C
export LANGUAGE=C
export LANG=C
function setup_config() {
# default configs
TARGET_ARCH=arm64
DISTRO_MIRROR='http://ports.ubuntu.com/ubuntu-ports'
DISTRO_CODE=bionic
USERNAME=analog
PASSWORD=analog
TZ_AREA=Etc
TZ_CITY=UTC
LOCALE_LANG=en_US.UTF-8
ADD_LIST='git build-essential gcc autoconf nano vim'
ADD_LIST_ST_3='i2c-tools v4l-utils rfkill wpasupplicant libtool libconfig-dev avahi-daemon'
# output example of the config file
# cat <<EOF>config-example
# config file example
# modify below, then rename to 'config'
#TARGET_ARCH=arm64
#DISTRO_MIRROR='http://jp.ports.ubuntu.com/ubuntu-ports'
#DISTRO_CODE=distro
#USERNAME=myusername
#PASSWORD=complex!password#123
#TZ_AREA=Asia
#TZ_CITY=Tokyo
#LOCALE_LANG=ja_JP.UTF-8
#ADD_LIST='git build-essential ubuntu-desktop'
#EOF
# user overrides the default configs
if [ -f config ]; then
source config
fi
# check configs
echo TARGET_ARCH : ${TARGET_ARCH}
echo MIRROR : ${DISTRO_MIRROR}
echo CODE : ${DISTRO_CODE}
echo USERNAME : ${USERNAME}
echo PASSWORD : ${PASSWORD}
echo TZ_AREA : ${TZ_AREA}
echo TZ_CITY : ${TZ_CITY}
echo LANG : ${LOCALE_LANG}
echo ADD_LIST : ${ADD_LIST}
echo ADD_LIST_STAGE_3 : ${ADD_LIST_ST_3}
# language pack
LANG_PACK=language-pack-${LOCALE_LANG%%_*}-base
INCLUDE_LIST=${ADD_LIST}" "${LANG_PACK}
echo INCLUDE_LIST: ${INCLUDE_LIST}
#INCLUDE_LIST=language-pack-en-base
#INCLUDE_LIST=language-pack-ja-base
#INCLUDE_LIST=language-pack-${LOCALE_LANG:0:2}-base
#EXCLUDE_LIST=lightdm-gtk-greeter
#COMPONENT_LIST=main,restricted,universe,multiverse
SCRIPT_DIR=$(cd $(dirname $0);pwd)
OUTPUT_DIR=${SCRIPT_DIR}/build/ubuntu
ROOTFS_TMP=${SCRIPT_DIR}/build/ubuntu/rootfs_tmp
ROOTFS_CACHE=rfs-cache-${DISTRO_CODE}-${TARGET_ARCH}
ROOTFS=rootfs-${DISTRO_CODE}-${TARGET_ARCH}
APT_CACHE=apt-cache-${DISTRO_CODE}-${TARGET_ARCH}
TIME_STAMP=$(echo `date +%Y%m%d_%H%M%S`)
case ${TARGET_ARCH} in
"arm64")
QEMU_ARCH=aarch64
;;
"armhf")
QEMU_ARCH=arm
;;
"ppc64el")
QEMU_ARCH=ppc64le
;;
"powerpc")
QEMU_ARCH=ppc
;;
*)
exit 1
;;
esac
}
function extract_apt_cache() {
if [ -f ${APT_CACHE}.tgz ]; then
sudo mkdir -p ${ROOTFS_TMP}/var/cache
sudo tar zxf ${APT_CACHE}.tgz -C ${ROOTFS_TMP}/var/cache/
fi
}
function archive_apt_cache() {
sudo rm -f ${APT_CACHE}.tgz
cd ${ROOTFS_TMP}/var/cache
# archive apt cache
sudo tar zcf ${OUTPUT_DIR}/${APT_CACHE}_${TIME_STAMP}.tgz apt/
# remove apt cache from rootfs to reduce rootfs image size
sudo rm -rf apt/
cd -
ln -s ${APT_CACHE}_${TIME_STAMP}.tgz ${APT_CACHE}.tgz
}
function install_qemu() {
sudo mkdir -p ${ROOTFS_TMP}/usr/bin
sudo cp /usr/bin/qemu-${QEMU_ARCH}-static ${ROOTFS_TMP}/usr/bin
}
function uninstall_qemu() {
sudo rm ${ROOTFS_TMP}/usr/bin/qemu-${QEMU_ARCH}-static
echo ;
}
function archive_rootfs_base() {
rm -f ${ROOTFS_CACHE}.tgz
cd ${ROOTFS_TMP}
sudo tar zcf ${OUTPUT_DIR}/${ROOTFS_CACHE}_${TIME_STAMP}.tgz .
cd -
ln -s ${ROOTFS_CACHE}_${TIME_STAMP}.tgz ${ROOTFS_CACHE}.tgz
}
function archive_rootfs() {
rm -f ${ROOTFS}.tgz
cd ${ROOTFS_TMP}
sudo tar zcf ${OUTPUT_DIR}/${ROOTFS}_${TIME_STAMP}.tgz .
cd -
ln -s ${ROOTFS}_${TIME_STAMP}.tgz ${ROOTFS}.tgz
}
function run_3rd_stage_script() {
# gen 3rd stage script
cat<<EOF>stage3.sh
#!/bin/bash -ex
# set locale
echo 'LANG="${LOCALE_LANG}"' > /etc/default/locale
locale-gen ${LOCALE_LANG}
dpkg-reconfigure -f noninteractive locales
# set timezone
echo "tzdata tzdata/Areas select ${TZ_AREA}" > /tmp/tmptz
echo "tzdata tzdata/Zones/${TZ_AREA} select ${TZ_CITY}" >> /tmp/tmptz
debconf-set-selections /tmp/tmptz
rm -f /etc/timezone
rm -f /etc/localtime
dpkg-reconfigure -f noninteractive tzdata
rm -f /tmp/tmptz
# set default hostname
echo aditof > /etc/hostname
# create user and passwd
useradd -m -d /home/${USERNAME} -s /bin/bash ${USERNAME}
usermod -a -G sudo,video ${USERNAME}
echo -e "${PASSWORD}\n${PASSWORD}\n" | passwd ${USERNAME}
# overwrite apt source list
rm -f /etc/apt/sources.list
echo deb ${DISTRO_MIRROR} ${DISTRO_CODE} main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src ${DISTRO_MIRROR} ${DISTRO_CODE} main restricted universe multiverse >> /etc/apt/sources.list
echo deb ${DISTRO_MIRROR} ${DISTRO_CODE}-updates main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src ${DISTRO_MIRROR} ${DISTRO_CODE}-updates main restricted universe multiverse >> /etc/apt/sources.list
echo deb ${DISTRO_MIRROR} ${DISTRO_CODE}-security main restricted universe multiverse >> /etc/apt/sources.list
echo deb-src ${DISTRO_MIRROR} ${DISTRO_CODE}-security main restricted universe multiverse >> /etc/apt/sources.list
apt update -y
apt upgrade -y
apt install -y ${ADD_LIST_ST_3}
#systemd configs
systemctl enable systemd-networkd.service
systemctl enable avahi-daemon.service
systemctl enable usb-gadget-uvc.service
systemctl enable uvc-gadget.service
systemctl enable adi-tof.service
EOF
# Apply step1 overlay
sudo cp -R ${SCRIPT_DIR}/patches/ubuntu_overlay/step1/* ${ROOTFS_TMP}/
sudo mv stage3.sh ${ROOTFS_TMP}/tmp
sudo chmod +x ${ROOTFS_TMP}/tmp/stage3.sh
sudo chroot ${ROOTFS_TMP} /tmp/stage3.sh
sudo rm -f ${ROOTFS_TMP}/tmp/stage3.sh
# I don't know who creates empty .cache which is owned by root, remove it.
sudo rm -rf ${ROOTFS_TMP}/home/${USERNAME}/.cache
# Apply step3 overlay (configs)
sudo cp -R ${SCRIPT_DIR}/patches/ubuntu_overlay/step3/* ${ROOTFS_TMP}/
}
function main() {
setup_config
cd ${OUTPUT_DIR}
# create 1GB ext2 image
dd if=/dev/zero of=rootfs.ext2 bs=1M count=2000
mkdir -p ${ROOTFS_TMP}
mkfs.ext2 rootfs.ext2
sudo mount -o loop rootfs.ext2 ${ROOTFS_TMP}
if [ -f ${ROOTFS_CACHE}.tgz ]; then
# extract rfs-cache.tgz
sudo tar zxf ${ROOTFS_CACHE}.tgz -C ${ROOTFS_TMP}/
else
extract_apt_cache
install_qemu
# debootstrap 1st
sudo debootstrap --arch=${TARGET_ARCH} --include="${INCLUDE_LIST}" --foreign ${DISTRO_CODE} ${ROOTFS_TMP} ${DISTRO_MIRROR}
# debootstrap 2nd
sudo chroot ${ROOTFS_TMP} /debootstrap/debootstrap --second-stage
uninstall_qemu
archive_apt_cache
archive_rootfs_base
fi
extract_apt_cache
install_qemu
run_3rd_stage_script
uninstall_qemu
archive_apt_cache
# cleanup
sync
sudo umount rootfs_tmp
rm -R rootfs_tmp
}
main