新购了一台服务器,加了一块磁盘,随手写一下操作过程。
服务器环境 centos7 64位
使用fdisk命令查看所有能被linux识别的磁盘,例如fdisk -l列出所有磁盘
挂载磁盘步骤:
1.要挂载新磁盘需要先建一个文件夹用来挂载新磁盘,例如mkdir ./wwwroot
用fdisk -l 查到一个新磁盘
Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
2.给新磁盘分区,我就直接分成一个区;
fdisk /dev/vdb #开始分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9dc66e42.
Command (m for help): m #输入m查看帮助会列出很多选项
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition#添加一个新分区
o create a new empty DOS partition table
p print the partition table #打印分区表
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit #写入分区表,分完区后一定要写入
x extra functionality (experts only)
Command (m for help): n #输入n表示我们要添加一个新分区
Partition type:
p primary (0 primary, 0 extended, 4 free)
e extended
Select (default p): #这里直接回车或者输入p表示要建立一个主分区 默认就是p建立主分区
Using default response p
Partition number (1-4, default 1): #分区号默认为1,如果该磁盘已有分区这里显示会是2-4,default 2 这里直接默认就行,系统已经计算好了的
First sector (2048-419430399, default 2048): #这里是输入分区的起始扇区,也直接回车就行了;
Using default value 2048
#这里直接加车的话就是把这个磁盘所有空间分为一个区,如果你想给第一个分区分80G 那么请输入 +80G
#Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): +80G
Last sector, +sectors or +size{K,M,G} (2048-419430399, default 419430399): #我这里是一个数据盘我就直接全部分了,直接回车
Using default value 419430399
Partition 1 of type Linux and of size 200 GiB is set #提示分区1的大小设置为200G
#这里接下来又会回到最开始分区的时候
Command (m for help): m #我们输入m,
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)
Command (m for help): p #然后输入p我们打印一下这个磁盘的分区表看一下
Disk /dev/vdb: 214.7 GB, 214748364800 bytes, 419430400 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x9dc66e42
Device Boot Start End Blocks Id System
/dev/vdb1 2048 419430399 209714176 83 Linux
#看到分区信息,这个时候分区表还在内存中,并没有写入磁盘;
#输入w,写入分区表到磁盘
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#写入分区表成功 让调用ioctl()加载分区表 这东西不好用,个人感觉太复杂因为我也没用过还是用下面的一个命令来完成这个伟大的任务
partprobe /dev/vdb#用这个命令重新加载磁盘分区表
ls -l /sbin/mkfs.* #查看支持的文件系统
#这里我就用ext4文件系统
/sbin/mkfs.ext4 /dev/vdb1 #/dev/vdb1这个分区就是我们刚才分的200G的那个
#格式化完成,接下来挂载一下就OK了
mount /dev/vdb1 /wwwroot
[root@xxx /]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 41151808 1467808 37570568 4% /
devtmpfs 8124356 0 8124356 0% /dev
tmpfs 8133860 0 8133860 0% /dev/shm
tmpfs 8133860 332 8133528 1% /run
tmpfs 8133860 0 8133860 0% /sys/fs/cgroup
tmpfs 1626776 0 1626776 0% /run/user/0
/dev/vdb1 206291944 61464 195728388 1% /wwwroot
挂载成功,然后就是设置自动挂载,这样以后重启服务器就自动挂载了
#在设置自动挂载前,查询一下uuid使用blkid命令
[root@xxxxx /]# blkid
/dev/vda1: UUID="eb448abb-3012-4d8d-bcde-94434d586a31" TYPE="ext4"
/dev/vdb1: UUID="7a7502e2-2cb5-4cc6-b594-361c60ae8092" TYPE="ext4"
vi /etc/fstab #编辑这个文件离开入下面这一行
##当然可以用uuid来挂载,这样的话以后增加减少磁盘都不会有问题。
#UUID=7a7502e2-2cb5-4cc6-b594-361c60ae8092 /wwwroot ext4 defaults 0 0
/dev/vdb1 /wwwroot ext4 defaults 0 0
#/dev/vdb1 这个是分区 /wwwroot 这个是要挂载的目录
#最后最重要的一步就是要检查一下有没有问题
运行mount -a
如果没有报错那就一切正常,重启一下服务器。
运行df 查看就能看到咯。
最后总结一下流程
1.先查看磁盘fdisk -l 找到要挂载的磁盘 上例中要分区的磁盘是/dev/vdb
2.给磁盘分区 fdisk /dev/vdb 根据提示进行分区与格式化,分区完后不要忘了写入分区表
3.格式化分区/sbin/mkfs.ext4 /dev/vdb1
4.挂载分区前要先创建一个挂载点(就是一个目录),挂载分区mount /dev/vdb1 /wwwroot
5.设置自动挂载vi /etc/fstab
6.这个最重要一定要先检查是否有错再重启mount -a运行后没有任可信息显示那就表示一切正常了,否则就要根据错误提示解决问题,如果不解决错误就直接重启的话,可能造成开不了机的严重后果,服务器开不了机的后果自己想想.