Книга: Fedora™ Unleashed, 2008 edition

Creating a File System for Testing

Creating a File System for Testing

Because most of us do not have a spare computer or hard drive on which to experiment and practice, you can make one of your own by creating an image file containing the file system of your choice and using the loopback file system to mount it. That way, you do not run the risk of accidentally wreaking havoc on the system itself. Although you could also use a floppy drive for these same exercises, their small size limits your flexibility.

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 need to have enough free space on your hard drive to hold a file this big, so adjust the size accordingly.) You need 10,000 1KB (1,024-byte) blocks, so select a count of 10000.

If you wanted a floppy-sized image, you would have selected a block size (bs) of 512 and a count of 2880 for a 1.4MB floppy or 5760 for a 2.88MB floppy. Here's how to do that:

# dd if=/dev/zero of=/tmp/fedoratest.img bs=1024 count=10000

You can see the computer respond with the following:

10000+0 records in
10000+0 records out

If you check your new file command, you see this:

# file /tmp/fedoratest.img /tmp/fedoratest.img: data

Step 2 — Make a File System

Now you need to make the system think that the file is a block device instead of an ASCII file, so you use losetup, a utility that associates loop devices with regular files or block devices; you will be using the loopback device, /dev/loop0.

# losetup /dev/loop0 /tmp/fedoratest.img

Now you can format the file as an ext2 file system:

# mke2fs /dev/loop0

You can see the computer respond as follows:

mke2fs 1.27 (8-Mar-2003)
File System label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
2512 inodes, 10000 blocks
500 blocks (5.00%) reserved for the super user
First data block=1
2 block groups
8192 blocks per group, 8192 fragments per group
1256 inodes per group
Superblock backups stored on blocks: 8193
Writing inode tables: done
Writing superblocks and file system accounting information: done
This file system will be automatically checked every 21 mounts or 180 days,
whichever comes first. Use tune2fs -c or -i to override.

Step 3 — Mount the Test File System

After your test file system has been created, you can experiment with the different options for the formatting commands you will be using. It will be useful to make a mount point for your image file:

# mkdir /mnt/image

and then mount it:

# mount /dev/loop0 /mnt/image

You can do this now because you already have the loopback file system associated with the image file. Later on, if you remount it, you must use the following format to use the loopback option:

# mount -o loop /tmp/fedoratest.img /mnt/image

After mounting the new file system, you can look at it and see that the /lost+found directory has been created on it and that the df command returns:

# df -h /mnt/image
File System Size Used Avail Use% Mounted on/dev/loop0
9.5M 13k 8.9M 1% /mnt/image

To unmount it, use this:

# umount /mnt/image

Make a backup of the image just in case you break the original:

# cp /tmp/fedoratest.img fedoratest.bak

After the test file system is created, you can create directories, copy files to it, delete files, attempt to recover them, and, in general, create controlled chaos on your computer while you are learning and practicing valuable skills. If you damage the file system on the image beyond repair, unmount it, delete it, and create a new one (or copy a new one from that backup).

Оглавление книги


Генерация: 1.075. Запросов К БД/Cache: 3 / 1
поделиться
Вверх Вниз