User:Wuseman/Cheatsheet

From Gentoo Wiki
Jump to:navigation Jump to:search

Introduction: wuseman's Scripts and One-Liners for Optimizing Gentoo Linux

Welcome to the realm of wuseman's meticulously designed scripts and one-liners, a testament to years of unwavering dedication and a profound passion for optimizing Gentoo Linux. These invaluable tools have been honed to perfection to streamline tasks, empower system administration, and elevate your overall Gentoo Linux experience.

A Legacy of Unparalleled Efficiency

Many of the utilities featured here have played an integral role in my enduring journey with Gentoo Linux since 2008. They have not only served as the cornerstone of my automated installation process, capable of installing Gentoo in under a minute, but have also become indispensable instruments for countless Gentoo enthusiasts seeking unparalleled efficiency and convenience on various pages as cmdlinefu, stackoverflow, github and much much more.

Battle-Tested and Meticulously Refined

Every script and one-liner has undergone rigorous benchmarking and extensive fine-tuning throughout the years.

Your Comprehensive Toolkit

All the scripts and one-liners featured on this page are released under the permissive MIT License, affording you the freedom to employ, modify, and distribute them as you see fit. Feel at liberty to duplicate, adapt, employ, and refine any of these tools to cater to your specific requirements. If you happen upon a superior solution or possess suggestions for enhancement, do not hesitate to reach out and share your insights. I possess an insatiable ardor for Linux, particularly in the realms of swift and precise execution.

A Unified Collaborative Endeavor

I am wholeheartedly committed to offering the most exceptional one-liners for the diverse array of tasks frequently discussed within support channels. This page serves as my personal cheatsheet and a communal resource when giving support

One Liners

Misc

Bluetooth Percentage

For view bluetooth percentage in bluetoothctl and plasma-desktop tray icon

user $sed -i 's/experimental=false/experimental=true/g' /etc/bluetooth/main.conf
user $bluetoothctl info
bluetoothctl info
  Device 14:3F:A6:15:93:43 (public)
        Name: WH-1000XM4
        Alias: WH-1000XM4
        Class: 0x00240404
        Icon: audio-headset
        Paired: yes
        Bonded: yes
        Trusted: yes
        Blocked: no
        Connected: yes
        LegacyPairing: no
        UUID: Vendor specific           (00000000-deca-fade-deca)
        UUID: Headset                   (00001108-0000-1000-8000)
        UUID: Audio Sink                (0000110b-0000-1000-8000)
        UUID: A/V Remote Control Target (0000110c-0000-1000-8000)
        UUID: Advanced Audio Distribu.. (0000110d-0000-1000-8000)
        UUID: A/V Remote Control        (0000110e-0000-1000-8000)
        UUID: Handsfree                 (0000111e-0000-1000-8000)
        UUID: PnP Information           (00001200-0000-1000-8000)
        UUID: Vendor specific           (81c2e72a-0591-443e-a1ff)
        UUID: Vendor specific           (8901dfa8-5c7e-4d8f-9f0c)
        UUID: Vendor specific           (931c7e8a-540f-4686-b798)
        UUID: Vendor specific           (96cc203e-5068-46ad-b32d)
        UUID: Vendor specific           (b9b213ce-eeab-49e4-8fd9)
        UUID: Vendor specific           (f8d1fbe4-7966-4334-8024)
        Modalias: usb:v054Cp0D58d
        Battery Percentage: 0x32 (50)

List what package FILES belong to

This script provides a faster alternative to the `equery b <bin_file>` command for finding which package a specific binary or file (`<bin_file>`) belongs to within a Gentoo Linux system. It significantly improves search performance, making it ideal for efficiently identifying package ownership.

The script utilizes a parallelized search algorithm and provides quick results. For instance, while the standard `equery b` command may take considerable time to execute, this script can complete the same task in a fraction of the time, providing results within seconds.

FILE wbelongstoList what package FILES belong to
#!/bin/bash
# Author: wuseman@bugcrowdninja.com
# License: WTFPL <http://www.wtfpl.net/>
# Same as `equery b <bin_file>` but 98% faster 
# `equery b ffprobe` = 12.912s
# `wbelongsto ffprobe = 0.078s

find_package() {
  target_file="$1"
  pkg_dirs=$(rg -l "$target_file" /var/db/pkg/*/*/CONTENTS)
  for pkg_dir in $pkg_dirs; do
    pkg_name=$(basename $(dirname "$pkg_dir"))
    actual_path=$(rg "$target_file" "$pkg_dir" | awk '{print $2}' | grep -E "^(/usr/bin/|/bin/|/usr/sbin/|/sbin/)")
    
    if [ -n "$actual_path" ]; then
      echo -e " * Searching for \e[92m$target_file\e[0m ..."
      echo -e "\e[92m\e[1m$pkg_name\e[0m (\e[97m\e[1m$actual_path\e[0m)"
      return
    fi
  done
  echo -e " * Searching for \e[92m$target_file\e[0m ..."
  echo -e "\e[91m\e[1mFile does not belong to any package\e[0m"
}

export -f find_package

printf "%s\n" "$@" | xargs -P 4 -I {} bash -c 'find_package "$@"' _ {}

Gentoo Installation

Setting Up /etc/fstab

This script is designed for use in an automated Gentoo Linux installation process, specifically for setting up the `/etc/fstab` file. It automatically detects the root and boot partitions and adds their UUIDs to the `/etc/fstab` configuration. Additionally, it checks for the presence of a swap partition and includes it in the `/etc/fstab` file if found.

FILE auto_fstab_setup.shAutomated Gentoo /etc/fstab Setup Script
#!/bin/bash

 # Detect and set up the root and boot partitions in /etc/fstab
 rootDevice=$(df / | awk 'NR==2 {print $1}')
 rootUUID=$(blkid -o value -s UUID "$rootDevice")
 swapUUID=$(awk '$2 == "swap" {print $1}' /etc/fstab)
 bootUUID=$(lsblk -no UUID /dev/sda2 | awk '{print $1}')

 cat << EOF > /etc/fstab
 UUID=${bootUUID}                 /boot           vfat            noauto,noatime  1 2
 UUID=${rootUUID}                 /               ext4            noatime         0 1
 EOF

 if [ -n "$swapUUID" ]; then
     echo "UUID=${swapUUID}                 none            swap            sw              0 0" >> /etc/fstab
 fi

Setting Up /etc/default/grub

This script is designed to configure the GRUB bootloader for Gentoo Linux during my auto install of gentoo It sets various GRUB parameters, including the timeout, root device, and kernel command line options.

FILE grub_config.shGentoo GRUB Configuration
#!/bin/bash

# Update GRUB configuration in /etc/default/grub
cat << GRUB_EOF > /etc/default/grub
#GRUB_DEFAULT=0
GRUB_TIMEOUT=5
GRUB_DISTRIBUTOR="Gentoo Linux"
GRUB_CMDLINE_LINUX_DEFAULT="crypt_root=UUID=$cryptUUID real_root=UUID=$realRootUUID resume=UUID=$swapUUID root_keydev=UUID=$rootKeyDevUUID root_key=keyFiles/elitedesk-rootfs_key-latest-$(date +%Y-%m-%d).gpg rootfstype=ext4 key_timeout=0 dolvm quiet"
#GRUB_GFXMODE=1920x1080x32
GRUB_GFXPAYLOAD_LINUX=keep
GRUB_DISABLE_LINUX_UUID=false
GRUB_DISABLE_RECOVERY=false
GRUB_EOF

Setting Up Drives and Flags for Partitions

This section outlines the steps to set up drives and configure flags for partitions during the Gentoo Linux installation process. These commands are essential for preparing the storage devices and partitions for your system.

FILE drive_partition_setup.shDrive and Partition Setup Script
#!/bin/bash

# Inspect the drive and print its information
parted -a optimal $drive -s "print"

# Create a GPT label for the drive
parted -a optimal $drive -s "mklabel gpt"

# Create partitions with specific sizes
parted -a optimal $drive -s "mkpart 1 1 2"
parted -a optimal $drive -s "mkpart 2 2 2000"
parted -a optimal $drive -s "mkpart 3 2000 95%"

# Set flags for partitions
parted -a optimal $drive -s "set 1 bios_grub on"
parted -a optimal $drive -s "set 2 boot on"

# Print the drive and partition information again
parted -a optimal $drive -s "print"

These commands are used to inspect the drive, create a GPT label, create partitions, and set flags such as `bios_grub` and `boot` to ensure a proper Gentoo Linux installation.

Additionally, the script generates encryption keys and formats a partition for encryption, ensuring data security.

Encrypt and Store Root Filesystem Key

These commands generate encryption keys, create an encrypted partition, open it for use, and back up the LUKS header for added data protection.

FILE encrypt_store_key.shEncrypt and Store Root Filesystem Key Script
#!/bin/bash

export GPG_TTY=$(tty)

KEY_FOLDER=""

HOSTNAME=$(hostname)

CIPHER="twofish-xts-plain64"
CIPHER_ALGO="TWOFISH"
HASH="sha512"
KEY_SIZE="512"

USB_DEVICE="/path/to/usb/device"
DEVICE_NAME=$(basename "$USB_DEVICE")

if [ -z "$HOSTNAME" ]; then
  echo "Hostname is empty. Hostname is required."
  exit 1
fi

