Linux

From indicium
Revision as of 06:54, 2 October 2023 by Stefan (talk | contribs)
Jump to navigation Jump to search

Debugging Linux applications

LDD

Linux LDD is just a wrapper for:

LD_TRACE_LOADED_OBJECTS=1 <cmd>

Core dumps

To check core dump handler:

cat /proc/sys/kernel/core_pattern
|/usr/sbin/coredump-handler %h_%t_%p.core

Valgrind

valgrind --trace-children=yes --leak-check=full --show-leak-kinds=all <binary>

Configuring Linux

Secure Boot

If you want to run Linux, but you also dual boot into Windows and need to have your BIOS set up to boot in Secure Boot (for instance if you play Valorant in Windows), then you have two choices.

  1. Boot Windows in secure mode but Linux in insecure mode.
  2. Boot both Windows and Linux in secure mode.

Option 1

To get the first option working, you need to install Fedora/Nobara with UEFI enabled, but secure boot disabled in BIOS. If you at this point enable secure boot, Linux will not boot (complain about shim error).

Boot up with secure boot disabled and do:

sudo mokutil --disable-validation

Follow the instructions and then reboot your PC and choose to "Change Secure boot state" as described here: https://wiki.ubuntu.com/UEFI/SecureBoot/DKMS

If you do this, you can install GRUB2 and enable Secure Boot in your BIOS again. If you now boot into GRUB2, you can select to boot Windows (which will then properly boot up in Secure Mode) or you can choose to boot Linux which will then boot up in insecure mode.

Option 2

If you want to have also Linux booting up in secure mode, you can do that as well, but it requires a bit more work. There is a good guide here: https://wiki.archlinux.org/title/Unified_Extensible_Firmware_Interface/Secure_Boot#PreLoader

Basically you need to:

1 Create your own MOK key

openssl req -newkey rsa:4096 -nodes -keyout mok.key -new -x509 -sha256 -days 3650 -subj "/CN=my Machine Owner Key/" -out mok.crt
openssl x509 -outform DER -in mok.crt -out mok.cer

2 Sign the vmlinuz file in your /boot/ folder with this key

sbsign --key mok.key --cert mok.crt --output /boot/vmlinuz-linux /boot/vmlinuz-linux

3 Enroll your key into your BIOS with mokutil

mokutil --import mok.cer

Then Linux will boot up fine in Secure Mode. You will however have 2 issues:

  • Whenever a new kernel is installed on your system, you need to sign it with your own MOK key, otherwise it will not boot.
  • You can not install third party drivers such as the Nvidia graphics drivers unless you sign also them with your own MOK key.

Graphics drivers

Disable Nvidia drivers and enable Intel embedded HD graphics driver

sudo apt-get purge nvidia-*
dpkg --list | grep nvidia

Make sure the list of nvidia packages is empty

sudo apt-get install --reinstall xserver-xorg-video-intel libgl1-mesa-glx libgl1-mesa-dri mesa-utils xserver-xorg-core
sudo dpkg-reconfigure xserver-xorg
sudo update-alternatives --remove gl_conf /usr/lib/nvidia-current/ld.so.conf
reboot

Disable Nvidia drivers and enable nouveau drivers

sudo apt-get purge nvidia-*
dpkg --list | grep nvidia

Make sure the list of nvidia packages is empty

sudo apt-get install --reinstall xserver-xorg-video-nouveau

Disable nouveau drivers and enable Nvidia drivers

Option 1, using package manager:

sudo apt-get install --reinstall nvidia-driver

or if your graphics card is not supported by the latest drivers (you can install the nvidia-detect package and run it to get this information):

sudo apt-get install --reinstall nvidia-legacy-390xx-driver

Option 2, using latest official NVidia driver:

  • Go to [1] and download the latest Linux driver for your system.
  • Switch to console mode (Ctrl+Alt+F1), kill any Xorg process (or mask/stop them with systemctl if you run systemd)
  • Run the NVidia installer as root
systemctl mask gdm
systemctl stop gdm
killall Xorg
sudo ./NVIDIA-<driver-version-name>.run

Display configuration set in nvidia-settings is not retained after boot

