找回密码

linux 挂载硬盘扩容的2种方法

硬盘快满了,加硬盘扩容,不管是独立的服务器,还是云空间新买的硬盘,扩容方式一样。下面以阿里VPS为例,详细说一下,挂载硬盘扩容的2种方法。

阿里VPS,个人觉得有以下二个方面需要改进

1,默认不是lvm,所以系统盘/dev/xvda1是不能扩容的,所以如果有大数据的东西,一开始就要考虑到扩容的问题。数据量越大,操作风险越大。

2,不能实现无缝扩容,也就是说,在后台扩容一下,不用登录到VPS,进行调整。非专业人士,扩容困难

一,不采用lvm,直接将硬盘挂载到目录

1,查看硬盘分区情况

  1. [[email protected] ~]# df
  2. Filesystem 1K-blocks Used Available Use% Mounted on
  3. /dev/xvda1 20641404 14778400 4814480 76% /
  4. tmpfs 509300 0 509300 0% /dev/shm
  5. [[email protected] ~]# fdisk -l
  6. Disk /dev/xvda: 21.5 GB, 21474836480 bytes
  7. 255 heads, 63 sectors/track, 2610 cylinders
  8. Units = cylinders of 16065 * 512 = 8225280 bytes
  9. Sector size (logical/physical): 512 bytes / 512 bytes
  10. I/O size (minimum/optimal): 512 bytes / 512 bytes
  11. Disk identifier: 0x00078f9c
  12. Device Boot Start End Blocks Id System
  13. /dev/xvda1 * 1 2611 20970496 83 Linux //id是83,非lvm
  14. Disk /dev/xvdb: 23.6 GB, 23622320128 bytes //新买的硬盘
  15. 255 heads, 56 sectors/track, 3230 cylinders
  16. Units = cylinders of 14280 * 512 = 7311360 bytes
  17. Sector size (logical/physical): 512 bytes / 512 bytes
  18. I/O size (minimum/optimal): 512 bytes / 512 bytes
  19. Disk identifier: 0x27cc1f5a

2,硬盘分区并查看分区情况

  1. [[email protected] ~]# fdisk -S 56 /dev/xvdb //分区
  2. WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
  3. switch off the mode (command ‘c’) and change display units to
  4. sectors (command ‘u’).
  5. Command (m for help): n //输入n
  6. Command action
  7. e extended
  8. p primary partition (1-4)
  9. p //主分区
  10. Partition number (1-4): 1 //分区号1
  11. First cylinder (1-3230, default 1):
  12. Using default value 1
  13. Last cylinder, +cylinders or +size{K,M,G} (1-3230, default 3230):
  14. Using default value 3230
  15. Command (m for help): wq //保存并退出
  16. The partition table has been altered!
  17. [[email protected] ~]# fdisk -l
  18. Disk /dev/xvda: 21.5 GB, 21474836480 bytes
  19. 255 heads, 63 sectors/track, 2610 cylinders
  20. Units = cylinders of 16065 * 512 = 8225280 bytes
  21. Sector size (logical/physical): 512 bytes / 512 bytes
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes
  23. Disk identifier: 0x00078f9c
  24. Device Boot Start End Blocks Id System
  25. /dev/xvda1 * 1 2611 20970496 83 Linux
  26. Disk /dev/xvdb: 23.6 GB, 23622320128 bytes
  27. 255 heads, 56 sectors/track, 3230 cylinders
  28. Units = cylinders of 14280 * 512 = 7311360 bytes
  29. Sector size (logical/physical): 512 bytes / 512 bytes
  30. I/O size (minimum/optimal): 512 bytes / 512 bytes
  31. Disk identifier: 0x27cc1f5a
  32. Device Boot Start End Blocks Id System
  33. /dev/xvdb1 1 3230 23062172 83 Linux //分区后

3,格式化新分区xvdb1

  1. [[email protected] ~]# mkfs.ext4 /dev/xvdb1 //格式化分区
  2. mke2fs 1.41.12 (17-May-2010)
  3. Filesystem label=
  4. OS type: Linux
  5. Block size=4096 (log=2)
  6. Fragment size=4096 (log=2)
  7. Stride=0 blocks, Stripe width=0 blocks
  8. 1441792 inodes, 5765543 blocks
  9. 288277 blocks (5.00%) reserved for the super user
  10. First data block=0
  11. Maximum filesystem blocks=4294967296
  12. 176 block groups
  13. 32768 blocks per group, 32768 fragments per group
  14. 8192 inodes per group
  15. Superblock backups stored on blocks:
  16. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  17. 4096000
  18. Writing inode tables: done
  19. Creating journal (32768 blocks): done
  20. Writing superblocks and filesystem accounting information: done
  21. This filesystem will be automatically checked every 35 mounts or
  22. 180 days, whichever comes first. Use tune2fs -c or -i to override.

