Powered by Blogger.

Tuesday, June 1, 2010

Play With Your Hard Disk Partitions


A partitioning tool

There have been many operating systems and live CDs that provide partitioning utilities. Often, they turn out to be either proprietary or not able to support all filesystems used for Linux, Solaris, etc. But GParted is one such important partitioning utility that supports many filesystems and comes with a friendly user interface.
GParted (Figure 1) stands for ‘GNOME partition editor’. With it, you can create, modify and delete partitions. Although you can definitely install it on any distribution and run it, I wouldn’t recommend running it on a booted OS as problems might occur with certain mounted partitions. A better solution is to run GParted from a live CD.
Figure 1: The GParted application
Figure 1: The GParted application
GParted itself comes on a separate live CD; boot it and double click on the GParted icon from the UI presented. It is also a part of a live CD called System Rescue CD. To run GParted from here, first start X by issuing the startx command, and then open the GParted application from the X terminal. It can also be run from a bootable USB drive or via a PXE boot.
Once GParted is run, you can use it for the following operations:
  • To create a new partition table (by erasing the old one, if one exists). Use this option very carefully. Unlike other operations, Undo is not supported for this.
  • A new partition can be created from unallocated/free space. Decide on new partitions as primary, extended or logical, as required.
  • To delete an existing partition.
  • To resize/move existing partitions (Figure 2). Sizes can be increased only when free space is available on any adjacent side.
  • To change flags/labels of a partition.
  • To format a partition to any supported filesystem—ext3, reiserfs, etc.
  • To check filesystems for errors.
  • To view partition information.
Figure 2: Resize and move a partition
Figure 2: Resize and move a partition
All requested operations are added to the pending queue, which is performed in sequence upon clicking the Apply option.
The following filesystems are supported by GParted presently: btrfs, crypt-luks, ext2, ext3, ext4, fat16, fat32, hfs, hfs+,jfs, lvm2 pv, ntfs, reiser4, reiserfs, swap, ufs and xfs.

Disk cloning

g4u is a disk cloning software. It is helpful in larger organisations like educational institutes where there are several machines of identical configuration. Every system may need multiple operating systems along with some updates and customisation. Repeating these for every system is a tedious task. So cloning is a good technique to avoid such problems by copying many disks from one fully prepared one. Also, when one or more operating systems crash on one system, it can be recovered quickly by copying the OS from an identical system.
This software has a few advantages over other cloning utilities. One is that it’s free. Second, it is independent of partitions, filesystems and operating systems that exist on a disk, as cloning is performed block by block. The only requirement is that the target disk should be larger than the original disk.
g4u comes as live media, in the form of two bootable floppies or a bootable CD.  As of today, the latest stable version of g4u is 2.3.
Disk cloning can be done in two ways — locally, by connecting two or more disks to a system at a time, and remotely, where the image of the original disk is stored on an FTP server, which is used for other disks to be copied using FTP.
So, to perform disk cloning, connect two or more disks for the local copy, or one disk and the network set up if you plan to use FTP, and run g4u from a live CD or bootable floppies. A terminal will be presented to use the following commands:
  1. disks lists all connected disks, and specifies identifiers for each. IDE hard disks are identified by wd0, wd1, etc, whereas SATA disks are identified by sd0, sd1, etc. Here, wd0/sd0 represents the first/primary hard disk.
  2. parts lists the available partitions in a specified disk. For example, parts wd0 lists partitions of disks identified by wd0. See Figure 3.
  3. copydisk copies all data from disk1 to disk2, block wise (Figure 4). For example, copydisk wd0 wd1.
  4. copypart copies a partition from one disk to a partition on another disk. For example, copypart wd0e wd1f, copies the second partition of disk1 to the third partition of disk2. (Here the letters d, e, f, etc, in no way concern the drive letters of any OS. g4u assigns a character for each partition, starting from d for the first partition.)
  5. uploaddisk stores the image of the whole disk on an FTP server:
    uploaddisk ftp-address imagename disk-id
    …where ftp-address is the IP address of the FTP server. By default, the FTP account named ‘install’ is used. Use account@ftp-address for any other account. disk-id is the identifier of the hard disk (for example, wd0, wd1, sd0, sd1, etc) whose image needs to be uploaded. It is optional. The absence of disk-id implies taking the image of the first hard disk, which is identified by rwd0d. Also, by default, gzip-9 compression is used while saving images with a typical extension like .gz. To use lower levels of gzip compressions (higher levels of compression take a longer time to back up/restore) use the following:
    GZIP=1 uploaddisk ftp-address imagename disk-id
  6. uploadpart stores the image of a particular partition on the FTP server:
    uploadpart ftp-address imagename disk+part
  7. slurpdisk restores a disk from the FTP image:
    slurpdisk ftp-address imagename
  8. slurppart restores a partition from the FTP image:
    slurppart ftp-address imagename disk+part
Figure 3: Output of the parts command
Figure 3: Output of the parts command
Figure 4: Disk cloning in progress
Figure 4: Disk cloning in progress
It is preferable to run g4u in a DHCP-enabled network if FTP transactions are planned. If DHCP is not available, the network needs to be set up manually.

partimage for back up/restore

