Raspberry Pi 3 Model B の起動時間の高速化


ラズパイを使ってIoTデバイスを作りテストを行っている。
PoCに耐えきれるかテストするため、電源のON/OFFテスト1000回や、周辺デバイスの調子が悪い環境での動作テスト1000回ノック等をやっている。
こうも起動回数が増えてくると、起動時間の長さが気になってきたので、高速化を検討した。

結果(今回の場合だと) 起動時間 __10秒__ が __5秒__に__短縮__されたので、備忘録を兼ねて記事にした。

## ラズパイの起動時間を調べる

現在の状態での起動時間を調べる。

現状10秒かかっている。

```bash

$ systemd-analyze time

Startup finished in 2.117s (kernel) + 8.655s (userspace) = 10.772s

$ systemd-analyze blame

          5.020s hciuart.service
          3.711s apt-daily.service
          1.464s apt-daily-upgrade.service
          1.446s dev-mmcblk0p2.device
           617ms keyboard-setup.service
           505ms raspi-config.service
           449ms networking.service
           424ms dphys-swapfile.service
           416ms ssh.service
           406ms systemd-fsck@dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.service
           343ms systemd-timesyncd.service
           326ms rsyslog.service
           280ms systemd-udev-trigger.service
           280ms systemd-udevd.service
           277ms wpa_supplicant.service
           230ms dhcpcd.service
           223ms systemd-fsck-root.service
           221ms systemd-tmpfiles-setup-dev.service
           218ms systemd-logind.service
           175ms avahi-daemon.service
           169ms plymouth-start.service
           168ms systemd-journald.service
           151ms sys-kernel-debug.mount
           130ms run-rpc_pipefs.mount
           125ms fake-hwclock.service
           125ms systemd-remount-fs.service
           122ms triggerhappy.service
           120ms dev-mqueue.mount
           120ms kmod-static-nodes.service
           120ms systemd-journal-flush.service
           113ms bluetooth.service
           107ms systemd-rfkill.service
           105ms systemd-random-seed.service
            90ms wifi-country.service
            84ms systemd-tmpfiles-setup.service
            81ms user@1000.service
            81ms systemd-modules-load.service
            71ms systemd-sysctl.service
            66ms alsa-restore.service
            64ms systemd-update-utmp.service
            56ms sys-kernel-config.mount
            52ms systemd-update-utmp-runlevel.service
            52ms console-setup.service
            49ms boot.mount
            49ms nfs-config.service
            44ms rc-local.service
            33ms plymouth-read-write.service
            30ms plymouth-quit.service
            30ms plymouth-quit-wait.service
            21ms systemd-user-sessions.service

$ systemd-analyze critical-chain
The time after the unit is active or started is printed after the "@" character.
The time the unit takes to start is printed after the "+" character.

graphical.target @8.494s
└─multi-user.target @8.493s
  └─hciuart.service @3.470s +5.020s
    └─basic.target @3.275s
      └─sockets.target @3.265s
        └─triggerhappy.socket @3.265s
          └─sysinit.target @3.256s
            └─systemd-timesyncd.service @2.908s +343ms
              └─systemd-tmpfiles-setup.service @2.802s +84ms
                └─local-fs.target @2.755s
                  └─boot.mount @2.701s +49ms
                    └─systemd-fsck@dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.service @2.278s +406ms
                      └─dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.device @2.274s


```

サービスの起動を追っていくと、
__hciuart.service__ が__5秒__もかかっているぞ。



```bash

$ systemctl | grep running

init.scope                        loaded active running   System and Service Manager                                                            
session-c4.scope                  loaded active running   Session c4 of user pi                                                                 
session-c5.scope                  loaded active running   Session c5 of user pi                                                                 
avahi-daemon.service              loaded active running   Avahi mDNS/DNS-SD Stack                                                               
bluetooth.service                 loaded active running   Bluetooth service                                                                     
cron.service                      loaded active running   Regular background program processing daemon                                          
dbus.service                      loaded active running   D-Bus System Message Bus                                                              
dhcpcd.service                    loaded active running   dhcpcd on all interfaces                                                              
getty@tty1.service                loaded active running   Getty on tty1                                                                         
hciuart.service                   loaded active running   Configure Bluetooth Modems connected by UART                                          
rsyslog.service                   loaded active running   System Logging Service                                                                
ssh.service                       loaded active running   OpenBSD Secure Shell server                                                           
systemd-journald.service          loaded active running   Journal Service                                                                       
systemd-logind.service            loaded active running   Login Service                                                                         
systemd-timesyncd.service         loaded active running   Network Time Synchronization                                                          
systemd-udevd.service             loaded active running   udev Kernel Device Manager                                                            
triggerhappy.service              loaded active running   triggerhappy global hotkey daemon                                                     
user@1000.service                 loaded active running   User Manager for UID 1000                                                             
wpa_supplicant.service            loaded active running   WPA supplicant                                                                        
avahi-daemon.socket               loaded active running   Avahi mDNS/DNS-SD Stack Activation Socket                                             
dbus.socket                       loaded active running   D-Bus System Message Bus Socket                                                       
syslog.socket                     loaded active running   Syslog Socket                                                                         
systemd-journald-dev-log.socket   loaded active running   Journal Socket (/dev/log)                                                             
systemd-journald.socket           loaded active running   Journal Socket                                                                        
systemd-udevd-control.socket      loaded active running   udev Control Socket                                                                   
systemd-udevd-kernel.socket       loaded active running   udev Kernel Socket                                                                    
triggerhappy.socket               loaded active running   triggerhappy.socket                                                                   
                                                              

```

