Adding Swap Space


UML is currently running everything in the memory that it has been assigned since it has no swap space. Normal Linux machines have some swap, so let's fix that now.

We need some sort of disk to swap onto, and since UML disks are generally host files, we need to make a file on the host to be the swap device:

host% dd if=/dev/zero of=swap bs=1024 seek=$[ 1024 * 1024 ] count=1 1+0 records in 1+0 records out host% ls -l swap -rw-rw-rw-  1 jdike jdike 1073742848 Feb 18 12:31 swap


This technique uses dd to create a 1GB sparse file on the host by seeking 1 million 1K blocks and then writing a 1K block of zeros there. The use of sparse files is pretty standard with UML since it allows host disk space to be allocated only when it is needed. So, this swap device file consumes only 1K of disk space, even though it is technically 1GB in length.

We can see the true size, that is, the actual disk space consumption, of the file by adding -s to the ls command line:

host% ls -ls swap 12 -rw-rw-r-   1 jdike jdike 1073742848 Oct 27 17:27 swap


The 12 in the first column is the number of disk blocks actually occupied by the file. A disk block is 512 bytes, so this file that looks like it's 1GB in length is taking only 6K of disk space.

Now, we need to plug this new file into the UML as an additional block device, which we will do with the management console:

host% uml_mconsole debian config ubdb=swap OK


We can check this by asking for the configuration of ubdb in the same way we asked about consoles earlier:

host% uml_mconsole debian config ubdb OK /home/jdike/swap


Now, back in the UML, we have a brand-new second block device, so let's set it up for swapping, then swap on it, and look at /proc/meminfo to check our work:

UML# mkswap /dev/ubdb Setting up swapspace version 1, size = 1073737728 bytes UML# swapon /dev/ubdb UML# grep Swap /proc/meminfo SwapCached:          0 kB SwapTotal:     1048568 kB SwapFree:      1048568 kB


Let's further check our work by forcing the new swap device to be used. The following command creates a large amount of data by repeatedly converting the contents of /dev/mem (the UML's memory) into readable hex and feeds that into a little perl script that turns it into a very large string. We will use this string to fill up the system's memory and force it into swap.

UML# while true; do od -x /dev/mem ; done | perl -e 'my $s ; \ while(<STDIN>){ $s .= $_; } print length($s);'


At the same time, let's log in on a second console and watch the free memory disappear:

UML# while true; do free; sleep 10; done


You'll see the system start with almost all of its memory free:

             total     used       free   shared   buffers \ cached Mem:        126696    21624     105072        0       536 \ 7808 -/+ buffers/cache:    13280     113416 Swap:      1048568        0    1048568


The free memory will start disappearing, until we see a nonzero entry under used for the Swap row:

             total     used      free   shared   buffers   cached Mem:         126696  124548      2148        0        76     7244 -/+ buffers/cache:   121823      9468 Swap:       1048568    6524    1042044


Here UML is behaving exactly as any physical system wouldit is swapping when it is out of memory. Note that the host may have plenty of free memory, but the UML instance is confined to the memory we gave it.



User Mode Linux
User Mode Linux
ISBN: 0131865056
EAN: 2147483647
Year: N/A
Pages: 116
Authors: Jeff Dike

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net