4,创建目录,并挂载分区

  1. [[email protected] ~]# mkdir /mnt/fastdfs //挂载目录
  2. [[email protected] ~]# echo “/dev/xvdb1 /mnt/fastdfs ext4 defaults 0 0” >> /etc/fstab //重启会自动挂载
  3. [[email protected] ~]# mount -a //挂载所有目录
  4. [[email protected] ~]# df
  5. Filesystem 1K-blocks Used Available Use% Mounted on
  6. /dev/xvda1 20641404 14778404 4814476 76% /
  7. tmpfs 509300 0 509300 0% /dev/shm
  8. /dev/xvdb1 22694396 176064 21365516 1% /mnt/fastdfs //新的分区已挂载

到这儿,就把一块独立的硬盘加到系统当中了。这种扩容的方式操作简单,但是扩展性不强,不推荐这种扩容方式。

二,采用lvm的方式,进行硬盘扩容(推荐)

1,取消前面测试的挂载,并删除分区

  1. [[email protected] ~]# umount -a //取消挂载
  2. [[email protected] ~]# fdisk /dev/xvdb //分区
  3. WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
  4. switch off the mode (command ‘c’) and change display units to
  5. sectors (command ‘u’).
  6. Command (m for help): d //删除分区
  7. Selected partition 1
  8. Command (m for help): wq //保存
  9. The partition table has been altered!

2,安装lvm

  1. [[email protected] nginx]# uname -a //查看内核信息
  2. Linux iZ94zz3wqciZ 2.6.32-431.23.3.el6.x86_64 #1 SMP Thu Jul 31 17:20:51 UTC 2014 x86_64 x86_64 x86_64 GNU/Linux
  3. [[email protected] ~]# yum install lvm2 device-mapper //安装 LVM2,2.6.9以后版本不用装device-mapper
  4. [[email protected] nginx]# lsmod | grep dm_mod //是否加载了dm_mod
  5. dm_mod 84337 5 dm_mirror,dm_log

lvm的安装,首先加载device-mapper模块,从linux内核2.6.9开始,device-mapper模块就已经包含在内,所以你只需加载即可。加载mapper模块:modprobe dm_mod。