Consider the following scenario. You have installed a Linux distribution that comes with minimal packages. Additional packages have been installed from the Internet using a package manager. So, patiently, you’ve updated and customised the OS according to your needs. Now your friend does not have an Internet connection and needs all these additional packages on his system. Or, all of a sudden, the OS has crashed, and you don’t have enough time or resources to repeat all the steps. Under the circumstances, back up your partitions every time you update your system—this back-up can be shared with any other system or used to restore the OS when it crashes.
partimage helps you with these kinds of back-ups. It can be installed on many distros via package managers or, preferably, you can run it off a live CD. It is a part of the System Rescue CD.
Here’s how you back up a partition:
  1. Run the partimage command from a terminal.
  2. Select partitions for which a back-up needs to be taken.
  3. Specify the image name along with a suitable path. Another partition/removable disk has to be mounted in order to store the image in it. (In case of single partitions, or if none of the additional partitions have enough free space, you can use NFS- and Samba-shared mounts to save images on a remote computer.)
  4. Opt for ‘Save partition…’ and go to the next screen (short cut: F5).
  5. In the next screen, select the compression level, split mode and finish with other default options (Figure 5). Split mode (auto) means that when a volume in which the image is being stored is full, the image is split into multiple parts. Otherwise, fixed size parts can also be created for the convenience of storing on CDs/DVDs.
  6. You’ll be prompted to give a description of the image for later use, and one more confirmation screen before copying starts.
  7. The copying process will start now. Options are available to cancel, pause and resume the operation.
  8. A confirmation screen appears after the completion of the process.
Figure 6: Options to save the partion
Figure 5: Options to save the partion
The following are the steps for restoring a partition:
  1. Run partimage and choose the partition that needs to be restored.
  2. Give a path to the image (from a mounted partition/removable disk).
  3. Opt for ‘Restore Partition…’ and go to the next screen (shortcut: F5).
  4. Opt for defaults and hit ‘Continue’ (F5).
  5. Now, the image description should appear, which is given at the time of backup, and one more confirmation screen before restore starts. Figure 6 shows a typical restoration in progress.
  6. A confirmation screen appears after completion of the process.
Figure 7: Restore in progress
Figure 6: Restore in progress
Note: When restoring an image, the size of the partition that’s being restored should be greater or equal to the size of the original partition on which the backup is taken. However, the target partition is accessible up to the size of the original partition only, in case it is larger. Even though the image size is smaller with only used blocks, the target partition can’t be smaller than the original one.
Tip: When an image is created for a partition holding the MBR, only MBR can be restored to the system from that image at a later time, by choosing the option “Restore an MBR…” from the initial screen (refer to Figure 7).
Figure 5: Initial screen of partimage
Figure 7: Initial screen of partimage
Some machines may come with a single partition or not enough space on any of the existing partitions. One solution that was mentioned earlier is to use NFS/Samba to store images on a remote machine. Another simple solution is to use the partimage-server. A remote machine running partimage-server can be used to store images whose IP address and port can be specified during backup/restore of a partition.
Install partimage-server on any distribution and run it using the command partimaged. By default, images are stored in /var/lib/partimaged/. You can use the -d or --dest options to specify any other directory to store images on the server. Similarly, you can change the default port number 4025 with the -p or --port option.
In order to connect to partimage-server during back-up/restore operations, simply opt for ‘Connect to Server’ in the initial screen (refer to Figure 5) and enter the IP address of the server and the encryption mode. Also change the port number to connect to if the server is using a port other than the default one.

System Rescue CD

A good utility that combines many of the tools discussed here in one CD is the System Rescue CD distro. It comes with many additional utilities like:
  • An SSH server to allow other systems to log in remotely and copy files
  • Filesystem checkers, scanners, mount and view contents of other partitions
  • mkfs to create a new filesystem and sfdisk to set up partitions
  • parted and GParted for partition manipulation
  • Firefox/Dillo for instant browsing
  • PXE server capability
Figure 8: System Rescue CD
Figure 8: System Rescue CD

Tips and tricks

  1. To set up a network manually on any live CD, use the following code:
    ifconfig eth0 192.168.1.1
    ifconfig eth0 up
    Use a different device number other than eth0, if required. On the System Rescue CD you can use the net-setup eth0 command instead.
  2. While running g4u, make sure of your disk IDs by running the parts command first. For example, if the original disk is detected as wd1, and the second one as wd0, running copydisk wd0 wd1 will mean losing all your data in the original disk. Or a safer method is erasing all partitions in the target disk before cloning, by using GParted or any similar utility. Now parts on the target disk will not show any partitions, whereas on the original disk, partitions will be listed.
  3. In the System Rescue CD, if your partimage is already running, go to another virtual console (using Ctrl+Alt+F2, Ctrl+Alt+F3, etc) for mounting, checking image paths, etc. Use Ctrl+Alt+F1 to come back to the first console.
  4. Don’t try to save an image in the same partition you’re backing up.
  5. As Linux distros are compatible with many configurations, cloned disks or partitions work fine in systems with different configurations also— with the exception of one or two features. For example, an image taken for a Linux distro on a Core 2 Duo system works well with PIII or AMD machines also, only requiring you to tweak /etc/X11/xorg.conf according to new display settings, if you have a problem with starting the GUI.

References

0 comments

Post a Comment