Monday, 30 July 2007

Backup your system using TAR

My system is a Debian Sid one (This article was first tested in an Ubuntu Feisty system)
Anyway this article is also useful in most other linux distros.

Tar tool allows us to make backups or even restore them, while our system is running.


It is a good idea becoming root to be able to access every file on the system, or at least you should use sudo command:

$sudo tar cvzpf /home/Backup.tgz --same-owner --exclude=/home/Backup.tgz --exclude=/home/error.log --exclude=/proc/* --exclude=/media/* --exclude=/dev/* --exclude=/mnt/* --exclude=/sys/* --exclude=/tmp/* / 2>/home/error.log


Tar options and arguments


c creates a tar backup
v activates verbose mode
z will compress data using gzip format
p preserves file permissions
f sends output to a file instead of standard output.

After f option it is necessary to write the file name where backup will be stored.
i.e: tar cvzpf /home/Backup.tgz is correct, but
tar cvzfp /home/Backup.tgz is wrong.

/home/Backup.tgz is our backup file name.

--same-owner preserve file ownership, it is not necessary because, being root, is a default option.



Excluding files from Backup


--exclude option gives us the possibility of excluding files and directories from backup.

--exclude=/home/Backup.tgz --exclude=/home/error.log Prevent Backup file and error log file to be stored.


--exclude=/proc/* --exclude=/dev/* --exclude=/sys/*

These are virtual directories so will not be stored.

NOTE: Difference between --exclude=/proc/* and --exclude=/proc is that first option would store a void proc directory, and second one would store nothing.

--exclude=/tmp/*

/tmp is a temporary directory, and is deleted on every boot, so will not be backed up either.


--exclude=/mnt/* --exclude=/media/*

If you want to save your main system only, and not what you have mounted on it, you should exclude these directories.


WHICH DIRECTORIES TO EXCLUDE?

Wednesday, 25 July 2007

Errors and Solutions

Solutions to common linux related errors appear frequently along this blog.
Unfortunately it is very difficult to track them from main page.

This blog entry shows a list of common errors and corresponding articles where solutions are explained.

Wednesday, 18 July 2007

Installing Qemu on a Ubunty Feisty System

That simply command will install qemu emulator:
$sudo aptitude install qemu

Right now, qemu works as an emulator, that is, emulates every guest system instruction on the host system.
Emulating is a slow working mode, but if qemu runs on a x86 platform, it is possible to execute guest code directly as host code so speed will improve. (That is called virtualization)


INSTALLING KQEMU MODULE

To use virtualization mode in qemu, we have to install kqemu.

#aptitude install kqemu-common
That package has some documentation and a configuration file, but is not really kqemu module

$sudo aptitude install kqemu-source # Installs kqemu module sources


Compiling sources:

NOTE: You will need libc6-dev package to be able to compile kqemu sources.
If you haven't it: $sudo aptitude install libc6-dev

$sudo su # Becoming root

#cd /usr/src
# tar xvfj kqemu.tar.bz2
#cd modules/kqemu
# ./configure
#make
#make install


kqemu module is now installed!!

Gonna load kqemu module:
#modprobe kqemu

and then run qemu:
#qemu -m 256 -cdrom .....

Appears an error:
"Could not open '/dev/kqemu' - QEMU acceleration layer not activated"

This bug occurs because qemu searchs for a module which major number equals zero and kqemu-common has set it to 250.

To solve that:
#rmmod kqemu
#gedit /etc/modprobe.d/kqemu


and change 250 by 0

again:
#modprobe kqemu
#qemu -m 256 -cdrom .......



SETTING THE NETWORK IN QEMU

If you dont specify network options when invoking qemu, then defaults apply.

This default options are: -net nic -net user
-net nic : emulates a NE2000 network interface card.
-net user: use the user mode network stack which requires no administrator priviledge to run.

That should be enough to have network working in your virtual machine but with some limitations:
* Pings are not available.
* By default User mode network stack allow us to navigate and connect to our host system, but not to act as a server.

User mode network stack has a built in dhcp server that will provide us an ip address.

If everything has gone fine you can open a browser and surf on the internet.

If you cannot connect to internet, open a console and type:
$ifconfig

check if virtual nic has its associated ip address and if not, exec this command:
$sudo dhclient eth0 # eth0 is your virtual nic

try your browser again.


NOTE: Another way to configure the network in qemu consists in using a tap device and a network software bridge, instead of using user mode network stack.

Saturday, 7 July 2007

Apt, Aptitude and Dpkg Reference

Apt, Aptitude and Dpkg are Debian package managing tools. Let's see a small reference about them:

# apt-get install foo ....................... Installs foo package
# apt-get remove foo ........................ Removes foo package
# apt-get remove --purge foo ................ Removes foo package and its configuration files.
# apt-get update ............................ Updates package database.
# apt-get -f install......................... Installs and removes packages in order to fix dependency problems.
# apt-get clean ............................. Deletes all .deb packages from local repository.
# apt-get upgrade ........................... Upgrades all packages to its newer versions.
# apt-get dist-upgrade ...................... Upgrades packages but also deals with dependency problems caused by new packages.
$ apt-get source foo ........................ Downloads foo package source files.
# apt-get -t unstable foo ................... Installs foo package searching for foo package and its dependencies at unstable version.
# apt-get foo/unstable ...................... Installs foo package from unstable version, but searchs for dependencies without overriding priorities at /etc/apt/preferences file.
# apt-get build-dep foo ..................... Installs all necessary packages to satisfy the build dependencies for foo source package.

$ apt-cache show foo ........................ Gives a long info description about foo package.
$ apt-cache search foo ...................... Searchs for packages that match "foo" pattern.

# apt-file update ........................... Resynchronize the package contents from their sources (/etc/apt/sources.list)
$ apt-file search foo ....................... Lists all packages containing files that match foo pattern. Searchs also in not installed packages.

# aptitude install foo ...................... Installs foo package
# aptitude remove foo ....................... Removes foo package
# aptitude remove --purge foo ............... Removes foo package and its configuration files.
$ aptitude search foo ....................... Searchs for packages that match "foo" pattern
# aptitude update ........................... Updates package database.
# aptitude upgrade .......................... Upgrades packages to its newer versions.
# aptitude safe-upgrade ..................... Same as aptitude upgrade.
# aptitude full-upgrade ..................... Upgrades packages to its newer versions, taking care about dependencies. Sames as dist-upgrade.
# aptitude hold foo ......................... Marks foo package so it will not be removed or upgraded when executing safe-upgrade or full-upgrade
# aptitude unhold foo ....................... Turn off the hold on foo package.
$ aptitude show foo ......................... Gives a long info description about foo package.
# aptitude clean ............................ Removes all previously downloaded .deb files from the package cache directory.
$ aptitude download foo ..................... Downloads foo package to current directory.
#aptitude build-deps foo .................... Installs all binary dependencies needed to build foo source package.

# dpkg -i foo ............................... Installs foo package
# dpkg -i --force-depends foo ............... Converts all dependency errors into warnings and installs foo package.
# dpkg -r foo ............................... Removes foo package
# dpkg --purge foo .......................... Removes foo package and its configuration files too.
$ dpkg -l *foo* ............................. Lists packages containing "foo" pattern.
$ dpkg -L foo ............................... Lists files pertaining to foo package.
# dpkg-reconfigure -plow foo ................ Reconfigures previously installed "foo" package, asking all configuration questions.
# dpkg --configure -a ........................... Configures all packages that have been unpacked but not yet configured.
$ dpkg --search foo ......................... Lists packages containing files matching foo pattern. Only searchs on installed packages.
$ dpkg -c foo.deb ........................... List files contained in foo.deb package.
$ dpkg -I foo.deb ........................... Shows info about foo.deb package.

$ dpkg-query -S foo ......................... Searchs for a filename from installed packages listed in the local dpkg database.

$ dpkg-buildpackage -us ..................... Builds debian package from its source code.


More info at:
The Debian package management tools.
APT Howto.
Debian package management.

How to create an Ubuntu UsbLive with persistence

First step is downloading Ubuntu Dapper iso file:

NOTE: Newer versions(Feisty and Gutsy), by now, july 2007, have a bug that prevents to use persistence feature.

You can navigate to Ubuntu download page and select Ubuntu 6.06 LTS Desktop edition (alias Dapper),

or simply enter at console command line:
$wget http://releases.ubuntu.com/dapper/ubuntu-6.06.1-desktop-i386.iso


I recommend a usb pendrive with a capacity higher than 1 Gigabyte, because you will need at least 700 megabytes to copy dapper iso file on it.
In my case I use a 2G pendrive.


Connect the usb pendrive so it be detected.

$dmesg command will show something like:

[ 2697.454838] usb-storage: device found at 4
...
[ 2704.387453] SCSI device sdh: 4029440 512-byte hdwr sectors (2063 MB)


$fdisk -l also shows the new device.

If you still dont find it, you can seek for it at $cat /proc/bus/usb/devices output.


On my system the new device appears as /dev/sdh, in this tutorial, to be more general, I will write it as /dev/sdX.


Next step will be partitionating our usb pendrive: (choose the program you prefer: fdisk, cfdisk, gparted ...)

#fdisk /dev/sdX

(for help on this step read $man fdisk)


I partitionate the device into two partitions sdX1 and sdX2, both sized one gigabyte, and make bootable the first one, showing this result with fdisk:

DeviceActiveBeginningEndBlocksIdSystem
/dev/sdX1*1493978081bW95 FAT32
/dev/sdX24941015103564883Linux





Umount both partitions if they are mounted and then format them with:

#mkfs.vfat -F 32 -n usb /dev/sdX1
#mkfs.ext3 -L casper-rw -b 4096 /dev/sdX2

NOTE: casper-rw label is important, do not change it.


Mount dapper iso file in your harddisk:
#mkdir dapper_directory
#mount -o loop ubuntu-6.06.1-desktop-i386.iso dapper_directory



Copy iso files to first usb pendrive partition:
#mount /dev/sdX1 /media/foo
#cd dapper_directory
#cp -dpR . /media/foo



Installing grub bootloader:

#grub-install --root-directory=/media/foo /dev/sdX

this does not install menu.lst file so we will create it manually:
$sudo gedit /media/foo/boot/grub/menu.lst


Howto change isolinux.cfg entries (/media/foo/isolinux/isolinux.cfg) into menu.lst format:

E.g: isolinux.cfg entry

LABEL live
menu label ^Start or install Ubuntu
kernel /casper/vmlinuz
append boot=casper initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash --


changes into: (menu.lst entry)

title Start or install Ubuntu
root (hd0,0)
<---------------- this is necessary to identify usb device at boot. kernel /casper/vmlinuz boot=casper initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw quiet splash
initrd /casper/initrd.gz
boot


NOTE: If you have problems creating menu.lst file, copy the one that appears at the end of this article.

Now you have written a correct menu.lst file at /media/foo/boot/grub directory exec again:
#grub-install --root-directory=/media/foo /dev/sdX

NOTE: If grub gives this error message "/dev/sdX does not have any corresponding BIOS drive"
execute this command:
#grub-install --root-directory=/media/foo --recheck /dev/sdX


NOTE: to be able to run your usb pendrive from qemu you have to trick the emulator giving a usb dapper iso as a cdrom device e.g:
#qemu -hda /dev/sdX -cdrom ubuntu-6.06.1-desktop-i386.iso -boot c


If you want to restart your computer to probe your creation, run:
#eject /dev/sdX

NOTE: eject /dev/sdX umounts sdX partitions and deletes /dev/sdX, /dev/sdX1,... devices

Restart and boot from your usb pendrive.


Have fun!!


APPENDIX: Example of menu.lst file

NOTE: persistent tag at first kernel line enables persistence mode in our usb pendrive.

timeout         10

color cyan/blue white/blue


title           Start Ubuntu in Persistent mode
root            (hd0,0)
kernel          /casper/vmlinuz boot=casper initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw persistent
initrd          /casper/initrd.gz
boot

title           Live - Start or install Ubuntu
root            (hd0,0)
kernel          /casper/vmlinuz boot=casper initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw
initrd          /casper/initrd.gz
boot

title           Start Ubuntu in safe graphics mode
root            (hd0,0)
kernel          /casper/vmlinuz boot=casper xforcevesa initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw
initrd          /casper/initrd.gz
boot

title           Check CD for defects
root            (hd0,0)
kernel          /casper/vmlinuz boot=casper integrity-check initrd=/casper/initrd.gz ramdisk_size=1048576 root=/dev/ram rw
initrd          /casper/initrd.gz
boot

title           Memory test
root            (hd0,0)
kernel          /install/mt86plus
boot



This article is based on this excelent spanish one extracted from Daniel Perella's Blog.