File System Manipulation


For some people, reading books and going to lectures or discussions are the best way to learn new things. For others, playing around on a system is the way to go. If you want to see for yourself how things work in different file systems, try creating a file system using the loopback file system a special file system that lets you accomplish this interesting and useful feat. You can use the file system you create to experiment with and practice almost all the shell commands found in this chapter with no fear of damaging your system.

Tip

To do this on a larger scale, you can install a basic SUSE Linux system into the Xen virtual machine. Pick a file system, don't install the graphical end, and you have another playground apart from your working system. See the Xen section of Chapter 11, "Going Cross-Platform," for details.


Creating a File System for Testing

People who run Linux tend to be a tinkerer's lot. They like to try new things and are not afraid of getting under the hood and plumbing the depths of their computers to see what exactly is going on. Although many Linux users have multiple machines (and parts of machines) cluttering up the house, most people have just one computer with a single hard drive to play with. If you then have data on that computer that is really important, you don't want to blow away your operating system on a regular basis after an experiment goes awry. This is perhaps the reason the loopback file system was born. With this handy tool, you can create an image file containing the file system of your choice, and mount it leaving your "real" file system alone and safe.

You could do this exercise on a floppy or other removable drive, but if you want ample room to play in, working off a hard drive is a better way.

Step 1 Make a Blank Image File

Use the dd command to create a file with a block size of 1,024 bytes (a megabyte) and create a file that is 10MB in size. You should have enough free space on your drive to accommodate a file that size, of course. You need 10,000 blocks of 1KB (1,024 bits) in this 10MB file, so here's what you type:

Dd if=/dev/zero of=/tmp/test-img bs=1024 count=10000 

The shell responds with

10000+0 records in 10000+0 records out 

Step 2 Make a File System

Now we need to make the system think the file is a block device instead of an ASCII file, so we use losetup, a utility that associates loop devices with regular files or block devices. You will use the loopback device /dev/loop0. losetup /dev/loop0 /tmp/test-img.

Then format the file with an ext3 file system:

mkfs -t ext3 -q /tmp/test-img 

If prompted that test.img is not a block device, enter y to proceed anyway.

Step 3 Mount the Test File System

Your test file system is ready to go, except that you can't do much with it until it is mounted on your system. Let's start with a mount point, then.

mkdir /mnt/image 

Now we can mount it:

mount  o loop /tmp/test.img /mnt/image 

After mounting the file system, look at it with the df command:

df  h /mnt/image 

And get this response:

Filesystem        Size    Used    Avail    Use%    Mounted on /tmp/test-img     10M     1.1M    9M       2%      /mnt/image 

To unmount the image:

umount /mnt/image

You can even back up the image, in case something happens while you're playing:

cp /tmp/test-img test-img.bak 

When you've confirmed that you have a mounted image file, you can create directories, copy files to it, delete files, attempt to recover them, and, generally speaking, do anything you want with this file system. It's a playpen where you can learn valuable lessons with no risk. If you somehow irreparably damage the file system on the image, unmount it, delete it, and start over, perhaps with that backup you just made. So have fun!

Let's now discuss how to mount a read-only partition on a running system.

Mounting a Read-Only Partition on a Running System

From time to time, you may need to mount a partition in your file system in such a manner that you can view the data that it contains but not be able to make changes to it. This can be accomplished by mounting the partition in read-only mode. You can do this in two ways. The first is with the mount command. At the shell prompt, enter

mount -r device mount_point 

For example, if you had a partition at /dev/sda3 and you wanted to mount it at the /extra directory, you would enter the following:

mount -r /dev/sda3 /extra 

This is shown in Figure 18.18.

Figure 18.18. Mounting a partition in read-only mode.


In Figure 18.18, you can see that the /dev/sda3 partition is mounted in the /extra directory. Notice that when the mount command is subsequently issued without arguments, the mounting for /dev/sda3 is shown with a (ro) designation, indicating that the partition is mounted read-only.

In addition to using mount, you can automatically mount the partition read-only using your fstab file. Simply add ro to the mount options field, as shown in Figure 18.19.

Figure 18.19. Mounting a partition in read-only mode using fstab.


With this in mind, let's now look at mounting an image file as a floppy disk device.

Examining a Floppy Image File

Earlier in this chapter, you learned how to mount floppy disks on your SUSE Linux system. Using the loopback filesystem, you can also mount an image file as a floppy disk. This is done by completing the following:

1.

Open a terminal session.

2.

Switch to your root user.

3.

Create the image file by entering dd if=/dev/zero of=floppy.img bs=512 count=2880 at the shell prompt.

4.

At the shell prompt, enter losetup /dev/loop0 floppy.img.

5.

Format the file using the MSDOS file system by entering mkdosfs /dev/loop0 at the shell prompt.

6.

Mount the image file as a floppy by entering mount -t msdos /dev/loop0 /media/floppy.

This process is shown in Figure 18.20.

Figure 18.20. Mounting an image file as a floppy.


Before ending this chapter, we need to discuss one more topic: working with character and block devices. Let's look at this next.



SUSE Linux 10 Unleashed
SUSE Linux 10.0 Unleashed
ISBN: 0672327260
EAN: 2147483647
Year: 2003
Pages: 332

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