-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathiommu.sh
More file actions
168 lines (147 loc) · 4.23 KB
/
Copy pathiommu.sh
File metadata and controls
168 lines (147 loc) · 4.23 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
#!/bin/bash
# Enable IOMMU for Proxmox - Enhanced Version
# Usage: ./enable_iommu_proxmox.sh [-h] [-d] [-v]
# -h: Show help
# -d: Dry-run mode (simulate changes)
# -v: Verbose output
# Constants
GRUB_FILE="/etc/default/grub"
MODULES_FILE="/etc/modules"
LOG_FILE="/var/log/iommu_setup.log"
BACKUP_SUFFIX=".bak.$(date +%Y%m%d_%H%M%S)"
# Default options
DRY_RUN=0
VERBOSE=0
# Function to log messages
log() {
local message="$1"
echo "$(date '+%Y-%m-%d %H:%M:%S') - $message" | tee -a "$LOG_FILE"
[ "$VERBOSE" -eq 1 ] && echo "$message"
}
# Function to show usage
show_usage() {
echo "Usage: $0 [-h] [-d] [-v]"
echo " -h: Show this help message"
echo " -d: Dry-run mode (simulate changes without applying)"
echo " -v: Verbose output"
echo "Enables IOMMU for Proxmox by configuring GRUB and kernel modules."
exit 0
}
# Parse command-line options
while getopts "hdv" opt; do
case "$opt" in
h) show_usage ;;
d) DRY_RUN=1 ;;
v) VERBOSE=1 ;;
?) exit 1 ;;
esac
done
# Check for root privileges
if [ "$EUID" -ne 0 ]; then
echo "Error: This script must be run as root."
exit 1
fi
# Function to detect CPU manufacturer
get_cpu_manufacturer() {
if lscpu | grep -i "GenuineIntel" >/dev/null 2>&1; then
echo "intel"
elif lscpu | grep -i "AuthenticAMD" >/dev/null 2>&1; then
echo "amd"
else
echo "unknown"
fi
}
# Function to backup a file
backup_file() {
local file="$1"
if [ -f "$file" ]; then
cp "$file" "$file$BACKUP_SUFFIX" || {
log "Error: Failed to backup $file"
exit 1
}
log "Backed up $file to $file$BACKUP_SUFFIX"
fi
}
# Function to configure GRUB
configure_grub() {
local cpu_type="$1"
local grub_entry=""
[ "$cpu_type" == "intel" ] && grub_entry="GRUB_CMDLINE_LINUX_DEFAULT=\"quiet intel_iommu=on iommu=pt\""
[ "$cpu_type" == "amd" ] && grub_entry="GRUB_CMDLINE_LINUX_DEFAULT=\"quiet amd_iommu=on iommu=pt\""
if [ -z "$grub_entry" ]; then
log "Error: Invalid CPU type for GRUB configuration"
exit 1
fi
if [ ! -w "$GRUB_FILE" ]; then
log "Error: $GRUB_FILE is not writable"
exit 1
}
backup_file "$GRUB_FILE"
if [ "$DRY_RUN" -eq 1 ]; then
log "Dry-run: Would update $GRUB_FILE with: $grub_entry"
else
sed -i "s|GRUB_CMDLINE_LINUX_DEFAULT=\"quiet.*|$grub_entry|" "$GRUB_FILE" || {
log "Error: Failed to update $GRUB_FILE"
exit 1
}
update-grub || {
log "Error: Failed to run update-grub"
exit 1
}
log "Updated GRUB configuration for $cpu_type CPU"
fi
}
# Function to configure modules
configure_modules() {
local vfio_entries=("vfio" "vfio_iommu_type1" "vfio_pci" "vfio_virqfd")
if [ ! -w "$MODULES_FILE" ]; then
log "Error: $MODULES_FILE is not writable"
exit 1
}
backup_file "$MODULES_FILE"
if [ "$DRY_RUN" -eq 1 ]; then
log "Dry-run: Would update $MODULES_FILE with VFIO modules"
else
sed -i '/vfio\|vfio_iommu_type1\|vfio_pci\|vfio_virqfd/d' "$MODULES_FILE" || {
log "Error: Failed to clean $MODULES_FILE"
exit 1
}
for entry in "${vfio_entries[@]}"; do
echo "$entry" >> "$MODULES_FILE" || {
log "Error: Failed to append $entry to $MODULES_FILE"
exit 1
}
done
log "Updated $MODULES_FILE with VFIO modules"
fi
}
# Main logic
log "Starting IOMMU configuration for Proxmox"
cpu_type=$(get_cpu_manufacturer)
case "$cpu_type" in
"intel"|"amd")
log "Detected $cpu_type CPU"
configure_grub "$cpu_type"
configure_modules
log "Configuration completed for $cpu_type CPU"
;;
*)
log "Error: Unsupported CPU type or detection failed"
exit 1
;;
esac
# Reboot prompt with timeout
if [ "$DRY_RUN" -eq 1 ]; then
log "Dry-run: Would prompt for reboot"
else
echo -n "Reboot now? (y/n, default y in 10s): "
read -t 10 choice
choice=${choice:-y}
if [[ "$choice" =~ ^[Yy]$ ]]; then
log "Rebooting system"
reboot
else
log "Please reboot later to apply changes"
fi
fi
exit 0