Tuesday 31 January 2012

Modifing ramdisk for creating custom initrd images

initrd.img is a compressed file in gzip format
So move initrd.img to initrd.gz as shown below.
# cp /tftpboot/el5/initrd.img  .

# ls
cdrom   initrd.img

# mv initrd.img initrd.gz
Unzip the initrd.gz file as shown below.
# gunzip initrd.gz

# ls
cdrom  initrd
After unziping the initrd.gz file, the initrd is further in cpio ‘newc’ format. So extract the files from initrd using cpio ‘newc’ format as shown below.
Note: info cpio will give more information about ‘newc’ format.
# mkdir tmp2

# cd tmp2/

# cpio -id < ../initrd
16524 blocks
Now you can view the content of initrd.img file
# ls
bin  dev  etc  init  modules proc  sbin  selinux  sys  tmp  var 
After extracting the file as shown below, make appropriate modification to any of those files. Then pack the files back into the archive using the following commands. Pack the modified files back to cpio ‘newc’ format

# find . | cpio --create --format='newc' > /tmp/newinitrd
16524 blocks

# ls /tmp/
cdrom  initrd  newinitrd  tmp2

# ls -l /tmp/newinitrd
-rw-r--r-- 1 root root 8460288 Jul  2 14:50 /tmp/newinitrd
Gzip the archive file.
# gzip newinitrd

# ls
cdrom  initrd  newinitrd.gz  tmp2

# ls -l newinitrd.gz
-rw-r--r--  1 root root 6649867 Jul  2 14:50 newinitrd.gz
Move file as an image file. You can use the newinitrd.img as your new boot image.
# mv newinitrd.gz newinitrd.img

# ls -l newinitrd.img
-rw-r--r-- 1 root root 6649867 Jul  2 14:50 newinitrd.img

If the file is in  .cgz extention use the following commands:


How to decompress .cgz file

Decompress:

zcat modules.cgz | cpio -ivd


Compress:

find . | cpio -ov -H crc | gzip -c9 > modules.cgz

Monday 16 January 2012

Rsync Backup root filesystem

This rsync script allows creating a full backup copy across filesystems. It is setup so that the copy includes intact booting capabilities, optionally excluding selected files.
The approach has benefits over omitting system files by just copying personal data; if the system becomes corrupted in the main partition, overcoming the problem means booting into the backup as opposed to identifying and reinstalling affected programs.

Files

Two files are needed: the backup script and a file stating which files to include/exclude from the backup source.

Backup script

The script is very simple; it rsyncs in archive mode, ensuring that symbolic links, devices, permissions and ownerships, among other file attributes are preserved, while excluding files that match the patterns from the include/exclude list.
Save it as rbackup.sh and make it executable:
rbackup.sh
#!/bin/sh
# rsync backup script

sudo sh -c "
    rsync -av --delete-excluded --exclude-from=backup.lst / $1;
    touch $1/BACKUP
"
Backup source; /
In this case it's performing a backup on the whole root.
Backup destination; $1
Passed as an argument to the script; e.g. /media/backup
Include/exclude list; --exclude-from=backup.lst
This example uses backup.lst.

Include/exclude list

As deciding which files should populate this list can be difficult, here's a typical backup example that excludes common files that do not need to be backed up, such as the vast majority of /dev. Note that specifying every desired file or directory in Include is not needed; this section only acts as a filter for statements in Exclude. This file is in the traditional include/exclude rsync format.
Save the following as backup.lst:
backup.lst
# Include
+ /dev/console
+ /dev/initctl
+ /dev/null
+ /dev/zero

# Exclude
- /dev/*
- /proc/*
- /sys/*
- /tmp/*
- lost+found/
- /media/backup/*
Exclude
Content in system directories; /dev, /proc, /sys and /tmp are excluded because they are created by the system at runtime, while the directories themselves need to be preserved since they are not regenerated at boot. Lastly, all lost+found instances are skipped since they are partition-specific. For Archlinux /var/lib/pacman/sync/* can also be excluded. This can save a lot of time on every backup since the directory contains many small files that tend to change quite often. These are description files for every package from the repositories. These files can be regenerated with pacman -Syu.
Warning: don't forget to also exclude the mounted directory where you'll put the backup to avoid an infinite loop (in this example /media/backup/).
Include
Even though /dev is excluded, 4 files that are not dynamically created by udev need to be preserved. These are console, initctl, null and zero.

Backing up

Substitute /media/backup as appropriate, and mount the destination device:
# mount /dev/sdb1 /media/backup
Tip: if the ability to boot the backup isn't important, omit the previous step and simply backup to an arbitrary directory.
Run the backup script (note that the trailing "/" character is necessary):
# ./rbackup.sh /media/backup/

Boot setup

After the sync is finished, the backup destination's /etc/fstab has to be modified, a boot loader needs to be installed on the backup destination, and configuration in the destination's /boot/grub/menu.lst requires to reflect the new location.

Modify fstab

Edit the backup destination's fstab:
$ nano /media/backup/etc/fstab
none         /dev/pts      devpts    defaults      0 0
none         /dev/shm      tmpfs     defaults      0 0

/dev/sda1    /boot         ext4      defaults      0 1
/dev/sda5    /var          ext4      defaults      0 1
/dev/sda6    /usr          ext4      defaults      0 1
/dev/sda7    /             ext4      defaults      0 1
/dev/sda8    /home         ext4      defaults      0 1
/dev/sda9    swap          swap      defaults      0 0
Because rsync has performed a recursive copy of the entire root filesystem, all of the sda mounpoints are problematic and will cause the backup boot to fail. In this example, all of the offending entries are replaced with a single one:
$ nano /media/backup/etc/fstab
none         /dev/pts      devpts    defaults      0 0
none         /dev/shm      tmpfs     defaults      0 0

/dev/sdb1    /             ext4      defaults      0 1
As before, remember to use the proper device name and filesystem type.

Install bootloader

While these instructions assume GRUB is being employed, they could easily be adapted to other bootloaders, such as LILO
Open the GRUB console:
# grub
Direct the install towards the destination device:
root (hd1,0)
setup (hd1)
root; hd 1,0
This should point to where the GRUB files are located--in this case, "hd 1" means the second storage device (/dev/sdb) and "0" is the first partition (/dev/sdb1).
setup; hd 1
The command specifies where the actual boot loader is to be installed. In this example it is installed to the MBR of the second storage device.

Configure bootloader

The problem here is that even though the boot loader installs correctly, its menu entries are for the main system's partitions, not the backup system's.
It's possible to fix this by creating a custom /boot/grub/menu.lst for the backup destination. In order to do this, modify rbackup.sh so that it copies a custom menu.lst:
rbackup.sh
#!/bin/sh
# rsync backup script

sudo sh -c "
    rsync -av --delete-excluded --exclude-from=backup.lst / $1;
    cp ~/custom.menu.lst $1/boot/grub/menu.lst;
    touch $1/BACKUP
"
Tip: instead of replacing menu.lst with a custom version solely for the backup, add a new GRUB entry pointing to the backup device or simply edit GRUB's menu during boot time.
References:
https://wiki.archlinux.org/index.php/Full_System_Backup_with_rsync