How to add new partition in centos

Mounted File Systems or Logical Volumes

There are two ways to configure a new disk drive into a CentOS system. One very simple method is to create one or more Linux partitions on the new drive, create Linux file systems on those partitions and then mount them at specific mount points so that they can be accessed. This is the approach that will be covered in this chapter.

Another approach is to add the new space to an existing volume group or create a new volume group. When CentOS is installed using the default disk configuration layout, a volume group is created and called VolGroup00. Within this volume group are two logical volumes named LogVol00 and LogVol01 that are used to store the / file system and swap partition respectively. By configuring the new disk as part of a volume group we are able to increase the disk space available to the existing logical volumes. Using this approach we are able, therefore, to increase the size of the / file system by allocating some or all of the space on the new disk to LogVol00. This topic will be discussed in detail in Adding a New Disk to a CentOS Volume Group and Logical Volume.

Getting Started

This tutorial assumes that the new physical hard drive has been installed on the system and is visible to the operating system. The best way to do this is to enter the system BIOS during the boot process and ensuring that the BIOS sees the disk drive. Sometimes the BIOS will provide a menu option to scan for new drives. If the BIOS does not see the disk drive double check the connectors and jumper settings (if any) on the drive.

Finding the New Hard Drive in CentOS

Assuming the drive is visible to the BIOS it should automatically be detected by the operating system. Typically, the disk drives in a system are assigned device names beginning hd or sd followed by a letter to indicate the device number. For example, the first device might be /dev/sda, the second /dev/sdb and so on.

The following is output from a system with only one physical disk drive:

ls /dev/sd*
/dev/hda  /dev/hda1  /dev/hda2  /dev/hdc

This shows that the disk drive represented by /dev/hda is itself divided into 2 partitions, represented by /dev/hda1 and /dev/hda2. In this instance, /dev/hdc is the CDROM. In all likelihood, when a second disk drive is detected by the system it will be assigned to /dev/hdb.

Another option is to install and run the Hardware Browser. If this is already installed it may be launched by selecting Hardware from the System -> Administration menu. If this option is not available it may be installed as follows:

su -
yum install hwbrowser

In the Hardware Browser scroll down the list of devices until the disk drives become visible. Select the new disk device and click on the Advanced tab to identify the device name. In the following figure the new hard disk has been assigned to /dev/hdb:


A new disk drive listed in the CentOS Hardware Manager tool


The following output is from the same system after a second hard disk drive has been installed:

ls /dev/hd*
/dev/hda  /dev/hda1  /dev/hda2  /dev/hdb  /dev/hdc

As shown above, the new hard drive has been assigned to the device file /dev/hdb. At this point the drive has no partitions shown (because we have yet to create any).

At this point we have a choice of creating partitions and file systems on the new drive and mounting them for access or adding the disk as a physical volume as part of a volume group. To perform the former continue with this chapter, otherwise readAdding a New Disk to a CentOS Volume Group and Logical Volume for details on configuring Logical Volumes.

Creating Linux Partitions

The next step is to create one or more Linux partitions on the new disk drive. This is achieved using the fdisk utility which takes as a command-line argument the device to be partitioned:

fdisk /dev/hdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel. Changes will remain in memory only,
until you decide to write them. After that, of course, the previous
content won't be recoverable.

The number of cylinders for this disk is set to 47536.
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)
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

Command (m for help):

In order to view the current partitions on the disk enter the p command:

Command (m for help): p

Disk /dev/hdb: 24.5 GB, 24533532672 bytes
16 heads, 63 sectors/track, 47536 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes

   Device Boot      Start         End      Blocks   Id  System

As we can see from the above fdisk output the disk currently has no partitions because it is a previously unused disk. The next step is to create a new partition on the disk, a task which is performed by entering n (for new partition) and p (for primary partition):

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 

In this example we only plan to create one partition which will be partition 1. Next we need to specify where the partition will begin and end. Since this is the first partition we need it to start at cylinder 1 and since we want to use the entire disk we specify the last cylinder as the end. Note that if you wish to create multiple partitions you can specify the size of each partition by cylinders, bytes, kilobytes or megabytes.

Partition number (1-4): 1
First cylinder (1-47536, default 1):
Using default value 1
Last cylinder or +size or +sizeM or +sizeK (1-47536, default 47536):
Using default value 47536

Now that we have specified the partition we need to write it to the disk using the w command:

Command (m for help): w
The partition table has been altered!

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

If we now look at the devices again we will see that the new partition is visible as /dev/hdb1:

ls /dev/hd*
/dev/hda  /dev/hda1  /dev/hda2  /dev/hdb  /dev/hdb1  /dev/hdc

The new partition will similarly be visible in the Hardware browser. The next step is to create a file system on our new partition.

Creating a File System on a CentOS Disk Partition

We now have a new disk installed, it is visible to CentOS and we have configured a Linux partition on the disk. The next step is to create a Linux file system on the partition so that the operating system can use it to store files and data. The easiest way to create a file system on a partition is to use the mkfs.ext3 utility which takes as arguments the label and the partition device:

/sbin/mkfs.ext3 -L /userdata /dev/hdb1

mke2fs 1.39 (29-May-2006)
Filesystem label=/userdata
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
2998272 inodes, 5989528 blocks
299476 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=0
183 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

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

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

Mounting a File System

Now that we have created a new file system on the Linux partition of our new disk drive we need to mount it so that it is accessible to the CentOS system and its users. In order to do this we need to create a mount point. A mount point is simply a directory or folder into which the file system will be mounted. For the purposes of this example we will create a /userdata directory to match our file system label (although it is not necessary that these values match):

mkdir /userdata

The file system may then be manually mounted using the mount command:

mount /dev/hdb1 /userdata

