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 8cefe92 commit ccb8c18
Show file tree
Hide file tree
Showing 8 changed files with 248 additions and 197 deletions.
227 changes: 30 additions & 197 deletions build_raspOVOS.sh
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,8 @@ set -e
# Update package list and install necessary tools
echo "Updating base system..."ã
apt-get update
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
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

# setup ovos-i2csound
echo "Installing ovos-i2csound..."
git clone https://github.com/OpenVoiceOS/ovos-i2csound /tmp/ovos-i2csound

mkdir /etc/OpenVoiceOS

cp /tmp/ovos-i2csound/i2c.conf /etc/modules-load.d/i2c.conf
cp /tmp/ovos-i2csound/bcm2835-alsa.conf /etc/modules-load.d/bcm2835-alsa.conf
cp /tmp/ovos-i2csound/i2csound.service /etc/systemd/system/i2csound.service
cp /tmp/ovos-i2csound/ovos-i2csound /usr/libexec/ovos-i2csound
cp /tmp/ovos-i2csound/99-i2c.rules /usr/lib/udev/rules.d/99-i2c.rules

chmod +x /usr/libexec/ovos-i2csound
systemctl enable i2csound.service

# Create virtual environment for ovos
echo "Creating virtual environment..."
Expand All @@ -35,13 +21,13 @@ source ~/.venvs/ovos/bin/activate

# install OVOS in venv
echo "Installing OVOS..."
pip3 install wheel cython
pip3 install sdnotify tflite_runtime
pip3 install ovos-core[lgpl,mycroft,plugins,skills-audio,skills-essential,skills-internet,skills-media]
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/
Expand All @@ -53,6 +39,9 @@ cp -v /mounted-github-repo/ovos-dinkum-listener.service /home/pi/.config/systemd
cp -v /mounted-github-repo/ovos-phal.service /home/pi/.config/systemd/user/
cp -v /mounted-github-repo/ovos-admin-phal.service /etc/systemd/system/

systemctl --user daemon-reload
systemctl daemon-reload

systemctl --user enable ovos
systemctl --user enable ovos-messagebus
systemctl --user enable ovos-dinkum-listener
Expand All @@ -61,184 +50,28 @@ systemctl --user enable ovos-skills
systemctl --user enable ovos-phal
systemctl enable ovos-admin-phal

systemctl --user daemon-reload
systemctl daemon-reload
#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

# setup ovos-i2csound
echo "Installing ovos-i2csound..."
git clone https://github.com/OpenVoiceOS/ovos-i2csound /tmp/ovos-i2csound

mkdir /etc/OpenVoiceOS

cp /tmp/ovos-i2csound/i2c.conf /etc/modules-load.d/i2c.conf
cp /tmp/ovos-i2csound/bcm2835-alsa.conf /etc/modules-load.d/bcm2835-alsa.conf
cp /tmp/ovos-i2csound/i2csound.service /etc/systemd/system/i2csound.service
cp /tmp/ovos-i2csound/ovos-i2csound /usr/libexec/ovos-i2csound
cp /tmp/ovos-i2csound/99-i2c.rules /usr/lib/udev/rules.d/99-i2c.rules

chmod +x /usr/libexec/ovos-i2csound
systemctl enable i2csound.service


echo "Tuning base system..."
bash /mounted-github-repo/setup_zram.sh
bash /mounted-github-repo/setup_cpugovernor.sh
bash /mounted-github-repo/setup_wlan0power.sh
bash /mounted-github-repo/setup_fstab.sh
bash /mounted-github-repo/setup_sysctl.sh
bash /mounted-github-repo/setup_udev.sh
bash /mounted-github-repo/setup_kernel_modules.sh
24 changes: 24 additions & 0 deletions setup_cpugovernor.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#!/bin/bash
# Exit on error
# If something goes wrong just stop.
# it allows the user to see issues at once rather than having
# scroll back and figure out what went wrong.
set -e

apt-get install -y --no-install-recommends linux-cpupower

# 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"
49 changes: 49 additions & 0 deletions setup_fstab.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#!/bin/bash
# Exit on error
# If something goes wrong just stop.
# it allows the user to see issues at once rather than having
# scroll back and figure out what went wrong.
set -e


# Define the backup file
FSTAB="/etc/fstab"

# 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."
35 changes: 35 additions & 0 deletions setup_kernel_modules.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#!/bin/bash
# Exit on error
# If something goes wrong just stop.
# it allows the user to see issues at once rather than having
# scroll back and figure out what went wrong.
set -e


# 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."
Loading

0 comments on commit ccb8c18

Please sign in to comment.