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

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 - Désactivation des hubs usb

7.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

7.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]

7.3 - 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

8 - Bilan

8.1 - Divers

$ 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'
$ free -h
total used free shared buff/cache available
Mem: 1,8Gi 37Mi 1,7Gi 16Mi 77Mi 1,7Gi
Swap: 0B 0B 0B

8.2 - Analyse du temps de démarrage

$ systemd-analyze
Startup finished in 2.243s (kernel) + 19.601s (userspace) = 21.844s
multi-user.target reached after 17.640s in userspace
$ systemd-analyze blame
13.770s dhcpcd.service
2.793s systemd-random-seed.service
1.946s man-db.service
1.456s dev-mmcblk0p2.device
1.127s rc-local.service
559ms systemd-journal-flush.service
545ms systemd-udev-trigger.service
513ms networking.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 @17.640s
└─dhcpcd.service @3.869s +13.770s
└─basic.target @3.808s
└─sockets.target @3.808s
└─triggerhappy.socket @3.807s
└─sysinit.target @3.792s
└─systemd-update-utmp.service @3.455s +131ms
└─systemd-tmpfiles-setup.service @3.281s +141ms
└─local-fs.target @3.208s
└─boot.mount @3.140s +66ms
└─systemd-fsck@dev-disk-by\x2dpartuuid-8acef004\x2d01.service> @2.646s +521ms
└─dev-disk-by\x2dpartuuid-8acef004\x2d01.device @2.640s
(pc) $ systemd-analyze plot -H pi@raspi.local >plot.svg
(pc) $ gthumb raspiplot.svg
(pc) $ sudo aptitude install librsvg2-bin

(pc) $ rsvg-convert -o plot.png plot.svg
(pc) $ rsvg-convert -f pdf -o plot.pdf plot.svg

plot.svg

On note que certains services périodiques (lograte, man-db, mlocate, ...) sont lancés après que la cible multi-user soit atteinte et retardent la disponibilité du système

end of plot

9 - Désactivation de services systemd

$ 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

9.1 - Services lancés à chaque amorçage

$ systemctl list-unit-files --type=service --state=enabled
UNIT FILE STATE VENDOR PRESET
avahi-daemon.service enabled enabled
console-setup.service enabled enabled
cron.service enabled enabled
dhcpcd.service enabled enabled
e2scrub_reap.service enabled enabled
fake-hwclock.service enabled enabled
getty@.service enabled enabled
networking.service enabled enabled
raspberrypi-net-mods.service enabled enabled
rpi-display-backlight.service enabled enabled
rsync.service enabled enabled
rsyslog.service enabled enabled
ssh.service enabled enabled
sshswitch.service enabled enabled
systemd-pstore.service enabled enabled
systemd-timesyncd.service enabled enabled
wpa_supplicant.service enabled enabled

17 unit files listed.
# Désactivation du démon de lecture des /dev/input
$ sudo systemctl disable triggerhappy.service
$ sudo systemctl disable keyboard-setup
$ sudo systemctl disable console-setup
$ sudo systemctl disable rpi-display-backlight
$ sudo systemctl disable raspi-config.service
$ sudo systemctl disable dphys-swapfile.service
$ sudo systemctl disable rpi-eeprom-update.service
$ sudo systemctl disable triggerhappy keyboard-setup console-setup rpi-display-backlight raspi-config dphys-swapfile rpi-eeprom-update
$ systemctl list-units --type service --state=enabled
$ analyze-blame 

9.2 - Services périodiques

$ systemctl list-unit-files --type timer
UNIT FILE STATE VENDOR PRESET
apt-daily-upgrade.timer disabled enabled
apt-daily.timer disabled enabled
e2scrub_all.timer enabled enabled
fstrim.timer disabled enabled
logrotate.timer enabled enabled
man-db.timer disabled enabled
mlocate.timer disabled enabled
sysstat-collect.timer disabled enabled
sysstat-summary.timer disabled enabled
systemd-tmpfiles-clean.timer static -

10 unit files listed.
$ systemctl list-timers --all
NEXT LEFT LAST PASSED UNIT ACTIVATES
Mon 2021-11-22 00:00:00 CET 5h 46min left Sun 2021-11-21 15:04:33 CET 3h 9min ago logrotate.timer logrotate.service
Mon 2021-11-22 00:00:00 CET 5h 46min left Sun 2021-11-21 15:04:33 CET 3h 9min ago man-db.timer man-db.service
Mon 2021-11-22 00:00:00 CET 5h 46min left Sun 2021-11-21 15:04:33 CET 3h 9min ago mlocate.timer mlocate.service
Mon 2021-11-22 00:30:07 CET 6h left Mon 2021-11-15 18:26:12 CET 5 days ago fstrim.timer fstrim.service
Mon 2021-11-22 15:19:51 CET 21h left Sun 2021-11-21 15:19:51 CET 2h 53min ago systemd-tmpfiles-clean.timer systemd-tmpfiles-clean.service
Sun 2021-11-28 03:10:57 CET 6 days left Sun 2021-11-21 15:04:59 CET 3h 8min ago e2scrub_all.timer e2scrub_all.service
n/a n/a n/a n/a apt-daily-upgrade.timer apt-daily-upgrade.service
n/a n/a n/a n/a apt-daily.timer apt-daily.service

8 timers listed.
$ sudo systemctl disable apt-daily.timer
$ sudo systemctl disable apt-daily-upgrade.timer
$ sudo systemctl disable fstrim.timer
$ sudo systemctl disable man-db.timer
$ sudo systemctl disable mlocate.timer
$ sudo systemctl disable apt-daily.timer apt-daily-upgrade.timer fstrim.timer man-db.timer mlocate.timer
$ systemctl list-timers --all
NEXT LEFT LAST PASSED UNIT AC>
Thu 2021-11-25 17:11:49 CET 11min left n/a n/a systemd-tmpfiles-clean.timer sy>
Fri 2021-11-26 00:00:00 CET 7h left Thu 2021-11-25 00:00:13 CET 16h ago logrotate.timer lo>
Sun 2021-11-28 03:10:06 CET 2 days left Sun 2021-11-21 15:04:59 CET 4 days ago e2scrub_all.timer e2>

3 timers listed.
$ systemd-analyze blame