In an earlier post I
discussed how to trace calls using GDB so that function calls and their
arguments can easily be viewed. What I neglected to mention was
rbreak , a
feature of GDB to be able to set breakpoints using a regular
expression.
Using rbreak you can get the same functionality but with much less
effort. For example, to get the same behavior as before (setting a
breakpoint on each function call and printing a trace of the bottom most
level) all you need to provide to GDB are the following commands:
\
Mount Complex Disk Images Using libguestfs
In my previous
post I went
over two ways to mount a partition within a disk image file. There is
actually another easy way to do this by utilizing some of the tools that
the virt community has provided us in recent years.
libguestfs is a fairly comprehensive library
for manipulating guest disk images and filesystems. It turns out that
the tools work pretty well even for disk images that aren’t specific to
any virtualized guest; after all a disk image is a disk image.
The guestmount utility is provided as part of the libguestfs-tools-c
package in Fedora 17 and allows us the ability to (in)directly mount the
second partition of our disk image. This is shown below:
\
Mounting a Partition Within a Disk Image
Last time
I walked through creating a sparse disk image using dd and
cp --sparse=always. OK, we have a disk image. Now what?
Normally it would suffice to just set up a loop device and then mount,
but this disk image doesn’t just contain a filesystem. It has 4
partitions each with their own filesystem. This means in order to mount
one of the filesystems we have to take a few extra steps.
There is an easy and a hard way to do this. I’ll start with the hard
way..\
Create A Disk Image Without Enough Free Space
Recently I purchased a new laptop. Typically my first move is to ditch
Windows or Mac (yes I’ve done both) and install Linux. This time, just
in case I ever want to completely restore the system (recovery partition
and all), I decided to make a disk image of the entire hard drive.
Being that the capacity of the hard drive is close to 500 GB it actually
turned out that I didn’t have enough free space to store the entire
image. However, since it was a brand new computer I knew there was
plenty of zeroes on the disk and thus plenty of opportunity to reduce
the actual size of the disk image by converting those zeroes into holes
in a sparse file .
Now usually when creating a disk image I would just use dd, but dd
doesn’t have any options that are sparse file aware. Luckily I was able
to find a
page
that addressed my particular predicament.
The secret sauce here is the --sparse=always option of cp. By piping
the output of dd to cp, I could then copy my disk image! The next
step was to fire up a live cd, connect an external hard drive and run
the following command to create the disk image:\
Trace Function Calls Using GDB
Sometimes it is easier to debug when you are able to view a call trace
of all function calls in a particular program. This is especially true
when working with code that isn’t yours or when debugging issues such as
infinite loops in your own code. The way I typically do this is by
creating a GDB commands file that defines breakpoints for each function
I would like to see in the trace.
For each function breakpoint, I instruct GDB to print a short backtrace
and then continue execution. I can then stop the program run at any time
using CTRL-C and observe where the program is, what functions are
being called, and what arguments they are being called with.
To illustrate this, consider the following short program that accepts
two arguments. It takes these two arguments, squares them, and then
finds the lowest common multiple of their squares. This means if you
pass in 4 and 10, it will find the LCM of 16 and 100.
NOTE: I have defined functions for basic operations such as squaring
a number and adding two numbers just for illustrative purposes.
\
Share a Folder Between KVM Host and Guest
I often find myself in situations where I need to share information or
files between a KVM host and KVM guest. With libvirt version 0.8.5 and
newer there is support for mounting a shared folder between a host and
guest. I decided to try this out on my Fedora 17 host, with a Fedora 17
guest.
Using the libvirt
<filesystem>
xml tag I created the following xml that defines a filesystem device.
\
Exchanging SSH keys using ssh-copy-id
It is common practice among Linux users to exchange ssh keys between
machines so that you can ssh between them without having to
authenticate. The manual process for doing this involves taking the
public key of the local host (~/.ssh/id_rsa.pub or
~/.ssh/id_dsa.pub) and appending it to the ~/.ssh/authorized_keys
file of the remote host you wish to log in without a password.
This process is simple, but requires a few different steps. Luckily,
openssh includes a nifty little shell script that will take care of all
of the manual steps for you. This script is called ssh-copy-id and
should be available on your Linux distro as long as you are using the
openssh client.
In order to use it all you need to do is provide a username and a remote
host to log in to. It will then copy your key to the authorized_keys
file of the remote host and from then on you should be able to log in
without authenticating. This is illustrated below:
\
Easily Resize LVs and Underlying Filesystems
Part of the reason I use Logical Volumes for my block devices rather
than standard partitions is because LVs are much more flexible when it
comes to sizing/resizing.
For example, in a particular setup you might have a 1 TB hard drive that
you want to be broken up into two block devices. You could either choose
two 500 GB partitions, or two 500 GB LVs. If you use partitions and
later find out that you really needed 300 GB for one and 700 GB for the
other then resizing might get a little complicated. On the other hand,
with LVs resizing is simple!
LVM has the ability to resize the LV and the underlying filesystem at
the same time (it uses fsadm under the covers to resize the
filesystem which on my system supports resizing
ext2/ext3/ext4/ReiserFS/XFS). In order to pull this off simply use
lvresize along with the --resizefs option. An example of this
command is shown below:
\
Send Magic SysRq to a KVM guest using virsh
When a linux computer is “hung” or “frozen” you can use a Magic
SysRq key sequence to
send various low level requests to the kernel in order to try to recover
from or investigate the problem. This is extremely useful when
troubleshooting server lockups, but until recently
libvirt did not expose this
functionality for
KVM
guests.
In v0.9.3 (and newer) of libvirt you can send a Magic SysRq sequence to
a guest by utilizing the send-key subcommand provided by virsh. In
other words, sending the ‘h’ Magic SysRq command is as simple as:
\
Terminal Screencast Revisit: Log output from a multiplexed terminal
Traditionally with script you have really only been able to log the
output of a simple “single session” terminal window. However, if you
capture the timing data from script you can actually log much more
than that.
In a previous post
I talked about how to save the timing data using script and then play
it back using scriptreplay. If you use this technique and also use a a
terminal multiplexer such as screen or tmux you can capture much
more information in your screencast.
To see an example of this you can watch the video below where I use
scriptreplay to play back an earlier recorded screencast.
\