3,创建lvm分区

  1. [[email protected] ~]# fdisk /dev/xvdb //分区
  2. WARNING: DOS-compatible mode is deprecated. It’s strongly recommended to
  3. switch off the mode (command ‘c’) and change display units to
  4. sectors (command ‘u’).
  5. Command (m for help): l
  6. 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
  7. 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-
  8. 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT-
  9. 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
  10. 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx
  11. 5 Extended 42 SFS 86 NTFS volume set da Non-FS data
  12. 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / .
  13. 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility
  14. 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt
  15. 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access
  16. a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O
  17. b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor
  18. c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs
  19. e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT
  20. f W95 Ext’d (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/
  21. 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b
  22. 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor
  23. 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor
  24. 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary
  25. 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS
  26. 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE
  27. 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto
  28. 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep
  29. 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT
  30. 1e Hidden W95 FAT1
  31. Command (m for help): n
  32. Command action
  33. e extended
  34. p primary partition (1-4)
  35. p
  36. Partition number (1-4): 1
  37. First cylinder (1-2871, default 1):
  38. Using default value 1
  39. Last cylinder, +cylinders or +size{K,M,G} (1-2871, default 2871):
  40. Using default value 2871
  41. Command (m for help): p
  42. Disk /dev/xvdb: 23.6 GB, 23622320128 bytes
  43. 255 heads, 63 sectors/track, 2871 cylinders
  44. Units = cylinders of 16065 * 512 = 8225280 bytes
  45. Sector size (logical/physical): 512 bytes / 512 bytes
  46. I/O size (minimum/optimal): 512 bytes / 512 bytes
  47. Disk identifier: 0x27cc1f5a
  48. Device Boot Start End Blocks Id System
  49. /dev/xvdb1 1 2871 23061276 83 Linux //不是lvm分区格式
  50. Command (m for help): t //转换
  51. Selected partition 1
  52. Hex code (type L to list codes): 8e //换成8e
  53. Changed system type of partition 1 to 8e (Linux LVM)
  54. Command (m for help): p
  55. Disk /dev/xvdb: 23.6 GB, 23622320128 bytes
  56. 255 heads, 63 sectors/track, 2871 cylinders
  57. Units = cylinders of 16065 * 512 = 8225280 bytes
  58. Sector size (logical/physical): 512 bytes / 512 bytes
  59. I/O size (minimum/optimal): 512 bytes / 512 bytes
  60. Disk identifier: 0x27cc1f5a
  61. Device Boot Start End Blocks Id System
  62. /dev/xvdb1 1 2871 23061276 8e Linux LVM //现在lvm分区格式了
  63. Command (m for help): wq
  64. The partition table has been altered!
  65. Calling ioctl() to re-read partition table.
  66. Syncing disks.

4,创建逻辑卷组,以及逻辑卷等

  1. [[email protected] ~]# pvcreate /dev/xvdb1 //创建物理卷
  2. Physical volume “/dev/xvdb1” successfully created
  3. [[email protected] ~]# vgcreate myfiles /dev/xvdb1 //创建逻辑卷组
  4. Volume group “myfiles” successfully created
  5. [[email protected] ~]# vgchange -ay myfiles //激活逻辑卷组
  6. 0 logical volume(s) in volume group “myfiles” now active
  7. [[email protected] ~]# vgdisplay myfiles | grep “Total PE” //查看该卷组所有的PE
  8. Total PE 5629
  9. [[email protected] ~]# lvcreate -l 5629 -n fastdfs myfiles //创建逻辑卷
  10. Logical volume “fastdfs” created.

5,格式化逻辑卷

  1. [[email protected] ~]# mkfs.ext4 /dev/myfiles/fastdfs //格式化逻辑卷
  2. mke2fs 1.41.12 (17-May-2010)
  3. Filesystem label=
  4. OS type: Linux
  5. Block size=4096 (log=2)
  6. Fragment size=4096 (log=2)
  7. Stride=0 blocks, Stripe width=0 blocks
  8. 1441792 inodes, 5764096 blocks
  9. 288204 blocks (5.00%) reserved for the super user
  10. First data block=0
  11. Maximum filesystem blocks=4294967296
  12. 176 block groups
  13. 32768 blocks per group, 32768 fragments per group
  14. 8192 inodes per group
  15. Superblock backups stored on blocks:
  16. 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
  17. 4096000
  18. Writing inode tables: done
  19. Creating journal (32768 blocks): done
  20. Writing superblocks and filesystem accounting information: done
  21. This filesystem will be automatically checked every 39 mounts or
  22. 180 days, whichever comes first. Use tune2fs -c or -i to override.

6,设置自动挂载,并查看分区

  1. [[email protected] ~]# echo ‘/dev/myfiles/fastdfs /mnt/fastdfs ext4 defaults 0 0’ >> /etc/fstab //自动挂载
  2. [[email protected] ~]# mount -a //手动挂载
  3. [[email protected] ~]# df
  4. Filesystem 1K-blocks Used Available Use% Mounted on
  5. /dev/xvda1 20641404 14778608 4814272 76% /
  6. tmpfs 509300 0 509300 0% /dev/shm
  7. /dev/mapper/myfiles-fastdfs
  8. 22694396 176064 21365516 1% /mnt/fastdfs //lvm逻辑卷已挂载
  9. [[email protected] ~]# reboot //操作完最好重启一下

7,再扩容一块硬盘到已有逻辑卷

  1. [[email protected] ~]# fdisk /dev/xvdc //详细过程同上,就不详细说明了
  2. [[email protected] ~]# reboot
  3. [[email protected] ~]# pvcreate /dev/xvdc1 //创建物理卷
  4. Physical volume “/dev/xvdc1” successfully created
  5. [[email protected] ~]# vgextend myfiles /dev/xvdc1 //将新硬盘加入卷组
  6. Volume group “myfiles” successfully extended
  7. [[email protected] ~]# vgdisplay myfiles | grep “Total PE” //查看所有PE
  8. Total PE 6907
  9. [[email protected] ~]# lvresize -l 6907 /dev/myfiles/fastdfs //重新规定大小
  10. Size of logical volume myfiles/fastdfs changed from 21.99 GiB (5629 extents) to 26.98 GiB (6907 extents).
  11. Logical volume fastdfs successfully resized
  12. [[email protected] ~]# resize2fs /dev/myfiles/fastdfs //重新规定大小
  13. resize2fs 1.41.12 (17-May-2010)
  14. Filesystem at /dev/myfiles/fastdfs is mounted on /mnt/fastdfs; on-line resizing required
  15. old desc_blocks = 2, new_desc_blocks = 2
  16. Performing an on-line resize of /dev/myfiles/fastdfs to 7072768 (4k) blocks.
  17. The filesystem on /dev/myfiles/fastdfs is now 7072768 blocks long.
  18. [[email protected] ~]# df -h
  19. Filesystem Size Used Avail Use% Mounted on
  20. /dev/xvda1 20G 15G 4.6G 76% /
  21. tmpfs 498M 0 498M 0% /dev/shm
  22. /dev/mapper/myfiles-fastdfs
  23. 27G 172M 26G 1% /mnt/fastdfs //新的5G硬盘加上去了

如果是频繁的扩容硬盘的话,LVM是首选,扩展真的很方便。

相关推荐