summaryrefslogtreecommitdiff
path: root/libvasile
blob: afa5b705ccb4196241a406a3b4756d234fdfbe2e (plain)
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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
#!/usr/bin/env bash

# import gentoo functions
source /lib/gentoo/functions.sh

# generic variables

export local releasename="redcore"
export local releasetarget="desktop"
export local releaseversion="1"
export local kernelver="4.8.5-"$releasename""
export local chrootx64=""$releasename"_"$releaseversion"_core_x64.squashfs"
export local chrootx64md5=""$chrootx64".md5"

# build variables

export local rodir="rodir"
export local rwdir="rwdir"
export local workdir="workdir" # only needed for overlayfs on kernel greather than 3.18
export local overlaydir="overlaydir"
export local chrootuser="root"
export local chroottarget="${@:2}"
export local chrootbuildtarget="emerge -kav "$chroottarget""
export local chrootdevtree="vasile --srcmode"
export local chrootmainoverlay="layman -f -a "$releasename"-"$releasetarget" -o https://gitlab.com/"$releasename"/"$releasename"-"$releasetarget"/raw/master/overlay.xml"
export local chrootbuildgit="cd /opt && git clone https://gitlab.com/"$releasename"/"$releasename"-build.git"

# iso variables

export local bootcorepath="/tmp/bootcore"
export local bootcorefiles="/tmp/bootcore/cdroot/boot"
export local isouser="root"
export local isousertree="vasile --binmode"
export local isomainoverlay="layman -f -a "$releasename"-"$releasetarget" -o https://gitlab.com/"$releasename"/"$releasename"-"$releasetarget"/raw/master/overlay.xml"
export local isobuildgit="cd /opt && git clone https://gitlab.com/"$releasename"/"$releasename"-build.git"

export local isodracutcmd="dracut -N -a dmsquash-live -a pollcdrom -o systemd -o systemd-initrd -o systemd-networkd -o dracut-systemd --force --kver "$kernelver""
export local isodracutrd="initramfs-"$kernelver".img"
export local isoeficmd="grub2-mkimage -d /usr/lib64/grub/x86_64-efi -o bootx64.efi -O x86_64-efi ext2 fat udf btrfs ntfs reiserfs xfs hfsplus lvm ata part_msdos part_gpt part_apple bsd search_fs_uuid normal chain iso9660 configfile help loadenv reboot cat search memdisk tar boot linux chain"
export local isochainloadcmd="grub2-mkimage -d /usr/lib64/grub/i386-pc -o core.img -O i386-pc biosdisk part_msdos fat"
export local isomainarch="x86_64"
export local isorootdir=""$releasename"-"$releasetarget"-"$releaseversion"-"$isomainarch""
export local isokernelname="kernel-genkernel-"$isomainarch"-"$kernelver""
export local isostagingdir="/tmp/"$isorootdir""
export local isosupersquashdir=""$isorootdir"/CDroot"
export local isolivesqfsdir=""$isosupersquashdir"/LiveOS"
export local isoliveosdir=""$isorootdir"/LiveOS"
export local isosyncdir=""$isorootdir"/rootfs"
export local isobootdir=""$isorootdir"/boot/grub"
export local isoefibootdir=""$isorootdir"/efi/boot"

# modechange variables

export local portagedir="/usr/portage"
export local confdir="/etc/portage"
export local gitdir="/opt/redcore-build/conf/intel/portage"
export local clonedir="/opt/redcore-build"

# generic functions (used for both build && iso creation)

kernelconfig () {
	if [[ $(zgrep 'CONFIG_OVERLAY_FS=' /proc/config.gz) && $(zgrep "CONFIG_SQUASHFS=" /proc/config.gz) &&  $(zgrep "CONFIG_BLK_DEV_LOOP=" /proc/config.gz) ]] ; then
		einfo "Kernel configuration seems OK, moving on"
		sleep 1
	else
		eerror "Vasile needs OVERLAYFS && SQUASHFS && LOOP DEVICES to work"
		eerror "Please rebuild the kernel with those activated to use it"
		exit 1
	fi
}

checkroot () {
	if [[ "$(whoami)" != root ]] ; then
		eerror "You're not root?...No cookies for you, go away !!!"
		exit 1
	fi
}

