• No results found

Doing Magic on Your File Systems with dd

In document Beginning Ubuntu Server Administration (Page 117-121)

In your life as a system administrator, it is often necessary to copy data around. If it’s ordinary data, an ordinary command like cpworks well enough. But if the data is not ordinary, cpjust isn’t powerful enough. We’ll now discuss some of the options that the ddcommand has to offer.

Caution

If the file system metadata contains important information, like a UUID which makes it possible for you to uniquely identify a file system,ddcan cause some unpredicted results. If you are using ddto copy the complete file system, you will copy information like the UUID as well, and thus you’ll be unable to differ- entiate the old file system from the new one.

Speaking in generic terms, the nice thing about the ddcommand is that it doesn’t just copy files; it can copy blocks as well. As a simple example, I’ll show you how to clone your complete hard drive. Assuming that /dev/sdais the drive that you want to clone and /dev/sdbis an empty drive that can be used as the target, the ddcommand is rather easy: dd if=/dev/sda of=/dev/sdb. In this example, ddis used with two parameters only: ifis used to specify an input file, and ofis used to specify the output file (both of which are device files in this case). Next, wait until the command is finished, and you will end up with an exact copy of the origi- nal hard drive.

In this example, the contents of one device were copied to another device. A slight varia- tion is the way that ddis used to clone a DVD or CD-ROM and write it to an ISO file. To do that, in case your optical drive can be accessed via /dev/cdrom, you can clone the optical disk using

dd if=/dev/cdrom of=/tmp/cdrom.iso. And of course you can mount that ISO file as well using

mount -o loop /tmp/cdrom.iso /mnt. Next, you can access the files in the ISO file from the directory where the ISO is mounted.

So far we have used ddonly to do things that can be done with other utilities as well. It becomes really interesting if we go beyond that. What do you think of the following exam- ple, where a backup of the master boot record (MBR) is created? Just make sure that the first 512 bytes of your hard drive, which contains the MBR, is copied to some file, as in dd if=/dev/sda of=/boot/mbr_backup bs=512 count=1. In this example, two new parameters are used. First, the parameter bs=512specifies that the block should be 512 bytes. Next, the parameter count=1indicates that only one such block has to be copied. Without this param- eter, you would copy your entire hard drive, which I don’t recommend. The backup copy of your MBR may be useful if some day you can’t boot your server anymore because of a prob- lem in the MBR. If this happens, just boot from a rescue disk and use the command dd if=/boot/mbr_backup of=/dev/sda bs=446 count=1. As you notice, in this restore command, only 446 bytes are written back. This is because you may have changed the partition table since you created the backup. By writing back only the first 446 bytes of your backup file, you don’t overwrite the original partition table which is between bytes 447 and 511.

Now I’ll show you how to extend your swap space by adding a swap file. This is useful if, in the middle of the night, you get an alert that your server is almost running completely out of memory because of a memory leak you hadn’t discovered so far. All you have to do is to cre- ate an empty file and specify that it should be added to the swap space. Creating this empty file is an excellent task for the ddcommand. In the following command you are using ddto create a file that is filled with zeros completely by using the /dev/zerodevice: dd if=/dev/zero of=/swapfile bs=1024 count=1000000. This would write a file of 1 GB that can be added to the swap space using mkswap /swapfileand swapon /swapfile.

In the next example of the marvelous things you can do with dd, let’s use it to recover the superblock on an Ext2 or Ext3 file system. To access a file system, you need the superblock, which is a 1 KB block that contains all metadata about the file system. It normally is the sec- ond 1 KB block on an Ext3 file system. In Listing 4-12, you can see a part of the contents of the superblock as displayed with the debugfsutility.

Listing 4-12.Partial Contents of the Superblock

Filesystem volume name: <none>

Last mounted on: <not available>

Filesystem UUID: 09979101-96e0-4533-a7f3-0a2db9b07a03 Filesystem magic number: 0xEF53

Filesystem revision #: 1 (dynamic)

Filesystem features: has_journal ext_attr filetype needs_recovery sparse_super large_file

Default mount options: (none) Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 5248992 Block count: 10486428 Reserved block count: 524321 Free blocks: 3888202 Free inodes: 4825213 First block: 0 Block size: 4096 Fragment size: 4096 Blocks per group: 32768 Fragments per group: 32768 Inodes per group: 16352 Inode blocks per group: 511

If, due to some error, the superblock isn’t accessible anymore, you have a serious chal- lenge. Fortunately, some backup copies of the superblock are written on the Ext3 file system by default. Using these backup copies, you can still mount a file system that you may have otherwise considered lost. And, as you can guess, the ddcommand is an excellent help.

The actual position on disk of the first backup of the superblock depends on the size of the file system. On modern large file systems, you will always find it at block 32768. To try if it really works, you can mount from it directly using the mount option -o sb. The issue, how- ever, is that mount expects you to specify the position of the superblock in 1,024 byte blocks, whereas the default block size for a modern Ext3 volume or partition is often 4,096 bytes. (Use

dumpe2fsif you want to be sure about that.) Therefore, to tell the mount command where it can find the superblock, you have to multiply the position of the superblock by 4, which in most cases results in a block value 131072. If, for example, your /dev/sda5file system should have a problem, you can try mounting it with the command mount -o sb=131072 /dev/hda5 /somewhere.

Did the file system mount successfully? If so, the problem really was in the superblock. So let’s fix that problem by copying the backup superblock back to the location of the old superblock. You can do this using dd if=/dev/hda5 of=/dev/hda5 bs=1024 skip=131072 count=1 seek=1. Once finished, your file system is accessible again, just as it was before.

Summary

In this chapter, you learned about the most important file system management tasks. In the first part of the chapter, you read how to mount and unmount file systems. You also learned how links can make your life easier. The second part of this chapter concerned the organiza- tion of a file system. You learned how to use partitions or logical volumes to set up a file system, and you read how to use the ddcommand to perform some advanced file system management tasks. In Chapter 5, you’ll find out how to secure your server with user and group accounts, permissions, sudo, and many more.

Configuring Your Server

In document Beginning Ubuntu Server Administration (Page 117-121)