使用虚拟机时发现磁盘空间不够了,需要挂载一个磁盘以供继续使用,但是磁盘不是添加就可以使用的,还需要进行挂载。以下为Linux下挂载磁盘的操作~

一、添加磁盘

添加新硬盘重启服务器

添加完之后就可以重启机器了,如果你机器是开启的,进入系统并不能看见你刚添加的那块磁盘,只有等系统重启,重新加载之后才会显示安装的那块磁盘

二、进入系统

使用root用户进入系统

三、 查看硬盘信息

[root@localhost ~]# fdisk -l //磁盘命令

  1. Disk /dev/sda: 21.5 GB, 21474836480 bytes
  2. 255 heads, 63 sectors/track, 2610 cylinders
  3. Units = cylinders of 16065 * 512 = 8225280 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk identifier: 0x000c4cb5
  7. Device Boot Start End Blocks Id System
  8. /dev/sda1 * 1 64 512000 83 Linux
  9. Partition 1 does not end on cylinder boundary.
  10. /dev/sda2 64 2611 20458496 8e Linux LVM
  11. Disk /dev/sdb: 21.5 GB, 21474836480 bytes
  12. 255 heads, 63 sectors/track, 2610 cylinders
  13. Units = cylinders of 16065 * 512 = 8225280 bytes
  14. Sector size (logical/physical): 512 bytes / 512 bytes
  15. I/O size (minimum/optimal): 512 bytes / 512 bytes
  16. Disk identifier: 0xd0f5c869
  17. Device Boot Start End Blocks Id System
  18. /dev/sdb1 1 2610 20964793+ 83 Linux
  19. Disk /dev/sdc: 21.5 GB, 21474836480 bytes
  20. 255 heads, 63 sectors/track, 2610 cylinders
  21. Units = cylinders of 16065 * 512 = 8225280 bytes
  22. Sector size (logical/physical): 512 bytes / 512 bytes
  23. I/O size (minimum/optimal): 512 bytes / 512 bytes
  24. Disk identifier: 0x00000000

可以看到这台机器加载了三个磁盘sda、sdb、sdc

其中sda是初始磁盘,sdb已经初始化且经过使用,sdc是刚刚加载的,未格式化的新磁盘

四、创建新硬盘分区

  1. [root@localhost ~]# fdisk /dev/sdc #进入磁盘
  2. Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
  3. Building a new DOS disklabel with disk identifier 0x45a3cadb.
  4. Changes will remain in memory only, until you decide to write them.
  5. After that, of course, the previous content won't be recoverable.
  6. Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
  7. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
  8. switch off the mode (command 'c') and change display units to
  9. sectors (command 'u').
  10. Command (m for help): m
  11. Command action
  12. a toggle a bootable flag #设定可启动标记
  13. b edit bsd disklabel
  14. c toggle the dos compatibility flag
  15. d delete a partition #删除一个分区
  16. l list known partition types #各分区类型所对应的ID
  17. m print this menu #菜单
  18. n add a new partition #添加一个分区
  19. o create a new empty DOS partition table
  20. p print the partition table #显示该磁盘下的当前分区信息
  21. q quit without saving changes #不保存退出
  22. s create a new empty Sun disklabel
  23. t change a partition's system id
  24. u change display/entry units
  25. v verify the partition table
  26. w write table to disk and exit #保存退出
  27. x extra functionality (experts only)

知道命令之后就可以进行分区了

  1. Command (m for help): p //打印分区信息,可以看到当前并没有分区
  2. Disk /dev/sdc: 21.5 GB, 21474836480 bytes
  3. 255 heads, 63 sectors/track, 2610 cylinders
  4. Units = cylinders of 16065 * 512 = 8225280 bytes
  5. Sector size (logical/physical): 512 bytes / 512 bytes
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes
  7. Disk identifier: 0x45a3cadb
  8. Device Boot Start End Blocks Id System
  9. Command (m for help): n //创建一个新的分区
  10. Command action
  11. e extended//输入e为创建扩展分区
  12. p primary partition (1-4) //输入p为创建逻辑分区
  13. p
  14. Partition number (1-4): 1//划分逻辑分区
  15. First cylinder (1-2610, default 1): //我这里直接回车,是不想把该磁盘分成多个分区,把整个磁盘作为1个分区
  16. Using default value 1
  17. Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610):
  18. Using default value 2610
  19. Command (m for help): p //再次查看可以看到该磁盘已经有1个分区了
  20. Disk /dev/sdc: 21.5 GB, 21474836480 bytes
  21. 255 heads, 63 sectors/track, 2610 cylinders
  22. Units = cylinders of 16065 * 512 = 8225280 bytes
  23. Sector size (logical/physical): 512 bytes / 512 bytes
  24. I/O size (minimum/optimal): 512 bytes / 512 bytes
  25. Disk identifier: 0x45a3cadb
  26. Device Boot Start End Blocks Id System
  27. /dev/sdc1 1 2610 20964793+ 83 Linux
  28. Command (m for help): w //保存分区
  29. The partition table has been altered!
  30. Calling ioctl() to re-read partition table.
  31. Syncing disks.

