Device versus File

holding data on a storage device with and without a fileystem

Read a little about devices versus files as alternative types of data containers. Work with 2 floppy diskettes.


The exercise for you to perform:

USB flash drive version

Acquire a copy of Abraham Lincoln's Gettysburg Address in file gettysburg.txt per your instructor. Below are two ways to put the contents of the Address onto a usb flash drive (usb). Do it both ways, first one then the other. First find out what your usb is called. Plug it in. Run dmesg, the last screen output will show it to be sdb or sdc or similar. Alternatively, the last messages in /var/log/messages will reveal. Alternatively, fdisk -l should show it.

dmesg

or

tail /var/log/messages

or

fdisk -l

Be careful, and be certain, which one is the usb and which is the hard drive. You are going to destroy any data on one, and destroying the hard drive is less preferred. Often, the hard drive might be /dev/sda while the usb would be /dev/sdb, but it varies and you must know what it is on your particular system. Here, in the commands below, I will assume it is /dev/sdx. Where I use sdx, you please substitute the real thing for your system.

1) put the Gettysburg Address onto the usb directly, i.e. using the usb itself as the housing container and starting at byte number one:

cat gettysburg.txt | dd of=/dev/sdx

Now to examine what you got:

dd if=/dev/sdx | xxd -g1 | less

Scroll up and down. Where is the Gettysburg Address?

2) put a file named "ga.txt" onto the usb, and put the Gettysburg Address into that instead. It won't start at usb byte number one; it will start at file byte number one instead. The housing container for the Address is the file, not the usb. But we can't talk about a file right now because we don't have a filesystem (no filesystem, no files!). Let's take care of that:

mkfs /dev/sdx
mkdir /media/usb
mount /dev/sdx /media/usb
cat gettysburg.txt > /media/usb/ga.txt
umount  /dev/sdx

Now to examine what you got:

dd if=/dev/sdx | xxd -g1 | less

to see a whole, beginning-go-end regurgitation of exactly what's on the usb. Where on the usb does the Mr. Lincoln's Address start? Locate it (you're within the "less" utility, where you can search for small fragments of known text from the Address by pressing the slash / key then typing target text and Enter, till the Address appears; exit less by pressing letter q. Short, somewhat unique words you might search for in the Address are "God" and "civil". It may take a while to find.) Try also searching for "ga.txt". If you find it, what are you looking at? When you find it, do you also see the Address itself?

3) put a partition onto the usb, then put a filesystem into the partition, then put "ga.txt" into the partition. The difference this time is that above the place where we put the filesystem, using mkfs, was on the device whereas here we will instead put it in a partition (which we'll make; above we had no partition).

parted /dev/sdx mklabel msdos
parted /dev/sdx mkpart primary ext2 100 101
mkfs /dev/sdx1
mkdir /media/usb
mount /dev/sdx1 /media/usb
cat gettysburg.txt > /media/usb/ga.txt
umount  /dev/sdx1

Now to examine what you got.

dd if=/dev/sdx | xxd -g1 | less

to again see a whole, beginning-to-end regurgitation of exactly what's on the usb. Where on the usb does the Mr. Lincoln's Address start? Locate it (you're within the "less" utility, where you can search for small fragments of known text from the Address by pressing the slash / key then typing target text and Enter, till the Address appears; exit less by pressing letter q. Short, somewhat unique words you might search for in the Address are "God" and "civil". It may take a while to find.) After you find it, renew the search. Maybe the copy of the Gettysburg Address placed on the file in the previous section is still there (should be) and you are finding it first. Whenever you find the Address, you can notice where it is from the offset address in the left column of xxd's output.

...How did you figure out where the file contents lay? When we ask the computer the same question (by "reading" the file), how can the computer figure it out?


Here is an image of the usb in the aftermath of performing this exercise.


Floppy disk version

Acquire a copy of Abraham Lincoln's Gettysburg Address in file gettysburg.txt per your instructor. Below are two ways to put the contents of the Address onto a floppy. Do it both ways, once onto each of your 2 floppies.

1) put the Gettysburg Address onto the floppy directly, i.e. using the floppy itself as the housing container and starting at byte number one:

cat gettysburg.txt | dd of=/dev/fd0

2) put a file named "ga.txt" onto the floppy, and put the Gettysburg Address into that instead. It won't start at floppy byte number one; it will start at file byte number one instead. The housing container for the Address is the file, not the floppy.

mkfs /dev/fd0
mount /dev/fd0 /media/floppy
cat gettysburg.txt > /media/floppy/ga.txt
umount  /dev/fd0


Now to examine what you got. Successively put your floppies into the drive and for each execute:

dd if=/dev/fd0 | xxd -g1 | less

to see an A-to-Z (or 1-to-1.4MB) regurgitation of exactly what's on the floppy. Where on the floppy does the Mr. Lincoln's Address start? Locate it in both cases. In the latter, "file" case, where is the Address within the floppy? (you're within the "less" utility, where you can search for small fragments of known text from the Address by pressing the slash / key then typing target text and Enter, till the Address appears; exit less by pressing letter q.)

...How did you figure out the answer? When we ask the computer the same question (by "reading" the file), how can the computer figure it out?