-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnotlfs.sh
More file actions
3624 lines (3017 loc) · 104 KB
/
Copy pathnotlfs.sh
File metadata and controls
3624 lines (3017 loc) · 104 KB
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
#!/bin/bash
# =============================================================================
# NotLFS: A Modular Linux Build Framework with Multi-Init System Support
# =============================================================================
#
# NotLFS (Not Linux From Scratch) is an extensible framework for building
# a custom Linux system from source, inspired by LFS but with modern
# features and multi-init system support.
#
# Features:
# - Multiple init system support (s6, s6-rc, dinit, runit, sysv, systemd)
# - Modular package system with dependency resolution
# - XML and YAML configuration support
# - Auto, interactive, and manual build modes
# - Smart build caching and resumption
# - Service management configuration
# - System hardening options
# - Customizable build profiles
#
# USAGE:
# ./notlfs.sh [OPTIONS] [COMMAND]
#
# COMMANDS:
# build - Start the build process (default)
# config - Generate/edit configuration
# list-packages - List available packages
# list-profiles - List available build profiles
# add-package - Add a new package definition
# add-profile - Add a new build profile
# clean - Clean build artifacts
# shell - Enter interactive build environment
# init - Initialize a new NotLFS project
# export - Export current configuration
# validate - Validate configuration and dependencies
#
# OPTIONS:
# -c, --config FILE Use specific configuration file
# -p, --profile NAME Use specific build profile
# -m, --mode MODE Build mode: auto|manual|interactive
# -i, --init SYSTEM Init system: s6, s6-rc, dinit, runit, sysv, systemd
# -t, --target ARCH Target architecture
# -d, --debug Enable debug output
# -y, --yes Skip confirmation prompts
# -h, --help Show this help
#
# EXAMPLES:
# ./notlfs.sh -p minimal -i s6-rc
# ./notlfs.sh --profile desktop --init runit --mode interactive
# ./notlfs.sh list-profiles
# ./notlfs.sh add-package neovim
# ./notlfs.sh validate
#
set -o nounset
set -o pipefail
# =============================================================================
# GLOBAL CONFIGURATION
# =============================================================================
SCRIPT_DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" && pwd)"
NOTLFS_ROOT="${SCRIPT_DIR}"
BUILD_ROOT="${NOTLFS_ROOT}/build"
CONFIG_DIR="${NOTLFS_ROOT}/configs"
PROFILES_DIR="${NOTLFS_ROOT}/profiles"
PACKAGES_DIR="${NOTLFS_ROOT}/packages"
INIT_DIR="${NOTLFS_ROOT}/init"
LOGS_DIR="${NOTLFS_ROOT}/logs"
SRC_DIR="${NOTLFS_ROOT}/sources"
TOOLS_DIR="${NOTLFS_ROOT}/tools"
CACHE_DIR="${NOTLFS_ROOT}/cache"
OUTPUT_DIR="${NOTLFS_ROOT}/output"
# Default values
CONFIG_FILE="${CONFIG_DIR}/notlfs.xml"
PROFILE=""
BUILD_MODE="interactive"
INIT_SYSTEM="s6-rc" # Default to s6-rc as it's lightweight and modern
TARGET_ARCH="$(uname -m)"
DEBUG_MODE=false
YES_MODE=false
JOB_COUNT="$(nproc)"
# Supported init systems
SUPPORTED_INITS=("s6" "s6-rc" "s6-init" "dinit" "runit" "sysv" "systemd" "openrc")
# Supported architectures
SUPPORTED_ARCHS=("x86_64" "aarch64" "i386" "armv7l" "riscv64")
# Colors for output
readonly RED='\033[0;31m'
readonly GREEN='\033[0;32m'
readonly YELLOW='\033[1;33m'
readonly BLUE='\033[0;34m'
readonly MAGENTA='\033[0;35m'
readonly CYAN='\033[0;36m'
readonly NC='\033[0m' # No Color
# =============================================================================
# LOGGING FUNCTIONS
# =============================================================================
log_info() {
echo -e "${GREEN}[INFO]${NC} $1"
}
log_warn() {
echo -e "${YELLOW}[WARN]${NC} $1"
}
log_error() {
echo -e "${RED}[ERROR]${NC} $1" >&2
}
log_debug() {
if [ "$DEBUG_MODE" = true ]; then
echo -e "${BLUE}[DEBUG]${NC} $1"
fi
}
log_section() {
echo ""
echo "============================================================================"
echo " $1"
echo "============================================================================"
}
log_subsection() {
echo ""
echo " --- $1 ---"
}
log_init() {
echo -e "${CYAN}[INIT]${NC} $1"
}
# =============================================================================
# ERROR HANDLING
# =============================================================================
die() {
log_error "$1"
exit 1
}
assert_root() {
if [ "$(id -u)" -ne 0 ]; then
die "This operation requires root privileges. Please run with sudo or as root."
fi
}
assert_command() {
if ! command -v "$1" &> /dev/null; then
die "Required command '$1' not found. Please install it."
fi
}
assert_file() {
if [ ! -f "$1" ]; then
die "File not found: $1"
fi
}
assert_dir() {
if [ ! -d "$1" ]; then
die "Directory not found: $1"
fi
}
# =============================================================================
# DIRECTORY STRUCTURE SETUP
# =============================================================================
setup_directories() {
log_section "Setting up NotLFS directory structure"
local dirs=(
"${BUILD_ROOT}"
"${CONFIG_DIR}"
"${PROFILES_DIR}"
"${PACKAGES_DIR}"
"${INIT_DIR}"
"${LOGS_DIR}"
"${SRC_DIR}"
"${TOOLS_DIR}"
"${CACHE_DIR}"
"${OUTPUT_DIR}"
"${BUILD_ROOT}/tools"
"${BUILD_ROOT}/sources"
"${BUILD_ROOT}/build"
"${BUILD_ROOT}/pkg"
"${BUILD_ROOT}/notlfs"
"${INIT_DIR}/systems"
"${INIT_DIR}/services"
"${PROFILES_DIR}/base"
"${PROFILES_DIR}/minimal"
"${PROFILES_DIR}/desktop"
"${PROFILES_DIR}/server"
)
for dir in "${dirs[@]}"; do
mkdir -p "$dir"
log_debug "Created directory: $dir"
done
# Create /tools symlink if not exists
if [ ! -L "/tools" ] && [ ! -d "/tools" ]; then
ln -s "${BUILD_ROOT}/tools" /tools 2>/dev/null || {
mkdir -p /tools
}
fi
log_info "Directory structure created at ${NOTLFS_ROOT}"
}
# =============================================================================
# INITIALIZATION
# =============================================================================
initialize() {
log_section "Initializing NotLFS Framework"
# Check if already initialized
if [ -f "${NOTLFS_ROOT}/.notlfs-initialized" ]; then
log_debug "NotLFS already initialized"
return
fi
# Create directories
setup_directories
# Generate default configuration
if [ ! -f "$CONFIG_FILE" ]; then
generate_sample_config "$CONFIG_FILE"
fi
# Generate default profiles
generate_default_profiles
# Generate init system templates
generate_init_templates
# Create marker file
touch "${NOTLFS_ROOT}/.notlfs-initialized"
log_info "NotLFS initialization complete"
}
# =============================================================================
# CONFIGURATION MANAGEMENT
# =============================================================================
# Generate sample XML configuration
generate_sample_config() {
local output_file="$1"
cat > "$output_file" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<!-- NotLFS Configuration File -->
<!-- This file defines your custom Linux system build -->
<notlfs>
<!-- System Configuration -->
<system>
<name>notlfs-system</name>
<architecture>x86_64</architecture>
<jobs>4</jobs>
<hostname>notlfs</hostname>
<root_password>changeme</root_password>
<timezone>UTC</timezone>
<locale>en_US.UTF-8</locale>
<kernel_version>6.6.0</kernel_version>
</system>
<!-- Build Settings -->
<build>
<mode>interactive</mode>
<profile>minimal</profile>
<init_system>s6-rc</init_system>
<download_mirror>https://ftp.gnu.org/gnu</download_mirror>
<optimization>-O2 -pipe</optimization>
<strip_debug>false</strip_debug>
<keep_sources>false</keep_sources>
<parallel_downloads>4</parallel_downloads>
<build_timeout>3600</build_timeout>
</build>
<!-- Security Hardening -->
<security>
<enable_hardening>true</enable_hardening>
<stack_protector>true</stack_protector>
<fortify_source>true</fortify_source>
<relro>true</relro>
<aslr>true</aslr>
<noexec_stack>true</noexec_stack>
<pie>true</pie>
</security>
<!-- Network Configuration -->
<network>
<hostname>notlfs</hostname>
<domain>local</domain>
<enable_dhcp>true</enable_dhcp>
<nameservers>8.8.8.8 8.8.4.4</nameservers>
</network>
<!-- Package Selection -->
<packages>
<!-- Core System (required) -->
<category name="core" enabled="true">
<package name="binutils" enabled="true" />
<package name="gcc" enabled="true" stage="1" />
<package name="glibc" enabled="true" />
<package name="bash" enabled="true" />
<package name="coreutils" enabled="true" />
<package name="diffutils" enabled="true" />
<package name="file" enabled="true" />
<package name="findutils" enabled="true" />
<package name="gawk" enabled="true" />
<package name="grep" enabled="true" />
<package name="gzip" enabled="true" />
<package name="m4" enabled="true" />
<package name="make" enabled="true" />
<package name="patch" enabled="true" />
<package name="perl" enabled="true" />
<package name="sed" enabled="true" />
<package name="tar" enabled="true" />
<package name="texinfo" enabled="true" />
<package name="xz" enabled="true" />
<package name="zlib" enabled="true" />
</category>
<!-- Init System -->
<category name="init" enabled="true">
<package name="s6" enabled="false" />
<package name="s6-rc" enabled="true" />
<package name="s6-init" enabled="false" />
<package name="dinit" enabled="false" />
<package name="runit" enabled="false" />
<package name="sysv" enabled="false" />
<package name="systemd" enabled="false" />
<package name="openrc" enabled="false" />
</category>
<!-- Development Tools -->
<category name="dev" enabled="true">
<package name="autoconf" enabled="true" />
<package name="automake" enabled="true" />
<package name="bison" enabled="true" />
<package name="flex" enabled="true" />
<package name="git" enabled="true" />
<package name="gperf" enabled="true" />
<package name="libtool" enabled="true" />
<package name="pkgconf" enabled="true" />
</category>
<!-- Utilities -->
<category name="utils" enabled="true">
<package name="curl" enabled="true" />
<package name="wget" enabled="false" />
<package name="less" enabled="true" />
<package name="man-db" enabled="true" />
<package name="vim" enabled="false" />
<package name="nano" enabled="true" />
<package name="tmux" enabled="false" />
<package name="htop" enabled="false" />
</category>
<!-- Network -->
<category name="network" enabled="true">
<package name="openssl" enabled="true" />
<package name="openssh" enabled="true" />
<package name="iproute2" enabled="true" />
<package name="iana-etc" enabled="true" />
<package name="dhcpcd" enabled="false" />
<package name="connman" enabled="false" />
</category>
<!-- Desktop (optional) -->
<category name="desktop" enabled="false">
<package name="mesa" enabled="true" />
<package name="xorg-server" enabled="true" />
<package name="xorg-apps" enabled="true" />
<package name="wayland" enabled="false" />
<package name="weston" enabled="false" />
<package name="sway" enabled="false" />
</category>
<!-- Language Runtimes -->
<category name="languages" enabled="true">
<package name="python" enabled="true" />
<package name="nodejs" enabled="false" />
<package name="lua" enabled="false" />
<package name="ruby" enabled="false" />
<package name="go" enabled="false" />
<package name="rust" enabled="false" />
</category>
</packages>
<!-- Service Configuration -->
<services>
<service name="sshd" enabled="true" runlevel="default" />
<service name="dhcpcd" enabled="false" runlevel="default" />
<service name="cronie" enabled="false" runlevel="default" />
<service name="ntpd" enabled="false" runlevel="default" />
</services>
<!-- Custom Commands (hooks) -->
<hooks>
<hook stage="pre-init">
echo "NotLFS build started at $(date)"
</hook>
<hook stage="pre-toolchain">
echo "Building temporary toolchain..."
</hook>
<hook stage="post-toolchain">
echo "Toolchain build complete"
</hook>
<hook stage="pre-system">
echo "Building base system..."
</hook>
<hook stage="post-system">
echo "Base system build complete"
</hook>
<hook stage="pre-init-system">
echo "Configuring init system: ${INIT_SYSTEM}"
</hook>
<hook stage="post-init-system">
echo "Init system configured: ${INIT_SYSTEM}"
</hook>
<hook stage="pre-packages">
echo "Installing additional packages..."
</hook>
<hook stage="post-install">
echo "All packages installed successfully"
echo "NotLFS build completed at $(date)"
</hook>
</hooks>
<!-- Custom Environment Variables -->
<environment>
<variable name="CFLAGS">-O2 -pipe</variable>
<variable name="CXXFLAGS">-O2 -pipe</variable>
<variable name="LDFLAGS"></variable>
<variable name="MAKEFLAGS">-j${JOB_COUNT}</variable>
</environment>
</notlfs>
EOF
log_info "Sample configuration generated at $output_file"
}
# Generate default build profiles
generate_default_profiles() {
log_subsection "Generating default build profiles"
# Minimal profile
cat > "${PROFILES_DIR}/minimal/profile.xml" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<profile name="minimal">
<description>Minimal NotLFS system with essential packages only</description>
<init_system>s6-rc</init_system>
<packages>
<include category="core" />
<include category="init" />
<package name="util-linux" />
<package name="e2fsprogs" />
</packages>
<features>
<feature name="minimal">true</feature>
<feature name="network">false</feature>
<feature name="development">false</feature>
</features>
</profile>
EOF
# Base profile
cat > "${PROFILES_DIR}/base/profile.xml" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<profile name="base">
<description>Base NotLFS system with development tools and networking</description>
<init_system>s6-rc</init_system>
<packages>
<include category="core" />
<include category="init" />
<include category="dev" />
<include category="utils" />
<include category="network" />
<include category="languages" />
</packages>
<features>
<feature name="minimal">false</feature>
<feature name="network">true</feature>
<feature name="development">true</feature>
</features>
</profile>
EOF
# Desktop profile
cat > "${PROFILES_DIR}/desktop/profile.xml" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<profile name="desktop">
<description>Desktop-oriented NotLFS system with GUI support</description>
<init_system>s6-rc</init_system>
<packages>
<include category="core" />
<include category="init" />
<include category="dev" />
<include category="utils" />
<include category="network" />
<include category="desktop" />
<include category="languages" />
<package name="alsa-lib" />
<package name="alsa-utils" />
<package name="pulseaudio" />
<package name="firefox" />
</packages>
<features>
<feature name="minimal">false</feature>
<feature name="network">true</feature>
<feature name="development">true</feature>
<feature name="gui">true</feature>
<feature name="audio">true</feature>
</features>
</profile>
EOF
# Server profile
cat > "${PROFILES_DIR}/server/profile.xml" << 'EOF'
<?xml version="1.0" encoding="UTF-8"?>
<profile name="server">
<description>Server-oriented NotLFS system with minimal GUI</description>
<init_system>runit</init_system>
<packages>
<include category="core" />
<include category="init" />
<include category="dev" />
<include category="utils" />
<include category="network" />
<package name="nginx" />
<package name="postgresql" />
<package name="redis" />
</packages>
<features>
<feature name="minimal">false</feature>
<feature name="network">true</feature>
<feature name="development">true</feature>
<feature name="server">true</feature>
</features>
</profile>
EOF
log_info "Generated default profiles: minimal, base, desktop, server"
}
# =============================================================================
# INIT SYSTEM TEMPLATES
# =============================================================================
generate_init_templates() {
log_subsection "Generating init system templates"
# Create init system directory structure
for init in "${SUPPORTED_INITS[@]}"; do
mkdir -p "${INIT_DIR}/systems/${init}"
mkdir -p "${INIT_DIR}/services/${init}"
done
# s6 init system template
cat > "${INIT_DIR}/systems/s6/install.sh" << 'EOF'
#!/bin/bash
# s6 init system installation
INSTALL_DIR="$1"
log_init "Installing s6 init system to ${INSTALL_DIR}"
# Create directory structure
mkdir -p "${INSTALL_DIR}/etc/s6"
mkdir -p "${INSTALL_DIR}/etc/s6/current"
mkdir -p "${INSTALL_DIR}/etc/s6/rc"
# Install s6
build_package s6
# Configure s6 as init
cat > "${INSTALL_DIR}/etc/s6/current/rc.init" << 'S6EOF'
#!/bin/sh
# s6 init script
exec s6-svscan /etc/s6/current
S6EOF
chmod +x "${INSTALL_DIR}/etc/s6/current/rc.init"
# Create symlink for init
ln -sf /etc/s6/current/rc.init "${INSTALL_DIR}/sbin/init"
log_init "s6 init system installed"
EOF
# s6-rc init system template
cat > "${INIT_DIR}/systems/s6-rc/install.sh" << 'EOF'
#!/bin/bash
# s6-rc init system installation
INSTALL_DIR="$1"
log_init "Installing s6-rc init system to ${INSTALL_DIR}"
# Create directory structure
mkdir -p "${INSTALL_DIR}/etc/s6-rc"
mkdir -p "${INSTALL_DIR}/etc/s6-rc/current"
mkdir -p "${INSTALL_DIR}/etc/s6-rc/rc"
# Install s6 and s6-rc
build_package s6
build_package s6-rc
# Configure s6-rc as init
cat > "${INSTALL_DIR}/etc/s6-rc/current/rc.init" << 'S6RCEOF'
#!/bin/sh
# s6-rc init script
exec s6-rc-init /etc/s6-rc/current default
S6RCEOF
chmod +x "${INSTALL_DIR}/etc/s6-rc/current/rc.init"
# Create symlink for init
ln -sf /etc/s6-rc/current/rc.init "${INSTALL_DIR}/sbin/init"
# Create default scan directory
mkdir -p "${INSTALL_DIR}/etc/s6-rc/rc.d"
log_init "s6-rc init system installed"
EOF
# dinit init system template
cat > "${INIT_DIR}/systems/dinit/install.sh" << 'EOF'
#!/bin/bash
# dinit init system installation
INSTALL_DIR="$1"
log_init "Installing dinit init system to ${INSTALL_DIR}"
# Install dinit
build_package dinit
# Configure dinit
mkdir -p "${INSTALL_DIR}/etc/dinit.d"
cat > "${INSTALL_DIR}/etc/dinit.conf" << 'DINITEOF'
# dinit configuration
DINIT_LOG_LEVEL=info
DINIT_SERVICE_DIR=/etc/dinit.d
DINITEOF
# Create init symlink
ln -sf /usr/bin/dinit "${INSTALL_DIR}/sbin/init"
log_init "dinit init system installed"
EOF
# runit init system template
cat > "${INIT_DIR}/systems/runit/install.sh" << 'EOF'
#!/bin/bash
# runit init system installation
INSTALL_DIR="$1"
log_init "Installing runit init system to ${INSTALL_DIR}"
# Install runit
build_package runit
# Configure runit
mkdir -p "${INSTALL_DIR}/etc/sv"
mkdir -p "${INSTALL_DIR}/etc/service"
cat > "${INSTALL_DIR}/etc/runit/1" << 'RUNITEOF'
#!/bin/sh
# runit stage 1
exec /usr/bin/runsvdir -P /etc/service default
RUNITEOF
chmod +x "${INSTALL_DIR}/etc/runit/1"
# Create init symlink
ln -sf /usr/bin/runit-init "${INSTALL_DIR}/sbin/init"
log_init "runit init system installed"
EOF
# sysv init system template
cat > "${INIT_DIR}/systems/sysv/install.sh" << 'EOF'
#!/bin/bash
# SysV init system installation
INSTALL_DIR="$1"
log_init "Installing SysV init system to ${INSTALL_DIR}"
# Install sysvinit
build_package sysvinit
# Configure SysV
mkdir -p "${INSTALL_DIR}/etc/rc.d"
mkdir -p "${INSTALL_DIR}/etc/init.d"
for level in 0 1 2 3 4 5 6; do
mkdir -p "${INSTALL_DIR}/etc/rc${level}.d"
done
# Create basic rc files
cat > "${INSTALL_DIR}/etc/inittab" << 'SYSVEOF'
# /etc/inittab
::sysinit:/etc/init.d/rcS
::askfirst:/etc/init.d/rc
::ctrlaltdel:/sbin/reboot
::shutdown:/sbin/halt
SYSVEOF
# Create init symlink
ln -sf /sbin/init "${INSTALL_DIR}/sbin/init"
log_init "SysV init system installed"
EOF
# systemd init system template
cat > "${INIT_DIR}/systems/systemd/install.sh" << 'EOF'
#!/bin/bash
# systemd init system installation
INSTALL_DIR="$1"
log_init "Installing systemd init system to ${INSTALL_DIR}"
# Install systemd
build_package systemd
# Configure systemd
mkdir -p "${INSTALL_DIR}/etc/systemd/system"
mkdir -p "${INSTALL_DIR}/usr/lib/systemd/system"
# Enable basic services
ln -sf /usr/lib/systemd/system/multi-user.target "${INSTALL_DIR}/etc/systemd/system/default.target"
# Create init symlink
ln -sf /usr/lib/systemd/systemd "${INSTALL_DIR}/sbin/init"
log_init "systemd init system installed"
EOF
# openrc init system template
cat > "${INIT_DIR}/systems/openrc/install.sh" << 'EOF'
#!/bin/bash
# OpenRC init system installation
INSTALL_DIR="$1"
log_init "Installing OpenRC init system to ${INSTALL_DIR}"
# Install openrc
build_package openrc
# Configure OpenRC
mkdir -p "${INSTALL_DIR}/etc/runlevels"
mkdir -p "${INSTALL_DIR}/etc/init.d"
# Create basic runlevels
for level in boot sysinit; do
mkdir -p "${INSTALL_DIR}/etc/runlevels/${level}"
done
# Create rc.conf
cat > "${INSTALL_DIR}/etc/rc.conf" << 'OPENRCEOF'
# OpenRC configuration
rc_sys="laptop"
rc_logger="yes"
rc_depend_strict="no"
OPENRCEOF
# Create init symlink
ln -sf /usr/sbin/openrc-init "${INSTALL_DIR}/sbin/init"
log_init "OpenRC init system installed"
EOF
# Service templates for each init system
for init in "${SUPPORTED_INITS[@]}"; do
cat > "${INIT_DIR}/services/${init}/sshd" << EOF
# Service template for sshd on ${init}
# This is a placeholder - actual implementation depends on the init system
EOF
done
chmod +x "${INIT_DIR}"/systems/*/install.sh
log_info "Generated init system templates for: ${SUPPORTED_INITS[*]}"
}
# Load configuration from XML file
load_config() {
local config_file="$1"
if [ ! -f "$config_file" ]; then
die "Configuration file not found: $config_file"
fi
log_info "Loading configuration from $config_file"
# Parse system configuration
NOTLFS_NAME="$(parse_xml "$config_file" '//system/name/text()')" || NOTLFS_NAME="notlfs"
TARGET_ARCH="$(parse_xml "$config_file" '//system/architecture/text()')" || TARGET_ARCH="x86_64"
JOB_COUNT="$(parse_xml "$config_file" '//system/jobs/text()')" || JOB_COUNT="$(nproc)"
HOSTNAME="$(parse_xml "$config_file" '//system/hostname/text()')" || HOSTNAME="notlfs"
ROOT_PASSWORD="$(parse_xml "$config_file" '//system/root_password/text()')" || ROOT_PASSWORD="changeme"
TIMEZONE="$(parse_xml "$config_file" '//system/timezone/text()')" || TIMEZONE="UTC"
LOCALE="$(parse_xml "$config_file" '//system/locale/text()')" || LOCALE="en_US.UTF-8"
KERNEL_VERSION="$(parse_xml "$config_file" '//system/kernel_version/text()')" || KERNEL_VERSION="6.6.0"
# Parse build settings
BUILD_MODE="$(parse_xml "$config_file" '//build/mode/text()')" || BUILD_MODE="interactive"
PROFILE="$(parse_xml "$config_file" '//build/profile/text()')" || PROFILE=""
INIT_SYSTEM="$(parse_xml "$config_file" '//build/init_system/text()')" || INIT_SYSTEM="s6-rc"
DOWNLOAD_MIRROR="$(parse_xml "$config_file" '//build/download_mirror/text()')" || DOWNLOAD_MIRROR="https://ftp.gnu.org/gnu"
OPTIMIZATION="$(parse_xml "$config_file" '//build/optimization/text()')" || OPTIMIZATION="-O2 -pipe"
STRIP_DEBUG="$(parse_xml "$config_file" '//build/strip_debug/text()')" || STRIP_DEBUG="false"
KEEP_SOURCES="$(parse_xml "$config_file" '//build/keep_sources/text()')" || KEEP_SOURCES="false"
PARALLEL_DOWNLOADS="$(parse_xml "$config_file" '//build/parallel_downloads/text()')" || PARALLEL_DOWNLOADS="4"
BUILD_TIMEOUT="$(parse_xml "$config_file" '//build/build_timeout/text()')" || BUILD_TIMEOUT="3600"
# Parse security settings
ENABLE_HARDENING="$(parse_xml "$config_file" '//security/enable_hardening/text()')" || ENABLE_HARDENING="true"
STACK_PROTECTOR="$(parse_xml "$config_file" '//security/stack_protector/text()')" || STACK_PROTECTOR="true"
FORTIFY_SOURCE="$(parse_xml "$config_file" '//security/fortify_source/text()')" || FORTIFY_SOURCE="true"
RELRO="$(parse_xml "$config_file" '//security/relro/text()')" || RELRO="true"
ASLR="$(parse_xml "$config_file" '//security/aslr/text()')" || ASLR="true"
NOEXEC_STACK="$(parse_xml "$config_file" '//security/noexec_stack/text()')" || NOEXEC_STACK="true"
PIE="$(parse_xml "$config_file" '//security/pie/text()')" || PIE="true"
# Parse network settings
NET_HOSTNAME="$(parse_xml "$config_file" '//network/hostname/text()')" || NET_HOSTNAME="$HOSTNAME"
NET_DOMAIN="$(parse_xml "$config_file" '//network/domain/text()')" || NET_DOMAIN="local"
ENABLE_DHCP="$(parse_xml "$config_file" '//network/enable_dhcp/text()')" || ENABLE_DHCP="true"
NAMESERVERS="$(parse_xml "$config_file" '//network/nameservers/text()')" || NAMESERVERS="8.8.8.8 8.8.4.4"
# Set LFS_TGT based on architecture
case "$TARGET_ARCH" in
x86_64) LFS_TGT="x86_64-notlfs-linux-gnu" ;;
aarch64) LFS_TGT="aarch64-notlfs-linux-gnu" ;;
i386) LFS_TGT="i386-notlfs-linux-gnu" ;;
armv7l) LFS_TGT="armv7l-notlfs-linux-gnueabihf" ;;
riscv64) LFS_TGT="riscv64-notlfs-linux-gnu" ;;
*) LFS_TGT="${TARGET_ARCH}-notlfs-linux-gnu" ;;
esac
# Set environment variables
export LFS="${BUILD_ROOT}/notlfs"
export LFS_TGT
export PATH="${LFS}/tools/bin:${PATH}"
export MAKEFLAGS="-j${JOB_COUNT}"
# Parse enabled packages
PACKAGES_TO_BUILD=()
local pkg_count=$(parse_xml "$config_file" "count(//packages//package[@enabled='true'])")
for ((i=1; i<=pkg_count; i++)); do
local pkg_name=$(parse_xml "$config_file" "//packages//package[@enabled='true'][$i]/@name")
PACKAGES_TO_BUILD+=("$pkg_name")
done
# Parse services
SERVICES_TO_ENABLE=()
local svc_count=$(parse_xml "$config_file" "count(//services/service[@enabled='true'])")
for ((i=1; i<=svc_count; i++)); do
local svc_name=$(parse_xml "$config_file" "//services/service[@enabled='true'][$i]/@name")
local svc_runlevel=$(parse_xml "$config_file" "//services/service[@enabled='true'][$i]/@runlevel")
SERVICES_TO_ENABLE+=("${svc_name}:${svc_runlevel}")
done
# Parse environment variables
ENV_VARIABLES=()
local env_count=$(parse_xml "$config_file" "count(//environment/variable)")
for ((i=1; i<=env_count; i++)); do
local var_name=$(parse_xml "$config_file" "//environment/variable[$i]/@name")
local var_value=$(parse_xml "$config_file" "//environment/variable[$i]/text()")
export "$var_name=$var_value"
ENV_VARIABLES+=("${var_name}=${var_value}")
done
# Set security-related compiler flags
if [ "$ENABLE_HARDENING" = "true" ]; then
export CFLAGS="${OPTIMIZATION}"
export CXXFLAGS="${OPTIMIZATION}"
[ "$STACK_PROTECTOR" = "true" ] && CFLAGS+=" -fstack-protector-strong" && CXXFLAGS+=" -fstack-protector-strong"
[ "$FORTIFY_SOURCE" = "true" ] && CFLAGS+=" -D_FORTIFY_SOURCE=2" && CXXFLAGS+=" -D_FORTIFY_SOURCE=2"
[ "$RELRO" = "true" ] && CFLAGS+=" -Wl,-z,now" && CXXFLAGS+=" -Wl,-z,now"
[ "$ASLR" = "true" ] && export LDFLAGS="-Wl,-z,relro -Wl,-z,now"
[ "$NOEXEC_STACK" = "true" ] && CFLAGS+=" -z noexecstack" && CXXFLAGS+=" -z noexecstack"
[ "$PIE" = "true" ] && CFLAGS+=" -fPIE" && CXXFLAGS+=" -fPIE" && LDFLAGS+=" -pie"
else
export CFLAGS="${OPTIMIZATION}"
export CXXFLAGS="${OPTIMIZATION}"
fi
log_info "Configuration loaded:"
log_info " System: ${NOTLFS_NAME} (${TARGET_ARCH})"
log_info " Init: ${INIT_SYSTEM}"
log_info " Mode: ${BUILD_MODE}"
log_info " Profile: ${PROFILE:-none}"
log_info " Packages: ${#PACKAGES_TO_BUILD[@]}"
log_info " Jobs: ${JOB_COUNT}"
}
# Simple XML parser using xmllint or fallback to grep/sed
parse_xml() {
local file="$1"
local xpath="$2"
if command -v xmllint &> /dev/null; then
xmllint --xpath "$xpath" "$file" 2>/dev/null | sed 's/<[^>]*>//g' | tr -d '[:space:]'
else
# Fallback: use grep and sed for simple parsing
local result
result=$(grep -oP "<[^>]*>${xpath#*/}<[^>]*>\K[^<]*" "$file" 2>/dev/null | head -1 | tr -d '[:space:]')
echo "$result"
fi
}
# Load profile configuration
load_profile() {
local profile_name="$1"
local profile_file="${PROFILES_DIR}/${profile_name}/profile.xml"
if [ ! -f "$profile_file" ]; then
die "Profile not found: $profile_name"
fi
log_info "Loading profile: $profile_name"
# Parse profile
PROFILE_INIT="$(parse_xml "$profile_file" '//profile/init_system/text()')"
if [ -n "$PROFILE_INIT" ]; then
INIT_SYSTEM="$PROFILE_INIT"
fi
# Parse included packages
local include_count=$(parse_xml "$profile_file" "count(//profile/packages/include)")
for ((i=1; i<=include_count; i++)); do
local category=$(parse_xml "$profile_file" "//profile/packages/include[$i]/@category")
# Enable all packages in this category
local pkg_count=$(parse_xml "$CONFIG_FILE" "count(//packages/category[@name='${category}']//package)")
for ((j=1; j<=pkg_count; j++)); do
local pkg_name=$(parse_xml "$CONFIG_FILE" "//packages/category[@name='${category}']//package[$j]/@name")
# Check if package is already in PACKAGES_TO_BUILD
if ! [[ " ${PACKAGES_TO_BUILD[@]} " =~ " ${pkg_name} " ]]; then
PACKAGES_TO_BUILD+=("$pkg_name")
fi
done
done
# Parse individual packages
local pkg_count=$(parse_xml "$profile_file" "count(//profile/packages/package)")
for ((i=1; i<=pkg_count; i++)); do
local pkg_name=$(parse_xml "$profile_file" "//profile/packages/package[$i]/@name")
if ! [[ " ${PACKAGES_TO_BUILD[@]} " =~ " ${pkg_name} " ]]; then
PACKAGES_TO_BUILD+=("$pkg_name")
fi
done
# Parse features
local feature_count=$(parse_xml "$profile_file" "count(//profile/features/feature)")
for ((i=1; i<=feature_count; i++)); do
local feature_name=$(parse_xml "$profile_file" "//profile/features/feature[$i]/@name")
local feature_value=$(parse_xml "$profile_file" "//profile/features/feature[$i]/text()")
case "$feature_name" in
minimal) FEATURE_MINIMAL="$feature_value" ;;
network) FEATURE_NETWORK="$feature_value" ;;
development) FEATURE_DEVELOPMENT="$feature_value" ;;
gui) FEATURE_GUI="$feature_value" ;;
audio) FEATURE_AUDIO="$feature_value" ;;
server) FEATURE_SERVER="$feature_value" ;;
esac
done
log_info "Profile loaded: $profile_name (${#PACKAGES_TO_BUILD[@]} packages)"