Introduction to Linux - 2020/2021

Lab 01

  • Introduction to the NSWI177 paper.
  • How to get Linux OS (Dual Boot, USB, Virtual).
  • Try a huge variety of desktop environments (with focus to i3).
  • Intro to GitLab web GUI.

Lab 02

  • Questions and Answers section.
  • Command-line basics.
  • Filesystem basics.
  • BASH wildcards.
  • Introduction to Vim (see also Vim na 20 min — Petr Hodač).

Lab 03

  • Questions and Answers section.
  • Simple shell scripts + executable bit.
  • Hashbang and its usage.
  • Git on command-line (must see MIT missing semester).

Lab 04

  • Questions and Answers section.
  • Talking about STDTIN, STDOUT, STDERR.
  • Pipes

Lab 05

  • Questions and Answers section.
  • Shell scripting general.
  • BASH variables expansion.
  • regex intro.
  • grep examples.
  • sed examples.

Lab 06

  • Easter holiday.

Lab 07

  • Questions and Answers section.
  • SSH basics.
  • Unix-style access rights.
  • tmux example.

Exercise for better undestanding of Linux access rights. Try to guess in advance what will and will not work.

mkdir data
cp /etc/passwd data

## Access rights with ordinary file

# Only W bit
chmod u-r data/passwd
cat data/passwd
echo 'Modification' >> data/passwd

# Only R bit
chmod u=r data/passwd
cat data/passwd
echo 'Modification' >> data/passwd
rm data/passwd

## Access rights with directory
cp /etc/passwd data

# Only WX bits
chmod u-r data
l data
cat data/passwd
echo 'Modification' >> data/passwd
cp /etc/group data

# Only X bit
chmod u-r data
l data
cat data/passwd
echo 'Modification' >> data/passwd
cp /etc/hostname data

# Only RW bits
chmod u=rw data
l data
cat data/passwd
echo 'Modification' >> data/passwd
cp /etc/resolv.conf data

Lab 08

  • Questions and Answers section.
  • Concept of root user.
  • Archiving and compression with tar.
  • Signals in Linux.
mkdir data
cp /etc/{passwd,group,hostname} data/

tar cvf archive.tar data
tar tf archive.tar
tar uf archive.tar signals.py
tar tf archive.tar

# GZip
tar cvzf archive.tar.gz data
tar c data | gzip > archive_gzip.tar.gz

# BZip2
tar cvjf archive.tar.bz2 data
tar c data | bzip2 > archive_bzip2.tar.bz2

file archive.tar
file archive.tar.gz
file archive.tar.bz2

See following explanation from quora.

SIGINT is the interrupt signal. The terminal sends it to the foreground process when the user presses <C-c>. The default behavior is to terminate the process, but it can be caught or ignored. The intention is to provide a mechanism for an orderly, graceful shutdown.

SIGQUIT is the dump core signal. The terminal sends it to the foreground process when the user presses <C-\>. The default behavior is to terminate the process and dump core, but it can be caught or ignored. The intention is to provide a mechanism for the user to abort the process. You can look at SIGINT as “user-initiated happy termination” and SIGQUIT as “user-initiated unhappy termination.”

SIGTERM is the termination signal. The default behavior is to terminate the process, but it also can be caught or ignored. The intention is to kill the process, gracefully or not, but to first allow it a chance to cleanup.

SIGKILL is the kill signal. The only behavior is to kill the process, immediately. As the process cannot catch the signal, it cannot cleanup, and thus this is a signal of last resort.

SIGSTOP is the pause signal. The only behavior is to pause the process; the signal cannot be caught or ignored. The shell uses pausing (and its counterpart, resuming via SIGCONT) to implement job control.

Lab 09

  • Questions and Answers section.
  • Details about 07/find_complementary.sh solution.
  • Details about 06/machine_status.sh -> show mem_usage.sh.
  • Python venv and why it is to use it everywhere.
  • poetry introduction.

See mem_usage.sh and export.qstat.

Lab 10

  • Questions and Answers section.
  • git live examples.
  • Some notes about git rebase and git rebase -i.

Tip: try git log --oneline --graph --all --decorate

# General shopping list
git init
cat > shopping_list.txt <<EOF
[general]
apples
bananas
1x bread
1x chocolate
EOF
git add shopping_list.txt
git commit -m 'Initial commit'

# Alice shopping list
git checkout -b alice
cat >> shopping_list.txt <<EOF

[alice]
1x nutella
EOF
git add shopping_list.txt
git commit -m 'I want Nutella'

git checkout master

# Bob shopping list
git checkout -b bob
cat >> shopping_list.txt <<EOF

[bob]
1x beef steak
EOF
git add shopping_list.txt
git commit -m 'I want steak'

# Merge
git checkout master
git merge alice
git merge bob  # Merge conflict -> vimdiff

# Cleaning branches
git branch -d alice bob

# Cenek shopping list
git checkout -b cenek
cat >> shopping_list.txt <<EOF

[cenek]
10x garlic
EOF
git add shopping_list.txt
git commit -m 'I want garlic soup'

git checkout master

# General more chocolate
sed -i 's/[0-9]x chocolate/3x chocolate/g' shopping_list.txt
git add shopping_list.txt
git commit -m 'More chocolates'

git checkout cenek
git branch cenek-bak
git rebase master
git checkout master
git merge cenek
git branch -d cenek
git branch -D cenek-bak

