Misc

<< Back to wiki homepage

Table of contents:

Devices

Disable/enable a USB device

First get the port of device:

for dev in $(ls /sys/bus/usb/devices/*/product); do echo $dev ;cat $dev ;done

You'll get a number like 1.1-3 or 3-6 which is the port. To deactivate it:

echo '3-6' | sudo tee /sys/bus/usb/drivers/usb/unbind

To mount it again, just use bind endpoint instead.

Query lid state of a laptop

cat /proc/acpi/button/lid/LID0/state

SSH

Get pubkey from an existing private key

ssh-keygen -f /path/to/private_key -y

IO

Check IO utilization

Thanks to konstantinbest!

iostat -xdmt 1

will show you extended metrics for every disk in your system, where %util is the utilization in percent, aqu-sz is the io queue where everything above 2 is not good (depending on your disk architecture)

sar -p -d 1

will show you the same with aggregated sum of iops (w and r) in a more readable way plus the wait time for an IO request

Modules

List all modules with their parameter values

cat /proc/modules | cut -f 1 -d " " | while read module; do \
 echo "Module: $module"; \
 if [ -d "/sys/module/$module/parameters" ]; then \
  ls /sys/module/$module/parameters/ | while read parameter; do \
   echo -n "Parameter: $parameter --> "; \
   cat /sys/module/$module/parameters/$parameter; \
  done; \
 fi; \
 echo; \
done

Permissions

Backup & restore file permissions:

getfacl -R /var/www/myweb > permissions.acl

setfacl --restore=permissions.acl

Size calculation

Calculate whatever total storage files/folders matching a regexp has

find /data/backup -regex '.*.war..*' -print0 | du --files0-from=- -ch | tail -1

Find open pipes

(find /proc -type l | xargs ls -l | grep pipe) 2>/dev/null

Find which PID occupies a port

fuser -v -n tcp 1111

List all child processes of a PID

ps --forest -o pid,tty,stat,time,cmd -g <PID>
find -L /path -type l

Systemtap scripts

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
#! /usr/bin/env stap

global slabs

probe vm.kmem_cache_alloc {
        slabs [execname(), bytes_req]<<<1
}

probe timer.ms(10000)
{
        dummy = "";
        foreach ([name, bytes] in slabs) {
            if (dummy != name)
                if (bytes*@count(slabs[name, bytes]) > 100000)
                    printf("Name:%s\t\t\tTotal:%d\n", name, bytes*@count(slabs[name, bytes]));
            dummy = name;
        }
        delete slabs
        printf("\n-------------------------------------------------------\n\n")
}

Detect SLAB leaks

Run like: stap -v --all-modules kmem_alloc7.stp dentry

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#! /usr/bin/env stap
# This script displays the number of given slab allocations and the backtraces leading up to it.

global slab = @1
global stats, stacks
probe kernel.function("kmem_cache_alloc") {
        if (kernel_string($s->name) == slab) {
                stats[execname()] <<< 1
                stacks[execname(),kernel_string($s->name),backtrace()] <<< 1
        }
}
# Exit after 10 seconds
# probe timer.ms(10000) { exit () }
probe end {
        printf("Number of %s slab allocations by process\n", slab)
        foreach ([exec] in stats) {
                printf("%s:\t%d\n",exec,@count(stats[exec]))
        }
        printf("\nBacktrace of processes when allocating\n")
        foreach ([proc,cache,bt] in stacks) {
                printf("Exec: %s Name: %s  Count: %d\n",proc,cache,@count(stacks[proc,cache,bt]))
                print_stack(bt)
                printf("\n-------------------------------------------------------\n\n")
        }
}

Awk

Split lines which is 20 chars to 10 chars

awk 'length != 20 { print ; next ; }
    { printf "%s\n%s\n",substr($0,0,10),substr($0,11) }' file.txt

Sed

Just the accepted parameters will be listed, either apply via pipe or -i etc.

Multiple stuff can be combined via semicolon.

Remove comments

(Considering they start with #)

'/^[ \t]*#/d'

Remove empty lines

'/^$/d'

Put empty line above matching lines

'/mypattern/{x;p;x;}'

Put empty line below matching lines

'/mypattern/G'

Add an empty line on each 5 lines

'0~5G' # GNU sed
'n;n;n;n;G;'

Let's say longer than 65:

'/^.\{65\}/p'