Lab 2: Creating a Root File System as a Ramdisk
The purpose of this lab is to make your simple root file system from lab 1 into
a more complete file system in the compressed form of a ramdisk. RAM disks are important
whenever storage is at a premium such as with diskless workstations, embedded appliances,
or emergency boot floppies. Ramdisk systems also run faster because everything is in RAM
and there is no disk i/o. The basic idea is that you create a filesystem, compress it
to fit into a minimal space, and when the kernel loads a ramdisk, it uncompresses the image
and loads it into RAM. You are trading a large amount of RAM for storage, and gaining speed.
This lab will involve five steps:
- Creating a partition in which to build the ramdisk, (fdisk)
- Copying your existing root file system into that partition (tar)
- Adding the init daemon and a few commands, with libraries, to the root file system
- Compressing the new file system and copying it to your floppy as a ramdisk (dd, gzip)
- Editing the syslinux.cfg file on your boot floppy to boot a ramdisk. (vi)
Your expanded root filesystem will contain the following files:
bin dev etc lib sbin proc tmp
sh console inittab ld-linux.so.2 init
mount kmem fstab libc.so.6 fdisk
umount mem rc.d
hostname null rc.sysinit
tty1
ram0
fd0
hda*
Step One
Creating partitions
Typically, filesystems are placed in partitions of a hard disk. You will use the fdisk
utility to add a small 8MB partition
- Log on as root.
- Use fdisk to create the following partition:
- hda7 8 MB Logical ( 1 cylinder)
Note:1 cylinder means the start and end cylinder are the same value!
fdisk /dev/hda
Note the device file name of the partition you made, and don't forget to write the partition table.
- Reboot your system by typing the command reboot.
- When the system comes back up, log back on as root, and take a "picture"
of your changes:
fdisk -l > /root/myfs
- For our ramdisk partition, we want to remove any junk that may be in the partition by
zeroing it out. This will allow for maximum compression.
dd if=/dev/zero of=/dev/hda7 bs=1k count=8000
Note: Be CAREFUL with the above command - be sure the count represents the correct size
of the partition.
- Now create an ext file system in the ramdisk partition.
mkfs -m 0 -N 200 /dev/hda7
Compare the blocksize and inode count allocated with the hda7 file system.
Step Two
Copy the files from your floppy disk root file system to the partition on /dev/hda7
- Mount your floppy root file system to the default location at /mnt/floppy
mount /dev/fd0
- Change directory to the root of your file system:
cd /mnt/floppy
- Extract all the files to a tarball in root's home directory:
tar cvf /root/myfiles.tar .
Note: don't forget the dot at the end of the above command
- Now, create a mount point for your ramdisk partition:
mkdir /mnt/ramdisk
- Mount your ramdisk partition to this directory:
mount /dev/hda7 /mnt/ramdisk
- Change directory to the above mount point (the root of your ramdisk) and extract the
files from the tarball you made:
cd /mnt/ramdisk; tar xvf /root/myfiles.tar
- Confirm that your files are all extracted:
ls -l
- Unmount your floppy disk:
umount /dev/fd0
Step Three
Add the directories, commands, libraries and configuration files required to support the startup of UNIX/Linux
- First, we need a couple new directories: /lib, /etc, /proc, and /tmp
First, chang directory to the root of your mounted file system:
cd /mnt/ramdisk
Now make the four directories listed above:
mkdir lib proc etc tmp
and set the sticky bit on the tmp directory:
chmod 1777 tmp
- Second, remove the init symbolic link from the sbin directory. Remember, this
was a pointer to the ash.static file from the first lab.
- We will be adding the following commands to the root ramdisk:
sh, hostname, mount, umount, init and fdisk, (We will use ash for the shell.)
Let's see what libraries these commands require. Use the ldd command to check:
ldd /bin/ash /bin/mount /bin/umount /bin/hostname /sbin/init /sbin/fdisk
- Note that there were only two libraries that these, and most, executables are dependent
on: ld-linux.so.2 and libc.so.6. Copy these libraries to your lib directory:
cp /lib/ld-linux.so.2 /lib/libc.so.6 /mnt/ramdisk/lib
- Now lets add the binaries:
From /bin copy ash to your ramdisk's bin directory and rename it to sh
From /bin copy the mount, umount and hostname commands to your bin directory.
From /sbin copy the init and fdisk commands to your sbin directory.
- Since we want to use fdisk to look at our hard drives, we must add the device files
that describe the drives:
cd /dev; cp -a hda hda? hdb hdb? /mnt/ramdisk/dev
- Now we must set up your etc directory with the confirguration files and scripts
for init to run:
- inittab - Your inittab file must have the following three lines in it:
id:1:initdefault:
si::sysinit:/etc/rc.d/rc.sysinit
l1:1:wait:/bin/sh
- fstab - Your fstab file must have the following two lines in it:
/dev/ram0 / ext3 defaults 1 1
none /proc proc defaults 0 0
- Now create the rc.d directory with the rc.sysinit script in it.
- mkdir rc.d; cd rc.d
- Create a shell script with at least the following 5 lines:
#!/bin/sh
echo "Welcome to ________ Linux" # Fill in your name for your brand of Linux
mount -av
hostname yourname
exit 0
- Make the shell script executable:
chmod +x rc.sysinit
Step Four
Take a snapshot of your file system, unmount the file system and save a compressed image of it on your
floppy disk.
- This command will take a "picture" of your ramdisk, and add it to your myfs file:
ls -lR /mnt/ramdisk >> /root/myfs
- Add to this file the content of your inittab and rc.sysinit files:
cat /mnt/ramdisk/etc/inittab /mnt/ramdisk/etc/rc.d/rc.sysinit >> /root/myfs
- Unmount your ramdisk:
cd /mnt; umount ramdisk
- Now copy a compressed image of your ramdisk partition to the floppy, (make sure your
floppy is still in the drive):
dd if=/dev/hda7 bs=1k count=8000 | gzip -v9 > /dev/fd0
Note: Make sure the count matches the number of blocks of your partition!
Note: Your floppy now has a compressed file system on it in raw format. You will not
be able to mount this floppy, but the kernel will be able to uncompress and mount the data.
Step Five
Edit the syslinux.cfg file on your bootdisk to create an entry for booting your ramdisk.
If you don't have your bootdisk, you can make a new one by inserting a floppy disk in the
floppy drive and typing the command:
mkbootdisk 2.4.20-8
- Mount your boot floppy
- Add another entry at the bottom of this file containing the following three lines:
label ramdisk
kernel vmlinuz
append prompt_ramdisk=1 load_ramdisk=1 rw root=/dev/fd0
- Append the contents of this file to your myfs file:
cat /mnt/floppy/syslinux.cfg >> /root/myfs
- Unmount your boot floppy.
To turn in
I will be looking at the following outputs:
- fdisk -l
- ls -lR /mnt/ramdisk
- cat inittab
- cat syslinux.cfg
Turn in your myfs file to the CIS191 account
on the machine, opus.cabrillo.edu using the following command:
scp myfs cis191@opus.cabrillo.edu:lab2.logname
Note: logname is your last name - all lowercase. e.g. my logname = griffin
(The password for the cis191 account is "ci$191" all lowercase.)
To clean up for the next person, please remove the parition you created on /dev/hda7.
Grading Rubric
- 4 points -
- For adding the correct binaries to the bin and sbin directories
- 4 points -
- For adding the correct two shared libraries to the lib directory
- 4 points -
- For adding the correct files to the etc directory
- 4 points -
- Properly partitioning the /dev/hda7 and updating the dev directory with the
corresponding device files.
- 4 points -
- For adding the correct entry to the syslinux.cfg file on your boot floppy.