This is a conflict between nvidia-settings and Debians internal settings application.

  • Remove ~/.config/monitors.xml (this is Debians settings)
  • Reboot
  • Set everything up the way you want with nvidia-settings
  • Go to Settings - Display
  • Do some minor change that brings up the "Apply" button and press it
  • Current settings are now saved to ~/.config/monitors.xml and will be used during next boot.

Display configuration for gdm3

After doing the steps above, do:

sudo cp /home/stefan/.config/monitors.xml /var/lib/gdm3/.config

Switch default compiler

(cc --version;c++ --version;gcc --version;g++ --version) | grep Debian
 cc (Debian 6.1.1-11) 6.1.1 20160802
 c++ (Debian 6.1.1-11) 6.1.1 20160802
 gcc (Debian 6.1.1-11) 6.1.1 20160802
 g++ (Debian 6.1.1-11) 6.1.1 20160802

sudo apt-get install gcc-4.9 g++-4.9

gcc-4.9 --version
 gcc-4.9 (Debian 4.9.3-14) 4.9.3

g++-4.9 --version
 g++-4.9 (Debian 4.9.3-14) 4.9.3

gcc-5 --version
 gcc-5 (Debian 5.4.1-1) 5.4.1 20160803

g++-5 --version
 g++-5 (Debian 5.4.1-1) 5.4.1 20160803

sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-5 10
sudo update-alternatives --install /usr/bin/gcc gcc /usr/bin/gcc-4.9 20
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-5 10
sudo update-alternatives --install /usr/bin/g++ g++ /usr/bin/g++-4.9 20
sudo update-alternatives --install /usr/bin/cc cc /usr/bin/gcc 30
sudo update-alternatives --set cc /usr/bin/gcc
sudo update-alternatives --install /usr/bin/c++ c++ /usr/bin/g++ 30
sudo update-alternatives --set c++ /usr/bin/g++

(cc --version;c++ --version;gcc --version;g++ --version) | grep Debian
 cc (Debian 4.9.3-14) 4.9.3
 c++ (Debian 4.9.3-14) 4.9.3
 gcc (Debian 4.9.3-14) 4.9.3
 g++ (Debian 4.9.3-14) 4.9.3

To go back:

sudo update-alternatives --config gcc
sudo update-alternatives --config g++

Console is not filling the screen completely

This may be due to that the console thinks it is connected to a different display device than it actually is. This can be fixed by adding kernel command line parameters to disable that specific video mode.

ls /sys/class/drm
card0@  card0-DisplayPort-1@  card0-DisplayPort-2@  card0-DisplayPort-3@  card0-HDMI Type A-1@  card0-HDMI Type A-2@  card0-LVDS-1@  card0-SVIDEO-1@  card0-VGA-1@  controlD64@  version

Now check the different "modes" and "connected" and "enabled" nodes under each device to figure out which needs to be disabled. Then add to your kernel command line things such as

video=LVDS-1:d video=SVIDEO-1:d video=TV-1:d

More information: https://bbs.archlinux.org/viewtopic.php?id=94990

udev

udev rules

Some sample udev rules which might come handy:

KERNEL=="tty*", GROUP="dialout", MODE="0660"
SUBSYSTEM=="gpio", GROUP:="gpio", MODE:="0660"
SUBSYSTEMS=="usb", KERNEL=="ttyUSB*", ATTRS{idVendor}=="0403", ATTRS{idProduct}=="6001", ATTRS{serial}=="XXXXXXXX", SYMLINK+="ftdiserial2usb"

Use %m for the device number in symlink names.

udev info

To query a device about its properties (which can be used in udev rules):

udevadm info --path=/sys/class/leds/example --query=all --attribute-walk
udevadm info --name /dev/sda
udevadm info -a /dev/sda

More info: https://linux.die.net/man/7/udev

udev debugging

Command to reload udev rules:

sudo udevadm control --reload-rules && sudo udevadm trigger

Command to test udev rules:

udevadm test --action=add /class/gpio

Screen

How to make the scrollback work in screen? Add the following to your .screenrc file:

termcapinfo xterm ti@:te@


LVM

How to extend your LVM disk

Investigate your current setup with:

lsblk

Then you can perform operations like these (which reduces size of live-root with 50GB and extends size of live-home with 50GB):

lvreduce -r -L -50G /dev/mapper/nobara_localhost--live-root
lvextend -r -L +50G /dev/mapper/nobara_localhost--live-home

Linux performance

http://www.brendangregg.com/