问题

ubuntu系统每次启动都非常慢

根据开机画面判断是网络这步的问题,ubuntu系列用的是netplan来管理网卡的。


排查

aibox@ov:~$ systemd-analyze blame
2min 135ms systemd-networkd-wait-online.service                                                     
     708ms cloud-init-local.service                                                                 
     661ms systemd-udev-settle.service                                                              
     644ms cloud-init.service                                                                       
     627ms dev-mapper-ubuntu\x2d\x2dvg\x2dubuntu\x2d\x2dlv.device                                   
     536ms snap.lxd.activate.service                                                                
     518ms cloud-config.service                                                                     
     499ms cloud-final.service                                                                      
     426ms snapd.service                                                                            
     256ms accounts-daemon.service                                                                  
     241ms networkd-dispatcher.service                                                              
     230ms udisks2.service                                                                          
     223ms systemd-logind.service                                                                   
     160ms ModemManager.service                                                                     
     159ms polkit.service                                                                           
     148ms ssh.service                                                                              
     143ms swap.img.swap                                                                            
     141ms grub-common.service                                                                      
     138ms keyboard-setup.service

systemd-networkd-wait-online.service

$ networkctl
IDX LINK   TYPE     OPERATIONAL SETUP      
  1 lo     loopback carrier     unmanaged  
  2 enp2s0 ether    routable    configured 
  3 enp3s0 ether    no-carrier  configuring

3 links listed.

问题出在enp3s0它的状态是configuring,这个口没有使用。


解决

$ sudo systemctl disable systemd-networkd-wait-online.service # 无效

$ sudo vi /etc/netplan/00-installer-config.yaml # 有效但不完美
# This is the network config written by 'subiquity'
network:
  ethernets:
    enp2s0:
      dhcp4: true
      #    enp3s0:
      #      dhcp4: true
  version: 2

这个服务调用的是/lib/systemd/systemd-networkd-wait-online程序。

$ /lib/systemd/systemd-networkd-wait-online -h
systemd-networkd-wait-online [OPTIONS...]

Block until network is configured.

  -h --help                 Show this help
     --version              Print version string
  -q --quiet                Do not show status information
  -i --interface=INTERFACE[:MIN_OPERSTATE[:MAX_OPERSTATE]]
                            Block until at least these interfaces have appeared
     --ignore=INTERFACE     Don't take these interfaces into account
  -o --operational-state=MIN_OPERSTATE[:MAX_OPERSTATE]
                            Required operational state
     --any                  Wait until at least one of the interfaces is online
     --timeout=SECS         Maximum time to wait for network connectivity

See the systemd-networkd-wait-online.service(8) man page for details.
$ sudo vi /usr/lib/systemd/system/systemd-networkd-wait-online.service
[Service]
Type=oneshot
ExecStart=/lib/systemd/systemd-networkd-wait-online --timeout=3 # 设置超时时间
RemainAfterExit=yes

$ sudo reboot
$ systemd-analyze blame
3.124s systemd-networkd-wait-online.service