How to Reduce read/write operations on Raspberry Pi

How to Reduce read/write operations on Raspberry Pi

Introduction

So, on the Internet, you can find articles about the fact that in Raspberry flash drives “live” for 2-3 months, after which they come into disrepair. Suggested solutions – replace the standard microSD card with a USB HDD. The solution is simple, reliable, plus the speed of reading/writing increases. But why flash drives so quickly “die”? Can the same factors damage the hard drive? And so proceed!

A bit about flash drives

Flash drives have a limited number of recording cycles. So if Linux is constantly something to write on it – it can break down prematurely. Hard drives work on a different principle. But for them, small read/write operations are also not very useful. The head of the hard drive can be parked if it is not used for a certain period of time, this can be done by the system, and can also be stored in the iron. In the case of a small read / write operations, the hard disk can not permanently park the head (not critical) or park the head, and then starts something to read/write (that’s worse).

Information is written in pages. The usual page size is 4 KB. So if we want to write one byte, either it will fit into the buffer, or it will be written 4 KB. If the buffer is full, then the page is recorded and further recording goes to the next page, which is in RAM.

Recording of small portions of information is used when journaling a file system (it allows not to lose information in case of a failure, for example, power off), as well as logging functions (save information about particular system events or program events). In raspberry, logging is performed by Syslog

In Raspbian, there are two daemons that can be disabled to reduce the load on the root storage medium. Having disabled them you can win in the recording operations, but lose in the reliability and recovery possibilities. This is the file system journaling daemon and Syslog. So if there are valuable data that will be stored in the root disk, then the logging service should not be disabled. If raspberry is used as a toy or there is an opportunity to quickly replace the main disk, file system logging can be disabled. About logging is similar, if you do not know why it is needed, then it suffices to say that if something does not work, then the malfunction must be looked for initially in the logs. If you want to ask a question to someone on the forum, they will most likely be asked to drop the logs. So if everything works or it’s easier for you to reinstall the system than to figure out what’s “poured” – you can safely turn off the logs.

You can use the top utility to view the access to the drives. To do this, run it with the following parameters:

 

sudo iotop -o -a

Installation iotop

sudo apt-get install iotop

Disabling Logging

To disable logging, open:

sudo nano /etc/rsyslog.conf

And comment in it two strokes in the MODULES section:

$ModLoad imuxsock # provides support for local system logging
$ModLoad imklog   # provides kernel logging support

It should look like this:


#  /etc/rsyslog.conf    Configuration file for rsyslog.
#
#            For more information see
#            /usr/share/doc/rsyslog-doc/html/rsyslog_conf.html


#################
#### MODULES ####
#################

#$ModLoad imuxsock # provides support for local system logging
#$ModLoad imklog   # provides kernel logging support
#$ModLoad immark  # provides --MARK-- message capability

# provides UDP syslog reception
#$ModLoad imudp
#$UDPServerRun 514

# provides TCP syslog reception
#$ModLoad imtcp
#$InputTCPServerRun 514


###########################
#### GLOBAL DIRECTIVES ####
###########################

#
# Use traditional timestamp format.
# To enable high precision timestamps, comment out the following line.
#
$ActionFileDefaultTemplate RSYSLOG_TraditionalFileFormat

#
# Set the default permissions for all log files.
#
$FileOwner root
$FileGroup adm
$FileCreateMode 0640
$DirCreateMode 0755
$Umask 0022

#
# Where to place spool and state files
#
$WorkDirectory /var/spool/rsyslog

#
# Include all config files in /etc/rsyslog.d/
#
$IncludeConfig /etc/rsyslog.d/*.conf


###############
#### RULES ####
###############

#
# First some standard log files.  Log by facility.
#
auth,authpriv.*            /var/log/auth.log
*.*;auth,authpriv.none        -/var/log/syslog
#cron.*                /var/log/cron.log
daemon.*            -/var/log/daemon.log
kern.*                -/var/log/kern.log
lpr.*                -/var/log/lpr.log
mail.*                -/var/log/mail.log
user.*                -/var/log/user.log

#
# Logging for the mail system.  Split it up so that
# it is easy to write scripts to parse these files.
#
mail.info            -/var/log/mail.info
mail.warn            -/var/log/mail.warn
mail.err            /var/log/mail.err

#
# Logging for INN news system.
#
news.crit            /var/log/news/news.crit
news.err            /var/log/news/news.err
news.notice            -/var/log/news/news.notice

#
# Some "catch-all" log files.
#
*.=debug;\
    auth,authpriv.none;\
    news.none;mail.none    -/var/log/debug
*.=info;*.=notice;*.=warn;\
    auth,authpriv.none;\
    cron,daemon.none;\
    mail,news.none        -/var/log/messages

#
# Emergencies are sent to everybody logged in.
#
*.emerg                :omusrmsg:*

#
# I like to have messages displayed on the console, but only on a virtual
# console I usually leave idle.
#
#daemon,mail.*;\
#    news.=crit;news.=err;news.=notice;\
#    *.=debug;*.=info;\
#    *.=notice;*.=warn    /dev/tty8

# The named pipe /dev/xconsole is for the `xconsole' utility.  To use it,
# you must invoke `xconsole' with the `-file' option:
# 
#    $ xconsole -file /dev/xconsole [...]
#
# NOTE: adjust the list below, or you'll go crazy if you have a reasonably
#      busy site..
#
daemon.*;mail.*;\
    news.err;\
    *.=debug;*.=info;\
    *.=notice;*.=warn    |/dev/xconsole

After reboot, the system will stop logging. We check:

sudo iotop -o -a

The output is no longer R Syslog.

Disabling logging

There is a small complexity – the partition should be unmounted. For this you can use:

  • Another computer with Linux
  • Another drive as the main (for example, a regular USB flash drive)
  • Another computer + LifeCD with Linux

In the case when you have a microSD card – the partition on which the system stands will be defined as mmcblk0p2.

Here mmcblk0 is the card itself, and p2 is the second partition on it (the first one is the default boot). If you are using a USB drive, then the partition is defined as sda2. If you are booting from another computer, it’s more likely to be defined as sdb2.

Further, we carry out:

sudo umount /dev/sdb2
sudo tune2fs -O ^has_journal /dev/sdb2
sudo e2fsck -f /dev/sdb2

After that we check:

dmesg | grep EXT4

Gives out:

[ 5890.967580] EXT4-fs (sdb2): mounted filesystem without journal. Opts: (null)

Download from our drive, enter:

sudo iotop -o -a

And we see that only LXDE accesses the hard disk occasionally. In view of the fact that these appeals are not systematic (we will observe 5-10 minutes), we can say that the load on the drive has decreased almost to zero.