forked from hakuna-m/wubiuefi
-
Notifications
You must be signed in to change notification settings - Fork 2
/
generate_lliuwin_img.sh
executable file
·357 lines (333 loc) · 7.63 KB
/
generate_lliuwin_img.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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
#!/bin/bash
# Copyright 2023 LliureX Team
#This script attempts to create a lliurex system inside a ext4 image
LOCAL_CHROOT=/home/${SUDO_USER}/lliuwin_chroot
LOCAL_IMG=/home/${SUDO_USER}/lliuwin2.img
SIZE=11G
RELEASE=jammy
LLIUREX_META=lliurex-meta-desktop-lite
EXTRA_PACKAGES="linux-firmware lliuwin-wizard rebost-gui"
UBUNTU_PACKAGES="zram-config"
function show_help()
{
printf "\n"
printf "Helper for building a lliurex rootfs for Lliuwin\n"
printf "Usage:\n"
printf "\t$0 --OPTION [option args]. The release is optional, defaults to $RELEASE\n"
printf "Config of extra-packages is done editing this shell\n"
printf "Options:\n"
printf "\t--process [release]: Make all. Release defaults to $RELEASE\n"
printf "\t--allocate [SIZE]: Allocates a file of SIZE ($SIZE by default)\n"
printf "\t--debootstrap: Mounts the rootfs file for debootstrap\n"
printf "\t--mount [--enable]: Mounts the chroot. If --enable then also mount bindings\n"
printf "\t--umount: Umounts the chroot\n"
printf "\t--install: Install the meta-package inside the chroot\n"
printf "\t--upgrade: Upgrades the image (needs a generated chroot)\n"
printf "\t--chroot: chroot to lliuwin image\n"
printf "\t--clean: Removes the image\n"
printf "\t--meta lliurex-meta-package: Sets target metapackage to lliurex-meta-package\n"
printf "\t--compress: Compress lliuwin.img\n"
printf "\t--img: Name of the .img file\n"
printf "\t--help: Shows this missage\n"
printf "\n"
exit 0
}
function generate_sources
{
echo "deb http://lliurex.net/$RELEASE $RELEASE main multiverse preschool restricted universe">$LOCAL_CHROOT/etc/apt/sources.list
echo "deb http://lliurex.net/$RELEASE $RELEASE-security main multiverse restricted universe">>$LOCAL_CHROOT/etc/apt/sources.list
echo "deb http://lliurex.net/$RELEASE $RELEASE-updates main multiverse restricted universe">>$LOCAL_CHROOT/etc/apt/sources.list
}
function disableGrubTheme
{
echo "GRUB_THEME=" > /etc/default/grub.d/99_zlliuwin-theme.cfg
}
function mount_binds()
{
echo "Binding mounts"
mount --bind /dev $LOCAL_CHROOT/dev
mount --bind /dev/pts $LOCAL_CHROOT/dev/pts
mount --bind /sys $LOCAL_CHROOT/sys
mount --bind /proc $LOCAL_CHROOT/proc
echo "proc /proc proc defaults 0 0" > $LOCAL_CHROOT/etc/fstab
echo "sysfs /sys sysfs defaults 0 0" >> $LOCAL_CHROOT/etc/fstab
echo "tmpfs /tmp tmpfs defaults 0 0" >> $LOCAL_CHROOT/etc/fstab
}
function mount_img()
{
if [ -z $CHKMOUNT ]
then
mkdir $LOCAL_CHROOT 2>/dev/null
EMPTY="$(ls ${LOCAL_CHROOT})"
if [ ${#EMPTY} -gt 0 ]
then
echo "$LOCAL_CHROOT seems not empty. Not mounting"
return
fi
mount $LOCAL_IMG $LOCAL_CHROOT
if [ ! -z $ENABLE ]
then
mount_binds
fi
echo "Mount ${LOCAL_CHROOT}: $?"
CHKMOUNT=1
else
echo "$LOCAL_CHROOT already mounted"
return
fi
}
function debootstrap_img()
{
debootstrap --no-check-gpg --arch amd64 $RELEASE $LOCAL_CHROOT http://lliurex.net/$RELEASE
if [ $? -ne 0 ]
then
echo "************ ERROR **********"
echo "There's no release named $RELEASE"
exit 1
fi
echo "LANG=$LANG" >> $LOCAL_CHROOT/etc/profile
echo "LANGUAGE=$LANGUAGE" >> $LOCAL_CHROOT/etc/profile
echo "LC_ALL=$LC_ALL" >> $LOCAL_CHROOT/etc/profile
}
function configure_chroot()
{
generate_sources
[ -e $LOCAL_CHROOT/etc/default/grub.d/99_zlliuwin-theme.cfg ] || disableGrubTheme
touch $LOCAL_CHROOT/etc/mtab
}
function enter_chroot()
{
chroot $LOCAL_CHROOT
}
function install_meta()
{
if [ ${#EXTRA_PACKAGES} -gt 0 ]
then
EXTRA_INSTALL="apt-get install -y $EXTRA_PACKAGES"
fi
if [ ${#UBUNTU_PACKAGES} -gt 0 ]
then
UBUNTU_INSTALL="echo deb http://archive.ubuntu.com/ubuntu/ $RELEASE main multiverse restricted universe >>/etc/apt/sources.list;echo deb http://archive.ubuntu.com/ubuntu/ $RELEASE-security main multiverse restricted universe >>/etc/apt/sources.list;echo deb http://archive.ubuntu.com/ubuntu/ $RELEASE-updates main multiverse restricted universe >>/etc/apt/sources.list;apt-get update -y;apt-get install -y $UBUNTU_PACKAGES"
fi
cat << EOF | chroot $LOCAL_CHROOT
rm /var/cache/apt/archives/*
apt-get update
dpkg --configure -a
apt clean
apt-get install -y $LLIUREX_META
apt-get install -f -y
apt clean
lliurex-upgrade -u
$EXTRA_INSTALL
$UBUNTU_INSTALL
apt-get autoremove -y
apt clean
rm /vmlinuz /initrd.gz /initrd.img 2>/dev/null
ln -s /boot/vmlinuz /vmlinuz
ln -s /boot/initrd.img /initrd.img
EOF
generate_sources
}
function upgrade_image()
{
cmd="apt-get -y dist-upgrade"
cat << EOF | chroot $LOCAL_CHROOT
lliurex-upgrade -u
apt-get autoremove -y
apt clean
EOF
}
function umount_img()
{
umount $LOCAL_CHROOT/dev/pts
umount $LOCAL_CHROOT/dev
umount $LOCAL_CHROOT/proc
umount $LOCAL_CHROOT/sys
rm $LOCAL_CHROOT/root/.bash_history 2>/dev/null
rm $LOCAL_CHROOT/root/var/cache/apt/archives/* 2>/dev/null
rm -r $LOCAL_CHROOT/tmp/* 2>/dev/null
rm $LOCAL_CHROOT/etc/mtab 2>/dev/null
umount $LOCAL_CHROOT
if [ $?==0 ]
then
echo "$LOCAL_CHROOT disabled"
else
echo "umount ERROR $LOCAL_CHROOT: $?"
fi
}
function allocate_img()
{
echo "Populating $LOCAL_IMG"
if [ -n $SIZE ] && [ "$SIZE" -eq "$SIZE" 2>/dev/null ]
then
SIZE="${SIZE}G"
fi
fallocate -l $SIZE $LOCAL_IMG
echo "Formatting"
mkfs.ext4 $LOCAL_IMG
}
function compress_img
{
rm -v $LOCAL_IMG.tar.xz 2>/dev/null
echo "Compressing $LOCAL_IMG"
FROMSIZE=`du -sk --apparent-size ${LOCAL_IMG} | cut -f 1`
CHECKPOINT=`echo ${FROMSIZE}/50 | bc`
echo "Estimated: [==================================================]"
echo -n "Progress: ["
tar --transform='s!.*/!!' -c --record-size=1K --checkpoint="${CHECKPOINT}" --checkpoint-action="ttyout=>" -f - "${LOCAL_IMG}" 2>/dev/null | xz > "$(dirname ${LOCAL_IMG})/$(basename ${LOCAL_IMG}).tar.xz"
echo "]"
}
function clean()
{
umount_img
rm -r $LOCAL_CHROOT
rm $LOCAL_IMG
}
function only_root()
{
if [ $UID -ne 0 ]
then
echo "Only root allowed"
exit 1
fi
}
#### MAIN ####
ACTION=0
while [ ! -z $1 ]
do
case $1 in
"--enable")
ENABLE=1
;;
"--process")
PROCESS=1
ACTION=$(expr $ACTION + 1)
;;
"--allocate")
ALLOCATE=1
ACTION=$(expr $ACTION + 1)
;;
"--mount")
MOUNT=1
ACTION=$(expr $ACTION + 1)
;;
"--umount")
UMOUNT=1
ACTION=$(expr $ACTION + 1)
;;
"--debootstrap")
DEBOOTSTRAP=1
ACTION=$(expr $ACTION + 1)
;;
"--install")
INSTALL=1
ACTION=$(expr $ACTION + 1)
;;
"--upgrade")
UPGRADE=1
ACTION=$(expr $ACTION + 1)
;;
"--chroot")
CHROOT=1
ACTION=$(expr $ACTION + 1)
;;
"--meta")
META=1
;;
"--compress")
COMPRESS=1
ACTION=$(expr $ACTION + 1)
;;
"--clean")
CLEAN=1
ACTION=$(expr $ACTION + 1)
;;
"--name")
shift
LOCAL_IMG=$1
;;
"--help")
show_help
;;
"lliurex-"*)
[ ! -z $META ] && LLIUREX_META=$1
;;
[0-9]*)
[ ! -z $ALLOCATE ] && SIZE=$1
;;
*)
RELEASE=$1
;;
esac
shift
done
if [ $ACTION -ne 1 ]
then
echo ""
echo "*****"
echo "/*Only one action allowed*/"
echo "*****"
show_help
fi
only_root
if [ ! -z $PROCESS ]
then
umount_img
allocate_img
mount_img
debootstrap_img
mount_binds
configure_chroot
install_meta
umount_img
# echo -n "Compress the image? [y/n]: "
# read RESP
# if [[ $RESP == "y" ]]
# then
compress_img
# fi
elif [ ! -z $ALLOCATE ]
then
allocate_img
elif [ ! -z $DEBOOTSTRAP ]
then
mount_img
debootstrap_img
umount_img
elif [ ! -z $MOUNT ]
then
mount_img
elif [ ! -z $UMOUNT ]
then
umount_img
elif [ ! -z $INSTALL ]
then
ENABLE=1
mount_img
configure_chroot
install_meta
umount_img
elif [ ! -z $UPGRADE ]
then
ENABLE=1
mount_img
configure_chroot
upgrade_image
umount_img
elif [ ! -z $CHROOT ]
then
ENABLE=1
mount_img
configure_chroot
enter_chroot
umount_img
elif [ ! -z $COMPRESS ]
then
compress_img
elif [ ! -z $CLEAN ]
then
clean
else
show_help
fi
exit 0