-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathmkrootfs.sh
executable file
·158 lines (135 loc) · 3.74 KB
/
mkrootfs.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
#!/bin/bash
export WORKDIR=$(dirname $(readlink -f "$0"))
cd ${WORKDIR}
if [ $# -lt 1 ];then
echo "Usage: $0 dist [clean]"
echo "dist: focal"
exit 1
fi
dist=$1
if [ -f "./env/linux/${dist}.env" ];then
source ./env/linux/${dist}.env
else
echo "./env/linux/${dist}.env not exists!"
if [ -f "./env/linux/private/${dist}.env" ];then
echo "But the private environment file ${WORKDIR}/env/linux/private/${dist}.env has been found."
source ./env/linux/private/${dist}.env
else
echo "./env/linux/private/${dist}.env not exists!"
exit 1
fi
fi
if [ ! -x "/usr/sbin/debootstrap" ];then
echo "/usr/sbin/debootstrap not found, please install debootstrap!"
echo "example: sudo apt install debootstrap"
exit 1
fi
host_arch=$(uname -m)
if [ "${host_arch}" == "aarch64" ];then
CROSS_FLAG=0
else
CROSS_FLAG=1
fi
if [ $CROSS_FLAG -eq 1 ] && [ ! -x "/usr/bin/qemu-aarch64-static" ];then
echo "/usr/bin/qemu-aarch64-static not found, please install qemu-aarch64-static!"
echo "example: sudo apt install qemu-user-static"
exit 1
fi
if [ ! -f "${CHROOT}" ];then
echo "The chroot script is not exists! [${CHROOT}]"
exit 1
fi
if [ -n "$OS_RELEASE" ];then
os_release=$OS_RELEASE
else
os_release=$dist
fi
if [ -n "$DIST_ALIAS" ];then
output_dir=build/${DIST_ALIAS}
else
output_dir=build/${dist}
fi
function unbind() {
cd ${WORKDIR}
umount -f ${output_dir}/dev/pts
umount -f ${output_dir}/dev
umount -f ${output_dir}/proc
umount -f ${output_dir}/sys
umount -f ${output_dir}/run
}
function on_trap() {
unbind
exit 0
}
trap "on_trap" 2 3 15
param=$2
if [ "$param" == "clean" ];then
rm -rf ${output_dir}
echo "${output_dir} cleaned"
exit 0
fi
echo "Stage 1 ..."
# first stage
mkdir -p ${output_dir}
mkdir -p ${output_dir}/dev
mkdir -p ${output_dir}/proc
mkdir -p ${output_dir}/run
mkdir -p ${output_dir}/sys
if [ $CROSS_FLAG -eq 1 ];then
debootstrap --arch=arm64 --foreign ${os_release} ${output_dir} "$DEBOOTSTRAP_MIRROR"
mkdir -p ${output_dir}/usr/bin && cp -fv /usr/bin/qemu-aarch64-static "${output_dir}/usr/bin/"
else
debootstrap --arch=arm64 ${os_release} ${output_dir} "$DEBOOTSTRAP_MIRROR"
fi
mount -o bind /dev ${output_dir}/dev
mount -o bind /dev/pts ${output_dir}/dev/pts
mount -o bind /sys ${output_dir}/sys
mount -o bind /proc ${output_dir}/proc
mount -o bind /run ${output_dir}/run
# second stage
echo "Stage 2 ..."
if [ $CROSS_FLAG -eq 1 ];then
chroot "${output_dir}" debootstrap/debootstrap --second-stage
fi
echo "done"
# third stage
echo "Stage 3 ..."
cp ${SOURCES_LIST_WORK} ${output_dir}/etc/apt/sources.list
env_file="${output_dir}/tmp/chroot.env"
cat > ${env_file} <<EOF
ENABLE_EXT_REPO="${CHROOT_ENABLE_EXT_REPO}"
EXT_REPOS="${CHROOT_EXT_REPOS}"
NECESSARY_PKGS="${CHROOT_NECESSARY_PKGS}"
OPTIONAL_PKGS="${CHROOT_OPTIONAL_PKGS}"
CUSTOM_PKGS="${CHROOT_CUSTOM_PKGS}"
INSTALL_LOCAL_DEBS="${CHROOT_INSTALL_LOCAL_DEBS}"
LOCAL_DEBS_HOME="/tmp/debs/"
LOCAL_DEBS_LIST="${CHROOT_LOCAL_DEBS_LIST}"
HAS_GRAPHICAL_DESKTOP="${CHROOT_HAS_GRAPHICAL_DESKTOP}"
DISPLAY_MANAGER="${CHROOT_DISPLAY_MANAGER}"
EOF
if [ "${CHROOT_INSTALL_LOCAL_DEBS}" == "yes" ];then
if [ "${CHROOT_LOCAL_DEBS_HOME}" != "" ] && [ -d "${CHROOT_LOCAL_DEBS_HOME}" ];then
mkdir -p ${output_dir}/tmp/debs && \
cp -av ${CHROOT_LOCAL_DEBS_HOME}/* ${output_dir}/tmp/debs/
fi
fi
cp -v "${CHROOT}" "${output_dir}/tmp/chroot.sh"
if [ $CROSS_FLAG -eq 1 ];then
chroot ${output_dir} /usr/bin/qemu-aarch64-static /bin/bash /tmp/chroot.sh
else
chroot ${output_dir} /bin/bash /tmp/chroot.sh
fi
echo "umount ... "
umount ${output_dir}/dev/pts
umount ${output_dir}/dev
umount ${output_dir}/proc
umount ${output_dir}/sys
umount ${output_dir}/run
echo 'done'
echo
if [ $CROSS_FLAG -eq 1 ];then
rm ${output_dir}/usr/bin/qemu-aarch64-static
fi
cp -v ${SOURCES_LIST_ORIG} ${output_dir}/etc/apt/sources.list
exit 0