首页 > 代码库 > CentOS挂载新硬盘

CentOS挂载新硬盘

1.查看当前硬盘使用状况:

df -h

 

root@VM_160_34_centos:~> df -hFilesystem            Size  Used Avail Use% Mounted on/dev/xvda1            7.9G  1.5G  6.1G  20% /

 

2.查看新硬盘 

 fdisk -l 

 

 1 root@VM_160_34_centos:~> fdisk -l             2  3 Disk /dev/xvda: 8589 MB, 8589934592 bytes 4 255 heads, 63 sectors/track, 1044 cylinders 5 Units = cylinders of 16065 * 512 = 8225280 bytes 6 Sector size (logical/physical): 512 bytes / 512 bytes 7 I/O size (minimum/optimal): 512 bytes / 512 bytes 8 Disk identifier: 0xcd6e8236 9 10     Device Boot      Start         End      Blocks   Id  System11 /dev/xvda1   *           1        1044     8385898+  83  Linux12 13 Disk /dev/xvdb: 53.7 GB, 53687091200 bytes14 255 heads, 63 sectors/track, 6527 cylinders15 Units = cylinders of 16065 * 512 = 8225280 bytes16 Sector size (logical/physical): 512 bytes / 512 bytes17 I/O size (minimum/optimal): 512 bytes / 512 bytes18 Disk identifier: 0x0000000019 20 Disk /dev/xvdb doesnt contain a valid partition table21 22 Disk /dev/xvdc: 2147 MB, 2147483648 bytes23 255 heads, 63 sectors/track, 261 cylinders24 Units = cylinders of 16065 * 512 = 8225280 bytes25 Sector size (logical/physical): 512 bytes / 512 bytes26 I/O size (minimum/optimal): 512 bytes / 512 bytes27 Disk identifier: 0x0000000028 29 Disk /dev/xvdc doesnt contain a valid partition table

这结果很明显让人烦躁,一坨屎一样,下面我们过滤一下

命令:

fdisk -l  |grep Disk

结果:

root@VM_160_34_centos:~> fdisk -l  |grep DiskDisk /dev/xvdb doesnt contain a valid partition tableDisk /dev/xvdc doesnt contain a valid partition tableDisk /dev/xvda: 8589 MB, 8589934592 bytesDisk identifier: 0xcd6e8236Disk /dev/xvdb: 53.7 GB, 53687091200 bytesDisk identifier: 0x00000000Disk /dev/xvdc: 2147 MB, 2147483648 bytesDisk identifier: 0x00000000

这个看起来让人舒心多了,

结果中:

提示这个

Disk /dev/xvdb doesnt contain a valid partition tableDisk /dev/xvdc doesnt contain a valid partition table

这两行应该就是说还没有挂载上去的硬盘了.

新添加的硬盘的编号为

/dev/xvdb
/dev/xvdc

3.硬盘分区 

  • 进入fdisk模式
 fdisk /dev/xvdb  

 

 1 root@VM_160_34_centos:~> fdisk /dev/xvdb   2 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel 3 Building a new DOS disklabel with disk identifier 0x7ca79642. 4 Changes will remain in memory only, until you decide to write them. 5 After that, of course, the previous content wont be recoverable. 6  7 Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite) 8  9 WARNING: DOS-compatible mode is deprecated. Its strongly recommended to10          switch off the mode (command c) and change display units to11          sectors (command u).12 13 Command (m for help): 
  •  输入n进行分区 
Command (m for help): nCommand action   e   extended   p   primary partition (1-4)
p

  这里有两个选项: 
?   p: 主分区 linux上主分区最多能有4个 
?   e: 扩展分区 linux上扩展分区只能有1个,扩展分区创建后不能直接使用,还要在扩展分区上创建逻辑分区。 


这里我选择的p。 

 

  • 选择分区个数 
Partition number (1-4): 1

可以选择4个分区,这里我只分成1个分区 

  • 设置柱面,这里选择默认值就可以,直接按回车过去
First cylinder (1-6527, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): Using default value 6527
  • 输入w,写入分区表,进行分区 
Command (m for help):w
Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

整个过程如下:

root@VM_160_34_centos:~> fdisk /dev/xvdb  Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0x7ca79642.Changes will remain in memory only, until you decide to write them.After that, of course, the previous content wont be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. Its strongly recommended to         switch off the mode (command c) and change display units to         sectors (command u).Command (m for help): nCommand action   e   extended   p   primary partition (1-4)Last login: Fri Jun 13 18:19:47 2014 from 112.90.239.105root@VM_160_34_centos:~>  Display all 1286 possibilities? (y or n)root@VM_160_34_centos:~> root@VM_160_34_centos:~> fdisk /dev/xvdxvda   xvda1  xvdb   xvdc   root@VM_160_34_centos:~> fdisk /dev/xvdb  Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabelBuilding a new DOS disklabel with disk identifier 0xc925c1ae.Changes will remain in memory only, until you decide to write them.After that, of course, the previous content wont be recoverable.Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)WARNING: DOS-compatible mode is deprecated. Its strongly recommended to         switch off the mode (command c) and change display units to         sectors (command u).Command (m for help): nCommand action   e   extended   p   primary partition (1-4)pPartition number (1-4): 1First cylinder (1-6527, default 1): Using default value 1Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): Using default value 6527Command (m for help): wThe partition table has been altered!Calling ioctl() to re-read partition table.Syncing disks.

