# Lab 9 --- Systemd
**Student**: Albert Akmukhametov (a.akmukhametov@innopolis.university)
## Question 1
### Time spent in the kernel space before the user space was reached.
```bash
$ systemd-analyze
Startup finished in 5.504s (kernel) + 14.439s (userspace) = 19.944s
graphical.target reached after 14.380s in userspace
```

~5.5 seconds in kernel space
~14.4 seconds in user space
### Show an SVG image that contains services that have been started, and how long it took for them to initialize.
```bash
$ systemd-analyze plot > plot.svg
```
Output file can be opened in any web-browser
Head:

Tail:

## Question 2
> `man 7 systemd.special` contains descrition of all services
`graphical.target` --- target which indicates graphical UI may be started (may be - because this target presents on non-gui systems like Ubuntu Server as well)
```
kinjalik-remote% cat /lib/systemd/system/graphical.target
[Unit]
Description=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes
```
`multi-user.target` --- indicates that system enters run level 2 i.e. system is in usable state for multiple users
```
kinjalik-remote% cat /lib/systemd/system/multi-user.target
[Unit]
Description=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes
```
`basic.target` --- this target ensures that all necessary things (sockets, timers, mountpoints) are initialized and synchronized to proceed the boot process.
```
kinjalik-remote% cat /lib/systemd/system/basic.target
[Unit]
Description=Basic System
Documentation=man:systemd.special(7)
Requires=sysinit.target
Wants=sockets.target timers.target paths.target slices.target
After=sysinit.target sockets.target paths.target slices.target tmp.mount
# We support /var, /tmp, /var/tmp, being on NFS, but we don't pull in
# remote-fs.target by default, hence pull them in explicitly here. Note that we
# require /var and /var/tmp, but only add a Wants= type dependency on /tmp, as
# we support that unit being masked, and this should not be considered an error.
RequiresMountsFor=/var /var/tmp
Wants=tmp.mount
```
`sysinit.target` --- initialization of low-level components of kernel and synchronization before proceed the boot process
```
kinjalik-remote% cat /lib/systemd/system/sysinit.target
[Unit]
Description=System Initialization
Documentation=man:systemd.special(7)
Conflicts=emergency.service emergency.target
Wants=local-fs.target swap.target
After=local-fs.target swap.target emergency.service emergency.target
```
Last service, `sysinit.target`, is the first synchronization point for initialization. `Wants` means that both `local-fs` and `swap` targets will be called. However, their success is not truly necessary, for example if SWAP is disabled or fstab (used for `local-fs`) have incorrect entry but does not make boot process failed.
## Question 3
### Server script
Location: `/usr/bin/script.sh`
```bash
#!/bin/bash
while true;
do dd if=/dev/zero of=/dev/null
done &
while true;
UPTIME="$(uptime)"
INODE_USAGE="$(df -i)"
RAM="$(free -h)"
MEMORY="$(df -h)"
LOGS=$(cat /var/log/syslog | tail -n 15)
OUTPUT="Uptime:\n$UPTIME\n\nInode Usage:\n$INODE_USAGE\n\nRAM Usage:\n$RAM\n\nDisk Usage:\n$MEMORY\n\n/var/log/syslog (last 15 lines):\n$LOGS"
do echo -e "HTTP/1.1 200 OK\n\n$OUTPUT" \
| nc -l -k -p 8080 -q 1;
done
```
### Service Unit
Location: `/lib/systemd/system/server.service`
```
[Unit]
Description=My custom web service to show system processes
[Service]
ExecStart=/usr/bin/script.sh
Restart=always
Slice=testslice.slice
[Install]
WantedBy=multi-user.target
```
### Slice
Location: `/etc/systemd/system/testslice.slice`
```
[Unit]
Description=Custom systemd slice for SNA lab 9 on systemd.
Before=slices.target
[Slice]
MemoryAccounting=true
CPUAccounting=true
MemoryMax=256M
CPUQuota=15%
```
### How to run
1. Place files in their corresponding files
2. `systemctl enable server.service` --- make service enabled i.e. it will be started as soon as possible at startup
3. `systemctl start server.service` --- actually start the service

# Question 4
## Service
```
[Unit]
Description=Update apt
Wants=updater.timer
[Service]
Type=oneshot
ExecStart=/usr/bin/apt update
[Install]
WantedBy=multi-user.target
```
## Timer
```
[Unit]
Description=Update apt
[Timer]
OnBootSec=5min
OnUnitActiveSec=1d
[Install]
WantedBy=timers.target
```
## Start this thing
```bash
systemctl enable updater.timer
systemctl start updater.timer
```
## SCreeshots
After startup:

After 5 min after startup:
