Dotfiles update 2025-12-24 17:36
This commit is contained in:
441
zsh/functions/btrfs-helpers.zsh
Normal file
441
zsh/functions/btrfs-helpers.zsh
Normal file
@@ -0,0 +1,441 @@
|
||||
# ============================================================================
|
||||
# Btrfs Helpers for Arch/CachyOS
|
||||
# ============================================================================
|
||||
# Quick commands for btrfs filesystem management
|
||||
# CachyOS defaults to btrfs, so these are highly useful
|
||||
#
|
||||
# Commands:
|
||||
# btrfs-usage - Show filesystem usage
|
||||
# btrfs-subs - List subvolumes
|
||||
# btrfs-balance - Start balance operation
|
||||
# btrfs-scrub - Start/check scrub
|
||||
# btrfs-defrag - Defragment file or directory
|
||||
# btrfs-compress - Show compression stats
|
||||
# btrfs-info - Full filesystem info
|
||||
# btrfs-health - Quick health check
|
||||
# ============================================================================
|
||||
|
||||
# Source shared colors (with fallback)
|
||||
source "${0:A:h}/../lib/colors.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || {
|
||||
typeset -g DF_GREEN=$'\033[0;32m' DF_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_RED=$'\033[0;31m' DF_BLUE=$'\033[0;34m'
|
||||
typeset -g DF_CYAN=$'\033[0;36m' DF_NC=$'\033[0m'
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Configuration
|
||||
# ============================================================================
|
||||
|
||||
typeset -g BTRFS_DEFAULT_MOUNT="${BTRFS_DEFAULT_MOUNT:-/}"
|
||||
|
||||
# ============================================================================
|
||||
# Detection
|
||||
# ============================================================================
|
||||
|
||||
_btrfs_check() {
|
||||
if ! command -v btrfs &>/dev/null; then
|
||||
echo -e "${DF_RED}✗${DF_NC} btrfs-progs not installed"
|
||||
echo "Install: sudo pacman -S btrfs-progs"
|
||||
return 1
|
||||
fi
|
||||
|
||||
# Check if root is btrfs
|
||||
local fstype=$(df -T / | awk 'NR==2 {print $2}')
|
||||
if [[ "$fstype" != "btrfs" ]]; then
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} Root filesystem is not btrfs (detected: $fstype)"
|
||||
return 1
|
||||
fi
|
||||
|
||||
return 0
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Core Commands
|
||||
# ============================================================================
|
||||
|
||||
# Show filesystem usage
|
||||
btrfs-usage() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Filesystem Usage: ${mount} "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
sudo btrfs filesystem usage "$mount" -h
|
||||
}
|
||||
|
||||
# List all subvolumes
|
||||
btrfs-subs() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Subvolumes "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
echo -e "${DF_CYAN}Subvolume List:${DF_NC}"
|
||||
sudo btrfs subvolume list "$mount" | while read -r line; do
|
||||
local path=$(echo "$line" | awk '{print $NF}')
|
||||
local id=$(echo "$line" | awk '{print $2}')
|
||||
echo -e " ${DF_GREEN}●${DF_NC} [$id] $path"
|
||||
done
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_CYAN}Default Subvolume:${DF_NC}"
|
||||
sudo btrfs subvolume get-default "$mount"
|
||||
}
|
||||
|
||||
# Start balance operation
|
||||
btrfs-balance() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
local usage="${2:-50}" # Default: rebalance chunks with <50% usage
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Starting btrfs balance on ${mount}"
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} This may take a while and use significant I/O"
|
||||
echo ""
|
||||
|
||||
read -q "REPLY?Continue? [y/N]: "; echo
|
||||
[[ ! "$REPLY" =~ ^[Yy]$ ]] && return 0
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Balancing data chunks with <${usage}% usage..."
|
||||
sudo btrfs balance start -dusage="$usage" -musage="$usage" "$mount" -v
|
||||
|
||||
if [[ $? -eq 0 ]]; then
|
||||
echo -e "${DF_GREEN}✓${DF_NC} Balance completed"
|
||||
else
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} Balance finished (may have been interrupted or had no work)"
|
||||
fi
|
||||
}
|
||||
|
||||
# Check balance status
|
||||
btrfs-balance-status() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
sudo btrfs balance status "$mount"
|
||||
}
|
||||
|
||||
# Cancel running balance
|
||||
btrfs-balance-cancel() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Cancelling balance on ${mount}..."
|
||||
sudo btrfs balance cancel "$mount"
|
||||
}
|
||||
|
||||
# Start scrub operation
|
||||
btrfs-scrub() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Scrub "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
# Check if scrub is already running
|
||||
local status=$(sudo btrfs scrub status "$mount" 2>/dev/null)
|
||||
if echo "$status" | grep -q "running"; then
|
||||
echo -e "${DF_CYAN}Scrub Status (running):${DF_NC}"
|
||||
echo "$status" | sed 's/^/ /'
|
||||
return 0
|
||||
fi
|
||||
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} Scrub verifies data integrity and may take hours"
|
||||
read -q "REPLY?Start scrub? [y/N]: "; echo
|
||||
[[ ! "$REPLY" =~ ^[Yy]$ ]] && return 0
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Starting scrub..."
|
||||
sudo btrfs scrub start "$mount"
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_CYAN}Scrub Status:${DF_NC}"
|
||||
sudo btrfs scrub status "$mount"
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_CYAN}Monitor with:${DF_NC} btrfs-scrub-status"
|
||||
}
|
||||
|
||||
# Show scrub status
|
||||
btrfs-scrub-status() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
sudo btrfs scrub status "$mount"
|
||||
}
|
||||
|
||||
# Cancel scrub
|
||||
btrfs-scrub-cancel() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Cancelling scrub on ${mount}..."
|
||||
sudo btrfs scrub cancel "$mount"
|
||||
}
|
||||
|
||||
# Defragment file or directory
|
||||
btrfs-defrag() {
|
||||
_btrfs_check || return 1
|
||||
local target="${1:-.}"
|
||||
|
||||
if [[ ! -e "$target" ]]; then
|
||||
echo -e "${DF_RED}✗${DF_NC} Target not found: $target"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Defragmenting: $target"
|
||||
|
||||
if [[ -d "$target" ]]; then
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} Recursive defrag on directory"
|
||||
read -q "REPLY?Continue? [y/N]: "; echo
|
||||
[[ ! "$REPLY" =~ ^[Yy]$ ]] && return 0
|
||||
|
||||
sudo btrfs filesystem defragment -r -v "$target"
|
||||
else
|
||||
sudo btrfs filesystem defragment -v "$target"
|
||||
fi
|
||||
|
||||
echo -e "${DF_GREEN}✓${DF_NC} Defragmentation complete"
|
||||
}
|
||||
|
||||
# Show compression stats (requires compsize)
|
||||
btrfs-compress() {
|
||||
_btrfs_check || return 1
|
||||
local target="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
if ! command -v compsize &>/dev/null; then
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} compsize not installed"
|
||||
echo "Install: sudo pacman -S compsize"
|
||||
return 1
|
||||
fi
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Compression Statistics "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
sudo compsize "$target"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Information Commands
|
||||
# ============================================================================
|
||||
|
||||
# Full filesystem info
|
||||
btrfs-info() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Filesystem Information "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}Filesystem Show:${DF_NC}"
|
||||
sudo btrfs filesystem show "$mount"
|
||||
|
||||
echo -e "\n${DF_CYAN}Filesystem df:${DF_NC}"
|
||||
sudo btrfs filesystem df "$mount"
|
||||
|
||||
echo -e "\n${DF_CYAN}Device Stats:${DF_NC}"
|
||||
sudo btrfs device stats "$mount"
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Quick health check
|
||||
btrfs-health() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Health Check "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
local issues=0
|
||||
|
||||
# Check device stats for errors
|
||||
echo -e "${DF_CYAN}Device Errors:${DF_NC}"
|
||||
local stats=$(sudo btrfs device stats "$mount" 2>/dev/null)
|
||||
local errors=$(echo "$stats" | grep -v " 0$" | grep -v "^$")
|
||||
|
||||
if [[ -z "$errors" ]]; then
|
||||
echo -e " ${DF_GREEN}✓${DF_NC} No device errors detected"
|
||||
else
|
||||
echo -e " ${DF_RED}✗${DF_NC} Errors detected:"
|
||||
echo "$errors" | sed 's/^/ /'
|
||||
issues=$((issues + 1))
|
||||
fi
|
||||
|
||||
# Check allocation
|
||||
echo -e "\n${DF_CYAN}Space Allocation:${DF_NC}"
|
||||
local usage=$(sudo btrfs filesystem usage "$mount" -b 2>/dev/null)
|
||||
local used_pct=$(echo "$usage" | grep "Used:" | head -1 | awk '{print $2}' | tr -d '%')
|
||||
|
||||
if [[ -n "$used_pct" ]]; then
|
||||
if (( used_pct >= 90 )); then
|
||||
echo -e " ${DF_RED}✗${DF_NC} Filesystem ${used_pct}% full - critical!"
|
||||
issues=$((issues + 1))
|
||||
elif (( used_pct >= 80 )); then
|
||||
echo -e " ${DF_YELLOW}⚠${DF_NC} Filesystem ${used_pct}% full - consider cleanup"
|
||||
else
|
||||
echo -e " ${DF_GREEN}✓${DF_NC} Filesystem ${used_pct}% used"
|
||||
fi
|
||||
fi
|
||||
|
||||
# Check last scrub
|
||||
echo -e "\n${DF_CYAN}Last Scrub:${DF_NC}"
|
||||
local scrub_status=$(sudo btrfs scrub status "$mount" 2>/dev/null)
|
||||
local scrub_date=$(echo "$scrub_status" | grep "Scrub started" | awk '{print $3, $4, $5}')
|
||||
local scrub_errors=$(echo "$scrub_status" | grep "Error summary" | grep -v "no errors")
|
||||
|
||||
if [[ -n "$scrub_date" ]]; then
|
||||
echo -e " Last scrub: $scrub_date"
|
||||
if [[ -n "$scrub_errors" ]]; then
|
||||
echo -e " ${DF_RED}✗${DF_NC} Scrub found errors"
|
||||
echo "$scrub_errors" | sed 's/^/ /'
|
||||
issues=$((issues + 1))
|
||||
else
|
||||
echo -e " ${DF_GREEN}✓${DF_NC} No errors in last scrub"
|
||||
fi
|
||||
else
|
||||
echo -e " ${DF_YELLOW}⚠${DF_NC} No scrub has been run (recommended monthly)"
|
||||
fi
|
||||
|
||||
# Summary
|
||||
echo ""
|
||||
if (( issues == 0 )); then
|
||||
echo -e "${DF_GREEN}✓${DF_NC} Btrfs filesystem appears healthy"
|
||||
else
|
||||
echo -e "${DF_RED}✗${DF_NC} Found $issues issue(s) - investigate above"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Snapshot Helpers (complement snapper.zsh)
|
||||
# ============================================================================
|
||||
|
||||
# Show snapshot space usage
|
||||
btrfs-snap-usage() {
|
||||
_btrfs_check || return 1
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Snapshot Space Usage "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
|
||||
if [[ -d "/.snapshots" ]]; then
|
||||
echo -e "${DF_CYAN}Snapshot Directory:${DF_NC}"
|
||||
sudo du -sh /.snapshots 2>/dev/null || echo " Unable to calculate"
|
||||
|
||||
echo -e "\n${DF_CYAN}Individual Snapshots:${DF_NC}"
|
||||
sudo du -sh /.snapshots/*/ 2>/dev/null | sort -h | tail -10 | sed 's/^/ /'
|
||||
else
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} No /.snapshots directory found"
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Maintenance
|
||||
# ============================================================================
|
||||
|
||||
# Full maintenance routine
|
||||
btrfs-maintain() {
|
||||
_btrfs_check || return 1
|
||||
local mount="${1:-$BTRFS_DEFAULT_MOUNT}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Maintenance Routine "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
echo ""
|
||||
echo "This will perform:"
|
||||
echo " 1. Health check"
|
||||
echo " 2. Balance (low usage chunks)"
|
||||
echo " 3. Scrub (data integrity)"
|
||||
echo ""
|
||||
echo -e "${DF_YELLOW}⚠${DF_NC} This may take several hours"
|
||||
read -q "REPLY?Continue? [y/N]: "; echo
|
||||
[[ ! "$REPLY" =~ ^[Yy]$ ]] && return 0
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Step 1/3: Health Check"
|
||||
btrfs-health "$mount"
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Step 2/3: Balance"
|
||||
sudo btrfs balance start -dusage=50 -musage=50 "$mount"
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Step 3/3: Scrub"
|
||||
sudo btrfs scrub start -B "$mount" # -B runs in foreground
|
||||
|
||||
echo ""
|
||||
echo -e "${DF_GREEN}✓${DF_NC} Maintenance complete"
|
||||
btrfs-health "$mount"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Aliases
|
||||
# ============================================================================
|
||||
|
||||
alias btru='btrfs-usage'
|
||||
alias btrs='btrfs-subs'
|
||||
alias btrh='btrfs-health'
|
||||
alias btri='btrfs-info'
|
||||
alias btrc='btrfs-compress'
|
||||
|
||||
# ============================================================================
|
||||
# Help
|
||||
# ============================================================================
|
||||
|
||||
btrfs-help() {
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Btrfs Helper Commands "
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
cat << 'EOF'
|
||||
|
||||
Information:
|
||||
btrfs-usage [mount] Filesystem usage summary
|
||||
btrfs-subs [mount] List all subvolumes
|
||||
btrfs-info [mount] Full filesystem information
|
||||
btrfs-health [mount] Quick health check
|
||||
btrfs-compress [path] Compression statistics (requires compsize)
|
||||
|
||||
Maintenance:
|
||||
btrfs-balance [mount] Start balance operation
|
||||
btrfs-balance-status Check balance progress
|
||||
btrfs-balance-cancel Cancel running balance
|
||||
btrfs-scrub [mount] Start scrub (integrity check)
|
||||
btrfs-scrub-status Check scrub progress
|
||||
btrfs-scrub-cancel Cancel running scrub
|
||||
btrfs-defrag <path> Defragment file/directory
|
||||
btrfs-maintain [mount] Full maintenance routine
|
||||
|
||||
Snapshots:
|
||||
btrfs-snap-usage Show snapshot space usage
|
||||
|
||||
Aliases:
|
||||
btru btrfs-usage
|
||||
btrs btrfs-subs
|
||||
btrh btrfs-health
|
||||
btri btrfs-info
|
||||
btrc btrfs-compress
|
||||
|
||||
Note: Most commands default to / if no mount point specified.
|
||||
|
||||
See also: snapper.zsh for snapshot management
|
||||
|
||||
EOF
|
||||
}
|
||||
@@ -3,10 +3,12 @@
|
||||
# MOTD (Message of the Day) - Dynamic System Info
|
||||
# ============================================================================
|
||||
# Displays system information on shell startup
|
||||
# Optimized for Arch/CachyOS with direct /proc access
|
||||
#
|
||||
# Functions:
|
||||
# show_motd - Compact box format
|
||||
# show_motd_mini - Single line format
|
||||
# show_motd_full - Extended info
|
||||
# ============================================================================
|
||||
|
||||
# Only run in interactive shells
|
||||
@@ -18,7 +20,7 @@ source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || {
|
||||
typeset -g DF_RESET=$'\033[0m' DF_BOLD=$'\033[1m' DF_DIM=$'\033[2m'
|
||||
typeset -g DF_BLUE=$'\033[38;5;39m' DF_CYAN=$'\033[38;5;51m'
|
||||
typeset -g DF_GREEN=$'\033[38;5;82m' DF_YELLOW=$'\033[38;5;220m'
|
||||
typeset -g DF_GREY=$'\033[38;5;242m' DF_NC=$'\033[0m'
|
||||
typeset -g DF_RED=$'\033[38;5;196m' DF_GREY=$'\033[38;5;242m' DF_NC=$'\033[0m'
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
@@ -28,32 +30,98 @@ source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || {
|
||||
typeset -g _M_WIDTH=66
|
||||
|
||||
# ============================================================================
|
||||
# Info Gathering
|
||||
# Optimized Info Gathering (using /proc directly - faster than spawning processes)
|
||||
# ============================================================================
|
||||
|
||||
# Uptime from /proc (no subprocess)
|
||||
_motd_uptime() {
|
||||
local up=$(uptime 2>/dev/null)
|
||||
if [[ "$up" =~ "up "([^,]+) ]]; then
|
||||
echo "${match[1]}" | sed 's/^ *//'
|
||||
local uptime_seconds=$(cut -d. -f1 /proc/uptime 2>/dev/null)
|
||||
[[ -z "$uptime_seconds" ]] && { echo "?"; return; }
|
||||
|
||||
local days=$((uptime_seconds / 86400))
|
||||
local hours=$(((uptime_seconds % 86400) / 3600))
|
||||
local mins=$(((uptime_seconds % 3600) / 60))
|
||||
|
||||
if (( days > 0 )); then
|
||||
echo "${days}d ${hours}h"
|
||||
elif (( hours > 0 )); then
|
||||
echo "${hours}h ${mins}m"
|
||||
else
|
||||
echo "?"
|
||||
echo "${mins}m"
|
||||
fi
|
||||
}
|
||||
|
||||
# Load from /proc (no subprocess)
|
||||
_motd_load() {
|
||||
if [[ -f /proc/loadavg ]]; then
|
||||
awk '{print $1}' /proc/loadavg
|
||||
else
|
||||
uptime | awk -F'load average:' '{print $2}' | awk -F, '{print $1}' | xargs
|
||||
cut -d' ' -f1 /proc/loadavg 2>/dev/null || echo "?"
|
||||
}
|
||||
|
||||
# Memory from /proc (single awk call)
|
||||
_motd_mem() {
|
||||
awk '/MemTotal/ {total=$2} /MemAvailable/ {avail=$2} END {
|
||||
if (total > 0) {
|
||||
used=(total-avail)/1024/1024
|
||||
total_gb=total/1024/1024
|
||||
printf "%.1fG/%.0fG", used, total_gb
|
||||
} else {
|
||||
print "N/A"
|
||||
}
|
||||
}' /proc/meminfo 2>/dev/null || echo "N/A"
|
||||
}
|
||||
|
||||
# Disk usage (single df call)
|
||||
_motd_disk() {
|
||||
df -h / 2>/dev/null | awk 'NR==2 {print $3 "/" $2}' || echo "N/A"
|
||||
}
|
||||
|
||||
# CPU governor (Arch-specific, direct file read)
|
||||
_motd_governor() {
|
||||
local gov=$(cat /sys/devices/system/cpu/cpu0/cpufreq/scaling_governor 2>/dev/null)
|
||||
[[ -n "$gov" ]] && echo "$gov"
|
||||
}
|
||||
|
||||
# Kernel version (simplified)
|
||||
_motd_kernel() {
|
||||
local kernel=$(uname -r)
|
||||
# Strip architecture suffix for cleaner display
|
||||
echo "${kernel%%-*}"
|
||||
}
|
||||
|
||||
# CachyOS scheduler detection
|
||||
_motd_scheduler() {
|
||||
if grep -q "cachyos" /proc/version 2>/dev/null; then
|
||||
if grep -q "bore" /proc/version 2>/dev/null; then
|
||||
echo "BORE"
|
||||
elif grep -q "eevdf" /proc/version 2>/dev/null; then
|
||||
echo "EEVDF"
|
||||
else
|
||||
echo "CachyOS"
|
||||
fi
|
||||
fi
|
||||
}
|
||||
|
||||
_motd_mem() {
|
||||
free -h 2>/dev/null | awk '/^Mem:/ {print $3 "/" $2}' || echo "N/A"
|
||||
# Failed systemd services count (cached)
|
||||
_motd_failed_services() {
|
||||
# Use cache to avoid slow systemctl calls on every prompt
|
||||
local cache_file="/tmp/.motd-failed-${UID}"
|
||||
local cache_age=300 # 5 minutes
|
||||
|
||||
if [[ -f "$cache_file" ]]; then
|
||||
local file_age=$(($(date +%s) - $(stat -c %Y "$cache_file" 2>/dev/null || echo 0)))
|
||||
if (( file_age < cache_age )); then
|
||||
cat "$cache_file"
|
||||
return
|
||||
fi
|
||||
fi
|
||||
|
||||
local count=$(systemctl --failed --no-pager --no-legend 2>/dev/null | wc -l)
|
||||
echo "$count" > "$cache_file" 2>/dev/null
|
||||
echo "$count"
|
||||
}
|
||||
|
||||
_motd_disk() {
|
||||
df -h / 2>/dev/null | awk 'NR==2 {print $3 "/" $4}' || echo "N/A"
|
||||
# Package updates (from environment, set by aliases.zsh)
|
||||
_motd_updates() {
|
||||
echo "${UPDATE_PKG_COUNT:-0}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
@@ -62,27 +130,13 @@ _motd_disk() {
|
||||
|
||||
_motd_line() {
|
||||
local char="$1"
|
||||
local i
|
||||
local line=""
|
||||
for ((i=0; i<_M_WIDTH; i++)); do
|
||||
line+="$char"
|
||||
done
|
||||
for ((i=0; i<_M_WIDTH; i++)); do line+="$char"; done
|
||||
echo "$line"
|
||||
}
|
||||
|
||||
_motd_pad() {
|
||||
local str="$1"
|
||||
local width="$2"
|
||||
local len=${#str}
|
||||
if (( len >= width )); then
|
||||
echo "${str:0:$width}"
|
||||
else
|
||||
printf "%-${width}s" "$str"
|
||||
fi
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Main Display Function
|
||||
# Main Display Function (Compact)
|
||||
# ============================================================================
|
||||
|
||||
show_motd() {
|
||||
@@ -95,7 +149,6 @@ show_motd() {
|
||||
local load=$(_motd_load)
|
||||
local mem=$(_motd_mem)
|
||||
local disk=$(_motd_disk)
|
||||
local local_ip=$(hostname -i 2>/dev/null | awk -F" " '{print $1}' || echo "N/A")
|
||||
local hline=$(_motd_line '═')
|
||||
local inner=$((_M_WIDTH - 2))
|
||||
|
||||
@@ -106,22 +159,38 @@ show_motd() {
|
||||
|
||||
# Header: hostname + datetime
|
||||
local h_left="✦ ${hostname}"
|
||||
local h_center="${local_ip}"
|
||||
local h_right="${datetime}"
|
||||
local h_pad=$(((inner - ${#h_left} - ${#h_center} - ${#h_right}) / 2 ))
|
||||
local h_pad=$(((inner - ${#h_left} - ${#h_right}) / 2))
|
||||
local h_spaces=""
|
||||
for ((i=0; i<h_pad; i++)); do h_spaces+=" "; done
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_BLUE}${h_left}${DF_NC}${h_spaces}${DF_YELLOW}${h_center}${h_spaces}${DF_NC}${DF_BOLD}${h_right}${DF_NC}${DF_GREY} │${DF_NC}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_BLUE}${h_left}${DF_NC}${h_spaces}${h_spaces}${DF_NC}${DF_BOLD}${h_right}${DF_NC} ${DF_GREY}│${DF_NC}"
|
||||
|
||||
# Separator
|
||||
echo "${DF_GREY}╘${hline}╛${DF_NC}"
|
||||
|
||||
# Stats line
|
||||
local s1="${DF_YELLOW}▲ up:${DF_NC}${uptime}"
|
||||
local s2="${DF_CYAN}◆ load:${DF_NC}${load}"
|
||||
local s3="${DF_GREEN}◇ mem:${DF_NC}${mem}"
|
||||
local s4="${DF_BLUE}⊡${DF_NC} ${disk}"
|
||||
echo "${DF_GREY}${DF_DIM} 〘${DF_NC}${s1}${DF_GREY}${DF_DIM}〙⎯〘${s2}${DF_GREY}${DF_DIM}〙⎯〘${s3}${DF_GREY}${DF_DIM}〙⎯〘${s4}${DF_GREY}${DF_DIM}〙 ${DF_NC}"
|
||||
local s1="${DF_YELLOW}▲${DF_NC}${uptime}"
|
||||
local s2="${DF_CYAN}◆${DF_NC}${load}"
|
||||
local s3="${DF_GREEN}◇${DF_NC}${mem}"
|
||||
local s4="${DF_BLUE}⊡${DF_NC}${disk}"
|
||||
echo " ${s1} ${s2} ${s3} ${s4}"
|
||||
|
||||
# Alerts line (if any issues)
|
||||
local alerts=""
|
||||
|
||||
# Check for failed services
|
||||
local failed=$(_motd_failed_services)
|
||||
if (( failed > 0 )); then
|
||||
alerts+="${DF_RED}⚠ ${failed} failed service(s)${DF_NC} "
|
||||
fi
|
||||
|
||||
# Check for updates
|
||||
local updates=$(_motd_updates)
|
||||
if (( updates > 0 )); then
|
||||
alerts+="${DF_YELLOW}⇑ ${updates} update(s)${DF_NC}"
|
||||
fi
|
||||
|
||||
[[ -n "$alerts" ]] && echo " $alerts"
|
||||
|
||||
echo ""
|
||||
}
|
||||
@@ -137,8 +206,64 @@ show_motd_mini() {
|
||||
local hostname="${HOST:-$(hostname -s 2>/dev/null)}"
|
||||
local uptime=$(_motd_uptime)
|
||||
local mem=$(_motd_mem)
|
||||
local failed=$(_motd_failed_services)
|
||||
|
||||
local alert=""
|
||||
(( failed > 0 )) && alert=" ${DF_RED}[${failed} failed]${DF_NC}"
|
||||
|
||||
echo "${DF_DIM}──${DF_NC} ${DF_BOLD}${hostname}${DF_NC} ${DF_DIM}│${DF_NC} up:${uptime} ${DF_DIM}│${DF_NC} mem:${mem} ${DF_DIM}──${DF_NC}"
|
||||
echo "${DF_DIM}──${DF_NC} ${DF_BOLD}${hostname}${DF_NC} ${DF_DIM}│${DF_NC} up:${uptime} ${DF_DIM}│${DF_NC} mem:${mem}${alert} ${DF_DIM}──${DF_NC}"
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Full Format (Extended Info)
|
||||
# ============================================================================
|
||||
|
||||
show_motd_full() {
|
||||
[[ -n "$_MOTD_SHOWN" && "$1" != "--force" ]] && return 0
|
||||
typeset -g _MOTD_SHOWN=1
|
||||
|
||||
local hostname="${HOST:-$(hostname -s 2>/dev/null)}"
|
||||
local datetime=$(date '+%A, %B %d %Y %H:%M:%S')
|
||||
local uptime=$(_motd_uptime)
|
||||
local load=$(_motd_load)
|
||||
local mem=$(_motd_mem)
|
||||
local disk=$(_motd_disk)
|
||||
local kernel=$(_motd_kernel)
|
||||
local governor=$(_motd_governor)
|
||||
local scheduler=$(_motd_scheduler)
|
||||
local hline=$(_motd_line '═')
|
||||
|
||||
echo ""
|
||||
echo "${DF_GREY}╒${hline}╕${DF_NC}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BOLD}${DF_BLUE}✦ ${hostname}${DF_NC}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_DIM}${datetime}${DF_NC}"
|
||||
echo "${DF_GREY}├$(_motd_line '─')┤${DF_NC}"
|
||||
|
||||
# System info
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_CYAN}Kernel:${DF_NC} ${kernel}"
|
||||
[[ -n "$scheduler" ]] && echo "${DF_GREY}│${DF_NC} ${DF_CYAN}Scheduler:${DF_NC} ${scheduler}"
|
||||
[[ -n "$governor" ]] && echo "${DF_GREY}│${DF_NC} ${DF_CYAN}Governor:${DF_NC} ${governor}"
|
||||
|
||||
echo "${DF_GREY}├$(_motd_line '─')┤${DF_NC}"
|
||||
|
||||
# Resources
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_YELLOW}▲ Uptime:${DF_NC} ${uptime}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_CYAN}◆ Load:${DF_NC} ${load}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_GREEN}◇ Memory:${DF_NC} ${mem}"
|
||||
echo "${DF_GREY}│${DF_NC} ${DF_BLUE}⊡ Disk:${DF_NC} ${disk}"
|
||||
|
||||
# Alerts section
|
||||
local failed=$(_motd_failed_services)
|
||||
local updates=$(_motd_updates)
|
||||
|
||||
if (( failed > 0 || updates > 0 )); then
|
||||
echo "${DF_GREY}├$(_motd_line '─')┤${DF_NC}"
|
||||
(( failed > 0 )) && echo "${DF_GREY}│${DF_NC} ${DF_RED}⚠ ${failed} failed systemd service(s)${DF_NC}"
|
||||
(( updates > 0 )) && echo "${DF_GREY}│${DF_NC} ${DF_YELLOW}⇑ ${updates} package update(s) available${DF_NC}"
|
||||
fi
|
||||
|
||||
echo "${DF_GREY}╘${hline}╛${DF_NC}"
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
@@ -147,3 +272,29 @@ show_motd_mini() {
|
||||
|
||||
alias motd='show_motd --force'
|
||||
alias motd-mini='show_motd_mini --force'
|
||||
alias motd-full='show_motd_full --force'
|
||||
|
||||
# ============================================================================
|
||||
# Quick System Overview (callable anytime)
|
||||
# ============================================================================
|
||||
|
||||
sysbrief() {
|
||||
echo -e "${DF_CYAN}Uptime:${DF_NC} $(_motd_uptime)"
|
||||
echo -e "${DF_CYAN}Load:${DF_NC} $(_motd_load)"
|
||||
echo -e "${DF_CYAN}Memory:${DF_NC} $(_motd_mem)"
|
||||
echo -e "${DF_CYAN}Disk:${DF_NC} $(_motd_disk)"
|
||||
|
||||
local kernel=$(_motd_kernel)
|
||||
echo -e "${DF_CYAN}Kernel:${DF_NC} ${kernel}"
|
||||
|
||||
local governor=$(_motd_governor)
|
||||
[[ -n "$governor" ]] && echo -e "${DF_CYAN}Governor:${DF_NC} ${governor}"
|
||||
|
||||
local scheduler=$(_motd_scheduler)
|
||||
[[ -n "$scheduler" ]] && echo -e "${DF_CYAN}Scheduler:${DF_NC} ${scheduler}"
|
||||
|
||||
local failed=$(_motd_failed_services)
|
||||
if (( failed > 0 )); then
|
||||
echo -e "${DF_RED}Failed:${DF_NC} ${failed} service(s)"
|
||||
fi
|
||||
}
|
||||
|
||||
348
zsh/functions/systemd-helpers.zsh
Normal file
348
zsh/functions/systemd-helpers.zsh
Normal file
@@ -0,0 +1,348 @@
|
||||
# ============================================================================
|
||||
# Systemd Integration for Arch/CachyOS
|
||||
# ============================================================================
|
||||
# Quick shortcuts and helpers for systemd service management
|
||||
#
|
||||
# Commands:
|
||||
# sc <args> - sudo systemctl
|
||||
# scu <args> - systemctl --user
|
||||
# scr <service> - restart and show status
|
||||
# sce <service> - enable and start
|
||||
# scd <service> - disable and stop
|
||||
# sclog <service> - follow journal logs
|
||||
# sc-failed - show failed services
|
||||
# sc-timers - show active timers
|
||||
# sc-recent - recently started services
|
||||
# sc-boot - boot time analysis
|
||||
# ============================================================================
|
||||
|
||||
# Source shared colors (with fallback)
|
||||
source "${0:A:h}/../lib/colors.zsh" 2>/dev/null || \
|
||||
source "$HOME/.dotfiles/zsh/lib/colors.zsh" 2>/dev/null || {
|
||||
typeset -g DF_GREEN=$'\033[0;32m' DF_YELLOW=$'\033[1;33m'
|
||||
typeset -g DF_RED=$'\033[0;31m' DF_BLUE=$'\033[0;34m'
|
||||
typeset -g DF_CYAN=$'\033[0;36m' DF_NC=$'\033[0m'
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Core Systemctl Shortcuts
|
||||
# ============================================================================
|
||||
|
||||
# System-level systemctl (with sudo)
|
||||
sc() {
|
||||
sudo systemctl "$@"
|
||||
}
|
||||
|
||||
# User-level systemctl
|
||||
scu() {
|
||||
systemctl --user "$@"
|
||||
}
|
||||
|
||||
# Restart service and show status
|
||||
scr() {
|
||||
local service="$1"
|
||||
[[ -z "$service" ]] && { echo "Usage: scr <service>"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Restarting ${service}..."
|
||||
if sudo systemctl restart "$service"; then
|
||||
echo -e "${DF_GREEN}✓${DF_NC} Restarted successfully"
|
||||
echo ""
|
||||
sudo systemctl status "$service" --no-pager -l
|
||||
else
|
||||
echo -e "${DF_RED}✗${DF_NC} Failed to restart ${service}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Enable and start service
|
||||
sce() {
|
||||
local service="$1"
|
||||
[[ -z "$service" ]] && { echo "Usage: sce <service>"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Enabling and starting ${service}..."
|
||||
if sudo systemctl enable --now "$service"; then
|
||||
echo -e "${DF_GREEN}✓${DF_NC} ${service} enabled and started"
|
||||
sudo systemctl status "$service" --no-pager -l | head -15
|
||||
else
|
||||
echo -e "${DF_RED}✗${DF_NC} Failed to enable ${service}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Disable and stop service
|
||||
scd() {
|
||||
local service="$1"
|
||||
[[ -z "$service" ]] && { echo "Usage: scd <service>"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Disabling and stopping ${service}..."
|
||||
if sudo systemctl disable --now "$service"; then
|
||||
echo -e "${DF_GREEN}✓${DF_NC} ${service} disabled and stopped"
|
||||
else
|
||||
echo -e "${DF_RED}✗${DF_NC} Failed to disable ${service}"
|
||||
return 1
|
||||
fi
|
||||
}
|
||||
|
||||
# Follow journal logs for a service
|
||||
sclog() {
|
||||
local service="$1"
|
||||
local lines="${2:-50}"
|
||||
[[ -z "$service" ]] && { echo "Usage: sclog <service> [lines]"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Following logs for ${service} (Ctrl+C to exit)..."
|
||||
sudo journalctl -xeu "$service" -f -n "$lines"
|
||||
}
|
||||
|
||||
# Show recent logs for a service (without follow)
|
||||
sclogs() {
|
||||
local service="$1"
|
||||
local lines="${2:-50}"
|
||||
[[ -z "$service" ]] && { echo "Usage: sclog <service> [lines]"; return 1; }
|
||||
|
||||
sudo journalctl -xeu "$service" -n "$lines" --no-pager
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Service Status Commands
|
||||
# ============================================================================
|
||||
|
||||
# Show failed services (system and user)
|
||||
sc-failed() {
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Failed Services ${DF_BLUE}║${DF_NC}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}System Services:${DF_NC}"
|
||||
local sys_failed=$(systemctl --failed --no-pager --no-legend 2>/dev/null)
|
||||
if [[ -z "$sys_failed" ]]; then
|
||||
echo -e " ${DF_GREEN}✓${DF_NC} No failed system services"
|
||||
else
|
||||
echo "$sys_failed" | sed 's/^/ /'
|
||||
fi
|
||||
|
||||
echo -e "\n${DF_CYAN}User Services:${DF_NC}"
|
||||
local user_failed=$(systemctl --user --failed --no-pager --no-legend 2>/dev/null)
|
||||
if [[ -z "$user_failed" ]]; then
|
||||
echo -e " ${DF_GREEN}✓${DF_NC} No failed user services"
|
||||
else
|
||||
echo "$user_failed" | sed 's/^/ /'
|
||||
fi
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Show active timers
|
||||
sc-timers() {
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Active Timers ${DF_BLUE}║${DF_NC}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}System Timers:${DF_NC}"
|
||||
systemctl list-timers --no-pager | head -20
|
||||
|
||||
echo -e "\n${DF_CYAN}User Timers:${DF_NC}"
|
||||
systemctl --user list-timers --no-pager 2>/dev/null | head -10
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Show recently started/stopped services
|
||||
sc-recent() {
|
||||
local count="${1:-15}"
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Recent Service Activity ${DF_BLUE}║${DF_NC}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}Recently Started:${DF_NC}"
|
||||
systemctl list-units --type=service --state=running --no-pager --no-legend | \
|
||||
head -"$count" | awk '{print " " $1}'
|
||||
|
||||
echo -e "\n${DF_CYAN}Recent Journal (services):${DF_NC}"
|
||||
journalctl -p 3 -xb --no-pager | tail -"$count" | sed 's/^/ /'
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# Boot time analysis
|
||||
sc-boot() {
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Boot Time Analysis ${DF_BLUE}║${DF_NC}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}Boot Summary:${DF_NC}"
|
||||
systemd-analyze
|
||||
|
||||
echo -e "\n${DF_CYAN}Slowest Services (top 10):${DF_NC}"
|
||||
systemd-analyze blame --no-pager | head -10 | sed 's/^/ /'
|
||||
|
||||
echo -e "\n${DF_CYAN}Critical Chain:${DF_NC}"
|
||||
systemd-analyze critical-chain --no-pager 2>/dev/null | head -15 | sed 's/^/ /'
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Service Search and Info
|
||||
# ============================================================================
|
||||
|
||||
# Search for services by name
|
||||
sc-search() {
|
||||
local query="$1"
|
||||
[[ -z "$query" ]] && { echo "Usage: sc-search <query>"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}==>${DF_NC} Searching for services matching: ${query}"
|
||||
echo ""
|
||||
|
||||
systemctl list-unit-files --type=service --no-pager | grep -i "$query"
|
||||
}
|
||||
|
||||
# Show detailed service info
|
||||
sc-info() {
|
||||
local service="$1"
|
||||
[[ -z "$service" ]] && { echo "Usage: sc-info <service>"; return 1; }
|
||||
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Service Info: ${service}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
echo -e "\n${DF_CYAN}Status:${DF_NC}"
|
||||
systemctl status "$service" --no-pager -l 2>/dev/null || \
|
||||
sudo systemctl status "$service" --no-pager -l
|
||||
|
||||
echo -e "\n${DF_CYAN}Unit File:${DF_NC}"
|
||||
systemctl cat "$service" 2>/dev/null | head -30
|
||||
|
||||
echo ""
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Quick Status for MOTD Integration
|
||||
# ============================================================================
|
||||
|
||||
# Get count of failed services (for MOTD/prompt)
|
||||
_systemd_failed_count() {
|
||||
local count=$(systemctl --failed --no-pager --no-legend 2>/dev/null | wc -l)
|
||||
echo "$count"
|
||||
}
|
||||
|
||||
# Check if a service is active (for scripts)
|
||||
_systemd_is_active() {
|
||||
local service="$1"
|
||||
systemctl is-active --quiet "$service" 2>/dev/null
|
||||
}
|
||||
|
||||
# Check if a service is enabled (for scripts)
|
||||
_systemd_is_enabled() {
|
||||
local service="$1"
|
||||
systemctl is-enabled --quiet "$service" 2>/dev/null
|
||||
}
|
||||
|
||||
# ============================================================================
|
||||
# Interactive Service Management (requires fzf)
|
||||
# ============================================================================
|
||||
|
||||
if command -v fzf &>/dev/null; then
|
||||
# Interactive service selector
|
||||
scf() {
|
||||
local service=$(systemctl list-units --type=service --no-pager --no-legend | \
|
||||
awk '{print $1, $2, $3, $4}' | \
|
||||
fzf --height=50% --layout=reverse --border=rounded \
|
||||
--prompt='Service > ' \
|
||||
--preview='systemctl status {1} --no-pager' \
|
||||
--preview-window=right:50%:wrap | \
|
||||
awk '{print $1}')
|
||||
|
||||
if [[ -n "$service" ]]; then
|
||||
echo -e "${DF_BLUE}Selected:${DF_NC} $service"
|
||||
echo ""
|
||||
echo "Actions: [s]tatus [r]estart [o]stop [l]ogs [e]nable [d]isable [q]uit"
|
||||
read -k 1 "action?Action: "
|
||||
echo ""
|
||||
|
||||
case "$action" in
|
||||
s) sudo systemctl status "$service" --no-pager -l ;;
|
||||
r) scr "$service" ;;
|
||||
o) sudo systemctl stop "$service" ;;
|
||||
l) sclog "$service" ;;
|
||||
e) sce "$service" ;;
|
||||
d) scd "$service" ;;
|
||||
q) return 0 ;;
|
||||
*) echo "Unknown action" ;;
|
||||
esac
|
||||
fi
|
||||
}
|
||||
|
||||
# Interactive log viewer
|
||||
sclogf() {
|
||||
local service=$(systemctl list-units --type=service --no-pager --no-legend | \
|
||||
awk '{print $1}' | \
|
||||
fzf --height=40% --layout=reverse --prompt='Service logs > ')
|
||||
|
||||
[[ -n "$service" ]] && sclog "$service"
|
||||
}
|
||||
fi
|
||||
|
||||
# ============================================================================
|
||||
# Aliases
|
||||
# ============================================================================
|
||||
|
||||
alias scs='sc status'
|
||||
alias scstart='sc start'
|
||||
alias scstop='sc stop'
|
||||
alias screload='sc daemon-reload'
|
||||
alias scmask='sc mask'
|
||||
alias scunmask='sc unmask'
|
||||
|
||||
# Journal shortcuts
|
||||
alias jctl='journalctl'
|
||||
alias jctlf='journalctl -f'
|
||||
alias jctlb='journalctl -b'
|
||||
alias jctlerr='journalctl -p err -b'
|
||||
|
||||
# ============================================================================
|
||||
# Help
|
||||
# ============================================================================
|
||||
|
||||
sc-help() {
|
||||
echo -e "${DF_BLUE}╔════════════════════════════════════════════════════════════╗${DF_NC}"
|
||||
echo -e "${DF_BLUE}║${DF_NC} Systemd Helper Commands ${DF_BLUE}║${DF_NC}"
|
||||
echo -e "${DF_BLUE}╚════════════════════════════════════════════════════════════╝${DF_NC}"
|
||||
|
||||
cat << 'EOF'
|
||||
|
||||
Core Commands:
|
||||
sc <args> sudo systemctl <args>
|
||||
scu <args> systemctl --user <args>
|
||||
scr <service> Restart and show status
|
||||
sce <service> Enable and start (--now)
|
||||
scd <service> Disable and stop (--now)
|
||||
sclog <service> Follow journal logs (-f)
|
||||
scogs <service> Show recent logs (no follow)
|
||||
|
||||
Status Commands:
|
||||
sc-failed Show failed services
|
||||
sc-timers Show active timers
|
||||
sc-recent Recently started services
|
||||
sc-boot Boot time analysis
|
||||
sc-search <query> Search services by name
|
||||
sc-info <service> Detailed service info
|
||||
|
||||
Interactive (requires fzf):
|
||||
scf Interactive service manager
|
||||
sclogf Interactive log viewer
|
||||
|
||||
Aliases:
|
||||
scs sc status
|
||||
scstart sc start
|
||||
scstop sc stop
|
||||
screload sc daemon-reload
|
||||
|
||||
Journal:
|
||||
jctl journalctl
|
||||
jctlf journalctl -f
|
||||
jctlb journalctl -b (current boot)
|
||||
jctlerr journalctl -p err -b
|
||||
|
||||
EOF
|
||||
}
|
||||
Reference in New Issue
Block a user