if [ -z "$(ls -A "$KEY_FOLDER")" ]; then
  echo "Folder is empty. Folder required."
  exit 1
fi


if ! grep -qs "$USB_DEVICE" /proc/mounts; then
  echo "USB device is not mounted. It should be mounted at $USB_DEVICE."
  exit 1
fi

if [ ! -d "/sys/block/$DEVICE_NAME" ]; then
  echo "Device $DEVICE_NAME does not exist as a physical device."
  exit 1
fi

if [[ "$DEVICE_NAME" =~ ^sd[a-z]+$ ]]; then
  echo "Device $DEVICE_NAME is an external device."
else
  echo "Device $DEVICE_NAME is not an external device."
  exit 1
fi

if ! gpg --version | grep -q "$CIPHER_ALGO"; then
  echo "GPG does not support the cipher algorithm: $CIPHER_ALGO."
  exit 1
fi

if ! cryptsetup benchmark | grep -q "$CIPHER"; then
  echo "Cryptsetup does not support the cipher: $CIPHER."
  exit 1
fi

KEY_FILE="$KEY_FOLDER/$HOSTNAME-rootfs_key-latest-$(date +%Y-%m-%d).gpg"
HEADER_FILE="$KEY_FOLDER/$HOSTNAME-rootfs_header-$(date +%Y-%m-%d).img"

dd if=/dev/urandom bs=8388607 count=1 \
    | gpg --symmetric --cipher-algo "$CIPHER_ALGO" --output "$KEY_FILE"

gpg --decrypt "$KEY_FILE" \
    | cryptsetup --cipher "$CIPHER" --hash "$HASH" --key-size "$KEY_SIZE" \
    --iter-time 7500 --type luks2 --key-file - luksFormat "/dev/$DEVICE_NAME"

gpg --decrypt "$KEY_FILE" \
    | cryptsetup -d - luksOpen "/dev/$DEVICE_NAME" rootfs

cryptsetup luksHeaderBackup "/dev/$DEVICE_NAME" --header-backup-file "$HEADER_FILE"

This script generates, encrypts, and stores the root filesystem encryption key, ensuring secure data encryption during the installation process.

Mounting System Directories

This script aligns with the standard Gentoo setup detailed in the Gentoo Handbook and aims to guide through re-entering their Gentoo system if the installation doesn't go as planned.

The anticipated default partition layout is as follows:

  • BIOS Boot (GRUB) partition: /dev/sda1
  • Boot partition: /dev/sda2
  • Root filesystem: /dev/sda3

Place this script onto your live USB, in the event of installation troubles, simply execute './gentoo-chroot.sh' to chroot into your system.

FILE gentoo-chroot.shMount System Directories
#!/bin/bash

# Exit on any error
set -e

MOUNT_POINT="/mnt/gentoo"
ROOT_PARTITION="/dev/sdaX"
BOOT_PARTITION="/dev/sdaX"
DIRECTORIES=("dev" "proc" "sys" "tmp" "run")

is_mountpoint() {
    mountpoint -q "$1"
}

if ! is_mountpoint "$MOUNT_POINT"; then
    mount "$ROOT_PARTITION" "$MOUNT_POINT"
fi

if [ -d "$MOUNT_POINT/boot" ] && ! is_mountpoint "$MOUNT_POINT/boot"; then
    mount "$BOOT_PARTITION" "$MOUNT_POINT/boot"
fi

for dir in "${DIRECTORIES[@]}"; do
    target="$MOUNT_POINT/$dir"
    mkdir -p "$target"
    if ! is_mountpoint "$target"; then
        case $dir in
            "dev")
                mount --rbind /dev "$target"
                mount --make-rslave "$target"
                ;;
            "proc")
                mount -t proc /proc "$target"
                ;;
            "sys")
                mount --rbind /sys "$target"
                mount --make-rslave "$target"
                ;;
            "tmp")
                mount --rbind /tmp "$target"
                ;;
            "run")
                mount --bind /run "$target"
                mount --make-slave "$target"
                ;;
            *)
                echo "Warning: Unknown directory $dir"
                ;;
        esac
        sleep 0.5
    fi
done

echo "Chroot is ready. To chroot into Gentoo, run:"
echo "chroot $MOUNT_POINT /bin/bash"
echo "source /etc/profile"
echo "export PS1=\"(chroot) $PS1\""

Automated Download and Verification of Stage3 Tarball

This script simplifies the initial step of the Gentoo Linux installation process by automating the download and verification of the latest Stage3 tarball. It ensures that users download the correct and secure version of the tarball, verifying its integrity with GPG. This step is crucial for a secure and reliable installation.

FILE gentoo_stage3_verify.shGentoo Stage3 Download and Verification Script
#!/bin/bash

