-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbuild.sh
executable file
·200 lines (164 loc) · 5.77 KB
/
build.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
#!/bin/bash
WORKDIR="$(dirname "$(realpath "$0")")"
source "${WORKDIR}"/config
function cleanmount () {
umount -l "${WORKDIR}/squashfs/var/tmp/portage" || true
umount -l "${WORKDIR}/squashfs/mnt/gen-iso" || true
exit
}
function fetchstage3 () {
wget -q "${DIST}/latest-stage3-${MICROARCH}-${SUFFIX}.txt" -O "${WORKDIR}/latest-stage3-${MICROARCH}-${SUFFIX}.txt"
STAGE3PATH="$(sed -n '6p' "${WORKDIR}/latest-stage3-${MICROARCH}-${SUFFIX}.txt" | cut -f 1 -d ' ')"
echo "STAGE3PATH:" "${STAGE3PATH}"
STAGE3="$(basename "${STAGE3PATH}")"
if ( ! grep 'stage3downloadok' "${WORKDIR}/stat" );then
rm -rf "squashfs/${STAGE3}"
wget -q "${DIST}/${STAGE3PATH}" -O "squashfs/${STAGE3}" \
&& echo 'stage3downloadok' >> "${WORKDIR}/stat" || exit 1
fi
}
function unpackstage3 () {
# unpack stage3
pushd "${WORKDIR}/squashfs" || exit 1
if ( ! grep 'unpackok' "${WORKDIR}/stat" );then
tar xpf "${STAGE3}" --xattrs-include='*.*' --numeric-owner \
&& echo 'unpackok' >> "${WORKDIR}/stat" \
&& rm "${STAGE3}" || exit 1
fi
popd || exit 1
}
function buildarchscript () {
# check arch-chroot tools
if [ ! -f "${WORKDIR}/arch-scripts/arch-chroot.in" ];then
git submodule update --init --recursive || exit 1
fi
if [ ! -x "${WORKDIR}/arch-scripts/arch-chroot" ];then
pushd "${WORKDIR}/arch-scripts" || exit 1
make || exit 1
popd || exit 1
fi
}
function crun () {
"${WORKDIR}"/arch-scripts/arch-chroot "${WORKDIR}/squashfs" bash -c "$*"
}
function syncrepo () {
# try three times to sync
if [ -d "${WORKDIR}/squashfs/var/db/repos/gentoo" ];then
for n in {1..3};do
if (crun which git);then
if (crun emerge --sync);then
break;
fi
else
pushd "${WORKDIR}/squashfs/var/db/repos/gentoo" || exit 1
if (git pull);then
popd || exit 1
break;
else
popd || exit 1
fi
fi
if [ "${n}" == "3" ];then
exit 1
fi
done
else
for n in {1..3};do
if (git clone --depth=1 "${GITMIRROR}" "${WORKDIR}/squashfs/var/db/repos/gentoo");then
break;
fi
if [ "${n}" == "3" ];then
exit 1
fi
done
fi
}
function refreshconfig() {
# refresh MAKEOPTS
sed -i "s/MAKEOPTS=\".*\"/MAKEOPTS=\""${MAKEOPTS}"\"/g" "${WORKDIR}/squashfs/etc/portage/make.conf/common"
# refresh MIRROR
echo "GENTOO_MIRRORS=\""${MIRROR}"/gentoo\"" > "${WORKDIR}/squashfs/etc/portage/make.conf/mirror"
}
function mounttmpfs () {
if [[ -n "${TMPFS}" ]];then
# init notmpfs dir
crun mkdir -p /var/tmp/{notmpfs,portage}
crun chown portage:portage /var/tmp/{notmpfs,portage}
crun chmod 775 /var/tmp/{notmpfs,portage}
# mount tmpfs
if ( ! findmnt "${WORKDIR}/squashfs/var/tmp/portage" ) && [ -n "${TMPFS}" ];then
crun mount -t tmpfs -o size="${TMPFS}",uid=portage,gid=portage,mode=775 tmpfs /var/tmp/portage
elif ( findmnt "${WORKDIR}/squashfs/var/tmp/portage" ) && [ -n "${TMPFS}" ];then
crun mount -o remount,size="${TMPFS}" /var/tmp/portage
fi
fi
}
function makesquashfs (){
mkdir -p "${WORKDIR}/iso/LiveOS"
rm -f "${WORKDIR}/iso/LiveOS/squashfs.img"
mksquashfs "${WORKDIR}/squashfs/" "${WORKDIR}/iso/LiveOS/squashfs.img" \
-wildcards -ef exclude.txt -b 1024K -comp xz -progress -processors "${CORES}" -Xdict-size 100% || exit 1
}
function buildbootfiles () {
# make initramfs with live support
KVER="$(ls "${WORKDIR}/squashfs/lib/modules" | sort -Vr | head -n1)"
crun dracut -f --kver "${KVER}" --add dmsquash-live --add dmsquash-live-autooverlay --add crypt || exit 1
# copy the kernel to iso workdir
mkdir -p "${WORKDIR}/iso/boot"
cp -v "${WORKDIR}/squashfs/boot/kernel-${KVER}" "${WORKDIR}/iso/boot/kernel" || exit 1
cp -v "${WORKDIR}/squashfs/boot/initramfs-${KVER}.img" "${WORKDIR}/iso/boot/initrd" || exit 1
}
function buildiso () {
# bind iso dir to rootfs to make iso
if ( ! findmnt "${WORKDIR}/squashfs/mnt/gen-iso" );then
mkdir -p "${WORKDIR}/squashfs/mnt/gen-iso"
mount --bind "${WORKDIR}" "${WORKDIR}/squashfs/mnt/gen-iso"
fi
crun grub-mkrescue -o /mnt/gen-iso/gig-os-"$(date +%Y%m%d)".iso /mnt/gen-iso/iso -- -as mkisofs -V 'Gig-OS' || exit 1
}
# ctrl+c anytime to stop
trap cleanmount INT
trap cleanmount EXIT
# must run as root
if (( EUID != 0 ));then
echo 'This script must be run with root privileges'
exit 1
fi
# Download the stage3
mkdir -p "${WORKDIR}/squashfs"
fetchstage3
unpackstage3
buildarchscript
# copy extra staff to squashfs but package.use
rsync -rl --copy-unsafe-links "${WORKDIR}"/include-squashfs/* "${WORKDIR}/squashfs/" --exclude etc/portage/package.use/ --exclude etc/portage/make.conf/use
refreshconfig
mounttmpfs
# DNS
cp --dereference /etc/resolv.conf "${WORKDIR}/squashfs"/etc/
syncrepo
# upgrade portage first
crun emerge -vu1q --jobs "${CORES}" portage
# we need git to sync overlay
if ( ! crun which git);then
crun emerge -vuDq --jobs "${CORES}" dev-vcs/git || exit 1
fi
syncrepo
# sync full extra staff
rsync -rl --copy-unsafe-links "${WORKDIR}"/include-squashfs/* "${WORKDIR}/squashfs/"
refreshconfig
# upgrade system
crun emerge -uvDNq --jobs "${CORES}" --keep-going @world || exit 1
crun emerge --jobs "${CORES}" @live-rebuild || exit 1
crun emerge -c || exit 1
crun eclean-kernel --no-bootloader-update --no-mount -n 1 || exit 1
crun eclean-pkg || true
# run hooks in squashfs
for hook in "${WORKDIR}"/hooks/*;do
source "${hook}" || exit 1
done
makesquashfs
buildbootfiles
# copy extra staff for iso
rsync -rl --copy-unsafe-links "${WORKDIR}"/include-iso/* "${WORKDIR}/iso" || true
buildiso
cleanmount