どうも
__Bluetooth関連__のサービスである。
今回、Bluetoothは不要なので、__bluetooth.service__ と一緒に停止する

```bash
bluetooth.service                 loaded active running   Bluetooth service                                                                     
hciuart.service                   loaded active running   Configure Bluetooth Modems connected by UART                                          
```


## bluetooth関連サービスの停止

BT関連のサービスを2つ停止

```bash

sudo systemctl disable bluetooth.service
sudo systemctl disable hciuart.service
```

再起動後の起動時間計測結果

```bash

$ systemd-analyze time

Startup finished in 1.842s (kernel) + 4.153s (userspace) = 5.995s

$ systemd-analyze blame

          1.353s dev-mmcblk0p2.device
           648ms keyboard-setup.service
           625ms dphys-swapfile.service
           433ms systemd-fsck@dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.service
           411ms networking.service
           367ms ssh.service
           341ms systemd-timesyncd.service
           295ms rsyslog.service
           292ms systemd-udev-trigger.service
           261ms wpa_supplicant.service
           253ms systemd-udevd.service
           250ms systemd-fsck-root.service
           240ms raspi-config.service
           216ms kmod-static-nodes.service
           212ms systemd-tmpfiles-setup-dev.service
           204ms plymouth-start.service
           196ms dhcpcd.service
           174ms avahi-daemon.service
           172ms dev-mqueue.mount
           161ms fake-hwclock.service
           158ms systemd-journald.service
           142ms run-rpc_pipefs.mount
           138ms systemd-modules-load.service
           131ms sys-kernel-debug.mount
           128ms systemd-logind.service
           123ms systemd-remount-fs.service
           113ms systemd-journal-flush.service
            99ms triggerhappy.service
            95ms systemd-tmpfiles-setup.service
            91ms user@1000.service
            90ms alsa-restore.service
            83ms systemd-random-seed.service
            77ms systemd-update-utmp.service
            65ms systemd-rfkill.service
            60ms wifi-country.service
            46ms systemd-sysctl.service
            44ms plymouth-read-write.service
            36ms console-setup.service
            33ms plymouth-quit-wait.service
            33ms rc-local.service
            32ms boot.mount
            32ms plymouth-quit.service
            31ms systemd-user-sessions.service
            31ms sys-kernel-config.mount
            30ms nfs-config.service
            28ms systemd-update-utmp-runlevel.service


$ systemd-analyze critical-chain

graphical.target @4.117s
└─multi-user.target @4.117s
  └─ssh.service @3.749s +367ms
    └─network.target @3.724s
      └─wpa_supplicant.service @3.462s +261ms
        └─dbus.service @3.278s
          └─basic.target @3.257s
            └─sockets.target @3.253s
              └─triggerhappy.socket @3.249s
                └─sysinit.target @3.230s
                  └─systemd-timesyncd.service @2.886s +341ms
                    └─systemd-tmpfiles-setup.service @2.773s +95ms
                      └─local-fs.target @2.756s
                        └─boot.mount @2.718s +32ms
                          └─systemd-fsck@dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.service @2.275s +433ms
                            └─dev-disk-by\x2dpartuuid-8fadf6a9\x2d01.device @2.271s
```

### 結果

```bash
// 前
Startup finished in 2.117s (kernel) + 8.655s (userspace) = 10.772s
// 後
Startup finished in 1.842s (kernel) + 4.153s (userspace) = 5.995s
```

起動時間が10秒から5秒に短縮されました。


## (おまけ) 起動時のメッセージを抑制


ほとんど早くなりませんが。起動を目視していると、表示項目が少し減るので、気持ち早く感じました。

/boot/cmdline.txtの末尾に「quiet」を追加して、起動時のメッセージを抑制します。
[参考](http://nw-electric.way-nifty.com/blog/2017/04/raspberry-pi-ze.html)



```bash

$ systemd-analyze time
Startup finished in 1.464s (kernel) + 3.909s (userspace) = 5.373s
```

以上