disk/partition/filesystem imaging with dd, squashfs, gzip, losetup and avfs

--To increase the compression ratio of the image, zero the free space in the filesystems before creating the initial image.--

Prepare the source filesystems.......

Windows

  1. delete any superfluous files... I use bleachbit http://bleachbit.sourceforge.net/
  2. defrag to reorder the bits... I just use the microsoft built in tool
  3. zero the free space...I use sdelete -z from sysinternals http://download.sysinternals.com/files/SDelete.zip

Linux

  1. delete any superfluous files... I use bleachbit http://bleachbit.sourceforge.net/
  2. defrag if you think it is necessary... e4defrag
  3. zero the free space... $ dd if=/dev/zero of=/{mounted filesystem}/tempfile bs=1M
    • remove the temporary zero file

Make block copies of the filesystems using dd

Individual Partition image

$ dd if=/dev/sdb1 of=/backups/sdb1.dd.img

Whole disk image

$ dd if=/dev/sdb of=/backups/sdb.dd.img

Compressing the image to save space -- note if you never plan on mounting the image to retrieve individual files you can pipe the dd through gzip

$ dd if=/dev/sdb1 bs=1M | gzip > /backups/sdb1.dd.img.gz

   or you can gzip later

$ dd if=/dev/sdb1 bs=1M of=/backups/sdb1.dd.img && gzip /backups/sdb1.dd.img
dd does not give you any progress indication.

you can however send the dd process a kill -USR1 signal and it will output its progress to std out.

Note: you will have to send this signal from another terminal

ps | grep dd  && kill -USR1 {pid}
or 
kill -USR1 `ps -aef | grep "dd\ if" | awk '{print $2}'
you should see output similar to the following in the terminal that is running the dd command
12917407744 bytes (13 GB) copied, 511.9 s, 25MB/s

Make file system copy using squashfs.

the advantages of squashfs over dd are..

  1. the compression is multithreaded so its faster on multicore machines
  2. easy to mount without having to decompress the whole image.

    dd if=/dev/sdb1 | gzip > /backups/sdb1.dd.img

The disadvantage of squashfs over dd is you cant use it to archive a whole disk including the partition table and boot block etc..x


Mounting and restoring from the disk/filesytems images..

mounting the mksquashfs image

$ mount /backups/sdb1.dd.img.sqfs /mnt/sdb1-squashfs

mounting the Partition image

$ mount /backups/sdb1.dd.img.sqfs/sdb1.dd.img sdb1-img

mounting a whole disk image you will have to use kpartx to create the the device nodes

$ kpartx -a /backup/sdb1.dd.img.sqfs/sdb.dd.img
   --then mount the individual file systems, the device node will be in /dev/mapper/loop
mount /dev/mapper/loop1p1 /mnt/sdb1

mounting a single partition from the disk image

--use parted to display the partition table in Bytes-- $ parted /backups/sdb.dd.img unit B print Model: (file) Disk /backups/sdb.dd.img: 268435456B Sector size (logical/physical): 512B/512B Partition Table: msdos

Number  Start      End         Size        Type      File system  Flags
 1      1024B      99980287B   99979264B   primary   ext4         boot
 2      99980288B  268435455B  168455168B  extended
 5      99981312B  268435455B  168454144B  logical   ext4

--specify and offset to the mount command-- $ mount -o loop,offset=99981312 /backups/sdb.dd.img /media/sdb5 --if there are file systems issues you may need to mount with the options -o ro,noload--

using avf and losetup to mount a gzipped dd partition image

install http://sourceforge.net/projects/avf/

create a virtual filesystem

$ mountavfs 
Mounting AVFS on /root/.avfs...

there is now a pseudo-fs replica of the real file system in /root/.avfs

$ ls /root/.avfs/
bin/        etc/        lib64/      mnt/        root/       selinux/    tmp/
boot/       home/       lost+found/ opt/        run/        srv/        usr/
dev/        lib/        media/      proc/       sbin/       sys/        var/
/backups/

$ cd /root/.avfs/backups/
256/      256sqash/ guess/    hdb1/     loop1p2/  loop2/    sdb5/
256MBimg/ avfs/     gziped/   loop1p1/  loop1p5/  sdb1/     sqfs/

$ ls
256       gziped   loop2             sdb1.tgz          sdb5.sqfs                     sdb.dd.img
256MBimg  hdb1     sdb1              sdb5              sdb5.tgz                      sdb.dd.img.sqfs
256sqash  loop1p1  sdb1.dd.img       sdb5.dd.img       sdb5.zero-filled.dd.img       sdb.zero-filled.dd.img
avfs      loop1p2  sdb1.dd.img.sqfs  sdb5.dd.img.gz    sdb5.zero-filled.dd.img.gz    sdb.zero-filled.dd.img.sqfs
guess     loop1p5  sdb1.sqfs         sdb5.dd.img.sqfs  sdb5.zero-filled.dd.img.sqfs  sqfs

$ losetup /dev/loop2 sdb5.gz#
--the hash at the end is needed, it is short hand for losetup /dev/loop2 sdb5.gz#gunzip

$ mount /dev/loop2 gziped
mount: block device /dev/loop2 is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/loop2,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail  or so

$ mount -o ro,noload /dev/loop2 gziped

$ df
Filesystem     1K-blocks    Used Available Use% Mounted on
/dev/sda1       14317616 6159480   7424184  46% /
none                   4       0         4   0% /sys/fs/cgroup
udev             1008392       4   1008388   1% /dev
tmpfs             205120     936    204184   1% /run
none                5120       0      5120   0% /run/lock
none             1025600      76   1025524   1% /run/shm
none              102400      36    102364   1% /run/user
/dev/loop2        159304    5646    145433   4% /root/.avfs/media/gziped