Navigation Menu

Skip to content

Instantly share code, notes, and snippets.

@siddhpant
Last active November 12, 2023 15:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save siddhpant/0af22835e6825b90869f583749af2070 to your computer and use it in GitHub Desktop.
Save siddhpant/0af22835e6825b90869f583749af2070 to your computer and use it in GitHub Desktop.
Bash aliases to aid in kernel dev
kgit-config() {
git config user.name "Your name"
git config user.email "your@email.com"
git config commit.gpgsign false
git config pull.rebase true
git config sendemail.smtpServer "smtp.email.com"
git config sendemail.smtpEncryption tls
git config sendemail.smtpServerPort 587
git config sendemail.smtpUser "your@email.com"
git config sendemail.confirm "always"
echo -e '#!/bin/bash
patch=$(git show --format=email HEAD)
./scripts/checkpatch.pl --show-types --strict --codespell - <<< "$patch"
' | awk '{$1=$1};1' > .git/hooks/post-commit
chmod u+x .git/hooks/post-commit
}
alias git-config-kernel=kgit-config
alias kcheckpatch="git diff | ./scripts/checkpatch.pl --show-types --strict - "
kconfig() {
case "$1" in
"--def")
make defconfig "${@:5}"
./scripts/kconfig/merge_config.sh .config \
./kernel/configs/kvm_guest.config \
$HOME/linux/config/common.config
;;
"--fix")
./scripts/kconfig/merge_config.sh .config \
./kernel/configs/kvm_guest.config \
$HOME/linux/config/common.config
;;
"--virt")
virtme-configkernel --arch=x86_64 --defconfig "${@:6}"
./scripts/kconfig/merge_config.sh .config \
$HOME/linux/config/common.config
;;
"--debug")
./scripts/kconfig/merge_config.sh .config \
./kernel/configs/kvm_guest.config \
./kernel/configs/debug.config \
$HOME/linux/config/common.config \
$HOME/linux/config/tracing.config
;;
"--merge")
if [[ "$2" == "" ]]; then
echo "Which config in the config folder?"
else
./scripts/kconfig/merge_config.sh .config \
"$HOME/linux/config/$2.config"
fi
;;
"--single")
./scripts/kconfig/merge_config.sh .config
;;
*)
printf "Pass --def, --fix, --virt, --debug, --merge, "
printf "or --single.\n"
;;
esac
}
kmake-all() {
(
set -e
if [[ -f ".llvm" ]]; then
echo -e ".llvm found - Using LLVM.\n"
make -j18 all LLVM=1 "$@"
else
echo -e ".llvm not found - Using GCC\n"
make -j18 all "$@"
fi
# Store loadable modules so we can use in QEMU via virtfs.
# Note: We clear the old folder to avoid failing mkdir et al.
rm -rf .compiled_modules
make INSTALL_MOD_PATH=./.compiled_modules modules_install
# Symlink to local dir in /lib/modules/*/build won't work in
# the VM, so remove it and just add the config file there.
rm ./.compiled_modules/lib/modules/*/build
mkdir $(ls -d ./.compiled_modules/lib/modules/*)/build
cp .config ./.compiled_modules/lib/modules/*/build
)
}
kemulate() {
qemu_args=""
# We will pass user args to QEMU, except the flags.
# Since flag processing is needed, we will append them in the end.
# Also, by appending the user args at the end, the default arguments
# can be overriden by user.
user_args="$@"
# Use host for the KVM on terminal.
qemu_args+="-cpu host -accel kvm -nographic"
qemu_args+=" "
# Memory size.
qemu_args+="-m 4G"
qemu_args+=" "
# Number of cores.
qemu_args+="-smp 6"
qemu_args+=" "
# Kernel image location.
qemu_args+="-kernel ./arch/x86_64/boot/bzImage"
qemu_args+=" "
# Kernel command line parameters.
qemu_args+="-append \""
qemu_args+="console=ttyS0 root=/dev/sda earlyprintk=serial nokaslr "
qemu_args+="net.ifnames=0 panic_on_warn=1 panic_on_io_nmi=1 "
qemu_args+="panic_on_rcu_stall=1 max_rcu_stall_to_panic=1 "
qemu_args+="panic_on_unrecovered_nmi=1"
if [[ "$1" == "--selinux" ]]; then
qemu_args+=" selinux=1"
user_args="${user_args:9}"
else
qemu_args+=" selinux=0"
fi
qemu_args+="\" "
# Debian image to boot.
qemu_args+="-drive file=$HOME/linux/qemu/image/bookworm.img,"
qemu_args+="format=raw"
qemu_args+=" "
# User settings for SSH.
qemu_args+="-net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22"
qemu_args+=" "
# NIC settings.
qemu_args+="-net nic,model=e1000"
qemu_args+=" "
# PID file.
qemu_args+="-pidfile vm.pid"
qemu_args+=" "
# Use virtfs to get access to loadable modules inside QEMU.
qemu_args+="-virtfs local,path=./.compiled_modules/lib/modules,"
qemu_args+="mount_tag=compiled_modules,security_model=passthrough"
qemu_args+=" "
# Append the user args.
qemu_args+="$user_args"
# Run QEMU (eval used to avoid bash splitting arguments on spaces)
eval "qemu-system-x86_64 $qemu_args 2>&1 | tee vm.log"
}
alias kemulate-one="kemulate -smp 1"
kemulate-virt() {
kernel="./arch/x86_64/boot/bzImage"
stretch_image="$HOME/linux/qemu/image/stretch.img"
qemu_args="$@"
cmd_line="console=ttyS0 root=/dev/sda earlyprintk=serial "
cmd_line+="net.ifnames=0 nokaslr "
if [[ "$1" == "--selinux" ]]; then
cmd_line+="selinux=1"
qemu_args="${qemu_args:9}"
else
cmd_line+="selinux=0"
fi
virtme-run \
--kdir . \
--mods auto \
--rw \
--graphics \
--qemu-opts \
-m 4G \
-smp 6 \
-kernel "$kernel" \
-append "$cmd_line" \
-drive file=$stretch_image,format=raw \
-net user,host=10.0.2.10,hostfwd=tcp:127.0.0.1:10021-:22 \
-net nic,model=e1000 \
-cpu host \
-pidfile vm.pid \
-vga virtio \
-display gtk,gl=on \
$qemu_args \
2>&1 | tee vm.log
}
klogin() {
ssh -i $HOME/linux/qemu/image/stretch.id_rsa \
-p 10021 -o "StrictHostKeyChecking no" root@localhost \
2>&1 | tee ssh_op.log
}
syzkemu() {
syzkaller_dir="$HOME/linux/syzkaller"
scp -P 10021 -i $HOME/linux/qemu/image/stretch.id_rsa \
$syzkaller_dir/bin/linux_amd64/syz-execprog \
$syzkaller_dir/bin/linux_amd64/syz-executor \
root@localhost:/root/syz/
}
ktrace-decode() {
if [[ "$1" == "" || "$2" == "" ]]; then
echo "Provide input and output filenames."
exit 1
fi
(./scripts/decode_stacktrace.sh vmlinux < "$1") > "$2"
}
kget-maintainers() {
./scripts/get_maintainer.pl "$@"
}
kunit-drm-run() {
make ARCH=um mrproper
./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/ \
--make_options=LLVM=1 \
--kunitconfig=$HOME/linux/kunit.config
}
kunit-drm-coverage() {
make ARCH=um mrproper
./tools/testing/kunit/kunit.py run \
--kunitconfig=drivers/gpu/drm/tests/ \
--make_options=CC=gcc-6 \
--kunitconfig=$HOME/linux/kunit.config
lcov -t "kunit_tests" -o coverage.info -c -d .kunit/ \
--gcov-tool=gcov-6
genhtml -o /tmp/coverage_html coverage.info
firefox /tmp/coverage_html/drivers/gpu/drm/index.html
}
ksend-patch() {
# This is for sending a single patch with Thunderbird.
# Prefer git send-email though, the existence of this
# implies I already tried to skirt it, unsuccessfully.
# Need argument for commit.
if [ $# -eq 0 ]; then
echo "Provide at least commit ID / branch."
return 1
fi
# Create and check the patch.
patch=$(git format-patch --stdout "$@")
echo -e "$patch\n\n"
./scripts/checkpatch.pl --show-types --strict - <<< "$patch"
if [ $? -ne 0 ]; then
# Last command failed.
return 1
fi
# Remove the first three lines, it is in mbox format but we are going for a GUI client.
patch=$(sed -e '1,3d' <<< "$patch")
if [[ ${patch:15:1} == "]" ]]; then # Starts with "Subject: [PATCH]"
# Save and remove the subject line.
subject=${patch#"Subject: "}
subject=${subject%%$'\n'*}
patch=${patch#*$'\n'}
patch="${patch:1}" # Removes the remaining newline.
else
echo "Supplied multiple patches in one email. Don't do it."
return 1
fi
thunderbird -compose "format='text',subject='$subject',body='$patch'" > /dev/null 2>&1 &
disown
}
CONFIG_DYNAMIC_DEBUG=y
CONFIG_DEBUG_FS=y
CONFIG_CONFIGFS_FS=y
CONFIG_SECURITYFS=y
CONFIG_DEBUG_INFO=y
CONFIG_KASAN=y
CONFIG_UBSAN=y
CONFIG_PANIC_ON_OOPS=y
CONFIG_PANIC_ON_OOPS_VALUE=1
CONFIG_PANIC_TIMEOUT=0
CONFIG_PREEMPT_BUILD=y
CONFIG_PREEMPT=y
CONFIG_PREEMPT_COUNT=y
CONFIG_PREEMPTION=y
CONFIG_PREEMPT_DYNAMIC=y
CONFIG_SCHED_CORE=y
CONFIG_PREEMPT_RCU=y
CONFIG_PREEMPT_NOTIFIERS=y
CONFIG_LOCKUP_DETECTOR=y
CONFIG_SOFTLOCKUP_DETECTOR=y
CONFIG_BOOTPARAM_SOFTLOCKUP_PANIC=y
CONFIG_HAVE_HARDLOCKUP_DETECTOR_BUDDY=y
CONFIG_HARDLOCKUP_DETECTOR=y
CONFIG_HARDLOCKUP_DETECTOR_PERF=y
CONFIG_HARDLOCKUP_DETECTOR_COUNTS_HRTIMER=y
CONFIG_HARDLOCKUP_CHECK_TIMESTAMP=y
CONFIG_BOOTPARAM_HARDLOCKUP_PANIC=y
CONFIG_DETECT_HUNG_TASK=y
CONFIG_DEFAULT_HUNG_TASK_TIMEOUT=140
CONFIG_BOOTPARAM_HUNG_TASK_PANIC=y
CONFIG_WQ_WATCHDOG=y
CONFIG_TUN=y
CONFIG_FAULT_INJECTION=y
CONFIG_FAILSLAB=y
CONFIG_FAIL_PAGE_ALLOC=y
CONFIG_FAULT_INJECTION_USERCOPY=y
CONFIG_FAIL_MAKE_REQUEST=y
CONFIG_FAIL_IO_TIMEOUT=y
CONFIG_FAIL_FUTEX=y
CONFIG_FAULT_INJECTION_DEBUG_FS=y
CONFIG_KEYS=y
CONFIG_TMPFS=y
CONFIG_CRYPTO_LIB_CHACHA20POLY1305=y
CONFIG_BIG_KEYS=y
CONFIG_TRUSTED_KEYS=y
CONFIG_SYSTEM_TRUSTED_KEYRING=y
CONFIG_SECONDARY_TRUSTED_KEYRING=y
CONFIG_SYSTEM_BLACKLIST_KEYRING=y
# Generate a dummy key with:
# openssl req -x509 -newkey rsa:4096 -out key.pem -sha256 -nodes -subj "/CN=."
CONFIG_SYSTEM_TRUSTED_KEYS="key.pem"
CONFIG_CRYPTO_DH=y
CONFIG_KEY_DH_OPERATIONS=y
CONFIG_INTEGRITY=y
CONFIG_INTEGRITY_SIGNATURE=y
CONFIG_INTEGRITY_ASYMMETRIC_KEYS=y
CONFIG_INTEGRITY_TRUSTED_KEYRING=y
CONFIG_VIRTIO_UML=y
CONFIG_UML_PCI_OVER_VIRTIO=y
CONFIG_DEBUG_KERNEL=y
CONFIG_DEBUG_INFO=y
CONFIG_DEBUG_INFO_DWARF_TOOLCHAIN_DEFAULT=y
CONFIG_GCOV=y
CONFIG_KCOV=y
CONFIG_KASAN=y
CONFIG_KASAN_INLINE=y
CONFIG_FTRACE=y
CONFIG_DYNAMIC_FTRACE=y
CONFIG_FUNCTION_TRACER=y
CONFIG_FUNCTION_GRAPH_TRACER=y
CONFIG_IRQSOFF_TRACER=y
CONFIG_PREEMPT_TRACER=y
CONFIG_SCHED_TRACER=y
CONFIG_STACK_TRACER=y
CONFIG_BLK_DEV_IO_TRACE=y
CONFIG_FUNCTION_PROFILER=y
CONFIG_FTRACE_MCOUNT_RECORD=y
CONFIG_NET_9P_DEBUG=y
@siddhpant
Copy link
Author

siddhpant commented Oct 13, 2023

I force-pushed squashing changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment