Skip to content

Instantly share code, notes, and snippets.

@kmpm
Last active September 7, 2020 17:41
Show Gist options
  • Save kmpm/bc7e0bb4a99f38fb4206 to your computer and use it in GitHub Desktop.
Save kmpm/bc7e0bb4a99f38fb4206 to your computer and use it in GitHub Desktop.

Codesys RPI HOWTO

This describes how you could configure your Raspberry PI to work, hopefully, better with Codesys.

Instructions

  • Based on raspbian image

These instructions are based on...

First Boot

raspi-config

  1. Expand filesystem (can skip this step if extra partitions needed)
  2. I18N -> Locales, Timezone, keyboard.
  3. Advanced Options -> Memory Split -> give 16MB to GPU
  4. Overclock -> Modest, 800Mhz

Finish and reboot

Packages

#update to a fresh install
sudo apt-get update && sudo apt-get -y upgrade

sudo apt-get -y purge libx11-6 libgtk-3-common xkb-data lxde-icon-theme \
raspberrypi-artwork penguinspuzzle wolfram-engine

sudo apt-get -y autoremove

sudo rpi-update

sudo reboot

Disable swapping

sudo dphys-swapfile swapoff && sudo dphys-swapfile uninstall && sudo update-rc.d dphys-swapfile remove

Setup kernel options

Change elevator from deadline to noop sudo nano /boot/cmdline.txt elevator=noop #sets the Network device not in turbo mode. turns off buffering network traffic smsc95xx.turbo_mode=N

(ctrl-x Save exit)

Resolveconf

Since the filesystem will be read-only. DHCP will have issues updating resolv.conf Switch DNS resolve to Google Server (So packages will not update)

sudo apt-get install resolvconf
sudo nano /etc/resolvconf/resolv.conf.d/base
nameserver 8.8.8.8
nameserver 8.8.4.4

(ctrl-x Save exit)

CPU Scaling

Set the CPU scaling governor to performance

sudo echo -n performance | sudo tee /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor

Checking filesystem

sudo nano /etc/default/rcS

# automatically repair filesystems with inconsistencies during boot
FSCKFIX=yes

(ctrl-x Save exit)

Scan every 3 reboots

sudo tune2fs -c 3 /dev/mmcblk0p2

Replace syslog with inetutils-syslogd

sudo su
apt-get -y remove --purge rsyslog
apt-get -y install inetutils-syslogd
echo -e "*.*;mail.none;cron.none\t -/var/log/messages\ncron.*\t -/var/log/cron\nmail.*\t -/var/log/mail" > /etc/syslog.conf
mkdir -p /etc/logrotate.d
echo -e "/var/log/cron\n/var/log/mail\n/var/log/messages {\n\trotate 4\n\tweekly\n\tmissingok\n\tnotifempty\n\tcompress\n\tsharedscripts\n\tpostrotate\n\t/etc/init.d/inetutils-syslogd reload >/dev/null\n\tendscript\n}" > /etc/logrotate.d/inetutils-syslogd
service inetutils-syslogd start
exit

Disable IP6

echo "net.ipv6.conf.all.disable_ipv6=1" >> /etc/sysctl.d/disableipv6.conf
echo "install ipv6 /bin/true" >> /etc/modprobe.d/blacklist.conf
echo "install ipv6 /bin/true" >> /etc/modprobe.d/raspi-blacklist.conf
sudo sed -i '/::/s%^%#%g' /etc/hosts

Replacing OpenSSH with Dropbear

sudo apt-get -y install dropbear openssh-client
sudo sed -i 's/NO_START=1/NO_START=0/g' /etc/default/dropbear
sudo /etc/init.d/ssh stop && sudo /etc/init.d/dropbear start
sudo apt-get purge openssh-server   

Remove extra tty

Remove the extra tty / getty’s | Save: +3.5 MB RAM

sudo sed -i '/[2-6]:23:respawn:\/sbin\/getty 38400 tty[2-6]/s%^%#%g' /etc/inittab   
sudo sed -i '/T0:23:respawn:\/sbin\/getty -L ttyAMA0 115200 vt100/s%^%#%g' /etc/inittab

Use Dash instead of BASH