checkiflive () {
	if [[ -L /dev/mapper/live-base ]] ; then
		eerror "Running in Live mode is unsupported"
		exit 1
	fi
}

chrootchecksum () {
	while : true ; do
		if [[ -f "$chrootx64" && -f "$chrootx64md5" ]] ; then
			einfo "Squashed chroot && Checksum file found"
			if [[ "$(md5sum -c "$chrootx64md5")" ]] ; then
				einfo "Squashed chroot checksum passed"
				sleep 1
				break
			else
				eerror "Squashed chroot checksum failed"
				exit 1
			fi
		else
			eerror "Squashed chroot || Checksum file not found"
			exit 1
		fi
	done
}

# build functions

chrootprepare () {
	while : true ; do
		if [[ ! -d "$rodir" && ! -d "$rwdir" && ! -d "$workdir" && ! -d "$overlaydir" ]] ; then
			for i in "$rodir" "$rwdir" "$workdir" "$overlaydir" ; do
				mkdir "$i"
			done
			chrootstart
			break
		elif [[ -d "$rodir" && -d "$rwdir" && -d "$workdir" && -d "$overlaydir" ]] ; then
			chrootstop
			for i in "$rodir" "$rwdir" "$workdir" "$overlaydir" ; do
				rm -rf "$i"
			done
			continue
		fi
	done

}

chrootstop () {
	umount -l "$overlaydir"/proc > /dev/null 2>&1
	umount -l "$overlaydir"/sys > /dev/null 2>&1
	umount -l "$overlaydir"/dev/pts > /dev/null 2>&1
	umount -l "$overlaydir"/dev/shm > /dev/null 2>&1
	umount -l "$overlaydir"/dev > /dev/null 2>&1
	umount -l "$overlaydir"/tmp > /dev/null 2>&1
	umount -l "$overlaydir"/usr/portage/packages > /dev/null 2>&1
	umount -l "$overlaydir"/usr/portage/distfiles > /dev/null 2>&1
	umount -l "$overlaydir" > /dev/null 2>&1
	umount -l "$rodir" > /dev/null 2>&1
}

chrootstart () {
	mount -t squashfs "$chrootx64" "$rodir"
	mount -t overlay -o lowerdir="$rodir",upperdir="$rwdir",workdir="$workdir" overlay "$overlaydir"
	mount -o bind packages "$overlaydir"/usr/portage/packages
	mount -o bind distfiles "$overlaydir"/usr/portage/distfiles
	mount -t proc proc "$overlaydir"/proc
	mount -t sysfs sysfs "$overlaydir"/sys
	mount -t devtmpfs -o relatime,size=3055348k,nr_inodes=763837,mode=755 none "$overlaydir"/dev
	mount -t devpts -o nosuid,noexec,relatime,gid=5,mode=620 none "$overlaydir"/dev/pts
	mount -t tmpfs -o nosuid,nodev none "$overlaydir"/dev/shm
	mount -t tmpfs -o nosuid,nodev,noexec none  "$overlaydir"/tmp
}

chrootdevtree () {
	einfo "Injecting devmode portage tree"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootdevtree"
}

chrootoverlays () {
	einfo "Injecting "$releasename" "$releasetarget" overlay"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootmainoverlay"
}

chrootbuildsystem () {
	einfo "Injecting "$releasename" buildsystem"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootbuildgit"
}

chrootbuild () {
	einfo "All systems are go!"
	sleep 1
	chroot "$overlaydir" su - "$chrootuser" -c "$chrootbuildtarget"
}

chrootx64 () {
	einfo "DROPPING YOU TO A ROOT SHELL INTO BUILD ENVIRONMENT"
	einfo "FIX FAILURES && ADJUST PORTAGE CONFIGURATION FILES"
	einfo "TEST USEFLAGS && KEYWORDS && MASKS IF REQUIRED"
	einfo "IN THE END : DO NOT FORGET TO COMMIT YOUR CHANGES"
	chroot "$overlaydir" su - "$chrootuser"
}

makepkg () {
	chrootchecksum
	chrootprepare
	chrootoverlays
	chrootbuildsystem
	chrootdevtree
	chrootbuild
	chrootx64
	chrootstop
}

# iso functions

isostart () {
	mount -o bind packages "$isosyncdir"/usr/portage/packages
	mount -t proc proc "$isosyncdir"/proc
	mount -t sysfs sysfs "$isosyncdir"/sys
	mount -t devtmpfs -o relatime,size=3055348k,nr_inodes=763837,mode=755 none "$isosyncdir"/dev
	mount -t devpts -o nosuid,noexec,relatime,gid=5,mode=620 none "$isosyncdir"/dev/pts
	mount -t tmpfs -o nosuid,nodev none "$isosyncdir"/dev/shm
	mount -t tmpfs -o nosuid,nodev,noexec none  "$isosyncdir"/tmp
}

isostop () {
	umount -l "$isosyncdir"/proc > /dev/null 2>&1
	umount -l "$isosyncdir"/sys > /dev/null 2>&1
	umount -l "$isosyncdir"/dev/pts > /dev/null 2>&1
	umount -l "$isosyncdir"/dev/shm > /dev/null 2>&1
	umount -l "$isosyncdir"/dev > /dev/null 2>&1
	umount -l "$isosyncdir"/tmp > /dev/null 2>&1
	umount -l "$isosyncdir"/usr/portage/packages > /dev/null 2>&1
}

isousertree () {
	einfo "Injecting usermode portage tree"
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isousertree"
}

isooverlays () {
	einfo "Injecting "$releasename" "$releasetarget" overlay"
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isomainoverlay"
}

isobuildsystem () {
	einfo "Injecting "$releasename" buildsystem"
	sleep 1
	chroot "$isosyncdir" su - "$isouser" -c "$isobuildgit"
}

isoramfs () {
	chroot "$isosyncdir" su - "$isouser" -c "$isodracutcmd"
}

isoefiimg () {
	chroot "$isosyncdir" su - "$isouser" -c "$isoeficmd"
}

isochainload () {
	chroot "$isosyncdir" su - "$isouser" -c "$isochainloadcmd"
}

isoservices () {
	for service in avahi-daemon cups connman dkms dm-event redcorelive lightdm lvm2-lvmetad lvm2-monitor NetworkManager ModemManager virtualbox-guest-additions ; do
		chroot "$isosyncdir" su - "$isouser" -c "systemctl enable "$service""
	done
}

isochroot () {
	einfo "DROPPING YOU TO A ROOT SHELL INTO ISO ENVIRONMENT"
	einfo "INSTALL APPS & TARGETS & DES YOU WANT INSIDE ISO"
	einfo "EXIT WHEN YOU'RE DONE AND I'LL CREATE THE IMAGE"
	chroot "$isosyncdir" su - "$isouser"
}

prepareiso () {
	mkdir -p "$isosyncdir"
	dd if=/dev/zero of=""$isosyncdir".img" bs=50M count=400
	mkfs.ext4 ""$isosyncdir".img"
	mkdir -p "$isostagingdir"
	mkdir -p "$isobootdir"
	mkdir -p "$isoefibootdir"
	mkdir -p "$isoliveosdir"
	mkdir -p "$isosupersquashdir"
	mkdir -p "$isolivesqfsdir"
	mount -t squashfs "$chrootx64" "$isostagingdir"
	mount -t ext4 ""$isosyncdir".img" "$isosyncdir"
	rsync -aHAXr --progress "$isostagingdir/" "$isosyncdir/"
	cp -avx ""$isosyncdir"/boot/"$isokernelname"" ""$isorootdir"/boot/vmlinuz"
	isostart
	isoramfs
	isostop
	mv ""$isosyncdir"/boot/"$isodracutrd"" ""$isorootdir"/boot/initrd"
	isostart
	isoefiimg
	isostop
	mv ""$isosyncdir"/root/bootx64.efi" "$isoefibootdir"
	chmod 755 ""$isoefibootdir"/bootx64.efi"
	isostart
	isochainload
	isostop
	mv ""$isosyncdir"/root/core.img" "$isobootdir"
	cp -avx ""$isosyncdir"/usr/lib64/grub/i386-pc/lnxboot.img" "$isobootdir"
	isostart
	isooverlays
	isobuildsystem
	isousertree
	isochroot
	isostop
	isostart
	isoservices
	isostop
	umount -l "$isosyncdir" > /dev/null 2>&1
	mv ""$isosyncdir".img"  "$isolivesqfsdir"
	mksquashfs "$isosupersquashdir" ""$isorootdir"/squashfs.img" -b 1048576 -comp xz -Xdict-size 100%
	mv ""$isorootdir/"squashfs.img" "$isoliveosdir"
}

