You are not logged in.

#1 2009-10-14 23:45:32

xduugu
Member
Registered: 2008-10-16
Posts: 292

fakeuname - faking uname's output

Some weeks ago, I wrote a small script which I want to present to you now. It's called fakeuname and this is pretty much what is does. I wrote it originally because it bugged me that I had to have a specific kernel running when I want to compile kernel module PKGBUILDs for this kernel (or had to hardcode the right version strings).

Basically, I only needed a possibility to change to output of `uname -r`, but because it was not much more work to do to support all options of uname, everything can be faked now (which is probably quite useless, but at least funny wink ).

It's usage is fairly simple. You pass the uname option whose output you want to alter along with the new string and append the command which should be run in this faked uname environment at the end, e.g.

fakeuname -r 2.6.31.4-vanilla makepkg

or

fakeuname -s HURD uname -a

Maybe this script is useful for someone else, too.


#!/bin/sh

uname=$(which uname)

dir=$(mktemp -d --tmpdir uname.XXXXXXXX)

kernel=$($uname -s)
nodename=$($uname -n)
kernel_rel=$($uname -r)
kernel_ver=$($uname -v)
machine=$($uname -m)
processor=$($uname -p)
hw=$($uname -i)
os=$($uname -o)

# print usage
usage() {
    echo "Usage: $( basename "$0" ) [options] [ -- ] command"
    echo "Options:"
    echo "    -s kernel-name"
    echo "    -n nodename"
    echo "    -r kernel-release"
    echo "    -v kernel-version"
    echo "    -m machine"
    echo "    -p processor"
    echo "    -i hardware-platform"
    echo "    -o operating-system"
    exit 0
}

cleanup() {
    rm -rf -- "$dir"
}


# always clean up temporary folder
trap 'cleanup' EXIT TERM HUP QUIT INT

[ $# -eq 0 ] && usage

current=""
for arg in "$@"; do
    case "$arg" in
        -s|--kernel-name)       shift ; current=kernel     ;;
        -n|--nodename)          shift ; current=nodename   ;;
        -r|--kernel-release)    shift ; current=kernel_rel ;;
        -v|--kernel-version)    shift ; current=kernel_ver ;;
        -m|--machine)           shift ; current=machine    ;;
        -p|--processor)         shift ; current=processor  ;;
        -i|--hardware-platform) shift ; current=hw         ;;
        -o|--operating-system)  shift ; current=os         ;;
        --) shift ; break ;;
        -*) echo -e >&2 "$( basename "$0" ): Invalid option \`$arg' found\n" && usage ;;
        *) [ -n "$current" ] && eval "$current='$arg'" && current="" && shift || break ;;
    esac
done

for arg in "$@"; do
    cmd="$cmd '$arg'"
done

# write uname file
cat > "$dir/uname" <<EOF
#!/bin/sh

uname_output() {
    $uname "\$1";
    exit \$?
}

for arg in "\$@"; do
    if [ "\${arg:0:1}" = "-" ]; then
        case "\$arg" in
            --help) uname_output "\$arg" ;;
            --version) uname_output "\$arg" ;;
            -a|--all)
                echo "$kernel $nodename $kernel_rel $kernel_ver $machine $processor $hw $os"
                exit \$? ;;
            -s|--kernel-name) ;;
            -n|--nodename) ;;
            -r|--kernel-release) ;;
            -v|--kernel-version) ;;
            -m|--machine) ;;
            -p|--processor) ;;
            -i|--hardware-platform) ;;
            -o|--operating-system) ;;
            *) uname_output "\$arg" ;;
        esac
    else
        uname_output "\$arg"
    fi
done

for arg in "\$@"; do
    case "\$arg" in
        -s|--kernel-name) echo -n "$kernel";;
        -n|--nodename) echo -n "$nodename";;
        -r|--kernel-release) echo -n "$kernel_rel";;
        -v|--kernel-version) echo -n "$kernel_ver";;
        -m|--machine) echo -n "$machine";;
        -p|--processor) echo -n "$processor";;
        -i|--hardware-platform) echo -n "$hw";;
        -o|--operating-system) echo -n "$os";;
    esac
done

# new line
echo

EOF

chmod a+x "$dir/uname"
export PATH="$dir/:$PATH"

eval $cmd

Offline

#2 2009-10-15 03:23:19

smartboyathome
Member
From: $HOME
Registered: 2007-12-23
Posts: 334
Website

Re: fakeuname - faking uname's output

Excellent! I used it, and produced this:

HURD Deathnote K16 Custom Configured x86 
Intel(R) Core(TM)2 Duo CPU T7300 @ 2.00GHz GenuineIntel GNU/Hurd

It's a great script for playing pranks on people! XD

Offline

#3 2009-10-15 05:34:48

Themaister
Member
From: Trondheim, Norway
Registered: 2008-07-21
Posts: 652
Website

Re: fakeuname - faking uname's output

Haha :V Awesome. That's actually useful when compiling some software from chroot.

Last edited by Themaister (2009-10-15 05:35:22)

Offline

#4 2015-04-17 09:43:45

thettalos
Member
Registered: 2015-04-17
Posts: 1

Re: fakeuname - faking uname's output

I managed to fake uname using the following python script. I copied the script inside /usr/local/bin, then  I movied /bin/uname to /bin/uname.bak and finally I created a symlink typing: ln -s /usr/local/bin/fake_uname.py /bin/uname.
My case is an installation of commercial software over docker which requires an older redhat kernel.
After faking the kernel version the installation proceeded normally.
I hope it helps.

-----------------------------------------------

#!/usr/bin/python

import sys
kernel_name="Linux"
kernel_name="linux_redhat_enterprise_6"
node_name="controlm"
kernel_release="2.6.32-220.2.1.el6.x86_64"
kernel_release="2.4.21-4.EL"
kernel_version="#1 SMP Tue Dec 13 16:21:34 EST 2011"
machine="x86_64"
processor="x86_64"
hardware_platform="x86_64"
operating_system="GNU/Linux"
param_map = {
    "-s" : kernel_name,
    "-n" : node_name,
    "-r" : kernel_release,
    "-v" : kernel_version,
    "-m" : machine,
    "-p" : processor,
    "-i" : hardware_platform,
    "-o" : operating_system,
    "-a" : "%s %s %s %s %s %s %s %s" % (kernel_name,node_name,kernel_release,kernel_version,machine,processor,hardware_platform,operating_system)
}

output=""
user_args=sys.argv
if len(user_args) == 1:
        user_args=["-s"]

for arg in user_args:
    if param_map.has_key(arg):
        output+=" "  + param_map[arg]

print output.strip()

Offline

Board footer

Powered by FluxBB