sudo dpkg-reconfigure dash

Network names

Assign network names based on location so image can boot on different PI

sudo nano /etc/udev/rules.d/70-persistent-net.rules
KERNEL=="eth*", KERNELS=="1-1.1", NAME="eth0"
KERNEL=="eth*", KERNELS=="1-1.[2-3]", NAME="eth1"

(ctrl-x Save exit)

Move/etc/network/interfaces to Symbolic Link

(http://rpi.tnet.com/project/faqs/winaccessinterfacesfile)

sudo cd /etc/network
sudo cp interfaces /boot/interfaces
sudo mv interfaces interfaces.save
sudo ln -s /boot/interfaces interfaces

Setup enable second network interface to have DHCP

sudo nano /etc/network/interfaces
auto lo
iface lo inet loopback

allow-hotplug eth0
iface eth0 inet dhcp

allow-hotplug eth1
iface eth1 inet dhcp

(ctrl-x Save exit)

Remove NTP

Optionally remove NTP from service so it does not interruppt Network Another way to disable NTP (http://windycitytech.blogspot.com/2013/03/disable-ntp-sending-out-requests-on.html)

sudo update-rc.d -f ntp remove

Tune system

sudo nano  /etc/sysctl.conf

fs.file-max = 150000
vm.min_free_kbytes = 16384
vm.dirty_background_ratio = 20
vm.dirty_expire_centisecs = 0
vm.dirty_ratio = 80
vm.dirty_writeback_centisecs = 1200
vm.overcommit_ratio = 2
vm.laptop_mode = 5
vm.swappiness = 10
net.ipv4.tcp_timestamps = 0 
net.ipv4.tcp_sack = 1 
net.ipv4.tcp_window_scaling = 1
net.core.rmem_default = 31457280
net.core.rmem_max = 12582912
net.core.wmem_default = 31457280
net.core.wmem_max = 12582912
net.core.somaxconn = 65536
net.core.netdev_max_backlog = 65536
net.core.optmem_max = 25165824
net.ipv4.tcp_mem = 65536 131072 262144
net.ipv4.udp_mem = 65536 131072 262144
net.ipv4.tcp_rmem = 8192 87380 16777216
net.ipv4.udp_rmem_min = 16384
net.ipv4.tcp_wmem = 8192 65536 16777216
net.ipv4.udp_wmem_min = 16384

(ctrl-x Save exit)

##Fix fstab

Add noatime to all, mount /var/run and other fast burning partitions to tmpfs (except /var/log). Mount /boot in ro mode sudo nano /etc/fstab

proc            /proc           proc    defaults          0       0
none            /var/run        tmpfs   size=1M,noatime   0       0
none            /tmp            tmpfs   size=1M,noatime   0       0
none            /var/tmp        tmpfs   size=1M,noatime   0       0
/dev/mmcblk0p1  /boot           vfat    noatime           0       2
/dev/mmcblk0p2  /               ext4    ro,defaults,noatime  0       1

(ctrl-x Save exit)

sudo sed -i 's/defaults,noatime/defaults,noatime,nodiratime/g' /etc/fstab

Install ramlog

Read more at http://abel-it.co.uk/pi/ramlog.html or http://www.xappsoftware.com/wordpress/2013/06/10/two-tricks-to-make-your-raspberry-pi-more-reliable/

It's basically a ram cashed /var/log that gets written to flash on intervals to save the SD card.

wget https://raw.github.com/swirepe/personalscripts/master/pi/ramlog_2.0.0_all.deb
sudo dpkg -i ramlog_2.0.0_all.deb

Edit /etc/init.d/ramlog and add the -After and -Before lines

### BEGIN INIT INFO
# Provides: ramlog
# Required-Start:
# X-Start-Before:   inetutils-syslogd
# X-Stop-After:     inetutils-syslogd

Set this up

sudo insserv

Clear cmd history

sudo cat /dev/null > ~/.bash_history && history -c

##Reboot to have all setting take effect sudo reboot

After everything is set up

Remount as rw

sudo mount -o remount,rw /

make image from prepared sd card

sudo dd bs=4M if=/dev/sdb | gzip > date +%Y-%m-%d-raspbian-ro.img.gz

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment