aboutsummaryrefslogtreecommitdiff
path: root/config.sh
blob: 843cdae8ac1b9c234fc614409c42cd01fe3fdcaa (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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
#!/bin/bash

set -euxo pipefail

installarch=$(uname -m)
kver=$(basename "$(ls -d /usr/lib/modules/*/)")

if [[ $installarch != "aarch64" ]]; then
    printf 'Error: this specific config is only meant for Surface Pro 12", and it is built on ARM64 chip (you have "%s" as build arch).' "$installarch"
    exit 1
fi

#======================================
# Functions...
#--------------------------------------
test -f /.kconfig && . /.kconfig
test -f /.profile && . /.profile

#======================================
# Greeting...
#--------------------------------------
echo "Configure image: [$kiwi_iname]-[$kiwi_profiles]..."

#======================================
# Set SELinux booleans
#--------------------------------------
if [[ "$kiwi_profiles" != *"Container"* ]] && [[ "$kiwi_profiles" != *"FEX"* ]] && [[ "$kiwi_profiles" != *"WSL"* ]]; then
	## Fixes KDE Plasma, see rhbz#2058657
	setsebool -P selinuxuser_execmod 1
fi

#======================================
# Clear machine specific configuration
#--------------------------------------
## Clear machine-id on pre generated images
rm -f /etc/machine-id
echo 'uninitialized' > /etc/machine-id
## remove random seed, the newly installed instance should make its own
rm -f /var/lib/systemd/random-seed

#======================================
# Configure grub correctly
#--------------------------------------
if [[ "$kiwi_profiles" != *"Container"* ]] && [[ "$kiwi_profiles" != *"FEX"* ]] && [[ "$kiwi_profiles" != *"WSL"* ]]; then
	## Works around issues with grub-bls
	## See: https://github.com/OSInside/kiwi/issues/2198
	echo "GRUB_DEFAULT=saved" >> /etc/default/grub
	## Disable submenus to match Fedora
	echo "GRUB_DISABLE_SUBMENU=true" >> /etc/default/grub
	## Disable recovery entries to match Fedora
	echo "GRUB_DISABLE_RECOVERY=true" >> /etc/default/grub
	## Surface Pro 12" (Snapdragon X1P-42-100): grub's gfxterm/GOP video probing
	## crashes on this GPU. Force plain text console, skip framebuffer handoff.
	echo "GRUB_TERMINAL_OUTPUT=console" >> /etc/default/grub
	echo "GRUB_GFXPAYLOAD_LINUX=text" >> /etc/default/grub
	## Surface Pro 12": GRUB_DEVICETREE is the only mechanism Fedora honours for
	## putting a devicetree line in a BLS entry -- 20-grub.install's mkbls emits
	## "devicetree /dtb-$kver/$GRUB_DEVICETREE". /etc/kernel/devicetree does
	## nothing here: /boot/<machine-id> does not exist, so kernel-install runs
	## with layout=other and 90-loaderentry.install (which would read it) bails.
	echo "GRUB_DEVICETREE=qcom/x1p42100-microsoft-surface-pro-12-inch.dtb" >> /etc/default/grub
	## Upstream sets menu_auto_hide=1 + boot_indeterminate=1 here for Disk images
	## to match anaconda's behaviour. Deliberately not done for Surface Pro 12":
	## with the menu hidden, GRUB counts down GRUB_TIMEOUT against a blank screen
	## and then crashes during the hand-off on this firmware, so the first boot
	## always fails and only the second succeeds. The equivalent for the
	## anaconda-installed system is the /etc/anaconda/conf.d drop-in below.
fi

#======================================
# Resize root partition on first boot
#--------------------------------------

if [[ "$kiwi_profiles" == *"Disk"* ]]; then
	mkdir -p /etc/repart.d/
	cat > /etc/repart.d/50-root.conf << EOF
[Partition]
Type=root
EOF
fi

#======================================
# Delete & lock the root user password
#--------------------------------------
if [[ "$kiwi_profiles" == *"Cloud"* ]] || [[ "$kiwi_profiles" == *"Disk"* ]] || [[ "$kiwi_profiles" == *"Live"* ]] || [[ "$kiwi_profiles" == *"WSL"* ]]; then
	passwd -d root
	passwd -l root
fi

#======================================
# Setup default services
#--------------------------------------

if [[ "$kiwi_profiles" == *"Live"* ]]; then
	## Configure livesys session
	if [[ "$kiwi_profiles" == *"GNOME"* ]]; then
		echo 'livesys_session="gnome"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"KDE"* ]]; then
		echo 'livesys_session="kde"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"Budgie"* ]]; then
		echo 'livesys_session="budgie"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"Cinnamon"* ]]; then
		echo 'livesys_session="cinnamon"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"COSMIC"* ]]; then
		echo 'livesys_session="cosmic"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"i3"* ]]; then
		echo 'livesys_session="i3"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"LXDE"* ]]; then
		echo 'livesys_session="lxde"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"LXQt"* ]]; then
		echo 'livesys_session="lxqt"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"MATE_Compiz"* ]]; then
		echo 'livesys_session="mate"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"MiracleWM"* ]]; then
		echo 'livesys_session="miraclewm"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"Sway"* ]]; then
		echo 'livesys_session="sway"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"SoaS"* ]]; then
		echo 'livesys_session="soas"' > /etc/sysconfig/livesys
	fi
	if [[ "$kiwi_profiles" == *"Xfce"* ]]; then
		echo 'livesys_session="xfce"' > /etc/sysconfig/livesys
	fi
fi

#======================================
# Setup firstboot initial setup
#--------------------------------------

if [[ "$kiwi_profiles" == *"Disk"* ]]; then
	if [[ "$kiwi_profiles" != *"GNOME"* ]] && [[ "$kiwi_profiles" != *"KDE"* ]] && [[ "$kiwi_profiles" != *"COSMIC"* ]]; then
		## Enable initial-setup
		systemctl enable initial-setup.service
		## Enable reconfig mode
		touch /etc/reconfigSys
	fi
fi

#======================================
# Setup default target
#--------------------------------------
if [[ "$kiwi_profiles" != *"Container"* ]] && [[ "$kiwi_profiles" != *"FEX"* ]]; then
	if [[ "$kiwi_profiles" == *"Desktop"* ]]; then
		systemctl set-default graphical.target
	else
		systemctl set-default multi-user.target
	fi
fi

#======================================
# Setup default customizations
#--------------------------------------

if [[ "$kiwi_profiles" == *"Server"* ]]; then
	# Trigger lvm-devices-import.path and .service to create
	# a new /etc/lvm/devices/system.devices for the root VG.
	rm -f /etc/lvm/devices/system.devices
	touch /etc/lvm/devices/auto-import-rootvg
fi

if [[ "$kiwi_profiles" == *"Azure"* ]]; then
cat > /etc/ssh/sshd_config.d/50-client-alive-interval.conf << EOF
ClientAliveInterval 120
EOF

cat >> /etc/chrony.conf << EOF
# Azure's virtual time source:
# https://docs.microsoft.com/en-us/azure/virtual-machines/linux/time-sync#check-for-ptp-clock-source
refclock PHC /dev/ptp_hyperv poll 3 dpoll -2 offset 0
EOF

# Support Azure's accelerated networking feature; without this the network fails
# to come up. It may need adjustments for additional drivers in the future.
cat > /etc/NetworkManager/conf.d/99-azure-unmanaged-devices.conf << EOF
# Ignore SR-IOV interface on Azure, since it's transparently bonded
# to the synthetic interface
[keyfile]
unmanaged-devices=driver:mlx4_core;driver:mlx5_core
EOF

# Configure DHCP timeout for Azure to retry indefinitely during VM provisioning
cat > /etc/NetworkManager/conf.d/99-azure-dhcp-timeout.conf << EOF
# Azure requires VMs to attempt DHCP for at least 300 seconds during provisioning,
# as that's the upper bound of how long Azure waits to make networking available.
# NetworkManager's ipv4.dhcp-timeout setting accepts 2147483647 as a special value
# representing infinity, which ensures DHCP keeps retrying indefinitely. This is
# more robust than a fixed timeout and matches systemd-networkd's default behavior.
# Both IPv4 and IPv6 are configured to prepare for IPv6-only scenarios.
# Reference: https://networkmanager.dev/docs/api/latest/nm-settings-nmcli.html
[connection]
ipv4.dhcp-timeout=2147483647
ipv6.dhcp-timeout=2147483647
EOF
fi

if [[ "$kiwi_profiles" == *"GCE"* ]]; then
cat <<EOF > /etc/NetworkManager/conf.d/gcp-mtu.conf
# In GCP it is recommended to use 1460 as the MTU.
# Set it to 1460 for all connections.
# https://cloud.google.com/network-connectivity/docs/vpn/concepts/mtu-considerations
[connection]
ethernet.mtu = 1460
EOF
fi

if [[ "$kiwi_profiles" == *"Vagrant"* ]]; then
sed -e 's/.*UseDNS.*/UseDNS no/' -i /etc/ssh/sshd_config
mkdir -m 0700 -p ~vagrant/.ssh
cat > ~vagrant/.ssh/authorized_keys << EOKEYS
ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAQEA6NF8iallvQVp22WDkTkyrtvp9eWW6A8YVr+kz4TjGYe7gHzIw+niNltGEFHzD8+v1I2YJ6oXevct1YeS0o9HZyN1Q9qgCgzUFtdOKLv6IedplqoPkcmF0aYet2PkEDo3MlTBckFXPITAMzF8dJSIFo9D8HfdOV0IAdx4O7PtixWKn5y2hMNG0zQPyUecp4pzC6kivAIhyfHilFR61RGL+GPXQ2MWZWFYbAGjyiYJnAmCP3NOTd0jMZEnDkbUvxhMmBYSdETk1rRgm+R4LOzFUGaHqHDLKLX+FIPKcF96hrucXzcWyLbIbEgE98OHlnVYCzRdK8jlqm8tehUc9c9WhQ== vagrant insecure public key
EOKEYS
chmod 600 ~vagrant/.ssh/authorized_keys
chown -R vagrant:vagrant ~vagrant/.ssh/

cat > /etc/sudoers.d/vagrant << EOSUDOER
## Ensure the vagrant user always can use sudo
Defaults:vagrant !requiretty
vagrant ALL=(ALL) NOPASSWD: ALL
EOSUDOER
chmod 600 /etc/sudoers.d/vagrant

cat > /etc/ssh/sshd_config.d/10-vagrant-insecure-rsa-key.conf <<EOF
# For now the vagrant insecure key is an rsa key
# https://github.com/hashicorp/vagrant/issues/11783
PubkeyAcceptedKeyTypes=+ssh-rsa
EOF

# Further suggestion from @purpleidea (James Shubin) - extend key to root users as well
mkdir -m 0700 -p /root/.ssh
cp /home/vagrant/.ssh/authorized_keys /root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
chown -R root:root /root/.ssh
fi

# https://bugzilla.redhat.com/show_bug.cgi?id=2356069
if [[ "$kiwi_profiles" == *"Container"* ]] && [[ "$kiwi_profiles" != *"Init"* ]]; then
	if [[ -f /usr/lib/tmpfiles.d/rootfiles.conf ]]; then
		systemd-tmpfiles --create /usr/lib/tmpfiles.d/rootfiles.conf
	fi
fi

if [[ "$kiwi_profiles" == *"Container"* ]] || [[ "$kiwi_profiles" == *"FEX"* ]]; then
	# Set install langs macro so that new rpms that get installed will
	# only install langs that we limit it to.
	LANG="en_US"
	echo "%_install_langs $LANG" > /etc/rpm/macros.image-language-conf

	# https://bugzilla.redhat.com/show_bug.cgi?id=1727489
	echo 'LANG="C.UTF-8"' >  /etc/locale.conf

	# https://bugzilla.redhat.com/show_bug.cgi?id=1400682
	echo "Import RPM GPG key"
	releasever=$(rpm --eval '%{?fedora}')

	# When building ELN containers, we don't have the %{fedora} macro
	if [ -z $releasever ]; then
		releasever=eln
	fi

	rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-fedora-$releasever-primary

	echo "# fstab intentionally empty for containers" > /etc/fstab

	# Remove machine-id on pre generated images
	rm -f /etc/machine-id
	touch /etc/machine-id

	echo "# resolv placeholder" > /etc/resolv.conf
	chmod 644 /etc/resolv.conf

	# Remove extraneous files
	rm -rf /tmp/*

	# https://pagure.io/atomic-wg/issue/308
	printf "tsflags=nodocs\n" >>/etc/dnf/dnf.conf

	if [[ "$kiwi_profiles" == *"Base-Generic-Minimal"* ]]; then
		# remove some random help txt files
		rm -fv /usr/share/gnupg/help*.txt

		# Pruning random things
		rm /usr/lib/rpm/rpm.daily
		rm -rfv /usr/lib64/nss/unsupported-tools/  # unsupported

		# Statically linked crap
		rm -fv /usr/sbin/{glibc_post_upgrade.x86_64,sln}
		ln /usr/bin/ln usr/sbin/sln

		# Remove some dnf info
		rm -rfv /var/lib/dnf

		# don't need icons
		rm -rfv /usr/share/icons/*

		#some random not-that-useful binaries
		rm -fv /usr/bin/pinky

		# we lose presets by removing /usr/lib/systemd but we do not care
		rm -rfv /usr/lib/systemd
	fi
	if [[ "$kiwi_profiles" == *"Toolbox"* ]]; then
		# Remove macros.image-language-conf file
		rm -f /etc/rpm/macros.image-language-conf

		# Remove 'tsflags=nodocs' line from dnf.conf
		sed -i '/tsflags=nodocs/d' /etc/dnf/dnf.conf
	fi
fi

if [[ "$kiwi_profiles" == *"Jam"* ]]; then
# Override boot configuration in Anaconda to enable threaded IRQs
cat > /usr/share/anaconda/post-scripts/85-jam-threadirqs.ks << THREADIRQS_EOF
%post

echo "Enable threaded IRQs"
grubby --update-kernel=ALL --args="threadirqs"
%end
THREADIRQS_EOF

# Override new user configuration to add audio groups
sed -e "s/UserGroups=.*/UserGroups=wheel,jackuser,audio/" -i /etc/xdg/plasmasetuprc

# Override livesys-kde settings
cat >> /var/lib/livesys/livesys-session-extra << EOF

#setup kickoff favorites
/bin/mkdir -p /etc/skel/.config

JAMFAVORITES=/usr/share/applications/firefox.desktop,/usr/share/applications/mozilla-thunderbird.desktop,/usr/share/applications/studio-controls.desktop,/usr/share/applications/ardour6.desktop,/usr/share/applications/carla.desktop,/usr/share/applications/org.kde.konsole.desktop,/usr/share/applications/org.kde.dolphin.desktop,/usr/share/applications/systemsettings.desktop
JAMFAVORITESLIVE=/usr/share/applications/liveinst.desktop,\$JAMFAVORITES

cat <<FOE  >> /etc/skel/.config/kickoffrc
[Favorites]
FavoriteURLs=\$JAMFAVORITES
FOE

/usr/sbin/usermod -a -G jackuser,audio liveuser
EOF
fi

if [[ "$kiwi_profiles" == *"Design_suite"* ]]; then
# Add link to lists of tutorials
cat >> /usr/share/applications/list-design-tutorials.desktop << FOE
[Desktop Entry]
Name=List of design tutorials
GenericName=List of Tutorials for Designers
Comment=Reference of Design Related Tutorials
Exec=xdg-open https://fedoraproject.org/wiki/Design_Suite/Tutorials
Type=Application
Icon=applications-graphics
Categories=Graphics;Documentation;
FOE
chmod a+x /usr/share/applications/list-design-tutorials.desktop

# Add information about Fedora Design Suite
cat >> /usr/share/applications/fedora-design-suite.desktop << FOE
[Desktop Entry]
Name=Design Suite Info
GenericName=About Design Suite
Comment=Wiki page of Design Suite
Exec=xdg-open https://fedoraproject.org/wiki/Design_Suite
Type=Application
Icon=applications-internet
Categories=Documentation;
FOE
chmod a+x /usr/share/applications/fedora-design-suite.desktop

# Add information about Fedora Design Team
cat >> /usr/share/applications/fedora-design-team.desktop << FOE
[Desktop Entry]
Name=Design Team Info
GenericName=About Design Team
Comment=Documentation about Design Team
Exec=xdg-open https://docs.fedoraproject.org/en-US/design/
Type=Application
Icon=applications-internet
Categories=Documentation;
FOE
chmod a+x /usr/share/applications/fedora-design-team.desktop

# Use Powerline in bash
cat >>  /etc/profile.d/powerline-shell.sh << FOE
# Enable powerline daemon
if [ -f `which powerline-daemon` ]; then
  powerline-daemon -q
  POWERLINE_BASH_CONTINUATION=1
  POWERLINE_BASH_SELECT=1
  . /usr/share/powerline/bash/powerline.sh
fi
FOE
chmod 644 /etc/profile.d/powerline-shell.sh
# End powerline override

# Override the favorite desktop application in Dash
cat >> /usr/share/glib-2.0/schemas/org.gnome.shell.gschema.override << FOE
[org.gnome.shell]
favorite-apps=['org.mozilla.firefox.desktop', 'shotwell.desktop', 'gimp.desktop', 'darktable.desktop', 'krita.desktop', 'inkscape.desktop', 'blender.desktop', 'libreoffice-writer.desktop', 'scribus.desktop', 'kdenlive.desktop', 'nautilus.desktop', 'anaconda.desktop', 'list-design-tutorials.desktop']
FOE

# Rebuild schema cache with any overrides we installed
glib-compile-schemas /usr/share/glib-2.0/schemas
fi

if [[ "$kiwi_profiles" == *"SoaS"* ]]; then
# Get proper release naming in the control panel
cat >> /boot/olpc_build << EOF
Sugar on a Stick
EOF
cat /etc/fedora-release >> /boot/olpc_build

# Set the default Plymouth theme to the Sugar Desktop one
/usr/sbin/plymouth-set-default-theme sugar

# Fix https://bugzilla.redhat.com/show_bug.cgi?id=2239137
cat > /etc/xdg/autostart/xfce-polkit.desktop <<EOF
[Desktop Entry]
Type=Application
Name=xfce-polkit
Exec=/usr/libexec/xfce-polkit
EOF

fi

#======================================
# Surface Pro 12" customizations
#--------------------------------------

# /etc/surface-dtb is the master copy 60-surface-dtb.install re-stages into each
# new kernel's module tree on update. The other two are what that hook and
# 10-devicetree.install would normally produce, done up front here because
# kernel-install never runs during this build -- without the module-tree copy,
# anything keyed off /usr/lib/modules/$kver/dtb (including the anaconda
# post-script) finds nothing and silently does nothing.
install -Dm644 /tmp/harrisonvanderbyl/surface-pro-12-inch-linux/boot/dtb \
	"/etc/surface-dtb/x1p42100-microsoft-surface-pro-12-inch.dtb"
install -Dm644 /tmp/harrisonvanderbyl/surface-pro-12-inch-linux/boot/dtb \
	"/usr/lib/modules/$kver/dtb/qcom/x1p42100-microsoft-surface-pro-12-inch.dtb"
install -Dm644 /tmp/harrisonvanderbyl/surface-pro-12-inch-linux/boot/dtb \
	"/boot/dtb-$kver/qcom/x1p42100-microsoft-surface-pro-12-inch.dtb"
# /boot/dtb -> dtb-$kver is what grubby's 10-devicetree.install maintains, and
# what the boot loader entry's devicetree path resolves through
ln -sfn "dtb-$kver" /boot/dtb
# Defer qcom_q6v5_pas past switch-root so the DSPs' auto_boot can actually find
# their firmware; see the comments in the file for the full failure mode.
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/dracut.conf.d/surface-pro-12-inch.conf \
    /etc/dracut.conf.d/surface-pro-12-inch.conf
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/kernel/cmdline \
    /etc/kernel/cmdline
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/kernel/devicetree \
    /etc/kernel/devicetree
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/kernel/install.conf \
    /etc/kernel/install.conf
install -Dm755 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/usr/lib/kernel/install.d/60-surface-dtb.install \
	/usr/lib/kernel/install.d/60-surface-dtb.install
# Puts the "devicetree" line into the BLS entry. This is the only mechanism that
# survives an anaconda install: /etc/kernel/devicetree is never read (layout is
# always "other" on Fedora, so 90-loaderentry.install bails) and GRUB_DEVICETREE
# is dropped when anaconda rewrites /etc/default/grub from its own key list.
# kernel-install plugins do run in the target during installation, and sorting
# after 20-grub.install means the entry it generates is already there to patch.
install -Dm755 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/usr/lib/kernel/install.d/95-surface-dtb-patch.install \
	/usr/lib/kernel/install.d/95-surface-dtb-patch.install
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/udev/rules.d/61-sensors-surface-pro-12-inch.rules \
    /etc/udev/rules.d/61-sensors-surface-pro-12-inch.rules

if [[ "$kiwi_profiles" == *"Live"* ]]; then
	# Stop anaconda hiding the GRUB menu on the installed system's first boot.
	# Only meaningful in the installer environment, hence Live-only; see the
	# comments in the file for why a hidden menu breaks this device.
	install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/anaconda/conf.d/90-surface-pro-12-inch.conf \
		/etc/anaconda/conf.d/90-surface-pro-12-inch.conf

	# Ship the CD-boot grub on the ISO, not the disk-boot one.
	#
	# kiwi picks the ISO's EFI loader by globbing the image root (see
	# Defaults.get_unsigned_grub_loader, target_type='iso'). That pattern list
	# has a CD-boot entry for x86_64 (gcdx64.efi) but none for aarch64, so here
	# it falls through to grubaa64.efi -- the *disk* image, whose baked-in grub
	# prefix is /EFI/fedora. Meanwhile kiwi writes its earlyboot config to
	# /EFI/BOOT/grub.cfg, and iso-esp-excludes.yaml deletes "fedora" from the
	# embedded ESP outright (rhbz#2358785). Net result on aarch64: the loader
	# looks for its config in a directory that was deliberately removed, fails
	# to load normal.mod, and dies before drawing anything -- black screen.
	#
	# gcdaa64.efi is byte-for-byte the same grub except its prefix is /EFI/BOOT,
	# which is exactly where kiwi puts the config. Overwrite the path kiwi globs
	# so it picks that one up instead.
	#
	# Only affects direct UEFI boot. Ventoy never executes this binary -- it
	# loopback-mounts the ISO and runs /boot/grub2/grub.cfg under its own grub.
	cp -a /boot/efi/EFI/fedora/gcdaa64.efi /boot/efi/EFI/fedora/grubaa64.efi
fi

# 51-dracut-rescue.install isn't UKI-aware -- it unconditionally sed's a BLS
# loader entry that layout=uki never creates. Mask it (kernel-install(8)'s
# documented way to disable a plugin: a /dev/null symlink of the same name).
mkdir -p /etc/kernel/install.d
ln -sf /dev/null /etc/kernel/install.d/51-dracut-rescue.install
# kernel-core's own package scriptlet already ran kernel-install once, before
# this mask existed (root overlay lands after package install), leaving a
# stale rescue image behind. Remove it now so kiwi's systemd_boot EFI-FAT-image
# step doesn't try to cram it in too.
rm -f /boot/initramfs-0-rescue-*.img /boot/loader/entries/*-0-rescue.conf

# GPU/ADSP/CDSP firmware + qcom DSP/sensor share config from the device tree repo
# (readme.md: "recursively copy the files in ./lib/ to /lib/", same for ./usr/)
cp -a /tmp/harrisonvanderbyl/surface-pro-12-inch-linux/lib/. /lib/
cp -a /tmp/harrisonvanderbyl/surface-pro-12-inch-linux/usr/. /usr/

build_tmp=$(mktemp -d)

# Snapshot what's installed before pulling in build-only tooling, so we can
# remove exactly what this block adds afterward -- and nothing Kiwi's own
# package lists (other team/desktop profiles) already wanted installed.
pkgs_before=$(mktemp)
rpm -qa --qf '%{NAME}\n' | sort > "$pkgs_before"

# Wi-Fi (ath12k) board file fixup, adapted from dwhinham/linux-surface-pro-11
dnf install -y python3 curl zstd
(
	cd "$build_tmp"
	cp /lib/firmware/ath12k/WCN7850/hw2.0/board-2.bin* .
	if [ -f board-2.bin.zst ]; then
		zstd -d board-2.bin.zst
	elif [ -f board-2.bin.xz ]; then
	    xz -d board-2.bin.xz
	fi
	curl --output bdencoder.py -sL \
	    https://raw.githubusercontent.com/qca/qca-swiss-army-knife/refs/heads/master/tools/scripts/ath12k/ath12k-bdencoder
	python3 bdencoder.py --extract board-2.bin
	rm -f bdencoder.py
	mv "bus=pci,vendor=17cb,device=1107,subsystem-vendor=17cb,subsystem-device=3378,qmi-chip-id=2,qmi-board-id=255.bin" \
	    /lib/firmware/ath12k/WCN7850/hw2.0/board.bin
)

# AudioReach topology; alsatplg (the topology *compiler*) is build-only, the
# compiled blob is what ships. alsa-ucm-utils' UCM profiles are read at runtime
# by the audio stack, so that one's installed separately, alongside v4l-utils below.
dnf install -y git cmake gcc gcc-c++ make pkgconf-pkg-config alsa-topology-utils m4
(
	cd /tmp/linux-msm/audioreach-topology
	git clone https://github.com/linux-msm/audioreach-topology
	cd audioreach-topology
	export FW_LOCATION=/lib/firmware
	cmake .
	make
	make install
)

# hexagonrpc + libssc + iio-sensor-proxy (fastrpc/sensor stack); not packaged in Fedora
dnf install -y \
	systemd-devel libgudev-devel polkit-devel gtk3-devel python3-devel \
	meson ninja-build gcc gcc-c++ git pkgconf-pkg-config \
	libqmi-devel glib2-devel protobuf3-c-devel gobject-introspection-devel
# meson's default prefix is /usr/local, and Fedora's dynamic linker does not
# search there -- /etc/ld.so.conf.d ships only iscsi and pipewire entries. Without
# this, libhexagonrpc.so installs fine but ld.so can't find it, and hexagonrpcd
# dies at startup with "no libhexagonrpc.so" (which then takes iio-sensor-proxy
# down with it via Requires=). Cover both libdir spellings.
printf '/usr/local/lib\n/usr/local/lib64\n' > /etc/ld.so.conf.d/surface-pro-12-inch.conf
(
	cd /tmp/harrisonvanderbyl/hexagonrpc
	meson setup build
	ninja -C build
	ninja -C build install
	ldconfig
)
(
	cd /tmp/DylanVanAssche/libssc
	meson setup _build
	meson compile -C _build
	meson install -C _build
	ldconfig
)
(
	cd /tmp/harrisonvanderbyl/iio-sensor-proxy
	meson _build -Dssc-support=enabled -Dprefix=/usr
	ninja -v -C _build install
)

# Remove exactly what got newly installed since the snapshot above (compilers,
# -devel headers, alsatplg, and whatever they pulled in transitively). Anything
# that was already installed before -- because some other team/desktop profile
# in this same build wanted it, or it's part of the base image -- is left alone.
pkgs_new=$(comm -13 "$pkgs_before" <(rpm -qa --qf '%{NAME}\n' | sort))
if [ -n "$pkgs_new" ]; then
	dnf remove -y $pkgs_new
fi
rm -f "$pkgs_before"

# UCM profiles read at runtime by the audio stack
dnf install -y alsa-ucm-utils

install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/systemd/system/hexagonrpc.service \
	/etc/systemd/system/hexagonrpc.service
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/systemd/system/iio-sensor-proxy.service \
	/etc/systemd/system/iio-sensor-proxy.service
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/udev/rules.d/61-sensors-surface-pro-12-inch.rules \
	/etc/udev/rules.d/61-sensors-surface-pro-12-inch.rules
systemctl enable hexagonrpc.service iio-sensor-proxy.service

# Rear camera pipeline (msm/camss) needs /dev/media0, which only exists once the
# real silicon probes on the booted system -- install the wiring but do not run
# it here, there is no camera hardware in the KIWI build chroot.
dnf install -y v4l-utils
install -Dm755 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/usr/local/bin/wireupcameras.sh \
    /usr/local/bin/wireupcameras.sh
install -Dm644 /tmp/SayaAndy/surface-pro-12-inch-linux-fedora/etc/systemd/system/wireupcameras.service \
	/etc/systemd/system/wireupcameras.service
systemctl enable wireupcameras.service

restorecon -Rv /usr /lib /etc/systemd/system /etc/udev/rules.d

#======================================
# Set the WSL name for ELN
#--------------------------------------
if [[ "$kiwi_profiles" == *"WSL"* ]] && [[ "$kiwi_iname" == *"ELN"* ]]; then
wsl-setup --name Fedora-ELN
fi

#======================================
# Finalization steps
#--------------------------------------
# Inhibit the ldconfig cache generation unit, see rhbz2348669
touch -r "/usr" "/etc/.updated" "/var/.updated"

if [[ "$kiwi_profiles" == *"FEX"* ]]; then
	# Remove most things except libraries used by FEX and wine stuff.
	# Most binaries that are present in non-x86 architectures should be removed,
	# so they do not run under emulation.

	# rm mingw static libs and headers
	rm -rf /usr/{x86_64,i686}-w64-mingw32/sys-root/mingw/{lib,include}

	# rm everything in libexec
	rm -rf /usr/libexec

	# rm everything in /usr/share except wine and mesa related stuff
	find /usr/share -mindepth 1 -maxdepth 1 \
		\! -name wine -a \
		\! -name mesa-demos -a \
		\! -name drirc.d -a \
		\! -name vulkan \
		\! -name licenses \
		-exec rm -rf {} \;

	# rm everything in /etc except /etc/alternatives and ld stuff
	find /etc -mindepth 1 -maxdepth 1 \
		\! -name alternatives -a \
		\! -name 'ld.so*' -a \
		\! -name '*.kiwi' \
		-exec rm -rf {} \;

	# rm non-libs in lib/lib64
	rm -rf /usr/{lib,lib64}/{locale,tmpfiles.d,systemd,modprobe.d,kbd,cmake}
	rm -rf /usr/{lib,lib64}/python*

	# lib/clc and lib64/clc are identical, replace with a symlink
	rm -rf /usr/lib/clc
	ln -s ../lib64/clc /usr/lib/clc

	# rm sbin except for ldconfig, we don't even have root in some setups
	find /usr/sbin -mindepth 1 -maxdepth 1 \
		\! -name 'ldconfig' \
		-exec rm -rf {} \;

	# rm misc stuff
	rm -rf /usr/{include,games,local,src,tmp}

	# Finally, remove most binaries except Wine stuff, Mesa stuff, the shell,
	# path-related stuff, and system info tools.
	find /usr/bin -mindepth 1 -maxdepth 1 \
		\! -name 'wine*' -a \
		\! -name 'mango*' -a \
		\! -name notepad -a \
		\! -name 'msi*' -a \
		\! -name regedit -a \
		\! -name regsvr32 -a \
		\! -name 'vulkan*' -a \
		\! -name 'vk*' -a \
		\! -name clinfo -a \
		\! -name eglinfo -a \
		\! -name glxinfo -a \
		\! -name ulimit -a \
		\! -name ldd -a \
		\! -name env -a \
		\! -name sh -a \
		\! -name bash -a \
		\! -name ls -a \
		\! -name stat -a \
		\! -name dirname -a \
		\! -name realpath -a \
		\! -name readlink -a \
		\! -name basename -a \
		\! -name nproc -a \
		\! -name uname -a \
		\! -name arch -a \
		\! -name rm \
		-exec rm -rf {} \;

	# Do this last for obvious reasons.
	rm /usr/bin/rm
fi

rm -rf /tmp/*

exit 0