Working with Linux Partitions


You can check your hard drive, but sometimes you need to specifically work with the partitions on your hard drives. Fortunately, Knoppix provides some excellent tools that enable you to create, manipulate, and restore partitions on your machines.

Partitioning with QTParted

If you've spent any time in the Windows world, you may be familiar with PartitionMagic, a popular — and powerful — program that makes it really easy to work with partitions, even those formatted with NTFS. Think of QTParted as a free, open-source PartitionMagic, and you'll be on the right track with this essential software.

To start QTParted, from the K menu select System QTParted. You'll see something similar to what is shown in Figure 4-1.

image from book
Figure 4-1: QTParted enables you to work with partitions from within Linux.

Out of the box, QTParted can do the following:

  • Create Ext2 and Ext3 partitions, but not resize them

  • Create JFS partitions but not resize them

  • Create XFS partitions but not resize them

  • Neither create nor resize ReiserFS partitions

  • Create and resize NTFS partitions

Using QTParted is pretty easy. Right-click on a partition to get its properties, or to format, resize, move, and delete it; right-click on free space on a disk to create a new partition. If you've ever used PartitionMagic, you should be right at home, and if you haven't, you'll pick this up quickly.

Note two caveats about using QTParted. First, if you right-click on a partition that's already been formatted with ReiserFS, you're only going to have the options Property, Format, Delete, and Set Active. What happened to Resize and Move? To get those capabilities, use APT to install software with the following two commands:

 $ sudo apt-get update $ sudo apt-get install progsreiserfs 

After running these two commands, you're told that some extra packages will be installed and some others will be removed, and asked whether you want to continue. Type Y and press Enter, and in a few moments the installation completes.

Restart QTParted, right-click on a ReiserFS-formatted partition, and you'll see the full complement of options: Property, Format, Resize, Move, Delete, and Set Active.

The second caution concerns ext3: You cannot resize a partition formatted with that filesystem. However, there is a workaround. It's a bit tedious, but it does the job. First, convert your ext3 filesystem to ext2 with this command:

 $ sudo tune2fs -O ^has_journal /dev/hda1 $ sudo e2fsck /dev/hda1 

Remember that ext3 is really just ext2 with journaling. You just removed the journaling, so now you're back to ext2. Don't forget to change /etc/fstab as well, running as root and using your favorite text editor, so that the partition listed there as ext3 is now correctly identified as ext2.

Now use QTParted to resize the partition. When you finish, convert the ext2 partition back to ext3 with this command:

 $ sudo tune2fs -j /dev/hda1 

Change /etc/fstab so that the partition once again is identified as ext3, and you're in business. Tedious, yes, but it works!

Note 

For more on QTParted, check out its home page at http://qtparted.sourceforge.net. Because QTParted is really a GUI for the GNU Parted program, you also may want to check that command-line-based app out at http://www.gnu.org/software/parted/parted.html.

Backing Up Partitions

Recently one of us had a problem with one of our hard drives and needed to back up all the data on one of the partitions before the drive failed entirely. Some of you are already thinking, I know what to do: use dd! Normally, dd is just fine if you want to copy all the data from one block device to another device or file, but it does have its limitations, especially if the disk has errors on it. Sure, you can use the noerror option, but it's still going to go verrrrrrrrrry sloooooooooowly. Knoppix includes something better than dd for cases in which the hard drive has bad blocks on it: dd_rescue.

Those of you who have used dd know that you typically use it like this:

 $ dd if=/dev/hda of=/dev/hda.img 
Note 

Basically, you give the command (dd) and then specify the input file (if) and the output file (of). The dd_rescue command is very similar, but without the if= and of=.

Note 

For more details about how to use dd_rescue, type man dd_rescue. The dd_rescue Web page also explains several key features of the program; it can be found at http://www.garloff.de/kurt/linux/ddrescue/.

Here's a simple example that uses dd_rescue to create an image of a partition:

 $ sudo dd_rescue /dev/hda1 /mnt/hdb1/hda1.img 

This copies an image of /dev/hda1 to hdb1. It assumes, of course, that there's room on hdb1 for all of hda1. If there's not, you'll have to use another partition.

The following command restores the disk image of hda1:

 $ sudo dd_rescue /mnt/hdb1/hda1.img /dev/hda1 

That was easy — it's just the reverse of the initial command.

Next is something that would be useful if you have only limited partitions on a machine or, for example, if hdb1 couldn't contain hda1: Use dd and ssh to create an image on another machine:

 $ sudo dd_rescue /dev/hda1 - | ssh username@machine 'cat image from book    > /home/username/hda1.img' 

