1+ #! /bin/bash
2+
3+ # CXL NUMA Configuration Script
4+ # This script automatically configures CXL memory as NUMA node 1 at boot
5+
6+ set -e
7+
8+ LOG_FILE=" /var/log/cxl_numa_setup.log"
9+ CXL_REGION_SIZE=" 256M"
10+ MAX_RETRIES=10
11+ RETRY_DELAY=2
12+
13+ log () {
14+ echo " [$( date ' +%Y-%m-%d %H:%M:%S' ) ] $1 " | tee -a " $LOG_FILE "
15+ }
16+
17+ wait_for_cxl_device () {
18+ local retries=0
19+ while [ $retries -lt $MAX_RETRIES ]; do
20+ if cxl list -M 2> /dev/null | grep -q " mem0" ; then
21+ log " CXL device mem0 detected"
22+ return 0
23+ fi
24+ log " Waiting for CXL device... (attempt $(( retries+ 1 )) /$MAX_RETRIES )"
25+ sleep $RETRY_DELAY
26+ retries=$(( retries+ 1 ))
27+ done
28+ log " ERROR: CXL device not found after $MAX_RETRIES attempts"
29+ return 1
30+ }
31+
32+ setup_cxl_region () {
33+ log " Creating CXL region..."
34+
35+ # Check if region already exists
36+ if cxl list -R 2> /dev/null | grep -q " region0" ; then
37+ log " Region0 already exists, skipping creation"
38+ return 0
39+ fi
40+
41+ # Create CXL region
42+ if cxl create-region -m -d decoder0.0 -w 1 mem0 -s " $CXL_REGION_SIZE " 2>&1 | tee -a " $LOG_FILE " ; then
43+ log " CXL region created successfully"
44+ return 0
45+ else
46+ log " ERROR: Failed to create CXL region"
47+ return 1
48+ fi
49+ }
50+
51+ setup_dax_namespace () {
52+ log " Setting up DAX namespace..."
53+
54+ # Check if namespace already exists
55+ if ndctl list -N 2> /dev/null | grep -q " namespace" ; then
56+ log " Namespace already exists"
57+ return 0
58+ fi
59+
60+ # Wait a bit for region to be fully initialized
61+ sleep 2
62+
63+ # Create DAX namespace
64+ if ndctl create-namespace -m dax -r region0 2>&1 | tee -a " $LOG_FILE " ; then
65+ log " DAX namespace created successfully"
66+ return 0
67+ else
68+ # Try to reconfigure if it fails
69+ log " Initial namespace creation failed, trying to reconfigure..."
70+ ndctl destroy-namespace all -f 2> /dev/null || true
71+ sleep 1
72+ if ndctl create-namespace -m dax -r region0 2>&1 | tee -a " $LOG_FILE " ; then
73+ log " DAX namespace created after reconfiguration"
74+ return 0
75+ else
76+ log " WARNING: Could not create DAX namespace"
77+ return 1
78+ fi
79+ fi
80+ }
81+
82+ configure_numa_node () {
83+ log " Configuring NUMA node..."
84+
85+ # Find the DAX device
86+ local dax_device=$( ls /sys/bus/dax/devices/ 2> /dev/null | head -n1)
87+
88+ if [ -z " $dax_device " ]; then
89+ log " WARNING: No DAX device found"
90+ return 1
91+ fi
92+
93+ # Online the memory as NUMA node 1
94+ if [ -f " /sys/bus/dax/devices/$dax_device /target_node" ]; then
95+ local target_node=$( cat " /sys/bus/dax/devices/$dax_device /target_node" )
96+ log " Target NUMA node: $target_node "
97+
98+ # Try to online the memory
99+ if daxctl reconfigure-device --mode=system-ram " $dax_device " 2>&1 | tee -a " $LOG_FILE " ; then
100+ log " Memory onlined as system RAM"
101+ else
102+ log " WARNING: Could not online memory as system RAM"
103+ fi
104+ fi
105+
106+ # Verify NUMA configuration
107+ numactl --hardware 2>&1 | tee -a " $LOG_FILE "
108+
109+ return 0
110+ }
111+
112+ main () {
113+ log " Starting CXL NUMA configuration..."
114+
115+ # Load required kernel modules
116+ modprobe cxl_core 2> /dev/null || true
117+ modprobe cxl_pci 2> /dev/null || true
118+ modprobe cxl_acpi 2> /dev/null || true
119+ modprobe cxl_port 2> /dev/null || true
120+ modprobe cxl_mem 2> /dev/null || true
121+ modprobe dax 2> /dev/null || true
122+ modprobe device_dax 2> /dev/null || true
123+ modprobe kmem 2> /dev/null || true
124+
125+ # Wait for CXL device to appear
126+ if ! wait_for_cxl_device; then
127+ log " Aborting: CXL device not available"
128+ exit 1
129+ fi
130+
131+ # Setup CXL region
132+ if ! setup_cxl_region; then
133+ log " Warning: CXL region setup failed, continuing anyway"
134+ fi
135+
136+ # Setup DAX namespace
137+ if ! setup_dax_namespace; then
138+ log " Warning: DAX namespace setup failed, continuing anyway"
139+ fi
140+
141+ # Configure NUMA node
142+ # if ! configure_numa_node; then
143+ # log "Warning: NUMA node configuration incomplete"
144+ # fi
145+
146+ log " CXL NUMA configuration completed"
147+
148+ # Display final configuration
149+ log " Final CXL configuration:"
150+ cxl list 2>&1 | tee -a " $LOG_FILE "
151+ log " Final NUMA configuration:"
152+ numactl --hardware 2>&1 | tee -a " $LOG_FILE "
153+ }
154+
155+ # Run main function
156+ main
157+ # dhcpcd
158+ ip link set enp0s2 up
159+ ip addr add 192.168.100.10/24 dev enp0s2
160+ ip route add default via 192.168.100.1
0 commit comments