User:Wuseman/Cheatsheet
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' /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.
wbelongsto
List what package FILES belong to#!/bin/bash
# Author: wuseman
# 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.
auto_fstab_setup.sh
Automated 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 durning my auto install of gentoo It sets various GRUB parameters, including the timeout, root device, and kernel command line options.
grub_config.sh
Gentoo 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.
drive_partition_setup.sh
Drive 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.
encrypt_store_key.sh
Encrypt 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
In this section, we'll mount essential system directories to the Gentoo Linux installation root directory (`/mnt/gentoo`) in preparation for the installation process.
mount_system_dirs.sh
Mount System Directories#!/bin/bash
MOUNT_POINT="/mnt/gentoo"
DIRECTORIES=("dev" "proc" "sys" "tmp" "run")
for dir in "${DIRECTORIES[@]}"; do
if ! mountpoint -q "$MOUNT_POINT/$dir"; then
case $dir in
"dev" |} "sys" | "tmp")
mount --rbind "/$dir" "$MOUNT_POINT/$dir"
;;
"proc")
mount -t proc /proc "$MOUNT_POINT/proc"
;;
"run")
mount --bind /run "$MOUNT_POINT/run"
;;
esac
sleep 0.5
fi
done
These commands are used to mount the necessary system directories to the Gentoo Linux installation root directory, ensuring the environment is properly set up for the installation process.
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.
chroot_into_arm.sh
Guide 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
Unmount
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}