cd /mnt/gentoo
gentooURL="https://bouncer.gentoo.org/fetch/root/all/releases/amd64/autobuilds"
latest_build_info=$(wget -qO- ${gentooURL}/latest-stage3-amd64-desktop-openrc.txt)
latest_build_path=$(echo "${latest_build_info}" | grep -oE '[0-9]{8}T[0-9]{6}Z/stage3-amd64-desktop-openrc-[0-9]{8}T[0-9]{6}Z.tar.xz')
full_url="${gentooURL}/${latest_build_path}"
sig_url="${full_url}.asc"

wget ${full_url}
wget ${sig_url}

gpg --verify stage3-amd64-*.tar.xz.asc
if [ $? -ne 0 ]; then
    echo "WARNING: GPG verification failed! Do you want to continue? (y/n)"
    read answer
    if [ "$answer" != "y" ]; then
        echo "Exiting due to failed verification."
        exit 1
    fi
fi

tar xvJpf stage3-amd64-*.tar.xz --xattrs-include='*.*' --numeric-owner 
rm -v -f stage3-amd64-*

Qemu Emulation

Chroot

  • Ensure qemu-arm-static binary is available
  • Extract the Router Firmware firmware (specifically the SquashFS filesystem you want to chroot into) to a known directory.
FILE chroot_into_arm.shGuide to Chrooting into Router Firmware
#!/bin/bash

# Define your firmware root directory for easier reference
FIRMWARE_ROOT="<some_extracted_firmware>.squashfs-root"

# Mount necessary filesystems
mount -t proc proc $FIRMWARE_ROOT/proc
mount --rbind /sys $FIRMWARE_ROOT/sys
mount --rbind /dev $FIRMWARE_ROOT/dev

# Copy the QEMU static binary for ARM to the root of the firmware's filesystem
cp /usr/bin/qemu-arm-static $FIRMWARE_ROOT/usr/bin/

# Register the ARM binary format with the kernel:
echo ':qemu-arm:M::\x7fELF\x01\x01\x01\x00\x00\x00\x00\x00\x00\x00\x00\x00\x02\x00\x28\x00:\xff\xff\xff\xff\xff\xff\xff\x00\xff\xff\xff\xff\xff\xff\xff\xff\xfe\xff\xff\xff:/usr/bin/qemu-arm-static:' > /proc/sys/fs/binfmt_misc/register

# Chroot into the firmware:
chroot $FIRMWARE_ROOT /usr/bin/qemu-arm-static /bin/sh

If I am working on minimal systems in stage3, for example, and I want to chroot into a system without installing the full qemu package, which might take time, it's possible to store the qemu files from another distro. Then it's possible to bindfmt by setting the correct values. Since this is always a mess for me, I have created a simple script for registering/unregistering each of these binfmts:

FILE qemu_binfmt_register_unregister.shQEMU binfmt Register/Unregister Script
#!/bin/bash

# Path to the QEMU binfmt configuration file
CONFIG_FILE="/usr/share/qemu/binfmt.d/qemu.conf"

# Function to unregister all formats
unregister_all_formats() {
    echo "Unregistering all formats..."
    for entry in /proc/sys/fs/binfmt_misc/*; do
        # Avoid unregistering the 'register' and 'status' entries
        if [[ $(basename "$entry") != "register" && $(basename "$entry") != "status" ]]; then
            echo -1 > "$entry"
        fi
    done
}

# Function to register formats from the config file
register_formats_from_config() {
    echo "Registering formats from the config file..."
    while IFS= read -r line; do
        if [[ -n "$line" ]]; then
            echo "$line" > /proc/sys/fs/binfmt_misc/register
        fi
    done < "$CONFIG_FILE"
}

# Unregister all formats
unregister_all_formats

# Register formats from the config file
register_formats_from_config

echo "All formats have been re-registered."

Now, it's feasible to chroot into the qemu chroot, and it will work to use the commands.

Unmount

Warning
Always remember to unmount the proc, sys, and dev directories after you're done working in the chroot environment to clean up.
root #umount $FIRMWARE_ROOT/{proc,sys,dev}

Function to protect against root path deletion

Warning
Please use this script with caution. Verify all commands before execution by adding a dry-run option if you are unsure.
FILE chroot_into_arm.shProtection against Accidental root path
#!/bin/bash
# Author: wuseman <wuseman@bugcrowdninja.com>
# License: WTFPL <http://www.wtfpl.net/>

# Define the protective_rm function
protective_rm() {
  for arg in "$@"; do
    if [[ "$arg" == "/" ]]; then
      echo "Attempted to delete the root directory. Operation aborted."
      return 1
    fi
  done
  /bin/rm "$@"
}

# Alias the 'rm' command to use the protective_rm function
alias rm='protective_rm'

# Export the function so it can be used in subshells
export -f protective_rm