**云Linux新增硬盘分区格式化/挂载数据盘

Eave 2016.02.09 15:54

Linux的云服务器数据盘未做分区和格式化,可以根据以下步骤进行分区以及格式化操作

下面的操作将会把数据盘划分为一个分区来使用。

一、查看数据盘

在没有分区和格式化数据盘之前,使用 "df –h"命令,是无法看到数据盘的,可以使用"fdisk -l"命令查看

$ fdisk -l
Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00078f9c

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1        2611    20970496   83  Linux

Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

二、对数据盘进行分区

执行"fdisk -S 56 /dev/xvdb"命令,对数据盘进行分区;根据提示,依次输入"n"、"p"、"1",两次回车,"wq",分区就开始了,很快就会完成

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

Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2937, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2937, default 2937):
Using default value 2937

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

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

三、查看新的分区

使用"fdisk -l"命令可以看到,新的分区xvdb1已经建立完成了

Disk /dev/xvda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00078f9c

    Device Boot      Start         End      Blocks   Id  System
/dev/xvda1   *           1        2611    20970496   83  Linux

Disk /dev/xvdb: 21.5 GB, 21474836480 bytes
255 heads, 56 sectors/track, 2937 cylinders
Units = cylinders of 14280 * 512 = 7311360 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xfc98c86c

    Device Boot      Start         End      Blocks   Id  System
/dev/xvdb1               1        2937    20970152   83  Linux

四、格式化新分区

使用"mkfs.ext4 /dev/xvdb1"命令对新分区进行格式化,格式化的时间根据硬盘大小有所不同

$ mkfs.ext4 /dev/xvdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242538 blocks
262126 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
160 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, 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 31 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

五、添加分区信息

使用"echo "/dev/xvdb1 /mnt ext4 defaults 0 0" >> /etc/fstab"命令写入新分区信息

然后使用“cat /etc/fstab”命令查看,出现以下信息就表示写入成功。

* 如果需要把数据盘单独挂载到某个文件夹,比如单独用来存放网页,可以修改以上命令中的/mnt部分

$ cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Thu Aug 14 21:16:42 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=abbbabbb-abbb-abbb-abbb-abbbabbbabbb /     ext4 defaults,barrier=0 1 1
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
none                    /proc/xen               xenfs   defaults        0 0
/dev/xvdb1              /mnt                    ext4    defaults        0 0

六、挂载新分区

使用"mount -a"命令挂载新分区,然后用“df -h”命令查看,出现以下信息就说明挂载成功,可以开始使用新的分区了

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
/dev/xvda1       20G  8.1G   11G  44% /
tmpfs           498M     0  498M   0% /dev/shm
/dev/xvdb1       20G   44M   19G   1% /mnt