Thursday, 22 December 2011

Partition a new drive in a server/VPS

-----------------------------------------------------------
How to partition a disk, set its file type & then mount it
-----------------------------------------------------------

1) To know all the disks attached to VPS/Server type

# fdisk -l

It will show you all the disk along with the unpartitioned disk. Let we attached a new disk "/dev/xvdb" to server. It will show the following message on "fdisk -l":

----
/dev/xvdb has no partition table.
----

2) Execute the following:

# parted /dev/xvdb

3) Type "mklabel"
4) It will ask "New disk label type?" type "msdos"
5) Type "print free" to know the partition details.
6) Type "mkpart primary 0.00kB 53.7GB" (let the total size is 53.7GB)
7) type "quit"

Please see the following snippet for reference:

=====================
root@server [~] parted /dev/xvdb
GNU Parted 1.8.1
Using /dev/xvdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Error: Unable to open /dev/xvdb - unrecognised disk label.
(parted) mklabel
New disk label type? msdos
(parted) print free

Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
0.00kB 53.7GB 53.7GB Free Space

(parted) mkpart primary 0.00kB 53.7GB
(parted) print free

Model: Xen Virtual Block Device (xvd)
Disk /dev/xvdb: 53.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number Start End Size Type File system Flags
1 0.51kB 53.7GB 53.7GB primary

(parted) quit
Information: Don't forget to update /etc/fstab, if necessary.

root@server [~]#
=====================

8) Execute the following command:

# fdisk -l

This time it will show the new disk too. Now we have to set the file system type on the disk and mount it.

====================
root@server [~]# fdisk -l

Disk /dev/xvda: 64.4 GB, 64424509440 bytes
255 heads, 63 sectors/track, 7832 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/xvda1 * 1 7832 62910508+ 83 Linux

Disk /dev/xvdb: 53.6 GB, 53687091200 bytes
255 heads, 63 sectors/track, 6527 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

Device Boot Start End Blocks Id System
/dev/xvdb1 1 6528 52428799+ 83 Linux
====================

9) Set the filesystem type for the new disk using "mkfs.ext3 /dev/xvdb1"

====================
root@server [~]# mkfs.ext3 /dev/xvdb1
mke2fs 1.39 (29-May-2006)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
6553600 inodes, 13107199 blocks
655359 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
400 block groups
32768 blocks per group, 32768 fragments per group
16384 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424

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

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

10) Take a backup of file "/etc/fstab" and then open it using "vi" editor and mount it

------
root@server [~]# cp -pv /etc/fstab /etc/fstab2
------

We will add the following line in "/etc/fstab". Here we are mounting the partition at "/backup".

------
/dev/xvdb1 /backup ext3 defaults 0 0
------
11) Use the following command to mount it.

------
root@server [~]# mount /backup
------

Thank you.

2 comments:

Website Hosting India said...

These are informative and useful steps you have shared and great knowledge and i get it here,
web hosting provider

Hemanth said...

Thank you for sharing your comments. Kindly share this site in social network. Also recommend to your friends.

We will keep updating the site with more technical articles.