Skip to content

Commit

Permalink
🎉
Browse files Browse the repository at this point in the history
  • Loading branch information
JarbasAl committed Nov 25, 2024
1 parent 97f74dd commit 8cefe92
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 3 deletions.
187 changes: 184 additions & 3 deletions build_raspOVOS.sh
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,9 @@
set -e

# Update package list and install necessary tools
echo "Updating base system..."
echo "Updating base system..."ã
apt-get update
apt-get install -y --no-install-recommends git curl i2c-tools swig libssl-dev libfann-dev portaudio19-dev libpulse-dev build-essential python3-dev python3-pip
apt-get install -y --no-install-recommends git curl systemd-zram-generator i2c-tools swig libssl-dev libfann-dev portaudio19-dev libpulse-dev build-essential python3-dev python3-pip linux-cpupower

# setup ovos-i2csound
echo "Installing ovos-i2csound..."
Expand Down Expand Up @@ -41,7 +41,10 @@ pip3 install ovos-dinkum-listener[extras,linux,onnx]
pip3 install ovos-phal[extras,linux,mk1]



echo "Setting up systemd..."
# copy system scripts over
mkdir -p /home/pi/.config/systemd/user/
cp -v /mounted-github-repo/ovos.service /home/pi/.config/systemd/user/
cp -v /mounted-github-repo/ovos-skills.service /home/pi/.config/systemd/user/
cp -v /mounted-github-repo/ovos-messagebus.service /home/pi/.config/systemd/user/
Expand All @@ -60,4 +63,182 @@ systemctl enable ovos-admin-phal

systemctl --user daemon-reload
systemctl daemon-reload
#chown -v pi:pi /home/pi/README.md
#chown -v pi:pi /home/pi/README.md


# tuning

# Define the backup file
FSTAB="/etc/fstab"
BACKUP="/etc/fstab.bak"

# Make a backup of the original /etc/fstab
cp "$FSTAB" "$BACKUP"
echo "Backup of /etc/fstab created at $BACKUP"

