Tag Archives: shell script

nbdkit inline scripts

I have proposed a patch for nbdkit, our flexible, pluggable Network Block Device server, to make writing Linux block devices into a (long) single command.

Here’s a simple block device with virtual size 1M that reads as zeroes:

nbdkit sh - <<'EOF'
    case "$1" in
        get_size) echo 1M ;;
        pread) dd if=/dev/zero count=$3 iflag=count_bytes ;;
        *) exit 2 ;;
    esac
EOF
Advertisements

Leave a comment

Filed under Uncategorized

Tip: List all files in a virtual machine

Note: This requires libguestfs 1.5.x or the libguestfs 1.4.3 + backports that we are shipping in Fedora 13 updates testing.

Quick tip: list all the files in a VM:

$ guestfish --ro -a disk.img -i find0 / - | tr '\000' '\n' | sort | less

Explanation:

  1. guestfish --ro -a disk.img -i is the new style way to inspect a disk image using guestfish. For a libvirt domain, use guestfish --ro -d GuestName -i instead.
  2. find0 / - lists all the files to stdout.
  3. The filenames are separated by ASCII NUL character, tr '\000' '\n' translates that to newlines.
  4. sort | less sorts and pages the output.

Leave a comment

Filed under Uncategorized

Quick tip: Timing things in a shell script

We all know about the ‘time’ command. That’s great if you have one command that you want to time.

But how about if you want to time several actions together in a shell script, eg:

#!/bin/sh -
start_the_clock
A
B
C
how_long_so_far
D
E
stop_the_clock

The ‘time’ command isn’t so useful for this, unless you want to stick ‘time’ in front of each action A, B, C, D & E and add them up in your head.

But there’s a nice little date/awk trick you can use for this:

#!/bin/sh -
date +%s.%N > /tmp/times
A
B
C
date +%s.%N >> /tmp/times
D
E
date +%s.%N >> /tmp/times
awk '{ if (!start) { start = $1 } else { print $1-start } }' < /tmp/times

which would print something out like this:

25.2957   # the time that actions A-C took
128.529   # the time that actions A-E took

(Thanks Jim Meyering for suggesting date +%s.%N)

1 Comment

Filed under Uncategorized