场景:服务器使用中发现磁盘空间不足,需要挂载一个磁盘以供继续使用,但是新磁盘不是添加就可以使用,需要进行挂载。
1、添加磁盘
添加新磁盘后重启服务器。
2、使用root用户进入系统
3、查看磁盘信息
[root@databack /]# fdisk -l //查看磁盘命令
Disk /dev/vda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x00053156
Device Boot Start End Blocks Id System
/dev/vda1 * 1 5222 41942016 83 Linux
Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes
16 heads, 63 sectors/track, 2080507 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4737cf47
Device Boot Start End Blocks Id System
/dev/vdb1 1 2080507 1048575496+ 83 Linux
Disk /dev/vdc: 322.1 GB, 322122547200 bytes
16 heads, 63 sectors/track, 624152 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000
可以看到这台服务器加载了三个磁盘vda、vdb、vdc
vda是初始磁盘
vdb已经初始化且正在使用
vdc是未格式化的新磁盘
4、创建新硬盘分区
[root@databack /]# fdisk /dev/vdc //进入新磁盘
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x69fdfcdf.
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): m
Command action
a toggle a bootable flag //设定可启动标记
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition //删除一个分区
l list known partition types //各分区类型所对应的ID
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 //打印分区信息,可以看到当前并没有分区
Disk /dev/vdc: 322.1 GB, 322122547200 bytes
16 heads, 63 sectors/track, 624152 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x69fdfcdf
Device Boot Start End Blocks Id System
Command (m for help): n //创建一个新的分区
Command action
e extended //创建扩展分区
p primary partition (1-4) //创建逻辑分区
p
Partition number (1-4): 1 //划分逻辑分区
First cylinder (1-624152, default 1): //直接回车,把整个磁盘作为1个分区
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-624152, default 624152):
Using default value 624152
Command (m for help): p //再次查看可以看到该磁盘已经有1个分区了
Disk /dev/vdc: 322.1 GB, 322122547200 bytes
16 heads, 63 sectors/track, 624152 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x69fdfcdf
Device Boot Start End Blocks Id System
/dev/vdc1 1 624152 314572576+ 83 Linux
Command (m for help): w //保存分区
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
5、分区完成,再次查看磁盘信息
[root@databack /]# fdisk -l
Disk /dev/vda: 42.9 GB, 42949672960 bytes
255 heads, 63 sectors/track, 5221 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: 0x00053156
Device Boot Start End Blocks Id System
/dev/vda1 * 1 5222 41942016 83 Linux
Disk /dev/vdb: 1073.7 GB, 1073741824000 bytes
16 heads, 63 sectors/track, 2080507 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x4737cf47
Device Boot Start End Blocks Id System
/dev/vdb1 1 2080507 1048575496+ 83 Linux
Disk /dev/vdc: 322.1 GB, 322122547200 bytes
16 heads, 63 sectors/track, 624152 cylinders
Units = cylinders of 1008 * 512 = 516096 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x69fdfcdf
Device Boot Start End Blocks Id System
/dev/vdc1 1 624152 314572576+ 83 Linux
6、格式化分区
[root@databack /]# mkfs.ext4 /dev/vdc1
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
19660800 inodes, 78643144 blocks
3932157 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
2400 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, 7962624, 11239424, 20480000, 23887872, 71663616
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 26 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
7、挂载分区以及开机自动挂载
[root@databack /]# df -h //目前只有vda1和vdb1两个磁盘挂载
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 3.8G 34G 11% /
tmpfs 498M 8.0K 498M 1% /dev/shm
/dev/vdb1 985G 93G 842G 10% /home
[root@databack /]# mkdir /data
[root@databack /]# mount /dev/vdc1 /data //挂载
[root@databack /]# df -h //再次查看
Filesystem Size Used Avail Use% Mounted on
/dev/vda1 40G 3.8G 34G 11% /
tmpfs 498M 8.0K 498M 1% /dev/shm
/dev/vdb1 985G 93G 842G 10% /home
/dev/vdc1 296G 63M 281G 1% /data
磁盘已经挂载完成
8、设置开机自动挂载
1、执行以下命令,将根分区的挂载模式变更为读写。
[root@databack /]# mount / -o remount,rw
2、执行blkid命令,查看ECS实例上磁盘的分区情况以及文件系统。
[root@databack /]# blkid
/dev/vda1: UUID="3d083579-f5d9-4df5-9347-8d27925805d4" TYPE="ext4"
/dev/vdb1: UUID="3c40f1da-7711-4d24-99a3-0c32fd902685" TYPE="ext4"
/dev/vdc1: UUID="c4207d39-d1cf-4473-9ce7-50dd7c5fa0aa" TYPE="ext4"
3、执行以下命令,检查磁盘的分区信息与文件系统和上一步返回的结果是否一致。
[root@databack /]# vim /etc/fstab
/dev/vdc1/data ext4 defaults,nofail 0 0
注意:建议您在挂载的时候添加nofail参数,在启动实例时,若设备不存在会直接忽略它,从而不发生报错。
4、执行reboot命令,重启系统,确认能成功登录系统。
[root@databack /]# reboot