# Process /etc/fstab
while IFS= read -r line; do
# Skip comments and empty lines
if [[ "$line" =~ ^# ]] || [[ -z "$line" ]]; then
echo "$line"
continue
fi

# Match lines with ext4, xfs, or btrfs filesystems
if [[ "$line" =~ ^(\S+)\s+(\S+)\s+(ext4|xfs|btrfs)\s+([^#]*)\s+(\d+)\s+(\d+)$ ]]; then
DEVICE="${BASH_REMATCH[1]}"
MOUNTPOINT="${BASH_REMATCH[2]}"
FSTYPE="${BASH_REMATCH[3]}"
OPTIONS="${BASH_REMATCH[4]}"
DUMP="${BASH_REMATCH[5]}"
PASS="${BASH_REMATCH[6]}"

# Check if "noatime" is already present
if [[ ! "$OPTIONS" =~ noatime ]]; then
OPTIONS="$OPTIONS,noatime"
fi

# Check if "nodiratime" is already present
if [[ ! "$OPTIONS" =~ nodiratime ]]; then
OPTIONS="$OPTIONS,nodiratime"
fi

# Print the updated line
echo -e "$DEVICE\t$MOUNTPOINT\t$FSTYPE\t$OPTIONS\t$DUMP\t$PASS"
else
# Print lines that do not match the filesystem types
echo "$line"
fi
done < "$FSTAB" > "${FSTAB}.tmp"

# Replace the original fstab with the updated one
mv "${FSTAB}.tmp" "$FSTAB"
echo "Updated /etc/fstab with noatime and nodiratime options."


# Configure the CPU governor to "performance"
CONFIG_FILE="/etc/default/cpupower"
if [[ -f "$CONFIG_FILE" ]]; then
sed -i 's/^CPU_DEFAULT_GOVERNOR=.*/CPU_DEFAULT_GOVERNOR="performance"/' "$CONFIG_FILE"
else
echo 'CPU_DEFAULT_GOVERNOR="performance"' > "$CONFIG_FILE"
fi

# Create the cpu governor systemd service file
cp -v /mounted-github-repo/cpu-governor.service /etc/systemd/system/cpu-governor.service
# Set permissions, enable, and start the service
chmod 644 /etc/systemd/system/cpu-governor.service
systemctl daemon-reload
systemctl enable cpu-governor.service
echo "CPU governor set to performance and service enabled"

# Load I2C, SPI, and I2S kernel modules and make persistent
for module in i2c-dev spidev; do
if ! grep -q "$module" /etc/modules; then
echo "$module" >> /etc/modules
fi
modprobe "$module"
done

# Enable I2C, SPI, and I2S in /boot/config.txt
CONFIG_FILE="/boot/config.txt"

declare -A dtparams=(
["dtparam=i2c_arm"]="on"
["dtparam=spi"]="on"
["dtparam=i2s"]="on"
)

for key in "${!dtparams[@]}"; do
value="${dtparams[$key]}"
if grep -q "^${key}=" "$CONFIG_FILE"; then
sed -i "s|^${key}=.*|${key}=${value}|" "$CONFIG_FILE"
else
echo "${key}=${value}" >> "$CONFIG_FILE"
fi
done

echo "Kernel modules and Device Tree parameters configured."

# Define the sysctl configuration file
SYSCTL_FILE="/etc/sysctl.d/99-ovos.conf"

# Sysctl options to apply
declare -A sysctl_options=(
["net.ipv4.tcp_slow_start_after_idle"]=0
["net.ipv4.tcp_tw_reuse"]=1
["net.core.netdev_max_backlog"]=50000
["net.ipv4.tcp_max_syn_backlog"]=30000
["net.ipv4.tcp_max_tw_buckets"]=2000000
["net.core.rmem_max"]=16777216
["net.core.wmem_max"]=16777216
["net.core.rmem_default"]=16777216
["net.core.wmem_default"]=16777216
["net.ipv4.tcp_rmem"]="4096 87380 16777216"
["net.ipv4.tcp_wmem"]="4096 65536 16777216"
["net.core.optmem_max"]=40960
["fs.inotify.max_user_instances"]=8192
["fs.inotify.max_user_watches"]=524288
)

# Create or update sysctl configuration
echo "# OVOS kernel tuning parameters" > "$SYSCTL_FILE"
for option in "${!sysctl_options[@]}"; do
echo "$option = ${sysctl_options[$option]}" >> "$SYSCTL_FILE"
done

echo "Kernel tuning parameters applied. Configuration saved to $SYSCTL_FILE."


# Define the udev rules file path
UDEV_RULES_FILE="/etc/udev/rules.d/60-mmc-usb-scheduler.rules"

# Check if the rules file exists and create if necessary
if [ ! -f "$UDEV_RULES_FILE" ]; then
echo "Creating udev rules file at $UDEV_RULES_FILE"
touch "$UDEV_RULES_FILE"
chown root:root "$UDEV_RULES_FILE"
chmod 644 "$UDEV_RULES_FILE"
fi

# Add I/O scheduler rules for MMC and USB
echo 'ACTION=="add|change", KERNEL=="mmc*", ATTR{queue/rotational}=="0", ATTR{queue/scheduler}="none"' >> "$UDEV_RULES_FILE"
echo 'ACTION=="add|change", KERNEL=="sd[a-z]", ATTR{removable}=="1", ATTR{queue/scheduler}="none"' >> "$UDEV_RULES_FILE"

echo "I/O scheduler rules applied for MMC and USB devices."


echo "Copying wlan0-power systemd service to /etc/systemd/system/wlan0-power.service"
cp -v /mounted-github-repo/wlan0-power.service /etc/systemd/system/wlan0-power.service
sudo systemctl daemon-reload
sudo systemctl enable wlan0-power.service
echo "wlan0-power service is now enabled."



# ZRAM
cp -v /mounted-github-repo/zram-generator.conf /etc/systemd/zram-generator.conf


SYSCTL_CONF="/etc/sysctl.d/98-zram.conf"


# Configure sysctl settings for ZRAM
echo "Applying sysctl settings for ZRAM..."
cat <<EOL | sudo tee "$SYSCTL_CONF"
vm.swappiness=100
vm.page-cluster=0
vm.vfs_cache_pressure=500
vm.dirty_background_ratio=1
vm.dirty_ratio=50
EOL

# Apply the sysctl settings
echo "Reloading sysctl settings..."
sudo sysctl --system

# Reload systemd to apply the configuration changes
systemctl daemon-reload
# Start the systemd ZRAM service
echo "Starting ZRAM service..."
systemctl enable /dev/zram0
12 changes: 12 additions & 0 deletions cpu-governor.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[Unit]
Documentation=https://wiki.archlinux.org/index.php/CPU_frequency_scaling
Description=Set CPU governor to performance to avoid contex switcing between ondemand and performance

[Service]
Type=oneshot
ExecStart=/usr/bin/cpupower frequency-set -g performance
ExecStop=/usr/bin/cpupower frequency-set -g ondemand
RemainAfterExit=yes

[Install]
WantedBy=default.target
13 changes: 13 additions & 0 deletions wlan0-power.service
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
[Unit]
Description=Disable wlan0 powersave
After=network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/sbin/iw wlan0 set power_save off
ExecStop=/sbin/iw wlan0 set power_save on
RemainAfterExit=yes

[Install]
WantedBy=default.target
5 changes: 5 additions & 0 deletions zram-generator.conf
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[zram0]
zram-size = ram / 2
compression-algorithm = lzo-rle
swap-priority = 100
fs-type = swap

0 comments on commit 8cefe92

Please sign in to comment.