Skip to content

Instantly share code, notes, and snippets.

@uPaymeiFixit
Last active December 19, 2023 21:18
Show Gist options
  • Save uPaymeiFixit/14f8e6bf9af6c4cc634f9b2f1f7ff1dd to your computer and use it in GitHub Desktop.
Save uPaymeiFixit/14f8e6bf9af6c4cc634f9b2f1f7ff1dd to your computer and use it in GitHub Desktop.
Fake lscpu output
#!/usr/bin/env bash
set -e
# Usage: Run this command from anywhere. It will back up the original lscpu
# command, and replace it with a new command that outputs hard-coded CPU
# information.
program="lscpu"
program_path="$(which "$program")"
program_backup_path="$program_path.bak"
function l () {
echo "$@" 2> /dev/stderr
}
if [ ! -f "$program_backup_path" ]; then
l "Backing up $program_path to $program_backup_path"
mv "$program_path" "$program_backup_path"
fi
touch "$program_path"
chmod +x "$program_path"
l "Installing fake $program in $program_path"
cat > "$program_path" << EOL
#!/usr/bin/env bash
set -e
native_output="\$($program_backup_path \$@)"
model_name='AMD EPYC 7763 64-Core Processor'
address_sizes='48 bits physical, 48 bits virtual'
model_number='1'
cpu_max_mhz='2450.0000'
cpu_min_mhz='1500.0000'
bogo_mips='4891.25'
native_output="\$(echo "\$native_output" \
| sed -E 's/(^\s*Model name:\s*)(\w.*)$/\1'"\$model_name"'/i' \
| sed -E 's/(^\s*Model:\s*)(\w.*)$/\1'"\$model_number"'/i' \
| sed -E 's/(^\s*Address sizes:\s*)(\w.*)$/\1'"\$address_sizes"'/i' \
| sed -E 's/(^\s*BogoMIPS:\s*)(\w.*)$/\1'"\$bogo_mips"'/i' \
| sed -E 's/(^\s*CPU max Mhz:\s*)(\w.*)$/\1'"\$cpu_max_mhz"'/i' \
| sed -E 's/(^\s*CPU min Mhz:\s*)(\w.*)$/\1'"\$cpu_min_mhz"'/i' \
)"
echo "\$native_output"
EOL
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment