Installation Raspberry Pi OS - Version serveur - Optimisation temps d'amorçage et consommation

Mise à jour : Raspi OS 10.6

Ce document fait suite à "Installation Raspberry Pi OS - Version serveur" et décrit différentes mesures permettant de réduire la consommation, le temps de démarrage ou la charge CPU d'un serveur RPi

Références : Documentation Raspberry

Sommaire

Avertissement

1 - Désactivation Bluetooth

$ sudo dmesg |grep Bluetooth
[ 13.414306] Bluetooth: Core ver 2.22
[ 13.414407] Bluetooth: HCI device and connection manager initialized
[ 13.414431] Bluetooth: HCI socket layer initialized
[ 13.414449] Bluetooth: L2CAP socket layer initialized
[ 13.414476] Bluetooth: SCO socket layer initialized
[ 13.422270] Bluetooth: HCI UART driver ver 2.3
[ 13.422287] Bluetooth: HCI UART protocol H4 registered
[ 13.422366] Bluetooth: HCI UART protocol Three-wire (H5) registered
[ 13.422640] Bluetooth: HCI UART protocol Broadcom registered
[ 13.760981] Bluetooth: BNEP (Ethernet Emulation) ver 1.3
[ 13.760990] Bluetooth: BNEP filters: protocol multicast
[ 13.761003] Bluetooth: BNEP socket layer initialized

$ lsmod |grep bluetooth
bluetooth 393216 2 hci_uart,btbcm
ecdh_generic 16384 1 bluetooth
rfkill 32768 4 bluetooth,cfg80211