4.格式化分区 

将新分区格式化为ext3文件系统 

  • 如果创建的是主分区 
#mkfs -t ext3  /dev/xvdb

 

root@VM_160_34_centos:~> mkfs -t ext3  /dev/xvdbmke2fs 1.41.12 (17-May-2010)Filesystem label=OS type: LinuxBlock size=4096 (log=2)Fragment size=4096 (log=2)Stride=0 blocks, Stripe width=0 blocks3276800 inodes, 13107200 blocks655360 blocks (5.00%) reserved for the super userFirst data block=0Maximum filesystem blocks=4294967296400 block groups32768 blocks per group, 32768 fragments per group8192 inodes per groupSuperblock backups stored on blocks:         32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,         4096000, 7962624, 11239424Writing inode tables: done                            Creating journal (32768 blocks): doneWriting superblocks and filesystem accounting information: doneThis filesystem will be automatically checked every 39 mounts or180 days, whichever comes first.  Use tune2fs -c or -i to override.

5.  挂载硬盘

  • 创建挂载点

  在根目录下创建storage目录 

#mkdir /storage 
  • 将/dev/xvdb挂载到/storage下 
#mount /dev/xvdb /storage
  • 设置开机启动自动挂载 

    新创建的分区不能开机自动挂载,每次重启机器都要手动挂载。 
    设置开机自动挂载需要修改/etc/fstab文件 

 #vi /etc/fstab 

    在文件的最后增加一行 

 /dev/xvdb /storage ext3 defaults 1 2 

 

过程如下:

 1 root@VM_160_34_centos:~> mkdir /storage 2 root@VM_160_34_centos:~> mount /dev/xvdb /storage 3 root@VM_160_34_centos:~> vi /etc/fstab  4  5  6  7 /dev/xvda1            /                    ext3       noatime,acl,user_xattr 1 1 8 /dev/xvdc            swap                 swap       defaults 0 0 9 proc                 /proc                proc       defaults              0 010 sysfs                /sys                 sysfs      noauto                0 011 debugfs              /sys/kernel/debug    debugfs    noauto                0 012 devpts               /dev/pts             devpts     mode=0620,gid=5       0 013 /dev/xvdb            /storage             ext3       defaults              1 214 ~15 ~16 ~17 ~18 ~19 "/etc/fstab" 7L, 536C written20 root@VM_160_34_centos:~> clear

 

 结果:

 

root@VM_160_34_centos:/storage> ll /total 104dr-xr-xr-x   2 root root  4096 May 15 15:33 bindr-xr-xr-x   4 root root  4096 May 31  2013 bootdrwxr-xr-x   2 root root  4096 Dec 16 11:42 datadrwxr-xr-x  15 root root  3360 Jun 13 19:21 devdrwxr-xr-x  73 root root  4096 Jun 13 19:37 etcdrwxr-xr-x   3 root root  4096 Jun  2 20:32 homedr-xr-xr-x  12 root root  4096 Jun 20  2013 libdr-xr-xr-x   9 root root 12288 Jun 20  2013 lib64drwx------   2 root root 16384 May 31  2013 lost+founddrwxr-xr-x   2 root root  4096 Sep 23  2011 mediadrwxr-xr-x   2 root root  4096 Sep 23  2011 mntdrwxr-xr-x   2 root root  4096 Sep 23  2011 optdr-xr-xr-x 106 root root     0 Jun 14  2014 procdr-xr-x---   2 root root  4096 Jun 13 19:35 rootdr-xr-xr-x   2 root root 12288 Jun 19  2013 sbindrwxr-xr-x   2 root root  4096 May 31  2013 selinuxdrwxr-xr-x   2 root root  4096 Sep 23  2011 srvdrwxr-xr-x   3 root root  4096 Jun 13 19:37 storagedrwxr-xr-x  13 root root     0 Jun 14  2014 sysdrwxrwxrwt   3 root root  4096 Jun 13 18:26 tmpdrwxr-xr-x  13 root root  4096 May 31  2013 usrdrwxr-xr-x  19 root root  4096 Jul 16  2013 varroot@VM_160_34_centos:/storage> fdisk -l  |grep DiskDisk /dev/xvdb doesnt contain a valid partition tableDisk /dev/xvdc doesnt contain a valid partition tableDisk /dev/xvda: 8589 MB, 8589934592 bytesDisk identifier: 0xcd6e8236Disk /dev/xvdb: 53.7 GB, 53687091200 bytesDisk identifier: 0x00000000Disk /dev/xvdc: 2147 MB, 2147483648 bytesDisk identifier: 0x00000000root@VM_160_34_centos:/storage> df -lh              Filesystem            Size  Used Avail Use% Mounted on/dev/xvda1            7.9G  1.5G  6.1G  20% //dev/xvdb              50G  180M   47G   1% /storage

教程到此结束!