Running the mount command with no arguments shows us all currently mounted file systems (including our new file system):

mount
/dev/mapper/VolGroup00-LogVol00 on / type ext3 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
/dev/hda1 on /boot type ext3 (rw)
tmpfs on /dev/shm type tmpfs (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
sunrpc on /var/lib/nfs/rpc_pipefs type rpc_pipefs (rw)
/dev/hdc on /media/CentOS_5.4_Final type iso9660 (ro,noexec,nosuid,nodev,uid=500)
/dev/hdb1 on /userdata type ext3 (rw)

Configuring CentOS to Automatically Mount a File System

In order to set up the system so that the new file system is automatically mounted at boot time an entry needs to be added to the /etc/fstab file.

The following example shows an fstab file configured to automount our /userdata partition:

/dev/VolGroup00/LogVol00 /                      ext3    defaults        1 1
LABEL=/boot             /boot                   ext3    defaults        1 2
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/VolGroup00/LogVol01 swap                   swap    defaults        0 0
LABEL=/userdata         /userdata               ext3    defaults        1 2

With the appropriate configuration line added to the fstab file, the file system will automatically mount on the next system restart. 

增加硬盘后用用fdisk -l 查看是否认到,我的机器上认到是 /dev/sdb ,接下来进行分区。
  进行分区:
1. fdisk /dev/sdb
  进入 fdisk的界面:
     The number of cylinders for this disk is set to 19457.
     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):
    顺便说一下命令 ,“n" 新建一个分区 ”p“ 显示分区 ”d“删除分区 ”m“ 帮助  ”q“ 不保存刚才的操作并且退出,” 当操作错误时不要惊慌使用q回车后,就可以回到本次操作前 "w" 保存操作。
   我现在要创建分区,所以输入 n  回车 进入下面的界面:
   Command action
   e   extended
   p   primary partition (1-4)
   说明:”e“ 扩展分区 ”p“ 主分区 ,在这里我创建一个主分区,一个扩展分区 输入 p 回车 进入下面的界面:
    Partition number (1-4):   现在没有任何分区所以 输入  1 回车
 
    First cylinder (1-19457, default 1):      从那个扇区开始 一般为默认值,回车
    Using default value 1
    Last cylinder or +size or +sizeM or +sizeK (1-19457, default 19457)    分区大小 输入你想要分区的大小。
    Command (m for help):  回到了第一个fdisk界面,如何先以后对剩余磁盘进行分区 则输入w 保存退出, 
   在这里继续分区 输入n   下面的出来的界面跟上面的一样就不再重复了。
   接下来输入 e 进行扩展分区,分区编号输入数字 2 ,在这里我把剩余的磁盘全部分为扩展分区,所以一路回车,回到第一步,在里很多人扩展初学者犯了个错误,认为已经完成,输入 w 保存退出。
    接下来进行格式化 mkfs.ext3  /dev/sdb1  漫长的等待后终于ok完成,
    
   格式扩展分区:mkfs.ext3  /dev/sdb2   结果发生了下面的错误:
    mke2fs 1.39 (29-May-2006)
    /dev/sdb2: Invalid argument passed to ext2 library while setting up superblock  (今天我也犯了这个错误 ) ,主要原因是把扩展分区和逻辑分区的概念搞错了,硬盘分区组成是:主分区和扩展分区。扩展分区可以分成多个逻辑分区, 所以要把扩展分区分成逻辑分区才可以使用。
   
   我们接着上面的分区继续 输入 n 进入下面的界面:
   Command action
   l   logical (5 or over)
   p   primary partition (1-4)     
   输入字母 l  进行逻辑分区  ,逻辑分区的编号是从5开始的,所以输入5 回车,后面的则根据自己的需求把扩展分区分为几个逻辑分区,我只分了一个逻辑分区,所以一路回车 然后 w 保存退出 。
   格式化逻辑分区 mkfs.ext3  /dev/sdb5   ok 分区过程就结束了。
2.挂载磁盘 mount /dev/sdb5 /mnt/data  
  需要在系统启动时,自动挂载编辑 /etc/fstab文件,加入下面的代码:
LABEL=/data              /data                    ext3    defaults        1 2  
即可,前提是 /data目录必须存在。否则,可能导致系统无法启动。

Step #1 : Partition the new disk using fdisk command

Following command will list all detected hard disks:
# fdisk -l | grep '^Disk'
Output:

Disk /dev/sda: 251.0 GB, 251000193024 bytes
Disk /dev/sdb: 251.0 GB, 251000193024 bytes

A device name refers to the entire hard disk. For more information see Linux partition naming convention and IDE drive mappings.
To partition the disk - /dev/sdb, enter:
# fdisk /dev/sdb
The basic fdisk commands you need are:

  • m - print help
  • p - print the partition table
  • n - create a new partition
  • d - delete a partition
  • q - quit without saving changes
  • - write the new partition table and exit

Step#2 : Format the new disk using mkfs.ext3 command

To format Linux partitions using ext2fs on the new disk:
# mkfs.ext3 /dev/sdb1

Step#3 : Mount the new disk using mount command

First create a mount point /disk1 and use mount command to mount /dev/sdb1, enter:
# mkdir /disk1
# mount /dev/sdb1 /disk1
# df -H

Step#4 : Update /etc/fstab file

Open /etc/fstab file, enter:
# vi /etc/fstab
Append as follows:

/dev/sdb1               /disk1           ext3    defaults        1 2

Save and close the file.

Task: Label the partition

You can label the partition using e2label. For example, if you want to label the new partition /backup, enter
# e2label /dev/sdb1 /backup
You can use label name insted of partition name to mount disk using /etc/fstab:
LABEL=/backup /disk1 ext3 defaults 1 2



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值