怎样向linux服务器上添加磁盘?,linux添加


记录下怎样向linux服务器上添加磁盘,大致的步骤如下:

查看设备信息 格式化 创建文件系统 创建挂载点 挂载文件系统 设置开机挂载

查看设备信息

$ sudo fdisk -l

使用上面的命令查看设备信息,会发现有一块磁盘,大概输出如下

Disk /dev/vdc: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0xd975b614

发现了一块50G的磁盘,路径是/dev/vdc

格式化磁盘

$ sudo fdisk /dev/vdc
Welcome to fdisk (util-linux 2.27.1).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): 

输入m来查看帮助

Help:

  DOS (MBR)
   a   toggle a bootable flag
   b   edit nested BSD disklabel
   c   toggle the dos compatibility flag

  Generic
   d   delete a partition
   F   list free unpartitioned space
   l   list known partition types
   n   add a new partition
   p   print the partition table
   t   change a partition type
   v   verify the partition table
   i   print information about a partition

  Misc
   m   print this menu
   u   change display/entry units
   x   extra functionality (experts only)

  Script
   I   load disk layout from sfdisk script file
   O   dump disk layout to sfdisk script file

  Save & Exit
   w   write table to disk and exit
   q   quit without saving changes

  Create a new label
   g   create a new empty GPT partition table
   G   create a new empty SGI (IRIX) partition table
   o   create a new empty DOS partition table
   s   create a new empty Sun partition table

我们需要输入n来新建一个partition

Command (m for help): n
Partition type
   p   primary (0 primary, 0 extended, 4 free)
   e   extended (container for logical partitions)
Select (default p): 

Using default response p.
Partition number (1-4, default 1): 
First sector (2048-104857599, default 2048): 
Last sector, +sectors or +size{K,M,G,T,P} (2048-104857599, default 104857599): 

Created a new partition 1 of type 'Linux' and of size 50 GiB.

Command (m for help): 

输入n以后,一路回车保持默认(当然,你也可以按照提示输入别的参数),最终又到了让你输入命令的地方了,此时输入w来写入信息并退出

Command (m for help): w
The partition table has been altered.
Calling ioctl() to re-read partition table.
Syncing disks.

此时磁盘已经格式化好了,并创建了一个分区。再次运行fdisk -l命令来查看一下

$ sudo fdisk -l

下面是一部分输出

Disk /dev/vdc: 50 GiB, 53687091200 bytes, 104857600 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
Disklabel type: dos
Disk identifier: 0xd975b614

Device     Boot Start       End   Sectors Size Id Type
/dev/vdc1        2048 104857599 104855552  50G 83 Linux

会发现多了一个分区,名字叫/dev/vdc1

创建文件系统

使用下面的命令来为上面的分区创建为ext4格式的文件系统

$ sudo mkfs.ext4 /dev/vdc1 
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

在根目录下创建挂载点

$ sudo mkdir /data

上面的命令创建了一个/data文件夹,你也可以使用别的名字。

挂载文件系统

sudo mount /dev/vdc1 /data

如果没有报错的话,就挂载成功了。使用下面的命令来查看一下磁盘信息

$ df -h
Filesystem      Size  Used Avail Use% Mounted on
udev            3.9G     0  3.9G   0% /dev
tmpfs           799M  9.2M  790M   2% /run
/dev/vda1        99G  2.1G   92G   3% /
tmpfs           3.9G     0  3.9G   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           3.9G     0  3.9G   0% /sys/fs/cgroup
tmpfs           799M     0  799M   0% /run/user/1000
/dev/vdc1        50G   52M   47G   1% /data

最下面的那一条记录显示出我们的挂载已经生效了。

设置开机挂载

到此为止,还没有结束。如果此时重启linux系统的话,原来挂载的磁盘是显示不出来的,需要重新挂载一下。为了避免这个问题,需要将必要的信息写入/etc/fstab文件中,这样的话,在系统启动时会自动挂载。

先查看一下/etc/fstab文件的内容,会发现有一条根目录/的挂载信息

UUID=5c51f0c7-6ad1-41e5-8026-a75466a07617 /               ext4    errors=remount-ro 0       1

这一行数据一共分为6列,分别是:

设备 挂载点 文件系统类型 文件系统参数 是否被dump备份命令作用 是否以fsck检查分区

就本例而言,我要先查看设备分区的UUID,可以使用blkid命令来查看

$ blkid
/dev/vdc1: UUID="32a10a4c-25d7-47e8-b60e-77183bc557df" TYPE="ext4" PARTUUID="d975b614-01"

上面显示出了/dev/vdc1的UUID核文件系统类型,接下来我将向/etc/fstab文件写入下面一行数据

UUID=32a10a4c-25d7-47e8-b60e-77183bc557df /data           ext4    defaults          0       0

UUID是刚才查出来的UUID,挂载点是刚才设置的挂载点(/data),文件类型是ext4。

注意:这条记录要写在根目录挂载点的下面

到这里就大功告成了。你可以重启一下linux,并使用df -h命令查看一下磁盘使用情况,会发现刚才挂载的硬盘还在。

相关内容