From fb83703ef9514657b37e2e52843de29d065350cd Mon Sep 17 00:00:00 2001 From: Michele Cereda Date: Thu, 23 Feb 2023 20:11:42 +0100 Subject: [PATCH] Imported knowledge from a private repository --- .vscode/settings.json | 2 + examples/.gitconfig | 126 ++++++++++++ examples/git/configure.sh | 7 + knowledge base/awk.md | 9 + knowledge base/bash.md | 190 +++++++++++++++++- knowledge base/boinc.md | 71 ++++++- knowledge base/gcloud.md | 6 +- knowledge base/history.md | 10 + knowledge base/http response status codes.md | 9 + knowledge base/jsonpath.md | 6 +- knowledge base/nilfs.md | 10 + .../optimize battery on a linux system.md | 65 ++++++ 12 files changed, 499 insertions(+), 12 deletions(-) create mode 100644 examples/.gitconfig create mode 100755 examples/git/configure.sh create mode 100644 knowledge base/awk.md create mode 100644 knowledge base/history.md create mode 100644 knowledge base/http response status codes.md create mode 100644 knowledge base/nilfs.md create mode 100644 knowledge base/optimize battery on a linux system.md diff --git a/.vscode/settings.json b/.vscode/settings.json index 32a9192..96beebd 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -21,6 +21,7 @@ "fdupes", "firewalld", "flatpak", + "gcloud", "getfacl", "gpgtools", "growpart", @@ -60,6 +61,7 @@ "tmpfs", "tput", "usermod", + "userspace", "zstd" ] } diff --git a/examples/.gitconfig b/examples/.gitconfig new file mode 100644 index 0000000..4314cac --- /dev/null +++ b/examples/.gitconfig @@ -0,0 +1,126 @@ +################################################################################ +## ~/.gitconfig +## +## Global git configuration file. Settings in here override the system's ones, +## and are in turn overridden by the repositories' local ones. +## Check the resolved configuration in full and its origins with the following: +## `git config --list --show-origin`. +## +## Sources: +## - https://git-scm.com/docs/git-config +## - https://git-scm.com/book/en/v2/Customizing-Git-Git-Configuration +################################################################################ + +[alias] + branch-get-default = "!f() { \ + git remote show origin \ + | awk '/HEAD branch/ {print $NF}' \ + ;} && f" + branch-get-gone = "!f() { \ + git fetch -p && \ + git branch -vv \ + | awk '/origin/&&/gone/{print $1}' \ + ;} && f" + branch-prune-gone = "!f() { \ + git branch-get-gone \ + | xargs \ + git branch -D \ + ;} && f" + checkout-default-branch = "!f() { \ + git checkout $(git branch-get-default) \ + ;} && f" + config-show-final = "!f() { \ + git config --list \ + | awk -F '=' '{print $1}' \ + | sort -u \ + | xargs -I {} \ + sh -c 'printf \"{}=\" && git config --get {}' \ + ;} && f" + pull-from-all-remotes = "!f() { \ + git remote show \ + | xargs -I{} \ + git pull {} ${1-$(git branch --show-current)} \ + ;} && f" + pull-from-all-remotes-once = "!f() { \ + git remote-show-unique \ + | xargs -I{} \ + git pull {} ${1-$(git branch --show-current)} \ + ;} && f" + pull-from-reachable-remotes = "!f() { \ + git remote show \ + | xargs -I{} \ + sh -c \"timeout 1 git ls-remote -hq {} HEAD >/dev/null 2>&1 && echo {}\" \ + | xargs -I{} \ + git pull {} ${1-$(git branch --show-current)} \ + ;} && f" + pull-from-reachable-remotes-once = "!f() { \ + git remote-show-unique \ + | xargs -I{} \ + sh -c \"timeout 1 git ls-remote -hq {} HEAD >/dev/null 2>&1 && echo {}\" \ + | xargs -I{} \ + git pull {} ${1-$(git branch --show-current)} \ + ;} && f" + push-to-all-remotes = "!f() { \ + git remote show \ + | xargs -I{} \ + git push {} ${1-$(git branch --show-current)} \ + ;} && f" + push-to-all-remotes-once = "!f() { \ + git remote-show-unique \ + | xargs -I{} \ + git push {} ${1-$(git branch --show-current)} \ + ;} && f" + push-to-reachable-remotes = "!f() { \ + git remote show \ + | xargs -I{} \ + sh -c \"timeout 1 git ls-remote -hq {} HEAD >/dev/null 2>&1 && echo {}\" \ + | xargs -I{} \ + git push {} ${1-$(git branch --show-current)} \ + ;} && f" + push-to-reachable-remotes-once = "!f() { \ + git remote-show-unique \ + | xargs -I{} \ + sh -c \"timeout 1 git ls-remote -hq {} HEAD >/dev/null 2>&1 && echo {}\" \ + | xargs -I{} \ + git push {} ${1-$(git branch --show-current)} \ + ;} && f" + remote-show-unique = "!f() { \ + git remote -v \ + | sort -k2 -u \ + | awk '{print $1}' \ + | sort -u \ + ;} && f" + statis = status + statsu = status + switch-default-branch = "!f() { \ + git switch $(git branch-get-default) \ + ;} && f" + top-level = "rev-parse --show-toplevel" +[core] + # 'input' on unix, 'true' on windows + # 'false' only if you know what you are doing + autocrlf = input +[diff] + wsErrorHighlight = all +[init] + defaultBranch = main +[pull] + rebase = false +[submodule] + recurse = true +[user] + email = name.surname@company.com + name = Name Surname + +# Enable LFS. +[filter "lfs"] + clean = git-lfs clean -- %f + smudge = git-lfs smudge -- %f + process = git-lfs filter-process + required = true + +# Always sign commits. +[commit] + gpgSign = true +[user] + signingKey = 99C324BA diff --git a/examples/git/configure.sh b/examples/git/configure.sh new file mode 100755 index 0000000..dd711f6 --- /dev/null +++ b/examples/git/configure.sh @@ -0,0 +1,7 @@ +#!/bin/sh + +git config --local user.email name.surname@company.com +git config --local user.name "Name Surname" +git config --local user.signingkey 99C324BA +git config --local commit.gpgsign true +git config --local pull.rebase false diff --git a/knowledge base/awk.md b/knowledge base/awk.md new file mode 100644 index 0000000..0aba3e0 --- /dev/null +++ b/knowledge base/awk.md @@ -0,0 +1,9 @@ +# `awk` + +## Further readings + +- [Print line only if number in third field is greater than X] +- [Printing the last column of a line in a file] + +[print line only if number in third field is greater than x]: https://unix.stackexchange.com/questions/395588/print-line-only-if-number-in-third-field-is-greater-than-x#395593 +[Printing the last column of a line in a file]: https://stackoverflow.com/questions/13046167/printing-the-last-column-of-a-line-in-a-file#13046224 diff --git a/knowledge base/bash.md b/knowledge base/bash.md index 8eb9a74..79bd00b 100644 --- a/knowledge base/bash.md +++ b/knowledge base/bash.md @@ -1,8 +1,45 @@ # Bourne Again SHell +1. [TL;DR](#tldr) +2. [Startup files loading order](#startup-files-loading-order) +3. [Functions](#functions) +4. [Substitutions](#substitutions) + 1. [!! (command substitution)](#-command-substitution) + 2. [^^ (caret substitution)](#-caret-substitution) +5. [Here documents](#here-documents) +6. [Keys combinations](#keys-combinations) +7. [Check if a script is sourced by another](#check-if-a-script-is-sourced-by-another) +8. [Gotchas](#gotchas) + 1. [Exist statuses of killed commands](#exist-statuses-of-killed-commands) + 2. [Go incognito](#go-incognito) +9. [Further readings](#further-readings) +10. [Sources](#sources) + ## TL;DR ```sh +# Declare arrays. +ARRAY=( + "first_element" + "second_element" "nth_element" +) + +# Get the length of arrays. +# A.K.A. number of elements. +ARRAY_LEN=${#ARRAY[@]} + +# Access all elements in arrays with referencing. +echo ${ARRAY[@]} +echo ${ARRAY[*]} + +# Access the last value of arrays. +echo ${ARRAY[-1]} +echo ${ARRAY: -1} + +# Get a slice of 4 elements from an array. +# Start from the element with index number 2. +echo ${ARRAY:2:4} + # Declare functions. functionName () { … } function functionName { … } @@ -10,6 +47,23 @@ function functionName { … } # Declare functions on a single line. functionName () { command1 ; … ; command N ; } +# Get all the arguments in input. +echo $@ + +# Of all the arguments in input, return only those which are existing directories. +DIRECTORIES=() +for (( I = $# ; I >= 0 ; I-- )); do + if [[ -d ${@[$I]} ]]; then + DIRECTORIES+=${@[$I]} + else + local COMMAND="${@:1: 1-$I}" + break + fi +done + +# Print all shell and environment variables. +( set -o posix ; set ) + # Print exported variables only. export -p @@ -68,10 +122,118 @@ Upon exit: A function automatically returns the exit code of the last command in it. +## Substitutions + +### !! (command substitution) + +Substitutes `!!` with the last command in your history + +```sh +$ echo 'hallo!' +hallo! + +$ !! +echo 'hallo!' +hallo! + +$ sudo !! +sudo echo 'hallo!' +[sudo] password for user: +hallo! +``` + +### ^^ (caret substitution) + +Re-runs a command replacing a string. + +```sh +$ sudo apt search tmux +… + +$ ^search^show +sudo apt show tmux +… +``` + +## Here documents + +A _Here document_ (_heredoc_) is a type of redirection that allows you to pass multiple lines of input to a command. + +```sh +[COMMAND] <<[-] 'DELIMITER' + HERE-DOCUMENT +DELIMITER +``` + +- the first line must start with an **optional command** followed by the special redirection operator `<<` and the **delimiting identifier** +- one can use **any string** as a delimiting identifier, the most commonly used being `EOF` or `END` +- if the delimiting identifier is **unquoted**, the shell will substitute all variables, commands and special characters before passing the here-document lines to the command +- appending a **minus sign** to the redirection operator (`<<-`), will cause all leading tab characters to be **ignored** + this allows one to use indentation when writing here-documents in shell scripts + leading whitespace characters are not allowed, only tabs are +- the here-document block can contain strings, variables, commands and any other type of input +- the last line must end with the delimiting identifier + white space in front of the delimiter is not allowed + +```sh +$ cat << EOF +The current working directory is: $PWD +You are logged in as: $(whoami) +EOF +The current working directory is: /home/user +You are logged in as: user +``` + +```sh +$ cat <<-'EOF' | sed 's/l/e/g' > file.txt + Hello + World +EOF +$ cat file.txt +Heeeo +Wored +``` + +## Keys combinations + +- `Ctrl+L`: clear the screen (instead of typing `clear`) +- `Ctrl+R`: reverse search your Bash history for a command that you have already run and wish to run again + ## Check if a script is sourced by another ```sh -(return 0 2>/dev/null) && echo "this script is not sourced" || echo "this script is sourced" +(return 0 2>/dev/null) \ +&& echo "this script is not sourced" \ +|| echo "this script is sourced" +``` + +## Gotchas + +### Exist statuses of killed commands + +The exit status of a killed command is **128 + _n_** if the command was killed by signal _n_: + +```sh +$ pgrep tryme.sh +880 +$ kill -9 880 +$ echo $? +137 +``` + +### Go incognito + +See [How do I open an incognito bash session] on [unix.stackexchange.com] + +```sh +HISTFILE= +``` + +You can also avoid recording a single command simply preceding it with space + +```shell +echo $RECORDED + echo $NOT_RECORDED ``` ## Further readings @@ -93,3 +255,29 @@ A function automatically returns the exit code of the last command in it. [how to detect if a script is being sourced]: https://stackoverflow.com/questions/2683279/how-to-detect-if-a-script-is-being-sourced#28776166 [the bash trap command]: https://www.linuxjournal.com/content/bash-trap-command [upper- or lower-casing strings]: https://scriptingosx.com/2019/12/upper-or-lower-casing-strings-in-bash-and-zsh/ + + + +[add directory to $path if it's not already there]: https://superuser.com/questions/39751/add-directory-to-path-if-its-not-already-there +[append elements to an array]: https://linuxhint.com/bash_append_array/ +[bash-heredoc]: https://linuxize.com/post/bash-heredoc +[bashrc ps1 generator]: http://bashrcgenerator.com/ +[check if a string represents a valid path]: https://unix.stackexchange.com/questions/214886/check-if-string-represents-valid-path-in-bash +[command line arguments in a shell script]: https://tecadmin.net/tutorial/bash-scripting/bash-command-arguments/ +[find out the exit codes of all piped commands]: https://www.cyberciti.biz/faq/unix-linux-bash-find-out-the-exit-codes-of-all-piped-commands +[histsize vs histfilesize]: https://stackoverflow.com/questions/19454837/bash-histsize-vs-histfilesize#19454838 +[how do i open an incognito bash session]: https://unix.stackexchange.com/questions/158933/how-do-i-open-an-incognito-bash-session/158937#158937 +[how to find a bash shell array length]: https://www.cyberciti.biz/faq/finding-bash-shell-array-length-elements/ +[how to slice an array]: https://stackoverflow.com/questions/1335815/how-to-slice-an-array-in-bash#1336245 +[is there a way of reading the last element of an array?]: https://unix.stackexchange.com/questions/198787/is-there-a-way-of-reading-the-last-element-of-an-array-with-bash#198789 +[linux-terminal-trick]: https://opensource.com/article/20/1/linux-terminal-trick +[printing array elements in reverse]: https://www.unix.com/shell-programming-and-scripting/267967-printing-array-elements-reverse.html +[regular expressions in a case statement]: https://stackoverflow.com/questions/9631335/regular-expressions-in-a-bash-case-statement#9631449 +[reverse an array]: https://unix.stackexchange.com/questions/412868/bash-reverse-an-array +[set]: https://ss64.com/bash/set.html +[slice of positional parameters]: https://unix.stackexchange.com/questions/82060/bash-slice-of-positional-parameters#82061 +[how to list all variables names and their current values?]: https://askubuntu.com/questions/275965/how-to-list-all-variables-names-and-their-current-values#275972 + +[linuxize.com]: https://linuxize.com +[opensource.com]: https://opensource.com +[unix.stackexchange.com]: https://unix.stackexchange.com diff --git a/knowledge base/boinc.md b/knowledge base/boinc.md index 36414be..1d1d79a 100644 --- a/knowledge base/boinc.md +++ b/knowledge base/boinc.md @@ -3,9 +3,15 @@ 1. [TL;DR](#tldr) 2. [Client management](#client-management) 1. [Remote management](#remote-management) -3. [Use the GPU for computation](#use-the-gpu-for-computation) +3. [Use the GPU for computations](#use-the-gpu-for-computations) 1. [On OpenSUSE](#on-opensuse) -4. [Further readings](#further-readings) + 2. [Check the GPU is OpenCL-enabled](#check-the-gpu-is-opencl-enabled) +4. [Use VirtualBox for computations](#use-virtualbox-for-computations) +5. [Gotchas](#gotchas) +6. [Further readings](#further-readings) + +Files are located in `/var/lib/boinc` by default. +Some distribution (debian and derivate) use `/etc/boinc-client` for configuration files instead, and create links to the ones in the default location. ## TL;DR @@ -26,8 +32,14 @@ Name | Type | Description [boinccmd] | Command line | [boinctui] | Text | +Local control RPCs are authenticated using the GUI RPC password. This password is located in the `gui_rpc_auth.cfg` configuration file, as the single first line, with a max length of 255 characters. + +A password is **required** from version FIXME, and is automatically generated if the file is not found or it is empty. + ### Remote management +All remote RPCs (both status and control) are authenticated using the GUI RPC password. + Quick, dirty solution: use the `--allow_remote_gui_rpc` option when starting the client.
This will make the BOINC client accept connections from **any** host (subject to password authentication) even if the client's configuration files are set otherwise. @@ -42,12 +54,21 @@ Better solution: 1. check the `gui_rpc_auth.cfg` file in the BOINC data directory to get the password for authentication -## Use the GPU for computation +## Use the GPU for computations -Also see [AMD Linux drivers] and [Radeon™ Software for Linux® Installation]. +To use the GPU for projects you may need the proprietary nVidia or AMD drivers. See [AMD Linux drivers] and [Radeon™ Software for Linux® Installation]. + +```sh +amdgpu-install --usecase=workstation --opencl=rocr +``` The BOINC client seems to need to be added to the `video` group to be able to use the drivers correctly - this is something I still need to check. +```sh +gpasswd -a 'boinc' 'video' +usermod --append --groups 'video' 'boinc' +``` + ### On OpenSUSE Install the `amdgpu-install` package from [AMD's Linux drivers][amd linux drivers] page, then execute it. @@ -65,11 +86,41 @@ At the next restart of the boinc-client, something similar to this line should a Oct 09 23:09:40 hostnameHere boinc[1709]: 09-Oct-2022 23:09:40 [---] OpenCL: AMD/ATI GPU 0: gfx90c:xnack- (driver version 3452.0 (HSA1.1,LC), device ve> ``` +### Check the GPU is OpenCL-enabled + +Just install and run `clinfo`: + +```sh +$ clinfo +Number of platforms 1 + Platform Name NVIDIA CUDA + Platform Vendor NVIDIA Corporation + Platform Version OpenCL 1.2 CUDA 10.0.132 +… +``` + +## Use VirtualBox for computations + +Install VirtualBox, then add the `boinc` user to the `vboxusers` group: + +```sh +usermod --append --groups 'vboxusers' 'boinc' +``` + +## Gotchas + +- it seems to work much better on debian-based distribution +- In order to suspend computing when the computer is in use, the `boinc` user should have access to your X session so that mouse and keyboard input can be communicated to the client: + + ```sh + xhost +SI:localuser:boinc + ``` + ## Further readings - [BOINC Manager] -- [boinccmd] -- [boinctui] +- [boinccmd] for the bare CLI utility +- [boinctui] for a TUI manager - [GUI RPC bind to port 31416 failed: 98] @@ -83,3 +134,11 @@ Oct 09 23:09:40 hostnameHere boinc[1709]: 09-Oct-2022 23:09:40 [---] OpenCL: AMD [radeon™ software for linux® installation]: https://amdgpu-install.readthedocs.io/en/latest/ [gui rpc bind to port 31416 failed: 98]: https://boinc.mundayweb.com/wiki/index.php?title=GUI_RPC_bind_to_port_31416_failed:_98 + + + +[boinc on arch wiki]: https://wiki.archlinux.org/title/BOINC +[client configuration]: https://boinc.berkeley.edu/wiki/Client_configuration +[controlling boinc remotely]: https://boinc.berkeley.edu/wiki/Controlling_BOINC_remotely +[installing or uninstalling the amdgpu stack]: https://amdgpu-install.readthedocs.io/en/latest/install-installing.html +[linux suspend when computer is in use bug]: https://boinc.berkeley.edu/dev/forum_thread.php?id=14019&postid=101146#101146 diff --git a/knowledge base/gcloud.md b/knowledge base/gcloud.md index d48ca21..ad24287 100644 --- a/knowledge base/gcloud.md +++ b/knowledge base/gcloud.md @@ -1,8 +1,8 @@ # Google cloud platform CLI -- [TL;DR](#tldr) -- [Further readings](#further-readings) -- [Sources](#sources) +1. [TL;DR](#tldr) +2. [Further readings](#further-readings) +3. [Sources](#sources) ## TL;DR diff --git a/knowledge base/history.md b/knowledge base/history.md new file mode 100644 index 0000000..3b73578 --- /dev/null +++ b/knowledge base/history.md @@ -0,0 +1,10 @@ +# History + +1. [TL;DR](#tldr) + +## TL;DR + +```sh +# Delete everything. +history -c +``` diff --git a/knowledge base/http response status codes.md b/knowledge base/http response status codes.md new file mode 100644 index 0000000..d48c7c0 --- /dev/null +++ b/knowledge base/http response status codes.md @@ -0,0 +1,9 @@ +# HTTP response status codes + +## Further readings + +- [MDN] +- [http.cat] + +[mdn]: https://developer.mozilla.org/nl/docs/Web/HTTP/Status +[http.cat]: https://http.cat/ diff --git a/knowledge base/jsonpath.md b/knowledge base/jsonpath.md index 97c1a04..288e437 100644 --- a/knowledge base/jsonpath.md +++ b/knowledge base/jsonpath.md @@ -3,8 +3,8 @@ ## TL;DR ```sh -# filter elements -# only works on arrays, not on maps +# Filter elements. +# Only works on arrays, not on maps. kubectl get serviceaccounts \ -o jsonpath="{.items[?(@.metadata.name!='default')].metadata.name}" ``` @@ -12,5 +12,7 @@ kubectl get serviceaccounts \ ## Further readings - [JSONPath Syntax] +- [Live editor] [jsonpath syntax]: https://support.smartbear.com/alertsite/docs/monitors/api/endpoint/jsonpath.html +[live editor]: https://json8.github.io/patch/demos/apply/ diff --git a/knowledge base/nilfs.md b/knowledge base/nilfs.md new file mode 100644 index 0000000..c8dd916 --- /dev/null +++ b/knowledge base/nilfs.md @@ -0,0 +1,10 @@ +# NilFS + +* -O _feature_: set feature +* -m _percentage_: set percentage of segments reserved to garbage collection (default: 5) +* -n: dry run +* -v: verbose + +```sh +sudo mkfs -t 'nilfs2' -L 'label' -O 'block_count' -v '/dev/sdb1' +``` diff --git a/knowledge base/optimize battery on a linux system.md b/knowledge base/optimize battery on a linux system.md new file mode 100644 index 0000000..160471a --- /dev/null +++ b/knowledge base/optimize battery on a linux system.md @@ -0,0 +1,65 @@ +# Optimize battery on a linux system + +1. [TL;DR](#tldr) +2. [Disable unused services](#disable-unused-services) +3. [Improve battery performance](#improve-battery-performance) +4. [Further readings](#further-readings) + +## TL;DR + +```sh +# Summarize performance of the last boot. +sudo systemd-analyze + +# Show last boot performance. +# Also shows the process tree. +sudo systemd-analyze critical-chain + +# Check power stats. +sudo 'powertop' +``` + +## Disable unused services + +```sh +$ sudo systemd-analyze +Startup finished in 13.129s (firmware) + 5.413s (loader) + 1.746s (kernel) + 7.903s (userspace) = 28.192s +graphical.target reached after 1.239s in userspace + +$ sudo systemd-analyze critical-chain +The time when unit became active or started is printed after the "@" character. +The time the unit took to start is printed after the "+" character. + +graphical.target @1.239s +└─multi-user.target @1.239s + └─ModemManager.service @1.154s +84ms + └─polkit.service @937ms +215ms + └─basic.target @928ms + └─sockets.target @928ms + └─dbus.socket @928ms + └─sysinit.target @924ms + └─systemd-backlight@backlight:acpi_video0.service @2.273s +8ms + └─system-systemd\x2dbacklight.slice @2.272s + └─system.slice @197ms + └─-.slice @197ms +``` + +## Improve battery performance + +```sh +# Enable automatic power management. +# See `tlpui` on GitHub for UI. +sudo systemctl enable --now 'tlp.service' +sudo vim '/etc/tlp.conf' + +# Check power stats. +sudo 'powertop' +``` + +## Further readings + +- [laptop-mode-tools] +- [laptop-mode-tools in the Arch Wiki] + +[laptop-mode-tools]: https://www.unixmen.com/laptop-mode-tools-extend-laptop-battery-life/ +[laptop-mode-tools in the arch wiki]: https://wiki.archlinux.org/title/Laptop_Mode_Tools