dcsimg

Disk-O-Tech: Linux Disk Management

Are you bewildered by disk management in Linux? Don't be. Here are the nuts and bolts of adding a new disk to your system.

You might have worked with Linux for years and never added an additional disk to your system or perhaps you were too frustrated by Linux’s strange ways of dealing with disks to attempt it. In either case, here’s your opportunity to work through the steps required in adding a new disk to your system. The process, once you have your disk physically installed into the system, is simple.

Most of us install a new Linux system with a series of mouse clicks or taps on the keyboard without regard for how the disk setup takes place. Desktop users generally accept the default installation with little thought. But, what happens when you run out of space on a disk or filesystem?
Server system operators know the procedure well but perennial desktop jockeys might never have treaded the multiple disk waters. Well, it’s time to dive in to the deep end.

Initial Setup

The first step is to install the new disk into your system. Find an open bay in your computer, set the drive’s jumpers, plug the drive into system power and into a controller cable. Boot up your system and watch your BIOS for the new drive. Login to your system and su to root or use sudo and issue the following commands.

debian:~# sfdisk -l

Disk /dev/hda: 10402 cylinders, 16 heads, 63 sectors/track
Units = cylinders of 516096 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/hda1          0       -       0          0    0  Empty
/dev/hda2          0       -       0          0    0  Empty
/dev/hda3          0       -       0          0    0  Empty
/dev/hda4          0       -       0          0    0  Empty

Disk /dev/sda: 652 cylinders, 255 heads, 63 sectors/track
Units = cylinders of 8225280 bytes, blocks of 1024 bytes, counting from 0

   Device Boot Start     End   #cyls    #blocks   Id  System
/dev/sda1   *      0+    616     617-   4956021   83  Linux
/dev/sda2        617     651      35     281137+   5  Extended
/dev/sda3          0       -       0          0    0  Empty
/dev/sda4          0       -       0          0    0  Empty
/dev/sda5        617+    651      35-    281106   82  Linux swap / Solaris

I have two disks in the example system, /dev/sda (System SCSI disk) and /dev/hda (Empty IDE disk). The IDE disk is the new disk that we’ll use for this example. Since you know the device name of the new disk (/dev/hda), it’s easy to configure it.

debian:~# fdisk /dev/hda

The number of cylinders for this disk is set to 10402.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)

Command (m for help):

You’re now in the fdisk (fixed disk) utility, where you’ll setup partitions on the new disk. For simplicity, we’ll create one single partition on this disk. Please realize that you can split the disk into several partitions or “slices” as your needs dictate. It’s time to create that partition.

Begin by telling fdisk that you want to create a new partition and then provide the partition number that you want to create (1). To use the entire disk, hit ENTER to select the default cylinder (1) and hit ENTER again to select the last cylinder for the end of the partition. Write the changes to disk (w). Once you’ve written the changes to disk, fdisk closes and returns you to the system prompt.

Command (m for help): n 

Command action
   e   extended
   p   primary partition (1-4)
p 

Partition number (1-4): 1 

First cylinder (1-10402, default 1): 

Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-10402, default 10402): 

Using default value 10402

Command (m for help): p

Disk /dev/hda: 5368 MB, 5368709120 bytes
16 heads, 63 sectors/track, 10402 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Disk identifier: 0x365a714a

   Device Boot      Start         End      Blocks   Id  System
/dev/hda1               1       10402     5242576+  83  Linux

Command (m for help): w

The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

debian:~#

You’re now ready to create the filesystem. Because we used the entire disk, you only have to do this once.

debian:~# mkfs.ext3 /dev/hda1
mke2fs 1.41.3 (12-Oct-2008)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
327680 inodes, 1310644 blocks
65532 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
        32768, 98304, 163840, 229376, 294912, 819200, 884736

Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 24 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

debian:~#

Mounting the Disk

You’ve created the filesystem (ext3) on your new disk. You can’t “see” or use the disk yet until it’s mounted. Mounting a disk means to make its contents available to the system and its users by providing a mount point on which the disk will reside. To do this, you need to use an existing directory or create a new one onto which the disk will mount. Since this disk will provide space for backing up valuable files, create a directory named /backup.

debian:~# mkdir /backup

Mount the disk using the standard mount command syntax: mount device_name mount_point

debian:~# mount /dev/hda1 /backup

Congratulations, you’ve installed a new hard drive into your system, setup the disk in fdisk, created a mount point, mounted the drive onto the mount point and you’re ready to start filling up the new space with backups. And, not a moment too soon as you can see.

debian:~# df –h

Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             4.7G  4.2G  314M  94% /
tmpfs                 253M     0  253M   0% /lib/init/rw
udev                   10M  588K  9.5M   6% /dev
tmpfs                 253M     0  253M   0% /dev/shm
/dev/hda1             5.0G  139M  4.6G   3% /backup

Are we done here? Yes and no. We’re done as far as using this new disk for backups and moving off some non-essential files to free up some space. What happens if you reboot the system? You wouldn’t see the new disk. You might think it disappeared or failed. It’s still there, just like you left it—in manual mount mode.