Lab 11

  • Questions and Answers section.
  • Midway results.
  • Some tips for 07/extract_snippets.sh.
  • OSI Model.
  • ip, ping, traceroute, nmap tool.
  • systemd basics.
#!/bin/bash

# Pitfalls
# For [[ ]] see `man bash`
# For [ ]   see `help test`
line='```bash'
inside=false
if ! "$inside" && [[ "$line" =~ ^'```' ]]; then
  echo OK
else
  echo Fail
fi

if [[ ! "$inside" && "$line" =~ ^'```' ]]; then
  echo OK
else
  echo Fail
fi

# Here string in BASH
# Convenient for variables
echo 1+2 | bc
bc <<<'1+2'

# Escaping the quotes
echo ">"${TEST:-\'\'}"<"

Lab 12

  • Questions and Answers section.
  • Show git ignore from gitignore.io.
  • Show git lfs from git LFS.
  • Mention bfg from bfg.
  • Makefile example with LaTeX paper repository.
# NOTE: You should use something like `latexmk` or `arara`, rather than plain `lualatex`
LATEX = lualatex

# NOTE: Do not use this in your assignment as it would ruins our tests...
FIGURES_SVG = $(wildcard figures/svg/*.svg) $(wildcard figures/svg/*/*.svg)
FIGURES_PDF = $(subst svg/,pdf/,$(FIGURES_SVG:.svg=.pdf))

all: build figures  ## Run all

build: paper.pdf  ## Build paper

paper.pdf: paper.tex
	$(LATEX) paper.tex

figures: $(FIGURES_PDF)  ## Convert SVG to PDF

figures/pdf/%.pdf: figures/svg/%.svg  ## Figures for the manuscript
	inkscape $< --batch-process --export-area-drawing --export-type=pdf --export-filename=$@

clean:  ## Clean all git ignore files.
	git clean -Xdf

help:  # http://marmelab.com/blog/2016/02/29/auto-documented-makefile.html
	@grep -P '^[a-zA-Z_-]+:.*?## .*$$' $(MAKEFILE_LIST) | sort | awk 'BEGIN {FS = ":.*?## "}; {printf "\033[36m%-20s\033[0m %s\n", $$1, $$2}'

.DEFAULT_GOAL := help
.PHONY: clean build all figures help

Lab 13

  • Questions and Answers section.
  • Container introduction docker and podman (mention kubernetes).
  • Playing with containers.
  • Show GitLab CI.
  • Mention Dockerfile.
  • Mention GitLab Container Registry.

Small podman tutorial.

podman pull alpine:latest
podman run -it --name eddard alpine:latest /bin/sh
podman run -it --name robb   alpine:latest /bin/sh
podman ps -a
podman container ls -a
podman start eddard
podman attach eddard
podman exec -it eddard /bin/bash
podman rm eddard
podman rm robb
podman rmi alpine:latest

Let’s build the paper from previous lab inside the podman container.

## Local paper build
podman pull thomasweise/docker-texlive-full
# This will pass
podman run --rm -v .:/root/repo:rw thomasweise/docker-texlive-full /bin/bash -c "cd /root/repo && make build"
# This will fail because we have no rights to write into `/root/repo`
podman run --rm -v .:/root/repo:ro thomasweise/docker-texlive-full /bin/bash -c "cd /root/repo && make build"
# This will pass, but the produced file will be removed
podman run --rm -v .:/root/repo:ro thomasweise/docker-texlive-full /bin/bash -c "cp -r /root/repo /root/copy && cd /root/copy && make build"
# This will pass and the produced file will be copy to `build` direcotry
mkdir build
podman run --rm -v .:/root/repo:ro -v ./build:/build:rw thomasweise/docker-texlive-full /bin/bash -c "cp -r /root/repo /root/copy && cd /root/copy && make build && cp paper.pdf /build"

We can do the same with GitLab CI on remote server. The permanent link to the latest artifacts can be reach with:

https://<git-server>/<namespace>/<repository>/-/jobs/artifacts/<branch>/raw/<file>?job=<job-name>
build:
  image: thomasweise/docker-texlive-full
  script:
    - make build

  artifacts:
    paths:
      - "*.pdf"

Sometimes we need a package which is not install in given container. We can simply extend it by adding new layer. Build podman image base on thomasweise/dockertexlive-full with inkscape.

FROM docker.io/thomasweise/docker-texlive-full:latest

RUN apt-get update && \
    apt-get install -f -y --no-install-recommends \
      inkscape
podman build -t docker-textlive-full-inkscape .

Of course the rather than building a podman image locally and pushing to DockerHub, we can again use GitLab CI and GitLab Container Registry. For more information see Callr blog.

Example for this image can seen in repository docker-images/texlive-full-inkscape. Here is an example of the standard usage.

# Using our image
mkdir build
podman run --rm -v .:/root/repo:ro -v ./build:/build:rw registry.hollmann.cz/docker-images/texlive-full-inkscape:latest /bin/bash -c "cp -r /root/repo /root/copy && cd /root/copy && make all && cp paper.pdf /build"
build:
  image: registry.hollmann.cz/docker-images/texlive-full-inkscape:latest
  script:
    - make build

  artifacts:
    paths:
      - "*.pdf"

Lab 14

  • Questions and Answers section.
  • cron example.
  • Mention anacron. See also askubuntu.com.
  • vlc split example.
  • dotfiles management with dotbot.
  • fortune and cowsay.

change_desktop_background.sh