You'll be prompted for a password; enter it, and then wait for dd_rescue to do its job. Needless to say, this can take a while if you're dealing with a large partition. If you get an error message such as

 dd_rescue: (warning): output file is not seekable! dd_rescue: (warning): Illegal seek 

while you're running this command, it's safe to ignore it. It's just dd_rescue being overly cautious.

To restore the image from another machine using ssh, use this command, entering a password when prompted:

 $ ssh username@machine 'cat /home/username/hda1.img' image from book    | sudo dd_rescue - /dev/hda1 

Once again, you may have to wait a while. Watch TV. Go for a walk. Read a book. Better yet — read this book!

Tip 

It's a good idea to fsck any images you create by running sudo fsck -y /mnt/hdb1/ hda1.img after you create the image, especially if the partition you were backing up had problems to begin with.

Finally, you can use tar and ssh to compress the partition and copy it to another machine:

 $ tar zcvf - /mnt/hda1 | ssh username@machine 'cat /tmp image from book    /hda1_bak.tar.gz' 

Of course, you have to enter a password once more. Again, this process could take a long time, but you end up with a compressed copy of your partition.

To restore the partition, use the following command:

 $ ssh username@machine 'cat /tmp/hda1_bak.tar.gz' | tar zxvf - 

Don't forget the dash on the end! This process can take a while, but your partition will make it back onto your hard drive, which is the goal.

Caution 

Keep in mind that when you untar hda1_bak.tar.gz, everything is going to be recreated in a mnt/hda1 directory subdirectory from which you run the command, so you'll have to manually move your files and folders to the root of your hard drive.

What if you don't want to leave the copied images on another hard drive? If the images you've created are small enough, you can burn them to CD; if they're larger, burn them to DVD. If an image is too big for that, use the split command to break the file into sections and burn each section to disk. If you ever need to join the sections again, copy each section from your disk(s) and use the cat command to rejoin them (you can avoid this scenario by backing up to external hard drives).

To break up a 10GB gzipped tar file (named hda1_bak.tar.gz in this example) into two smaller files suitable for burning onto two DVDs, you'd use the following:

 $ split --bytes=4000m --verbose hda1_bak.tar.gz hda1_bak 

This command produces three files: hda1_bakaa, hda1_bakab, and hda1_bakac. The first two are 4GB each, and the last one is 2GB (all that's left of the 10 GB original). Burn them onto DVDs and you're good to go.

When it's time to rejoin what you've burned onto DVD, copy the three files from the DVDs and onto a hard drive, and then run the following:

 $ cat hda1_bak* > hda1_bak.tar.gz 

After some time, you have your single file back. Use ssh and tar to restore it to your hard drive on another machine, and you're as good as new!

Restoring Lost Partitions

OK, you've done it: you deleted your MBR, or at least the partition table, and you don't have a backup. Time to erase the hard drive and start over, right? Wrong! Before you take that drastic route, boot with Knoppix and see whether Gpart (which stands for Guess partitions) can save your bacon.

Gpart looks for areas of your hard drive it can identify as partitions, and then creates a partition table for them. Yes, it seems like magic, and it doesn't always work, but it's better than just throwing in the towel. What can it hurt?

Better still, Gpart supports a wide variety of filesystems, including but not limited to the following:

  • FAT and VFAT

  • ext2 and ext3

  • Linux swap

  • NTFS

  • ReiserFS

Before you actually turn Gpart loose on your hard drive, run the program in demo mode, so that it shows you what it would do. Here's how:

 $ sudo gpart /dev/hda 

You point Gpart at your hard drive, not at a partition — which makes sense because you don't have any partitions at this point!

Eventually — Gpart can take a long time to work, so be patient — you should get some output. Look over it carefully, and if Gpart seems like it will work for you, then it's time to run the program in write mode:

 $ gpart -W /dev/hda /mnt/hda 

If all goes well, you should now be able to boot, with your partitions newly raised from the dead! Thank Knoppix and Gpart next time you get a chance. They made your life easier, that's for sure.

Note 

For further info on Gpart, including what to do when gpart doesn't work right away, check out http://www.stud.uni-hannover.de/user/76201/gpart/, the project's home page.



Hacking Knoppix
Hacking Knoppix (ExtremeTech)
ISBN: 0764597841
EAN: 2147483647
Year: 2007
Pages: 118

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