And, that’s OK, if you want the disk available to you on an occasional basis and would prefer to mount it only when needed. If you’d like to have it available on a persistent basis, you need to do a bit more work.

Creating a Persistent Mount

Danger, Will Robinson! You have to edit a system file (/etc/fstab) for this one but rather than bore you with vi the vi version of file editing, we’re going to cheat. The fstab file has the following format:

file system    mount point    type   options   dump    pass

File system is /dev/hda1, mount point is /backup, type is ext3, options is defaults, dump is 0 and pass is 2. I’ll save the explanations of these options for a future entry. To add that list of parameters to the /etc/fstab file, we’ll use the echo command.

debian:~# echo "/dev/hda1    /backup   ext3   defaults    0    2" >> /etc/fstab

Now, your new disk will always mount automatically in read/write mode for you upon subsequent boot ups.

Certain aspects of Linux can challenge the saltiest (and most patient) among computer users. Adding new disks is a common sore point for users unfamiliar with the unusual methods used in Linux. Remember to be careful when editing system files (files in the /etc directory) and using fdisk, since both can cause irreparable (at worst) or time-consuming damage to your system.

Comments on "Disk-O-Tech: Linux Disk Management"

dwains

\”cfdisk\” found on many distros is a much easier to use tool than fdisk. Use it if it is available.

khess

cfdisk is a good utility. It all depends on what you like. Some people like vi, others hate it. I prefer fdisk because that\’s what I\’m used to from the early days of Linux when that was \’the\’ utility for modifying disk partitions.

here2serve

Nice but please back up fstab first and normally you should not create a new directory in root. Put the mount point in /mnt/back…

viralnexxus

@here2serve
Good lookin out there! I can\’t tell you how many times I have screwed up and wished I had backed it up. I learned the hard way… Cool article too btw.
Cheers,
-VB

aotto

When adding a new disk, it\’s fine to run mount manually, as shown above with:

debian:~# mount /dev/hda1 /backup

But if you add it to fstab later as suggested, you are assuming that it will mount when you reboot. You probably don\’t want to reboot just to verify if it remounts the way you expect it to.

There is a better way. You can unmount it, and then test it using the fstab entry:

debian:~# cd /
debian:~# umount /backup
debian:~# mount /backup

This will cause the mount command to consult the fstab entry for how to mount the partition. This way if you have any problems with your fstab entry you find it before you try a reboot.

–Adrian

shreedhan

I usually use gparted. The graphical mode is simple and easy. And you wouldn\’t need to worry that you mistakenly selected a wrong disk or partition in the command line.

khess

Yes, you should always make backups but the echo addition method is the safest for adding since it alleviates editor anomalies. You can also do an umount /backup and then do a mount -a to test your mounts. Rebooting is the true acid test though. I\’m not sure why *nix people have such an aversion to rebooting.

I just like fdisk. It\’s quick and I\’ve never blown away the wrong disk or partition in it (knock wood) because I use the \’p\’ (print) command after I do anything. If you don\’t write to disk with \’w\’ then you\’ve made no changes so it\’s pretty safe. Like all things in *nix systems, you have to be careful because the system assumes you know what you\’re doing when you do it.

ssbrshei

Using \’mount -a\’ here is probably not ideal in this situation. According to the man page:

mount -a [-t type] [-O optlist]
(usually given in a bootscript) causes all file systems mentioned in fstab (of the proper type and/or having or not having the proper options) to be mounted as indicated, …

This means it will try to remount the already mounted file systems again. Probably not fatal but usually I try to avoid this whenever possible. Since you already added the entry, /backup, to the fstab, it\’s safer just to do (just like aotto has pointed out):

(cd away from /backup if you were there)
umount /backup
mount /backup

Also you might also want to mention -L (to specify a disk label) or -U (to give a uuid) when making filesystem. This way, you don\’t have to specify partition in fstab:

# mkfs.ext3 -L/backup /dev/hdb1

and in your fstab

LABEL=/backup /backup ext3 defaults 0 2

Thanks for the article.

grdetil

As ssbrshei suggested, using volume labels is a good idea. If you don\’t add it to the mkfs command, you can always add it later with the e2label command.

But I\’ve never had a problem with \”mount -a\” trying to remount already mounted filesystems. It\’s smart enough to check what\’s mounted, and only mounts what\’s not already there.

porridge

You better make sure that fstab ends with a newline before you append do it…

mikerjohnson

I would recommend anyone reading this to seriously evaluate using LVM over a plain partition.

Additionally, echoing ssbrshei and grdetil, use either a label or uuid in the fstab. Do not put an entry in the fstab using the /dev/<drive> format. While it may not come back to bite you, it just might. Consider that if you add a second partition, then delete the first, your partitions just re-enumerated. What if you decided to move your hard drive to a different controller? Now your drives are enumerated differently.