再次使用”fdisk -l”命令查看磁盘信息

  1. Disk /dev/sdc: 21.5 GB, 21474836480 bytes
  2. 255 heads, 63 sectors/track, 2610 cylinders
  3. Units = cylinders of 16065 * 512 = 8225280 bytes
  4. Sector size (logical/physical): 512 bytes / 512 bytes
  5. I/O size (minimum/optimal): 512 bytes / 512 bytes
  6. Disk identifier: 0x406a4c58
  7. Device Boot Start End Blocks Id System
  8. /dev/sdc1 1 2610 20964793+ 83 Linux

第三块磁盘/dev/sdc已经分区好了

五、格式化分区

  1. [root@localhost ~]# mkfs.ext3 /dev/sdc1
  2. //将/dev/sdc1格式化为ext3类型,好像大部分的磁盘都是格式化为ext3类型,具体为什么没有深入研究,暂时不清楚,想了解的朋友可以自己查一下
  3. [root@localhost ~]# mkfs.ext4 /dev/sdc1
  4. mke2fs 1.41.12 (17-May-2010)
  5. 文件系统标签=
  6. 操作系统:Linux
  7. 块大小=4096 (log=2)
  8. 分块大小=4096 (log=2)
  9. Stride=0 blocks, Stripe width=0 blocks
  10. 1310720 inodes, 5241198 blocks
  11. 262059 blocks (5.00%) reserved for the super user
  12. 第一个数据块=0
  13. Maximum filesystem blocks=4294967296
  14. 160 block groups
  15. 32768 blocks per group, 32768 fragments per group
  16. 8192 inodes per group
  17. Superblock backups stored on blocks:
  18. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  19. 4096000
  20. 正在写入inode表: 完成
  21. Creating journal (32768 blocks): 完成
  22. Writing superblocks and filesystem accounting information: 完成
  23. This filesystem will be automatically checked every 31 mounts or
  24. 180 days, whichever comes first. Use tune2fs -c or -i to override.

格式化完毕,此时就可以使用“mount”命令挂载分区了,然后使用这个磁盘空间了
Linux磁盘挂载 - 图1
格式化报错,说明需要分区为逻辑分区 创建分区选择 p

六、挂载分区以及开机自动挂载

  1. [root@localhost ~]# df -h //此时只有sda1和sdb1两个磁盘挂载
  2. Filesystem Size Used Avail Use% Mounted on
  3. /dev/mapper/VolGroup-lv_root 18G 15G 1.5G 92% /
  4. tmpfs 932M 76K 932M 1% /dev/shm
  5. /dev/sda1 485M 40M 421M 9% /boot
  6. /dev/sdb1 20G 1.2G 18G 7% /disk/diskone
  7. /dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
  8. [root@localhost /]# cd /disk/
  9. [root@localhost disk]# ll
  10. 总用量 4
  11. drwxr-xr-x. 4 root root 4096 7 28 17:04 diskone
  12. [root@localhost disk]# cd diskone/
  13. [root@localhost diskone]# ll
  14. 总用量 20
  15. drwx------. 2 root root 16384 7 28 16:12 lost+found
  16. drwxr-xr-x. 2 root root 4096 7 28 17:09 software
  17. [root@localhost diskone]# cd ../
  18. [root@localhost disk]# mkdir disktwo //创建被挂载的路径
  19. [root@localhost disk]# ll
  20. 总用量 8
  21. drwxr-xr-x. 4 root root 4096 7 28 17:04 diskone
  22. drwxr-xr-x. 2 root root 4096 8 1 16:48 disktwo
  23. [root@localhost disk]# mount /dev/sdc1 /disk/disktwo/ //进行挂载
  24. [root@localhost disk]# df -h //再次查看,发现sdc1已经挂载在/disk/disktwo路径下
  25. Filesystem Size Used Avail Use% Mounted on
  26. /dev/mapper/VolGroup-lv_root 18G 15G 1.5G 92% /
  27. tmpfs 932M 76K 932M 1% /dev/shm
  28. /dev/sda1 485M 40M 421M 9% /boot
  29. /dev/sdb1 20G 1.2G 18G 7% /disk/diskone
  30. /dev/sr0 4.2G 4.2G 0 100% /media/CentOS_6.5_Final
  31. /dev/sdc1 20G 173M 19G 1% /disk/disktwo

磁盘已经挂载上了

下面设置开机自动挂载

  1. 编辑/etc/fstab文件,再最后面添加
  2. /dev/vdb1 /data ext4 defaults 0 0
  3. vim /etc/fstab
  4. /dev/sdc1(磁盘分区) /disk/disktwo(挂载目录) ext3(文件格式)defaults 0 0

之后重启就OK啦!!!