Entry
How can I add new hard disk after I've installed Linux?
May 25th, 2001 06:10
Stas Kysel, Kagan (Kai) MacTane, alesi way,
Naturally, you will need to be root to do this.
Power down your system, using the command:
# shutdown -h now
(In all cases in this example, the # at the beginning of the line
represents root's command prompt. Don't actually type the #.)
This tells the machine to shutdown and halt (rather than rebooting)
immediately. You may wish to make sure you have no other users online
first.
Open up the case and put the drive in. Ensure that jumpers are set
correctly, etc. Put the case back on and power the machine up, then log
in as root.
The drive will be recognized by the OS as /dev/XdY, where X is either
"h" (if the drive is IDE) or "s" (if the drive is SCSI), and Y is the
letter of the alphabet that corresponds with the drive number.
(Obviously not "a", if you already had a previous drive installed.)
So, if this is the third IDE drive, it will be /dev/hdc. If it's your
fifth SCSI drive, it will be /dev/sde.
You want to run fdisk on that drive. Just plain
# fdisk
will automatically default to your first drive, so you want to use
# fdisk /dev/sde
(or whatever the proper device name is). The fdisk utility (used to
partition and format "fixed disks" -- hence the name) will start
interactively. It will prompt you to use "m" for help; by all means do
so. The major commands you'll want to use are:
l list known partition types
m print this menu
n add a new partition
p print the partition table
q quit without saving changes
v verify the partition table
w write table to disk and exit
If you make a mistake, you may also want:
d delete a partition
Use the "n" command to add partitions, sizing them however you want. For
the most part, you will want to leave them on the default partiton type
setting of 83, or Linux ext2 filesystem. When you have created all your
desired partitions, it's useful to use "v" to verify it. (There will be
a little wasted space. If it's not much, don't worry about it; that's
normal.)
Don't forget to write the partition table to disk! Then exit fdisk.
Then you have to create filesystem layout on partitions. This can
be done with the command:
# mkfs <partition device name>
The partitions have the same name as the drive itself, with a number
after each one, matching their order on the drive. So the first
partition on /dev/hdb is /dev/hdb1, the next is /dev/hdb2, and so on.
The fdisk utility will probably have given you the names of all those
partitions while you were creating them.
Now you need to mount the new partitions in your directory structure. As
long as the partitions use the ext2 filesystem (the Linux standard), you
can simply use the command:
# mount <partition device name> <directory>
When you mount a partition onto a diretory, any files that are already
in that directory become inaccessible while the partition is mounted.
This can be a sneaky way to hide files if you know what you're doing,
but more likely, you'll want to create one or more empty directories to
mount your new partition(s) on.
If you want to make your system automatically mount these new partitions
at boot time (99% of the time, you do), you should edit the file
/etc/fstab. It will have a few lines something like this:
/dev/hda1 / ext2 defaults 1 1
/dev/hdc2 /usr ext2 defaults 1 2
/dev/hda5 /home ext2 defaults 1 2
/dev/hda6 /usr/local ext2 defaults 1 2
/dev/hda7 /var ext2 defaults 1 2
/dev/hda8 swap swap defaults 0 0
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0
/dev/fd0 /mnt/floppy ext2 noauto,owner 0 0
none /proc proc defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0
The lines with "defaults" in the fourth field refer to filesystems that
are mounted by default. Note that each line starts with the partition
device ID and the mount point (expressed as a directory). Next is the
filesystem type, then the options with which it should be mounted. (For
example, the CD-ROM has the option "ro", standing for "read-only". If
your system tried to mount the CD in read-write mode, problems would
ensue.)
The final two numbers are options for the dump() system call and the
order in which partitions are checked by fsck on startup. If you create
new partitions, you just go with options of "1 2", like the other
(non-root) filesystems.
More information about the format of the /etc/fstab file can be found by
typing
$ man fstab
(Note that the non-root prompt shows you don't need to be root for this
information; man will tell anyone how to run things.)
Assuming you just created two new partitions on /dev/hdc, and you want
them to be mounted on startup as /home/stuff and //usr/local/stuff,
you'll want to add the following two lines to fstab:
/dev/hdc1 /home/stuff ext2 defaults 1 2
/dev/hdc2 /usr/local/stuff ext2 defaults 1 2
It doesn't really matter where in the file you add them, but for easy
readability, you may want to put them along with the other normal
partitions (before swap, /proc, and the removable media). So /etc/fstab
should look like:
/dev/hda1 / ext2 defaults 1 1
/dev/hdc2 /usr ext2 defaults 1 2
/dev/hda5 /home ext2 defaults 1 2
/dev/hda6 /usr/local ext2 defaults 1 2
/dev/hda7 /var ext2 defaults 1 2
/dev/hdc1 /home/stuff ext2 defaults 1 2
/dev/hdc2 /usr/local/stuff ext2 defaults 1 2
/dev/hda8 swap swap defaults 0 0
/dev/cdrom /mnt/cdrom iso9660 noauto,owner,ro 0 0
/dev/fd0 /mnt/floppy ext2 noauto,owner 0 0
none /proc proc defaults 0 0
none /dev/pts devpts gid=5,mode=620 0 0
To be sure that things have been mounted properly, you can use the
mount command with no arguments, or df to show the perecntage of Disk
Free on all mounted partitions.
If you want to use some partition as swap instead of filesystem,
you have not to use mkfs and mount on it, but instead mkswap and
swapon. If you want swapon to be made on each system bootup, you
have to edit /etc/fstab, still.