$ sudo systemctl status bluetooth
● bluetooth.service - Bluetooth service
Loaded: loaded (/lib/systemd/system/bluetooth.service; enabled; vendor pre>
Active: active (running) since Tue 2021-11-16 14:47:43 CET; 4min 2s ago
.....
$ sudo systemctl stop hciuart.service
$ sudo systemctl disable hciuart.service

$ sudo systemctl stop bluetooth.service
$ sudo systemctl disable bluetooth.service
# Disable Bluetooth
dtoverlay=disable-bt
$ cat /etc/modprobe.d/blacklist-bluetooth.conf
blacklist hci_uart
blacklist btbcm
blacklist btintel
blacklist bnep
blacklist bluetooth
$ sudo aptitude purge bluez bluez-firmware pi-bluetooth

2 - Suppression affichage et capture graphique

2.1 - Changement de la cible par défaut de systemd

$ systemctl get-default
graphical.target
$ sudo systemctl set-default multi-user.target
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.

$ systemctl get-default
multi-user.target
$ systemctl get-default
multi-user.target

2.2 - Désactivation du chargement des modules video

$ lsmod |grep -E "v3d|vc4|video|bcm2835|drm"
v3d 73728 0
gpu_sched 45056 1 v3d
bcm2835_isp 32768 0
bcm2835_codec 40960 0
v4l2_mem2mem 36864 1 bcm2835_codec
bcm2835_v4l2 45056 0
videobuf2_dma_contig 20480 2 bcm2835_isp,bcm2835_codec
bcm2835_mmal_vchiq 32768 3 bcm2835_isp,bcm2835_codec,bcm2835_v4l2
videobuf2_vmalloc 16384 1 bcm2835_v4l2
videobuf2_memops 16384 2 videobuf2_dma_contig,videobuf2_vmalloc
videobuf2_v4l2 32768 4 bcm2835_isp,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem
videobuf2_common 61440 5 bcm2835_isp,bcm2835_codec,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
snd_bcm2835 24576 0
videodev 249856 6 bcm2835_isp,bcm2835_codec,videobuf2_common,bcm2835_v4l2,v4l2_mem2mem,videobuf2_v4l2
vc_sm_cma 32768 2 bcm2835_isp,bcm2835_mmal_vchiq
mc 45056 6 bcm2835_isp,bcm2835_codec,videobuf2_common,videodev,v4l2_mem2mem,videobuf2_v4l2
vc4 274432 4
cec 49152 1 vc4
drm_kms_helper 241664 2 vc4
snd_soc_core 225280 2 vc4,snd_soc_hdmi_codec
snd_pcm 110592 5 snd_compress,snd_pcm_dmaengine,snd_soc_hdmi_codec,snd_bcm2835,snd_soc_core
snd 77824 6 snd_compress,snd_soc_hdmi_codec,snd_timer,snd_bcm2835,snd_soc_core,snd_pcm
drm 528384 5 v3d,vc4,gpu_sched,drm_kms_helper
drm_panel_orientation_quirks 16384 1 drm
backlight 20480 1 drm
....
$ cat /etc/modprobe.d/blacklist-video.conf
blacklist v3d
blacklist bcm2835_isp
blacklist bcm2835_codec
blacklist bcm2835_v4l2
blacklist v4l2_mem2mem
blacklist vc_sm_cma
blacklist videodev
blacklist vc4
blacklist drm
$ lsmod |grep -E "v3d|vc4|video|bcm2835|drm"
snd_bcm2835 24576 0
snd_pcm 110592 1 snd_bcm2835
snd 77824 3 snd_timer,snd_bcm2835,snd_pcm
$ ls /dev/fb*
ls: impossible d'accéder à '/dev/fb*': Aucun fichier ou dossier de ce type

$ sudo aptitutde install fbset
$ fbset
open /dev/fb0: No such file or directory

$ sudo dmesg |grep -E "fb|frame buffer"
$ tvservice -s
tvservice is not supported when using the vc4-kms-v3d driver.
....

$ modetest -p |grep -v failed
no device found
# camera_auto_detect=1
# display_auto_detect=1
# disable_overscan
# dtoverlay=vc4-kms-v3d
dtoverlay=vc4-fkms-v3d
max_framebuffers=1

ne pas la supprimer, pour éviter la génération d'une erreur dans dmesg

[    1.207582] bcm2708_fb soc:fb: Unable to determine number of FBs. Disabling driver.
[ 1.207633] bcm2708_fb: probe of soc:fb failed with error -2

2. 3 - Réduction de la mémoire allouée au gpu

# Réduction taille mémoire partagée pour gpu 16M
gpu_ram=16
$ free -h
total used free shared buff/cache available
Mem: 1,8Gi 33Mi 1,7Gi 16Mi 77Mi 1,7Gi
Swap: 0B 0B 0B

3 - Désactivation console et entrées

$ cat /boot/cmdline
root=PARTUUID=461ca018-02 rootfstype=ext4 fsck.repair=yes rootwait loglevel=1 logo.nologo
$ cat /proc/cmdline 
coherent_pool=1M 8250.nr_uarts=1 snd_bcm2835.enable_compat_alsa=0 snd_bcm2835.enable_hdmi=1 smsc95xx.macaddr=DC:A6:32:C9:70:EB vc_mem.mem_base=0x3ec00000 vc_mem.mem_size=0x40000000 \
root=PARTUUID=8acef004-02 rootfstype=ext4 fsck.repair=yes rootwait loglevel=1 logo.nologo
# Désactivation de la gestion du clavier
$ sudo systemctl disable keyboard-setup.service
# Désactivation du démon de lecture des /dev/input
$ sudo systemctl disable triggerhappy.service

4 - Désactivation audio

$ aplay -l
**** Liste des Périphériques Matériels PLAYBACK ****
carte 0: Headphones [bcm2835 Headphones], périphérique 0: bcm2835 Headphones [bcm2835 Headphones]
Sous-périphériques: 8/8
Sous-périphérique #0: subdevice #0
Sous-périphérique #1: subdevice #1
Sous-périphérique #2: subdevice #2
Sous-périphérique #3: subdevice #3
Sous-périphérique #4: subdevice #4
Sous-périphérique #5: subdevice #5
Sous-périphérique #6: subdevice #6
Sous-périphérique #7: subdevice #7

$ lsmod |grep snd
snd_bcm2835 24576 0
snd_pcm 110592 1 snd_bcm2835
snd_timer 32768 1 snd_pcm
snd 77824 3 snd_timer,snd_bcm2835,snd_pcm
$ cat /etc/modprobe.d/blacklist-audio.conf
blacklist snd_bcm2835
# Enable audio (loads snd_bcm2835)
# dtparam=audio=on
$ aplay -l
aplay: device_list:272: aucune carte son n'a été trouvée...

$ lsmod |grep snd

5 - Désactivation des leds PWR et ACT

[pi4]

# Disable the PWR LED
dtparam=pwr_led_trigger=none
dtparam=pwr_led_activelow=off

# Disable the Activity LED
dtparam=act_led_trigger=none
dtparam=act_led_activelow=off

6 - Ethernet

6.1 - Suppression adresses ipv6

$ ip add show wlan0 |grep inet6
inet6 fe80::84e2:59d:fba6:4d2f/64 scope link

$ sysctl net.ipv6.conf.all.disable_ipv6
net.ipv6.conf.all.disable_ipv6 = 0
$ tail -2 /etc/sysctl.conf
# Disable ipv6
net.ipv6.conf.all.disable_ipv6 = 1
$ tail - 4 /etc/dhcpcd
#ipv4 only, no ipv6, no arping
noarp
noipv6
ipv4only
$ cat /etc/modprobe.d/blacklist-ipv6.conf
blacklist ipv6
$ sudo reboot
$ lsmod |grep ipv6

$ ip add show |grep inet6

6.3 - Adresse ip fixe

6.4 - Désactivation dhcp pour interface non utilisée

$ tail -2 /etc/dhcpcd.conf
#disable eth0 getting dhcp
denyinterfaces eth0
$ sudo reboot
$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST> mtu 1500 qdisc noop state DOWN mode DEFAULT group default qlen 1000
link/ether dc:a6:32:c9:70:eb brd ff:ff:ff:ff:ff:ff

$ ip route |grep eth0

6.5 - Prévention mode sommeil du wifi

$ iw wlan0 get power_save
Power save: on
$ sudo iwconfig wlan0 power off

$ iw wlan0 get power_save
Power save: off
iwconfig wlan0 power off

dans le fichier /etc/rc.local, juste avant la dernière ligne exit 0

$ tail -n 2 /etc/rc.local
iwconfi wlan0 power off
exit 0

7 - Systemd

7.1 - Désactivation d'un service

# Exemple : Affichage du fichier du service keyboard-setup.service
$ systemctl cat keyboard-setup
# /lib/systemd/system/keyboard-setup.service
[Unit]
Description=Set the console keyboard layout
DefaultDependencies=no
Before=local-fs-pre.target
Wants=local-fs-pre.target
ConditionPathExists=/bin/setupcon

[Service]
Type=oneshot
ExecStart=/lib/console-setup/keyboard-setup.sh
RemainAfterExit=yes

[Install]
WantedBy=sysinit.target
# Dans cet exemple, le service n'est pas un service static
$ systemctl is-enabled keyboard-setup
enabled

# Dans cet autre exemple, le service est static
$ systemctl status apt-daily.service |grep static
static
# Désactivation d'un service "enabled"
$ sudo systemctl disable keyboard-setup
Removed /etc/systemd/system/sysinit.target.wants/keyboard-setup.servicet

# Désactivation d'un service "static"
$ sudo systemctl mask apt-daily.service
Created symlink /etc/systemd/system/apt-daily.service → /dev/null.
# Cas d'un service non static
$ systemctl is-enabled keyboard-setup
disabled

# Cas d'un service static
$ systemctl is-enabled apt-daily.service
masked
# Cas d'un service non static
$ sudo systemctl enable keyboard-setup

# Cas d'un service static
$ sudo systemctl unmask apt-daily-service

7.2 - Liste de services pouvant être désactivés

# Désactivation du passage du gouverneur du CPU de conservative à ondemand
# Evite les pics de consommation au prix d'une moindre réactivité du processeur
$ sudo systemctl disable raspi-config.service

# Désactivation du fichier de swap, pour limiter les accès à la carte SD
$ sudo systemctl disable dphys-swapfile.service

# Désactivation de la mise à jour quotidienne de apt, sera à faire manuellement
$ sudo systemctl mask apt-daily.service

# Désactivation de l'installation quotidienne des paquets par apt, sera à faire manuellement
$ sudo systemctl mask apt-daily-upgrade

# Désactivation de la recherche et mise à jour automatique de l'eeprom, sera à faire manuellement
$ sudo systemctl disable rpi-eeprom-update.service
$ free -h
total utilisé libre partagé tamp/cache disponible
Mem: 1,8Gi 35Mi 1,7Gi 0,0Ki 73Mi 1,7Gi
Partition d'échange: 0B 0B 0B
$ systemctl is-enabled dphys-swapfile
disabled

$ systemctl is-enabled apt-daily
masked

$ systemctl is-enabled apt-daily-upgrade
masked

$ systemctl is-enabled rpi-eeprom-update
disabled

....

8 - Désactivation des hubs usb

8.1 - Installation

$ aptitude versions uhubctl
p 2.3.0-1 stable 500
$ wget http://ftp.us.debian.org/debian/pool/main/u/uhubctl/uhubctl_2.4.0-1_armhf.deb
$ aptitude versions libc6
p 2.31-13+rpi1+deb11u2 stable 500
i 2.31-13+rpt2+rpi1 stable 500
$ aptitude versions libusb-1.0-0
i A 2:1.0.24-3 stable 500
$ sudo dpkg -i uhubctl_2.4.0-1_armhf.deb 

$ uhubctl -v
2.4.0-1

8.2 - Test

$ lsusb
Bus 002 Device 001: ID 1d6b:0003 Linux Foundation 3.0 root hub
Bus 001 Device 003: ID 2109:3431 VIA Labs, Inc. Hub
Bus 001 Device 001: ID 1d6b:0002 Linux Foundation 2.0 root hub

$ lsusb -t
/: Bus 02.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/4p, 5000M
/: Bus 01.Port 1: Dev 1, Class=root_hub, Driver=xhci_hcd/1p, 480M
|__ Port 1: Dev 3, If 0, Class=Hub, Driver=hub/4p, 480M

$ sudo uhubctl
Current status for hub 2 [1d6b:0003 Linux 5.10.78-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports, ppps]
Port 1: 02a0 power 5gbps Rx.Detect
Port 2: 02a0 power 5gbps Rx.Detect
Port 3: 02a0 power 5gbps Rx.Detect
Port 4: 02a0 power 5gbps Rx.Detect
Current status for hub 1-1 [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
Port 1: 0100 power
Port 2: 0100 power
Port 3: 0100 power
Port 4: 0100 power
Current status for hub 1 [1d6b:0002 Linux 5.10.78-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 2.00, 1 ports, ppps]
Port 1: 0507 power highspeed suspend enable connect [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]

8.2 - Test

$ sudo uhubctl -l 1-1 -a 0
Current status for hub 2 [1d6b:0003 Linux 5.10.63-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports, ppps]
Port 1: 02a0 power 5gbps Rx.Detect
Port 2: 02a0 power 5gbps Rx.Detect
Port 3: 02a0 power 5gbps Rx.Detect
Port 4: 02a0 power 5gbps Rx.Detect
Sent power off request
New status for hub 2 [1d6b:0003 Linux 5.10.63-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports, ppps]
Port 1: 0080 off
Port 2: 0080 off
Port 3: 0080 off
Port 4: 0080 off
Current status for hub 1-1 [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
Port 1: 0100 power
Port 2: 0100 power
Port 3: 0100 power
Port 4: 0100 power
Sent power off request
New status for hub 1-1 [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
Port 1: 0000 off
Port 2: 0000 off
Port 3: 0000 off
Port 4: 0000 off

# Équivalent à
$ sudo uhubctl -l 2 -a 0
$ watch -n 3 "sudo dmesg |tail -2"
Toutes les 3,0s: sudo dmesg |tail -2 raspi: Tue Nov 16 21:54:13 2021

[ 6.063732] random: 7 urandom warning(s) missed due to ratelimiting
[ 6.121803] brcmfmac: brcmf_cfg80211_set_power_mgmt: power save enabled
$ ip link show eth0
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP mode DEFAULT group default qlen 1000
link/ether dc:a6:32:c9:70:eb brd ff:ff:ff:ff:ff:ff

$ ip link show wlan0
3: wlan0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc pfifo_fast state UP mode DORMANT group default qlen 1000
link/ether dc:a6:32:c9:70:ec brd ff:ff:ff:ff:ff:ff
$ tail -2 /etc/rc.local
uhubctl -l 1-1 -a 0
exit 0
$ sudo uhubctl -l 1-1
Current status for hub 2 [1d6b:0003 Linux 5.10.63-v7l+ xhci-hcd xHCI Host Controller 0000:01:00.0, USB 3.00, 4 ports, ppps]
Port 1: 0080 off
Port 2: 0080 off
Port 3: 0080 off
Port 4: 0080 off
Current status for hub 1-1 [2109:3431 USB2.0 Hub, USB 2.10, 4 ports, ppps]
Port 1: 0000 off
Port 2: 0000 off
Port 3: 0000 off
Port 4: 0000 off
$ sudo uhubctl -l 1-1 -a 1

9 - Bilan

$ cat /boot/config.txt
# For more options and information see
# http://rpf.io/configtxt
# Some settings may impact device functionality. See link above for details

# Additional overlays and parameters are documented /boot/overlays/README

# Enable audio (loads snd_bcm2835)
#dtparam=audio=on

# Automatically load overlays for detected cameras
#camera_auto_detect=1

# Automatically load overlays for detected DSI displays
#display_auto_detect=1

# Enable DRM VC4 V3D driver
dtoverlay=vc4-kms-v3d
max_framebuffers=1

# Disable compensation for displays with overscan
#disable_overscan=1

[pi4]
# Run as fast as firmware / board allows
arm_boost=1

[all]
dtoverlay=disable-bt
gpu_ram=16
$ cat /boot/cmdline.txt 
root=PARTUUID=8acef004-02 rootfstype=ext4 fsck.repair=yes rootwait loglevel=1 logo.nologo
$ lsmod |wc -l
18

$ lsmod
Module Size Used by
8021q 32768 0
garp 16384 1 8021q
stp 16384 1 garp
llc 16384 2 garp,stp
raspberrypi_hwmon 16384 0
brcmfmac 327680 0
brcmutil 20480 1 brcmfmac
cfg80211 761856 1 brcmfmac
rfkill 32768 4 cfg80211
i2c_brcmstb 16384 0
rpivid_mem 16384 0
nvmem_rmem 16384 0
uio_pdrv_genirq 16384 0
uio 20480 1 uio_pdrv_genirq
fuse 126976 1
ip_tables 28672 0
x_tables 32768 1 ip_tables
$ sudo dmesg -kl warn
[ 0.162693] usb_phy_generic phy: supply vcc not found, using dummy regulator
[ 1.470319] mmc1: queuing unknown CIS tuple 0x80 (2 bytes)
[ 1.472023] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[ 1.473728] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
[ 1.476802] mmc1: queuing unknown CIS tuple 0x80 (7 bytes)
[ 1.478506] mmc1: queuing unknown CIS tuple 0x80 (3 bytes)
$ systemctl --all --failed
0 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'
$ systemd-analyze
Startup finished in 2.013s (kernel) + 10.901s (userspace) = 12.915s
multi-user.target reached after 10.822s in userspace
$ systemd-analyze blame
7.898s dhcpcd.service
2.757s systemd-random-seed.service
1.616s dev-mmcblk0p2.device
1.328s man-db.service
1.153s rc-local.service
568ms user@1000.service
....
$ systemd-analyze critical-chain 
The time when unit became active or started is printed after the "@" character.
The time the unit took to start is printed after the "+" character.

multi-user.target @11.775s
└─dhcpcd.service @3.875s +7.898s
└─basic.target @3.811s
└─sockets.target @3.810s
└─triggerhappy.socket @3.810s
└─sysinit.target @3.794s
└─systemd-timesyncd.service @11.567s +141ms
└─systemd-tmpfiles-setup.service @3.287s +215ms
└─local-fs.target @3.251s
└─boot.mount @3.218s +31ms
└─systemd-fsck@dev-disk-by\x2dpartuuid-8acef004\x2d01.service @2.732s +477ms
└─dev-disk-by\x2dpartuuid-8acef004\x2d01.device @2.666s

$ free -h
total used free shared buff/cache available
Mem: 1,8Gi 37Mi 1,7Gi 16Mi 77Mi 1,7Gi
Swap: 0B 0B 0B