-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathinstall.sh
More file actions
1978 lines (1712 loc) · 74.9 KB
/
install.sh
File metadata and controls
1978 lines (1712 loc) · 74.9 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
# zramroot Installation Script
# This script installs zramroot components and configures your system to use it
# Colors for better UX
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
BLUE='\033[0;34m'
NC='\033[0m' # No Color
# Function to print messages with colors
print_info() {
echo -e "${BLUE}[INFO]${NC} $1"
}
print_warning() {
echo -e "${YELLOW}[WARNING]${NC} $1"
}
print_error() {
echo -e "${RED}[ERROR]${NC} $1"
}
print_success() {
echo -e "${GREEN}[SUCCESS]${NC} $1"
}
# Function to print header
print_header() {
clear
echo "============================================================"
echo " zramroot Installation "
echo "============================================================"
echo ""
}
# Check if script is run as root
if [ "$(id -u)" -ne 0 ]; then
print_error "This script must be run as root (with sudo)"
exit 1
fi
# Detect init system
detect_init_system() {
print_info "Detecting init system..." >&2
# Check for mkinitcpio (Arch Linux and derivatives)
# Priority check: Look for mkinitcpio command and directory first
if command -v mkinitcpio >/dev/null 2>&1 && [ -d "/usr/lib/initcpio" ]; then
print_info "Detected: Arch-based system (mkinitcpio)" >&2
echo "mkinitcpio"
return 0
fi
# Check for initramfs-tools (Debian, Ubuntu, and derivatives)
if command -v update-initramfs >/dev/null 2>&1 && [ -d "/usr/share/initramfs-tools" ]; then
print_info "Detected: Debian/Ubuntu (initramfs-tools)" >&2
echo "initramfs-tools"
return 0
fi
# Check for dracut (Fedora, RHEL, Qubes OS, openSUSE)
if command -v dracut >/dev/null 2>&1 && [ -d "/usr/lib/dracut" ]; then
print_info "Detected: Fedora/RHEL/Qubes OS (dracut)" >&2
echo "dracut"
return 0
fi
# Unknown init system
print_error "Could not detect a supported init system" >&2
print_info "Supported systems:" >&2
print_info " - Debian/Ubuntu (initramfs-tools)" >&2
print_info " - Arch Linux/Artix/Manjaro (mkinitcpio)" >&2
print_info " - Fedora/RHEL/Qubes OS (dracut)" >&2
echo "unknown"
return 1
}
INIT_SYSTEM=$(detect_init_system)
if [ "$INIT_SYSTEM" = "unsupported" ] || [ "$INIT_SYSTEM" = "unknown" ]; then
print_error "Cannot continue installation on unsupported system"
exit 1
fi
# --- Load configuration from zramroot.conf ---
# This allows user customizations to take effect during install
TRIGGER_PARAMETER="zramroot" # Default value
ZRAM_FS_TYPE="ext4" # Default filesystem type
CONFIG_FILE="zramroot.conf" # Local config in current directory
if [ -f "$CONFIG_FILE" ]; then
# Extract TRIGGER_PARAMETER from config file
trigger_setting=$(grep "^TRIGGER_PARAMETER=" "$CONFIG_FILE" 2>/dev/null | cut -d'"' -f2)
if [ -n "$trigger_setting" ]; then
TRIGGER_PARAMETER="$trigger_setting"
print_info "Using custom trigger parameter from config: ${TRIGGER_PARAMETER}"
fi
# Extract ZRAM_FS_TYPE from config file
fs_type_setting=$(grep "^ZRAM_FS_TYPE=" "$CONFIG_FILE" 2>/dev/null | cut -d'"' -f2)
if [ -n "$fs_type_setting" ]; then
ZRAM_FS_TYPE="$fs_type_setting"
print_info "Using filesystem type from config: ${ZRAM_FS_TYPE}"
fi
fi
print_header
# Dependency checking function
check_dependencies() {
print_info "=== Checking System Dependencies ==="
echo ""
# Core required binaries (from zramroot hook script copy_bin_list)
local core_bins="busybox sh mount umount mkdir rmdir echo cat grep sed df awk rsync cp touch date \
mountpoint zramctl lsmod modprobe fsck blkid udevadm fuser find tail head ls sync"
# Filesystem tools - based on configured ZRAM_FS_TYPE
local fs_bins="mkfs.ext4 fsck.ext4" # ext4 always included as fallback
case "${ZRAM_FS_TYPE}" in
btrfs)
fs_bins="$fs_bins mkfs.btrfs btrfs"
print_info "Including btrfs tools (ZRAM_FS_TYPE=${ZRAM_FS_TYPE})"
;;
xfs)
fs_bins="$fs_bins mkfs.xfs xfs_repair"
print_info "Including xfs tools (ZRAM_FS_TYPE=${ZRAM_FS_TYPE})"
;;
esac
# Additional binaries used in zramroot-boot script
local boot_bins="cut du kill mkfs mkswap nproc printf rm sort sleep timeout wait wc"
# System utilities that may not be in core but are needed
local util_bins="seq"
# Optional binaries (for different filesystems) - only checked if LVM detected
local lvm_bins="vgchange lvchange dmsetup lvscan vgscan pvscan"
local missing_core=()
local missing_optional=()
local all_good=true
# Function to check if binary exists
check_binary() {
local bin="$1"
# Check for shell builtins first
case "$bin" in
wait|kill|echo|printf|cd|pwd|test|true|false|shift|export|unset|set|read)
# These are shell builtins available in all POSIX shells
return 0
;;
esac
# Special handling for busybox on Arch/mkinitcpio systems
if [ "$bin" = "busybox" ] && [ "$INIT_SYSTEM" = "mkinitcpio" ]; then
# On Arch, busybox is in /usr/lib/initcpio/busybox
if [ -x "/usr/lib/initcpio/busybox" ]; then
return 0
fi
# Also check if mkinitcpio-busybox package is installed
if command -v pacman >/dev/null 2>&1; then
if pacman -Q mkinitcpio-busybox >/dev/null 2>&1; then
return 0
fi
fi
fi
# Check for external binaries
for dir in /bin /usr/bin /sbin /usr/sbin /usr/lib/initcpio; do
if [ -x "${dir}/${bin}" ]; then
return 0
fi
done
# For some binaries, also check if they're available via command -v
if command -v "$bin" >/dev/null 2>&1; then
return 0
fi
return 1
}
# Check all core binaries
print_info "Checking core utilities..."
for bin in $core_bins; do
if ! check_binary "$bin"; then
missing_core+=("$bin")
print_warning "Missing: $bin"
all_good=false
else
print_info "✓ Found: $bin"
fi
done
# Check filesystem tools
print_info "Checking filesystem utilities..."
for bin in $fs_bins; do
if ! check_binary "$bin"; then
missing_core+=("$bin")
print_warning "Missing: $bin"
all_good=false
else
print_info "✓ Found: $bin"
fi
done
# Check boot script binaries
print_info "Checking boot script utilities..."
for bin in $boot_bins; do
if ! check_binary "$bin"; then
missing_core+=("$bin")
print_warning "Missing: $bin"
all_good=false
else
print_info "✓ Found: $bin"
fi
done
# Check additional utilities
print_info "Checking additional utilities..."
for bin in $util_bins; do
if ! check_binary "$bin"; then
missing_core+=("$bin")
print_warning "Missing: $bin"
all_good=false
else
print_info "✓ Found: $bin"
fi
done
# Check LVM tools if system uses LVM
local root_device="${ROOT:-$(grep -oP 'root=\K[^[:space:]]+' /proc/cmdline)}"
if echo "$root_device" | grep -q "/dev/mapper/\|/dev/.*-.*" && command -v vgdisplay >/dev/null 2>&1; then
if vgdisplay 2>/dev/null | grep -q "VG Status.*available"; then
print_info "LVM detected, checking LVM utilities..."
for bin in $lvm_bins; do
if ! check_binary "$bin"; then
missing_optional+=("$bin")
print_warning "Missing LVM tool: $bin"
fi
done
fi
fi
echo ""
# Report results
if [ ${#missing_core[@]} -eq 0 ]; then
print_success "All core dependencies satisfied!"
return 0
else
print_error "Missing ${#missing_core[@]} required dependencies!"
echo ""
show_package_install_instructions "${missing_core[@]}"
return 1
fi
}
# Function to show package installation instructions
show_package_install_instructions() {
local missing_bins=("$@")
print_warning "The following packages need to be installed:"
echo ""
# Detect distribution
local distro=""
if [ -f /etc/os-release ]; then
. /etc/os-release
distro="${ID}"
elif [ -f /etc/debian_version ]; then
distro="debian"
elif [ -f /etc/redhat-release ]; then
distro="rhel"
fi
# Create package recommendations based on missing binaries
local packages=()
for bin in "${missing_bins[@]}"; do
case "$bin" in
# Core system utilities
rsync) packages+=("rsync") ;;
busybox) packages+=("busybox") ;;
# Filesystem and disk utilities
zramctl|blkid|mountpoint|mount|umount|df) packages+=("util-linux") ;;
mkfs.ext4|fsck.ext4|fsck) packages+=("e2fsprogs") ;;
mkfs|mkswap) packages+=("util-linux") ;;
# Process and system management
fuser) packages+=("psmisc") ;;
modprobe|lsmod) packages+=("kmod") ;;
udevadm) packages+=("udev") ;;
# Core utilities (coreutils package) - excluding shell builtins
mkdir|rmdir|cat|cp|touch|date|sync|find|tail|head|ls|cut|rm|sort|sleep|wc|nproc|du) packages+=("coreutils") ;;
# Note: echo and printf are shell builtins, no package needed
# Text processing
grep|sed|awk) packages+=("grep" "sed" "gawk") ;;
# Additional utilities
seq) packages+=("coreutils") ;;
timeout) packages+=("coreutils") ;;
# Note: kill and wait are shell builtins, no package needed
# Filesystem-specific tools
mkfs.btrfs|btrfs) packages+=("btrfs-progs") ;;
mkfs.xfs|xfs_repair) packages+=("xfsprogs") ;;
# LVM tools
vgchange|lvchange|dmsetup|lvscan|vgscan|pvscan) packages+=("lvm2") ;;
esac
done
# Remove duplicates
packages=($(printf '%s\n' "${packages[@]}" | sort -u))
# Show installation commands for different distributions
case "$distro" in
ubuntu|debian)
print_info "For Debian/Ubuntu systems, run:"
echo " sudo apt update"
echo " sudo apt install ${packages[*]}"
;;
fedora)
print_info "For Fedora systems, run:"
echo " sudo dnf install ${packages[*]}"
;;
qubes)
print_info "For Qubes systems, run:"
echo " sudo qubes-dom0-update --action=install ${packages[*]}"
;;
centos|rhel|rocky|alma)
print_info "For RHEL/CentOS systems, run:"
echo " sudo yum install ${packages[*]}"
;;
arch|manjaro|artix)
# Special handling for busybox on Arch-based systems
local arch_packages=()
for pkg in "${packages[@]}"; do
if [ "$pkg" = "busybox" ]; then
arch_packages+=("mkinitcpio-busybox")
else
arch_packages+=("$pkg")
fi
done
print_info "For Arch Linux/Artix systems, run:"
echo " sudo pacman -S ${arch_packages[*]}"
;;
opensuse|suse)
print_info "For openSUSE systems, run:"
echo " sudo zypper install ${packages[*]}"
;;
*)
print_info "Please install these packages using your system's package manager:"
for pkg in "${packages[@]}"; do
echo " - $pkg"
done
;;
esac
echo ""
print_warning "Please install the missing packages and run this script again."
}
# Run dependency check
if ! check_dependencies; then
echo ""
read -p "Would you like to continue anyway? (NOT RECOMMENDED) (y/n): " force_continue
if [[ ! "$force_continue" =~ ^[Yy]$ ]]; then
print_info "Installation cancelled. Please install the required packages first."
exit 1
fi
print_warning "Continuing with missing dependencies - installation may fail!"
fi
print_header
# Display warning and get confirmation
print_warning "zramroot might make your installation unbootable if not configured correctly."
print_warning "This script will:"
if [ "$INIT_SYSTEM" = "initramfs-tools" ]; then
print_info " • Replace /usr/share/initramfs-tools/scripts/local with zramroot version"
print_info " • Install zramroot hooks and configuration files"
print_info " • Optionally configure a bootloader entry (GRUB, systemd-boot, or extlinux)"
print_info " • Rebuild your initramfs to include zramroot support"
elif [ "$INIT_SYSTEM" = "mkinitcpio" ]; then
print_info " • Install zramroot hooks to /usr/lib/initcpio/"
print_info " • Install configuration file to /etc/zramroot.conf"
print_info " • Provide instructions for manual mkinitcpio.conf configuration"
elif [ "$INIT_SYSTEM" = "dracut" ]; then
print_info " • Install zramroot dracut module to an auto-detected /usr/lib/dracut/modules.d/XXzramroot/"
print_info " • Install configuration file to /etc/zramroot.conf"
print_info " • Rebuild initramfs with dracut --force --regenerate-all"
print_info " • Optionally configure a GRUB2 bootloader entry"
fi
echo ""
print_info "It is STRONGLY recommended to make a backup before continuing."
echo ""
read -p "Are you sure you want to continue? (y/n): " confirm
if [[ ! "$confirm" =~ ^[Yy]$ ]]; then
print_info "Installation aborted by user."
exit 0
fi
print_header
# Check for required files based on init system
print_info "Checking for required files for ${INIT_SYSTEM}..."
if [ "$INIT_SYSTEM" = "initramfs-tools" ]; then
FILES=(
"initramfs-tools/scripts/local-premount/zramroot-boot"
"initramfs-tools/scripts/local-bottom/zramroot-final"
"initramfs-tools/scripts/local"
"initramfs-tools/hooks/zramroot"
"initramfs-tools/conf.d/zramroot-config"
)
elif [ "$INIT_SYSTEM" = "mkinitcpio" ]; then
FILES=(
"mkinitcpio/hooks/zramroot"
"mkinitcpio/install/zramroot"
"zramroot.conf"
)
elif [ "$INIT_SYSTEM" = "dracut" ]; then
FILES=(
"dracut/zramroot/module-setup.sh"
"dracut/zramroot/zramroot-mount.sh"
"dracut/zramroot/zramroot-finalize.sh"
"dracut/rootfs-block/mount-root.sh"
"zramroot.conf"
)
else
print_error "Unknown init system: ${INIT_SYSTEM}"
exit 1
fi
MISSING=0
for file in "${FILES[@]}"; do
if [ ! -f "$file" ]; then
print_error "Required file not found: $file"
MISSING=1
fi
done
if [ $MISSING -eq 1 ]; then
print_error "Some required files are missing. Please make sure all files are in the correct directory structure."
print_info "Expected directory structure:"
if [ "$INIT_SYSTEM" = "initramfs-tools" ]; then
print_info " initramfs-tools/hooks/zramroot"
print_info " initramfs-tools/scripts/local"
print_info " initramfs-tools/scripts/local-premount/zramroot-boot"
print_info " initramfs-tools/scripts/local-bottom/zramroot-final"
print_info " initramfs-tools/conf.d/zramroot-config"
elif [ "$INIT_SYSTEM" = "mkinitcpio" ]; then
print_info " mkinitcpio/hooks/zramroot"
print_info " mkinitcpio/install/zramroot"
print_info " zramroot.conf"
elif [ "$INIT_SYSTEM" = "dracut" ]; then
print_info " dracut/zramroot/module-setup.sh"
print_info " dracut/zramroot/zramroot-mount.sh"
print_info " dracut/zramroot/zramroot-finalize.sh"
print_info " dracut/rootfs-block/mount-root.sh"
print_info " zramroot.conf"
fi
exit 1
fi
# Get additional modules from user - only for initramfs-tools
additional_modules=""
if [ "$INIT_SYSTEM" = "initramfs-tools" ]; then
print_header
print_info "You can specify additional kernel modules to load during early boot."
print_info "These might be needed for specific hardware support."
print_info "Enter module names separated by spaces, or leave empty for no additional modules."
echo ""
# Initialize module selection variables
module_selection_complete=false
while [ "$module_selection_complete" = false ]; do
# Show current selection if any
if [ -n "$additional_modules" ]; then
print_info "Current module selection: $additional_modules"
fi
# Read module input, pre-populate with current selection
read -e -p "Additional modules: " -i "$additional_modules" input_modules
additional_modules="$input_modules"
# If no modules specified, we're done
if [ -z "$additional_modules" ]; then
print_info "No additional modules selected."
module_selection_complete=true
continue
fi
echo ""
print_info "Checking if modules exist in your system..."
# Create arrays for existing and non-existing modules
# Important: Clear these arrays each time to prevent carrying over previous checks
existing_modules=()
nonexisting_modules=()
# Check each module
for module in $additional_modules; do
if modinfo $module &>/dev/null || [ -f "/lib/modules/$(uname -r)/kernel/$module.ko" ] || find /lib/modules/$(uname -r) -name "${module}.ko*" | grep -q .; then
existing_modules+=("$module")
print_info "Module '$module' exists in the system."
else
nonexisting_modules+=("$module")
print_warning "Module '$module' was not found in the system."
fi
done
# Warn about non-existing modules
if [ ${#nonexisting_modules[@]} -gt 0 ]; then
echo ""
print_warning "The following modules were not found in your system:"
for module in "${nonexisting_modules[@]}"; do
echo " - $module"
done
echo ""
print_warning "These modules might not load correctly during boot."
read -p "Do you want to continue with these modules? (y/n): " include_nonexisting
if [[ ! "$include_nonexisting" =~ ^[Yy]$ ]]; then
print_info "Please correct your module selection."
# Continue loop without setting module_selection_complete
continue
fi
fi
# Final confirmation of modules
echo ""
print_info "The following modules will be added:"
echo "$additional_modules"
echo ""
read -p "Is this correct? (y/n): " confirm_modules
if [[ "$confirm_modules" =~ ^[Yy]$ ]]; then
module_selection_complete=true
else
print_info "Please correct your module selection."
# Loop continues with current selection in the input field
fi
done
else
print_info "Skipping additional module selection (not applicable for mkinitcpio and dracut)"
fi
print_header
print_warning "Please wait until the installation is complete."
print_warning "Do not close this terminal or interrupt the script."
echo ""
print_info "Starting installation process..."
sleep 2
# Function to create backup of a file (but NOT in /etc/grub.d)
backup_file() {
local file=$1
# Skip backup for files in /etc/grub.d as they cause duplicate menu entries
if [[ "$file" == /etc/grub.d/* ]]; then
print_info "Skipping backup of $file (GRUB directory - backups cause duplicate menu entries)"
return
fi
if [ -f "$file" ]; then
# Create backup in /tmp instead of in-place to avoid issues
local backup_dir="/tmp/zramroot-backups-$(date +%Y%m%d)"
mkdir -p "$backup_dir"
local backup_file="$backup_dir/$(basename "$file").bak.$(date +%Y%m%d%H%M%S)"
cp "$file" "$backup_file"
print_info "Created backup of $file at $backup_file"
fi
}
# Install based on init system
if [ "$INIT_SYSTEM" = "initramfs-tools" ]; then
print_info "Installing for initramfs-tools (Debian/Ubuntu)..."
# Create directory structure
print_info "Creating directory structure..."
mkdir -p /usr/share/initramfs-tools/scripts/local-premount
mkdir -p /usr/share/initramfs-tools/scripts/local-bottom
mkdir -p /usr/share/initramfs-tools/hooks
mkdir -p /usr/share/initramfs-tools/conf.d
# Add user modules to the hook script if provided
if [ -n "$additional_modules" ]; then
print_info "Adding custom modules to hook script..."
# Create a temporary file with the modified content
cat "initramfs-tools/hooks/zramroot" | sed "s/^EXTRA_MODULES=.*/EXTRA_MODULES=\"$additional_modules\"/" > /tmp/zramroot.modified
mv /tmp/zramroot.modified initramfs-tools/hooks/zramroot
chmod +x initramfs-tools/hooks/zramroot
fi
# Backup existing files
backup_file "/usr/share/initramfs-tools/scripts/local"
backup_file "/usr/share/initramfs-tools/conf.d/zramroot-config"
backup_file "/etc/zramroot.conf"
# Copy files
print_info "Copying initramfs-tools files..."
cp "initramfs-tools/scripts/local-premount/zramroot-boot" "/usr/share/initramfs-tools/scripts/local-premount/zramroot-boot"
chmod +x "/usr/share/initramfs-tools/scripts/local-premount/zramroot-boot"
cp "initramfs-tools/scripts/local-bottom/zramroot-final" "/usr/share/initramfs-tools/scripts/local-bottom/zramroot-final"
chmod +x "/usr/share/initramfs-tools/scripts/local-bottom/zramroot-final"
cp "initramfs-tools/scripts/local" "/usr/share/initramfs-tools/scripts/local"
chmod +x "/usr/share/initramfs-tools/scripts/local"
cp "initramfs-tools/hooks/zramroot" "/usr/share/initramfs-tools/hooks/zramroot"
chmod +x "/usr/share/initramfs-tools/hooks/zramroot"
cp "initramfs-tools/conf.d/zramroot-config" "/usr/share/initramfs-tools/conf.d/zramroot-config"
cp "initramfs-tools/conf.d/zramroot-config" "/etc/zramroot.conf"
elif [ "$INIT_SYSTEM" = "mkinitcpio" ]; then
print_info "Installing for mkinitcpio (Arch Linux)..."
# Create directory structure
print_info "Creating directory structure..."
mkdir -p /usr/lib/initcpio/hooks
mkdir -p /usr/lib/initcpio/install
# Backup existing files
backup_file "/usr/lib/initcpio/hooks/zramroot"
backup_file "/usr/lib/initcpio/install/zramroot"
backup_file "/etc/zramroot.conf"
# Copy files
print_info "Copying mkinitcpio files..."
cp "mkinitcpio/hooks/zramroot" "/usr/lib/initcpio/hooks/zramroot"
chmod +x "/usr/lib/initcpio/hooks/zramroot"
cp "mkinitcpio/install/zramroot" "/usr/lib/initcpio/install/zramroot"
chmod +x "/usr/lib/initcpio/install/zramroot"
cp "zramroot.conf" "/etc/zramroot.conf"
print_success "mkinitcpio files installed successfully!"
# Automatically configure mkinitcpio.conf
print_info ""
print_info "=== Configuring mkinitcpio.conf ==="
MKINITCPIO_CONF="/etc/mkinitcpio.conf"
if [ ! -f "$MKINITCPIO_CONF" ]; then
print_error "mkinitcpio.conf not found at $MKINITCPIO_CONF"
exit 1
fi
# Backup mkinitcpio.conf
backup_file "$MKINITCPIO_CONF"
# Check if zramroot is already in HOOKS
if grep "^HOOKS=" "$MKINITCPIO_CONF" | grep -q "zramroot"; then
print_warning "zramroot hook already present in HOOKS array"
else
print_info "Adding zramroot hook to HOOKS array..."
# Add zramroot before filesystems in HOOKS array
# This handles both cases: with and without spaces around 'filesystems'
if grep "^HOOKS=" "$MKINITCPIO_CONF" | grep -q "filesystems"; then
# Use sed to add zramroot before filesystems
sed -i 's/\(HOOKS=([^)]*\)\<filesystems\>/\1zramroot filesystems/' "$MKINITCPIO_CONF"
print_success "Added zramroot hook before filesystems"
else
print_warning "Could not find 'filesystems' in HOOKS array"
print_info "Please manually add 'zramroot' to HOOKS in $MKINITCPIO_CONF"
print_info "Example: HOOKS=(base udev autodetect modconf block zramroot filesystems keyboard fsck)"
fi
fi
# Show the current HOOKS configuration
print_info "Current HOOKS configuration:"
grep "^HOOKS=" "$MKINITCPIO_CONF" | sed 's/^/ /'
echo ""
# Rebuild initramfs
print_info "=== Rebuilding initramfs ==="
print_warning "This may take a minute..."
if mkinitcpio -P; then
print_success "Initramfs rebuilt successfully!"
else
print_error "Failed to rebuild initramfs"
print_info "You may need to run 'sudo mkinitcpio -P' manually"
exit 1
fi
print_success "mkinitcpio configuration completed!"
elif [ "$INIT_SYSTEM" = "dracut" ]; then
print_info "Installing for dracut (Fedora/RHEL/Qubes OS)..."
DRACUT_MODULES_DIR="/usr/lib/dracut/modules.d"
# Determine a priority that is after any crypt module and before any fstab/filesystem module
get_dracut_zramroot_priority() {
local max_crypt=0
local min_post=999
local base prefix name
for d in "${DRACUT_MODULES_DIR}"/*; do
[ -d "$d" ] || continue
base=$(basename "$d")
prefix=$(echo "$base" | sed -n 's/^\([0-9][0-9]*\).*/\1/p')
[ -n "$prefix" ] || continue
name=${base#${prefix}}
case "$name" in
*crypt*)
if [ "$prefix" -gt "$max_crypt" ]; then
max_crypt="$prefix"
fi
;;
esac
case "$name" in
*fstab*|*fs-lib*|*rootfs*|*filesystem*|*filesystems*)
if [ "$prefix" -lt "$min_post" ]; then
min_post="$prefix"
fi
;;
esac
done
local target=94
if [ "$max_crypt" -gt 0 ] && [ "$min_post" -lt 999 ]; then
target=$((max_crypt + 1))
if [ "$target" -ge "$min_post" ]; then
target=$((min_post - 1))
fi
elif [ "$max_crypt" -gt 0 ]; then
target=$((max_crypt + 1))
elif [ "$min_post" -lt 999 ]; then
target=$((min_post - 1))
fi
if [ "$target" -lt 1 ]; then
target=94
fi
echo "$target"
}
ZRAMROOT_PRIORITY=$(get_dracut_zramroot_priority)
ZRAMROOT_MODULE_DIR="${DRACUT_MODULES_DIR}/${ZRAMROOT_PRIORITY}zramroot"
# Find existing rootfs-block module directory
get_rootfs_block_dir() {
for d in "${DRACUT_MODULES_DIR}"/*rootfs-block; do
if [ -d "$d" ] && [ -f "$d/mount-root.sh" ]; then
echo "$d"
return 0
fi
done
echo ""
return 1
}
ROOTFS_BLOCK_DIR=$(get_rootfs_block_dir)
if [ -z "$ROOTFS_BLOCK_DIR" ]; then
print_error "Could not find rootfs-block module directory in ${DRACUT_MODULES_DIR}"
print_error "Expected to find a directory like ${DRACUT_MODULES_DIR}/95rootfs-block/"
exit 1
fi
print_info "Found rootfs-block module at: ${ROOTFS_BLOCK_DIR}"
# Create directory structure
print_info "Creating directory structure..."
print_info "Using dracut zramroot module priority: ${ZRAMROOT_PRIORITY} (auto-detected)"
mkdir -p "${ZRAMROOT_MODULE_DIR}"
# Clean up legacy zramroot module directories to avoid conflicts
for d in "${DRACUT_MODULES_DIR}"/*zramroot; do
[ -d "$d" ] || continue
if [ "$d" != "${ZRAMROOT_MODULE_DIR}" ]; then
print_warning "Removing legacy zramroot module directory: $d"
rm -rf "$d"
fi
done
# Backup existing files
backup_file "${ZRAMROOT_MODULE_DIR}/module-setup.sh"
backup_file "${ZRAMROOT_MODULE_DIR}/zramroot-mount.sh"
backup_file "${ZRAMROOT_MODULE_DIR}/zramroot-finalize.sh"
backup_file "${ROOTFS_BLOCK_DIR}/mount-root.sh"
backup_file "/etc/zramroot.conf"
# Copy zramroot module files
print_info "Copying zramroot module files..."
cp "dracut/zramroot/module-setup.sh" "${ZRAMROOT_MODULE_DIR}/module-setup.sh"
chmod +x "${ZRAMROOT_MODULE_DIR}/module-setup.sh"
cp "dracut/zramroot/zramroot-mount.sh" "${ZRAMROOT_MODULE_DIR}/zramroot-mount.sh"
chmod +x "${ZRAMROOT_MODULE_DIR}/zramroot-mount.sh"
cp "dracut/zramroot/zramroot-finalize.sh" "${ZRAMROOT_MODULE_DIR}/zramroot-finalize.sh"
chmod +x "${ZRAMROOT_MODULE_DIR}/zramroot-finalize.sh"
# Replace rootfs-block mount-root.sh with our modified version
print_info "Installing modified mount-root.sh to ${ROOTFS_BLOCK_DIR}..."
cp "dracut/rootfs-block/mount-root.sh" "${ROOTFS_BLOCK_DIR}/mount-root.sh"
chmod +x "${ROOTFS_BLOCK_DIR}/mount-root.sh"
cp "zramroot.conf" "/etc/zramroot.conf"
# Ensure zram driver is included in initramfs even in hostonly mode
print_info "Configuring dracut to include zram driver..."
cat > /etc/dracut.conf.d/zramroot.conf <<'EOF'
add_drivers+=" zram "
EOF
print_success "Dracut module files installed successfully!"
# Rebuild initramfs
print_info ""
print_info "=== Rebuilding initramfs with dracut ==="
print_warning "This may take a minute..."
# Detect if this is Qubes OS
IS_QUBES=0
if [ -f "/etc/qubes-release" ] || command -v qubes-dom0-update >/dev/null 2>&1; then
IS_QUBES=1
print_info "Detected Qubes OS Dom0"
fi
# Rebuild initramfs for all kernels
if dracut --force --regenerate-all; then
print_success "Initramfs rebuilt successfully!"
# Verify module was included
print_info "Verifying zramroot module inclusion..."
if lsinitrd 2>/dev/null | grep -q "zramroot"; then
print_success "zramroot module successfully included in initramfs"
else
print_warning "Could not verify module inclusion (lsinitrd may not be available)"
print_info "You can verify manually with: lsinitrd | grep zramroot"
fi
else
print_error "Failed to rebuild initramfs"
print_info "You may need to run 'sudo dracut --force --regenerate-all' manually"
exit 1
fi
print_success "Dracut configuration completed!"
# Qubes-specific recommendations
if [ $IS_QUBES -eq 1 ]; then
echo ""
print_info "=== Qubes OS Specific Recommendations ==="
print_warning "Qubes Dom0 only sees its assigned RAM. zramroot will use Dom0's memory limit, not total system RAM."
print_info "If zramroot reports insufficient RAM, increase Dom0 memory in Qubes settings (e.g., dom0_mem) or set ZRAM_SIZE_MiB manually."
print_info "For Dom0 in Qubes OS, consider adding to /etc/zramroot.conf:"
print_info " ZRAM_MOUNT_ON_DISK=\"/var/lib/qubes\""
print_info " ZRAM_PHYSICAL_ROOT_OPTS=\"rw\""
print_info " ZRAM_EXCLUDE_PATTERNS=\"/var/lib/qubes/appvms/* /var/lib/qubes/vm-templates/*\""
print_info " ZRAM_ALGO=\"zstd\""
print_info " RAM_MIN_FREE_MiB=2048"
print_info " DEBUG_MODE=\"yes\" # Recommended for first boot"
echo ""
fi
fi
# Bootloader Configuration
echo ""
print_info "=== Bootloader Configuration ==="
print_info "${TRIGGER_PARAMETER} requires adding a kernel parameter to your bootloader."
print_info "You can either:"
print_info " 1. Let the script automatically configure a bootloader entry"
print_info " 2. Skip bootloader configuration and do it manually later"
echo ""
read -p "Would you like to configure a bootloader entry automatically? (y/n): " configure_bootloader
if [[ ! "$configure_bootloader" =~ ^[Yy]$ ]]; then
print_info "Skipping bootloader configuration."
print_warning "You'll need to manually add '${TRIGGER_PARAMETER}' to your kernel parameters."
bootloader="skip"
else
echo ""
print_info "Bootloader configuration options:"
print_info " 1. Auto-detect bootloader (recommended)"
print_info " 2. Manually choose bootloader"
echo ""
while true; do
read -p "Choose detection method (1-2): " detection_method
case $detection_method in
1)
print_info "Auto-detecting bootloader..."
break
;;
2)
print_info "Manual bootloader selection..."
break
;;
*)
print_warning "Please enter 1 or 2"
;;
esac
done
# Enhanced bootloader detection function
detect_all_bootloaders() {
local found_bootloaders=()
# Check for GRUB (both GRUB and GRUB2)
# GRUB can be in multiple locations: /boot/grub/, /boot/grub2/, or /boot/efi/EFI/*/
grub_detected=0
# Check for GRUB config files
if [ -f "/boot/grub/grub.cfg" ] || [ -f "/boot/grub2/grub.cfg" ] || \
[ -f "/boot/efi/EFI/qubes/grub.cfg" ] || [ -f "/boot/efi/EFI/fedora/grub.cfg" ] || \
[ -f "/boot/efi/EFI/centos/grub.cfg" ] || [ -f "/boot/efi/EFI/redhat/grub.cfg" ]; then
grub_detected=1
fi
# Also check if /etc/grub.d/ exists (common for all GRUB installations)
if [ -d "/etc/grub.d" ]; then
grub_detected=1
fi
# Verify GRUB commands are available
if [ $grub_detected -eq 1 ]; then
if command -v update-grub >/dev/null 2>&1 || \
command -v grub-mkconfig >/dev/null 2>&1 || \
command -v grub2-mkconfig >/dev/null 2>&1; then
found_bootloaders+=("grub")
fi
fi
# Check for systemd-boot
if [ -d "/boot/loader" ] && [ -f "/boot/loader/loader.conf" ]; then
if command -v bootctl >/dev/null 2>&1; then
found_bootloaders+=("systemd-boot")
fi
fi
# Check for systemd-boot in EFI directory
if [ -d "/boot/EFI/systemd" ] || [ -d "/boot/efi/systemd" ]; then
if command -v bootctl >/dev/null 2>&1; then
if [[ ! " ${found_bootloaders[@]} " =~ " systemd-boot " ]]; then
found_bootloaders+=("systemd-boot")
fi
fi
fi
# Check for extlinux/syslinux
if [ -f "/boot/extlinux/extlinux.conf" ] || [ -f "/boot/syslinux/syslinux.cfg" ] || [ -f "/boot/syslinux.cfg" ]; then
if command -v extlinux >/dev/null 2>&1 || command -v syslinux >/dev/null 2>&1; then
found_bootloaders+=("extlinux")
fi
fi
echo "${found_bootloaders[@]}"
}
if [ "$detection_method" = "1" ]; then
# Auto-detection
found_bootloaders=($(detect_all_bootloaders))
if [ ${#found_bootloaders[@]} -eq 0 ]; then
print_warning "No supported bootloaders detected automatically."
echo ""
print_warning "Supported bootloaders:"
print_info " - GRUB/GRUB2 (requires /etc/grub.d/ and grub-mkconfig/grub2-mkconfig)"
print_info " - systemd-boot (requires /boot/loader/ and bootctl command)"
print_info " - extlinux/syslinux (requires config files and commands)"
echo ""
print_info "Note: For GRUB2 systems (Fedora/RHEL/Qubes), the script looks for:"
print_info " - /boot/grub2/grub.cfg or /boot/efi/EFI/qubes/grub.cfg"
print_info " - /etc/grub.d/ directory"
print_info " - grub2-mkconfig command"
echo ""
read -p "Continue with manual configuration? (y/n): " continue_manual
if [[ ! "$continue_manual" =~ ^[Yy]$ ]]; then
print_info "Installation cancelled by user."
exit 0
fi
bootloader="manual"
elif [ ${#found_bootloaders[@]} -eq 1 ]; then
bootloader="${found_bootloaders[0]}"
print_info "Detected bootloader: $bootloader"