If you use labels, LVM or UUID, changing the partition table or moving drives around won\’t break things. At this point IMO, it is bad practice to setup persistent mounts using a dev path.

poloi

vi may be boring but imagine
echo \”/dev/hda1 /backup ext3 defaults 0 2\” > /etc/fstab
rather than
echo \”/dev/hda1 /backup ext3 defaults 0 2\” >> /etc/fstab

bendib

Did you REALLY think adding a disk was hard? I use GParted myself, by the way.

linuxfan62

Thanks for the detailed article. I learned something from it and also from the comments, i.e. how to check an fstab entry without rebooting. I use the simple gedit to edit fstab. \”gksudo gedit /etc/fstab\”. Now that people who haven\’t yet added a 2nd drive to their systems know the complicated way to do it, will you, or anyone, write a followup on how to do it with GParted, which is the simple way to do it? It allows creating drive Labels as well.

idknow

Hello Folks;

Since we\’re sharing our collective knowledge on installing fixed disks and what not, here\’s mine. (not in strict operational order)

A. ca. Summer 2009, I began using Xfs on my critical fs; not one hiccup since then; on my next distribution install I\’ll be using Xfs exclusively;

B. After I\’ve defined a new partition, my habit has been to run uuid and whatever applicable partition-info tool a fs provides and write that info to the newly-defined partition as /.uuid.<OEM-name> and /.info.<OEM-name>; for instance, when I purchase a new Western-Digital disk, those filenames being with WD and I append its model name.

C. IMHO having multiple disks in a computer\’s box can prevent a lot of headaches; for years I\’ve designated the first installed disk as the primary distro-boot-root disk: to that disk I install all the distribution\’s files to it exclusively; I break out /usr/local, /var and /home to partitions on other disks for non-distro-installed files and packages that must survive across any upgrade or refresh of the distro I use. Also, I usually define a swap partition on each new disk but that\’s not a rule written in stone;

Stay well.

– idknow.

silverwave

If you have a gui then GParted is the way to go. It has never let me down.

I just use a liveCD.

The posted cli process is a good refresher though.

You are in reality a just right webmaster. The web site loading pace is amazing. It kind of feels that you are doing any unique trick. Moreover, The contents are masterpiece. you’ve performed a wonderful task on this matter!

The live CD or USB must be of the same arhcitecture (32 or 64 bits) of the installed OS; otherwise you will get an horrible error (which I can’t remember now) at the command chroot..

Hey, thanks for the blog article.Really thank you! Will read on…

Greetings I am so glad I found your weblog, I really found you by accident, while I was looking on Aol for something else, Anyhow I am here now and would just like to say thanks for a marvelous post and a all round enjoyable blog (I also love the theme/design), I don’t have time to look over it all at the moment but I have book-marked it and also included your RSS feeds, so when I have time I will be back to read more, Please do keep up the awesome job.

Some truly good information, Gladiolus I noticed this. “Never put off until tomorrow what you can do the day after tomorrow.” by Mark Twain.

achat viagra viagra pas her
viagra generique viagra pas her
comprar viagra generico viagra

Awesome blog post. Really Great.

What you posted was very logical. However, what about this?
suppose you typed a catchier title? I mean, I don’t wish to tell you how to run your website, but what if you
added a post title that grabbed a person’s attention? I mean Disk-O-Tech: Linux Disk Management | Linux Magazine is kinda
plain. You ought to glance at Yahoo’s front page and see how they write
post titles to get people interested. You might add a video or a pic or two to grab people excited about everything’ve got to say.

Just my opinion, it could bring your posts a little livelier.

Usually posts some incredibly interesting stuff like this. If you?re new to this site.

I’m so happy to read this. This is the type of manual that needs to be given and not the random misinformation that’s at the other blogs. Appreciate your sharing this best doc.

hello there and thank you for your info – I’ve certainly
acquired anything new from right here. I did so
however expertise some technical issues employing this internet site, since I experienced to reload the website many times previous
to I could buy it to load correctly. I had been wondering should your
hosting is OK? Not really that I’m complaining, but sluggish loading instances times
will often affect your placement in the search engines and can damage your high quality score if advertising and marketing with Adwords.
Well I am just adding this RSS to my e-mail and can be aware
of a lot a greater portion of your respective intriguing content.
Be sure you update this again soon.

Feel free to visit my web-site; MiltonMKramp

Hello. remarkable job. I did not expect this. This is a excellent story. Thanks!

Wonderful story, reckoned we could combine a couple of unrelated data, nevertheless actually worth taking a search, whoa did one master about Mid East has got much more problerms at the same time.

w7CxfX uvplqtuxexoq, [url=http://enedegvpcnpz.com/]enedegvpcnpz[/url], [link=http://ajuufcrkials.com/]ajuufcrkials[/link], http://evxknrscjtcw.com/

That will be the end of this article. Here you will come across some internet sites that we believe you will enjoy, just click the links.

Very handful of web sites that occur to become comprehensive beneath, from our point of view are undoubtedly nicely worth checking out.

Leave a Reply