Upgrade to Pro — share decks privately, control downloads, hide ads and more …

今更聞けないsystemd - Now Beginning Systemd

nasa9084
August 31, 2019

今更聞けないsystemd - Now Beginning Systemd

builderscon tokyo 2019

nasa9084

August 31, 2019
Tweet

More Decks by nasa9084

Other Decks in Technology

Transcript

  1. What is “systemd”? 1. A “System and Service Manager” for

    Linux • An init system (like sysv/init) • A service manager 2. A software suite for Linux • Including 1.
  2. What is “systemd”? 1. A “System and Service Manager” for

    Linux • An init system • A service manager • Manages services as “unit” 2. A software suite for Linux • Including 1. • systemd(service manager), journald, systemd-tmpfiles, …
  3. Boot up process w/ systemd • Run /usr/lib/systemd/systemd as PID=1

    • Construct a dependency tree from default.target (target is a group of units) • Run dependencies concurrently
  4. How to configure systemd? • systemd is configured via plain-text

    files • Called “unit file” • Ini-style configuration file • There’re various type of units
  5. UZQF %FTDSJQUJPO TFSWJDF 4FSWJDFEBFNPONBOBHFNFOU TPDLFU 4PDLFUCBTFEBDUJWBUJPO MJLFYJOFUE UBSHFU (SPVQPGVOJUT EFWJDF

    TZTGTVEFWEFWJDFT EFWTEBEFWTEBEFWJDF NPVOU 'JMFTZTUFNNPVOUQPJOU GTUBCXJMMCFDPOWFSUFE BVUPNPVOU 'JMFTZTUFNBVUPNPVOUQPJOU GTUBCXJMMCFDPOWFSUFE TXBQ 4XBQEFWJDFTPSNFNPSZQBHJOH GTUBCXJMMCFDPOWFSUFE  UJNFS 5JNFS MJLFDSPOUBC QBUI 1BUINPOJUPSFECZTZTUFNE TDPQF (SPVQJOHXPSLFSQSPDFTTFTUPNBOBHFSFTPVSDFT TMJDF (SPVQJOH4FSWJDFTBOE4DPQFT Major unit types
  6. How to use systemd units? • Use systemctl to operate

    systemd units • Analyze system status • Use systemd units • Manage power
  7. Systemctl commands TUBUVT TIPXTZTUFNTUBUVT MJTUVOJUT TIPXMJTUPGSVOOJOHVOJUT TUBUVT6/*5 TIPXVOJUTUBUVT TUBSU6/*5 TUBSUVOJUJNNFEJBUFMZ

    TUPQ6/*5 TUPQVOJUJNNFEJBUFMZ SFTUBSU6/*5 SFTUBSUVOJU SFMPBE6/*5 SFMPBEVOJU FOBCMF6/*5 FOBCMFVOJUPOCPPU EJTBCMF6/*5 EJTBCMFVOJUPOCPPU EBFNPOSFMPBE SFMPBETZTUFNE SFCPPU SFCPPUTZTUFN QPXFSP⒎ TIVUEPXOBOEQPXFSP⒎TZTUFN
  8. Where is systemd unit files? • Basically placed in two

    places: • /usr/lib/systemd/system/ • Units provided by installed packages • /etc/systemd/system/ (primary) • Units installed by the system admin
  9. The structure of unit files • Unit files consists of

    three sections 1. [Unit] section • Generic information about the unit 2. [Install] section • Installation information for the unit 3. Type-specific section
  10. [Unit] Section %FTDSJQUJPO "IVNBOSFBEBCMFOBNFGPSUIFVOJU %PDVNFOUBUJPO 63*TSFGFSFODJOHEPDVNFOUBUJPO 8BOUT 3FRVJSFT %FQFOEFODZPOPUIFSVOJUT 6/*5@/".&XBOUTEJSIBTTBNFGVOD

    $POqJDUT /FHBUJWF%FQFOEFODZPOPUIFSVOJUT #FGPSF "GUFS %FpOFPSEFSJOHCXVOJUT $POEJUJPO999 "TTFU999 $POEJUJPOTGPSUIFVOJU
  11. Systemd Service Unit • Daemon / Service definition controlled and

    supervised by systemd • The extension is .service • Most of the cases, WantedBy may be multi- user.target
  12. [Service] Section Options 5ZQF TFSWJDFTUBSUVQUZQF &YFD4UBSU &YFD3FMPBE &YFD4UPQ $PNNBOETUPTUBSU SFMPBE

    TUPQ 3FTUBSU XIFUIFSUIFTFSWJDFTIBMMCF SFTUBSUFEXIFO &OWJSPONFOU EFpOFFOWJSPONFOUWBSJBCMFT 8PSLJOH%JSFDUPSZ EFpOFUIFEJSFDUPSZUPXPSL
  13. Service Startup Type • Simple(default): expected the unit started immediately

    after the main service process has been forked off • forking: expected the process will call fork() • oneshot: expected the unit up after the main process exits
  14. Restart &YJUDBVTFT OP BMXBZT PO TVDDFTT PO GBJMVSF PO BCOPSNBM

    PO BCPSU $MFBOFYJU $MFBOTJHOBM 㾎 㾎 6ODMFBOFYJU 㾎 㾎 6ODMFBO TJHOBM 㾎 㾎 㾎 㾎 5JNFPVU 㾎 㾎 㾎
  15. systemd timer • Timer-based service activation like crontab • The

    extension is .timer • foo.timer calls foo.service by default • WantedBy=timers.target • systemctl list-timers to list up current active timers • Two type of timer
  16. Monotonic Timers • Activated after a time span relatively •

    OnActivateSec: after activated the timer • OnBootSec: after booted up the machine • OnStartupSec: after systemd was started • OnUnitActiveSec: after last activated • OnUnitInactiveSec: after last deactivated
  17. Realtime Timers • a.k.a. wallclock timers • Activate on a

    calendar event • The same way that cron jobs do • Use OnCalendar option
  18. Realtime Timer Configs • Configure using systemd time spec •

    DayOfWeek Year-Month-Day Hour:Minute:Second Timezone • Some shorthand is predefined • … , monthly, weekly, yearly, quarterly… • Weekday is defined as English name • Wed or Wednesday • * is for wildcard, .. is for range, / is for repetition, ~ is for last
  19. UJNFTUBNQ NFBOJOH 5IV 'SJ   PGUIFTU UIPGFWFSZ NPOUIPGUIFZFBSCVUJGJU JTB5IVSTEBZPS'SJEBZ

    d UIFUIJSEMBTUEBZJO'FCSVBSZ  CVJMEFSTDPOUPLZP XFFLMZ PGFWFSZ.POEBZ .PO d UIFMBTU.POEBZJO.BZ Calendar Event Examples
  20. PROS: Diff with Crontab • Jobs can be easily ran

    independently of timers • So easy to debug • Jobs can be set up to depend on other units • Jobs can be set up with specific environment • The logs are reported into journal
  21. CONS: Diff with Crontab • Creation redundant • Need to

    create two files: service and timer • Need to run two times systemctl • There’s no built-in equivalent to MAILTO • But can configure like MAILTO
  22. systemd-run • run the specified command in a transient •

    to create transient service • to create transient timer # systemd-run --on-calendar=weekly curl -X POST https://...
  23. What is journald? • A log system provided by systemd

    • systemd-journald.service • Save logs into /var/run/log/journal • Auto-collect logs from stdout/stderr of systemd services • Syslog messages from local processes • Message posted to journald API
  24. How to Read journald Logs? • Use journalctl to read

    journald logs • Journals are binary-formatted • There're many, useful options • Match by fields • Colored
  25. PQUJPO EFTDSJQUJPO F JNNFEJBUFMZKVNQUPUIFFOE S TIPXUIFOFXFTUFOUSJFTpSTU G GPMMPXUIFKPVSOBM MJLFUBJMG V6/*5

    TIPXMPHTGSPNUIFTQFDJpFEVOJU OPQBHFS EPOPUQJQFPVUJOUPBQBHF MFTTJTVTFECZEFGBVMU EJTLVTBHF TIPXEJTLVTBHFPGBMMKPVSOBMpMFT P DIBOHFKPVSOBMPVUQVUNPEF journalctl options
  26. NPEF EFTDSJQUJPO TIPSU EFGBVMU BMNPTUTBNFXJUITZTMPH WFSCPTF TIPXUIFGVMMTUSVDUVSFEFOUSZJUFNTXJUIBMMpFMET KTPO GPSNBUTFOUSJFTBT+40/ POFQFSMJOF

    KTPOQSFUUZ TBNFXJUI+40/CVUIVNBOSFBEBCMFGPSN KTPOTTF GPSNBUTFOUSJFTBT4FSWFS4FOU&WFOUT DBU TIPXWFSZTJNQMFQSJOU Journal output mode
  27. Q?