bootcoreiso () {
	git clone https://gitlab.com/"$releasename"/boot-core.git "$bootcorepath"
	cp -avx "$bootcorefiles" "$isorootdir"
}

cleanupiso () {
	umount "$isostagingdir"
	rm -rf "$isosyncdir"
	rm -rf "$isostagingdir"
	rm -rf "$isosupersquashdir"
	rm -rf "$bootcorepath"
}

makeisoimg () {
	grub2-mkrescue -o ""$releasename"-"$releasetarget"-"$releaseversion"-"$isomainarch".iso" "$isorootdir"
}

makeiso () {
	chrootchecksum
	prepareiso
	bootcoreiso
	cleanupiso
	makeisoimg
}

# modechange functions

removeportagetree() {
	if [ -d ""$portagedir"/.git" ] ; then
		find "$portagedir" -mindepth 1 -name "packages" -prune -o -name "distfiles" -prune -o -exec rm -rf {} \; > /dev/null 2>&1
	fi
}

removeportageconfig() {
	rm ""$confdir"/make.conf" > /dev/null 2>&1
	rm ""$confdir"/make.profile" > /dev/null 2>&1
	rm "$confdir" > /dev/null 2>&1
	rm -rf "$clonedir" > /dev/null 2>&1
}

resetmode () {
	removeportagetree
	removeportageconfig
}

fetchportageconfig() {
	pushd /opt > /dev/null 2>&1
	git clone https://gitlab.com/redcore/redcore-build.git
	popd > /dev/null 2>&1
}

fetchfullportagetree() {
	if [[ ! -d "$portagedir"/.git ]] ; then
		cd "$portagedir" && git init > /dev/null 2>&1
		git remote add origin https://gitlab.com/redcore/portage.git
		git pull --depth=1 origin master
		git branch -u origin/master master
		rm -rf ""$portagedir"/profiles/updates"
	fi
}

fetchminimalportagetree() {
	if [[ ! -d "$portagedir"/.git ]] ; then
		cd "$portagedir" && git init > /dev/null 2>&1
		git remote add origin https://gitlab.com/redcore/portage.git
		git config core.sparsecheckout true
		echo "profiles/*" >> .git/info/sparse-checkout
		echo "metadata/*" >> .git/info/sparse-checkout
		echo "eclass/*" >> .git/info/sparse-checkout
		echo ".gitignore" >> .git/info/sparse-checkout
		git pull --depth=1 origin master
		git branch -u origin/master master
		rm -rf ""$portagedir"/profiles/updates"
	fi
}

setbinmodeconfig() {
	ln -sf "$gitdir" "$confdir"
	ln -sf "$confdir"/make.conf.amd64-binmode "$confdir"/make.conf
	eselect profile set 1
	env-update
	. /etc/profile
}

setmixedmodeconfig() {
	ln -sf "$gitdir" "$confdir"
	ln -sf "$confdir"/make.conf.amd64-mixedmode "$confdir"/make.conf
	eselect profile set 1
	env-update
	. /etc/profile
}

setsrcmodeconfig () {
	ln -sf "$gitdir" "$confdir"
	ln -sf "$confdir"/make.conf.amd64-srcmode "$confdir"/make.conf
	eselect profile set 1
	env-update
	. /etc/profile
}

binmode() {
	resetmode
	fetchportageconfig
	fetchminimalportagetree
	setbinmodeconfig
}

mixedmode() {
	resetmode
	ferchportageconfig
	fetchfullportagetree
	setmixedconfig
}

srcmode() {
	resetmode
	fetchportageconfig
	fetchfullportagetree
	setsrcmodeconfig
}