Sunday, March 18, 2012

Playing with Linux partitions

I always hated the obscure ways with which linux deals with certain things.
/dev for example. If you do 

ls /dev                                                                                                                                                          

This will list all the things that linux thinks is "devices". There's so much shit under it that you don't know which all of them are actually your hard disk partitions. Well, here's what I have learned
1. The disk partitions are always labeled such as sda, sdb, sda1, sdb1, hda, hdb... etc. So you can probably run  a command to find out about your partitions as below:

ls /dev/sd*                                                                                                                                                                                              

ls /dev/hd*                                                                                                                                                              

And this will list your partitions.

Now, the question remains... how do I find what lies under what partition? Here's a simple analogy:
You have a music CD, you want to find what files are there in the CD. How would you do it? You mount it on the CD player and browse it... right? Similarly, these partitions have to be mounted on an already existing directory in order to see their contents.
So, let's say you want to see what's under /dev/sda5, here's how you will do it.

1. Use and existing directory / create a new directory to mount this partition on. I will create a new directory below for the purpose of this discussion:

mkdir mount_point                                                                                                                                      

2. The above directory (aptly named as mount_point) will now be the directory on which the partition (/dev/sda5) will be mounted. You can mount the partition using following command

mount -v -t  ext3 /dev/sda5 mount_point                                                                                                                

Where, 
mount - is the linux command to mount the partition to a mount point (directory). Click on the mount ink to see the man page for mount and what those -v -t options mean
ext3 - is the filesystem with which the partition was formatted.
/dev/sda5 - is obviously the partition to be mounted
Finally, 
mount_point - is the directory on which the partition is being mounted.

Once the partition is mounted, you can simply

ls mount_point                                                                                                                                            

and see the contents of the /dev/sda5 partition.

To unmount, you can use the umount command as follows:

umount /dev/sda5                                                                                                                                        

This is the first of the many tips I am going to post as I learn linux myself. Comments, suggestions and corrections are most welcome.

Cheers!