Linux系统下对硬盘分区进行扩容的方法总结
程序员文章站
2023-12-27 21:42:21
这篇文章主要介绍了Linux系统下对硬盘分区进行扩容的方法总结,这里推荐LVM方式,注意操作时先对数据进行备份以防万一,需要的朋友可以参考下... 15-12-31...
硬盘快满了,加硬盘扩容,不管是独立的服务器,还是云空间新买的硬盘,扩容方式一样。下面以阿里vps为例,详细说一下,挂载硬盘扩容的2种方法。
阿里vps,个人觉得有以下二个方面需要改进
1,默认不是lvm,所以系统盘/dev/xvda1是不能扩容的,所以如果有大数据的东西,一开始就要考虑到扩容的问题。数据量越大,操作风险越大。
2,不能实现无缝扩容,也就是说,在后台扩容一下,不用登录到vps,进行调整。非专业人士,扩容困难
一,不采用lvm,直接将硬盘挂载到目录
1,查看硬盘分区情况
复制代码
代码如下:[root@iz94zz3wqciz ~]# df
filesystem 1k-blocks used available use% mounted on
/dev/xvda1 20641404 14778400 4814480 76% /
tmpfs 509300 0 509300 0% /dev/shm
[root@iz94zz3wqciz ~]# fdisk -l
disk /dev/xvda: 21.5 gb, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00078f9c
device boot start end blocks id system
/dev/xvda1 * 1 2611 20970496 83 linux //id是83,非lvm
disk /dev/xvdb: 23.6 gb, 23622320128 bytes //新买的硬盘
255 heads, 56 sectors/track, 3230 cylinders
units = cylinders of 14280 * 512 = 7311360 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x27cc1f5a
2,硬盘分区并查看分区情况
复制代码
代码如下:[root@iz94zz3wqciz ~]# fdisk -s 56 /dev/xvdb //分区
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): n //输入n
command action
e extended
p primary partition (1-4)
p //主分区
partition number (1-4): 1 //分区号1
first cylinder (1-3230, default 1):
using default value 1
last cylinder, +cylinders or +size{k,m,g} (1-3230, default 3230):
using default value 3230
command (m for help): wq //保存并退出
the partition table has been altered!
[root@iz94zz3wqciz ~]# fdisk -l
disk /dev/xvda: 21.5 gb, 21474836480 bytes
255 heads, 63 sectors/track, 2610 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: 0x00078f9c
device boot start end blocks id system
/dev/xvda1 * 1 2611 20970496 83 linux
disk /dev/xvdb: 23.6 gb, 23622320128 bytes
255 heads, 56 sectors/track, 3230 cylinders
units = cylinders of 14280 * 512 = 7311360 bytes
sector size (logical/physical): 512 bytes / 512 bytes
i/o size (minimum/optimal): 512 bytes / 512 bytes
disk identifier: 0x27cc1f5a
device boot start end blocks id system
/dev/xvdb1 1 3230 23062172 83 linux //分区后
3,格式化新分区xvdb1
复制代码
代码如下:[root@iz94zz3wqciz ~]# mkfs.ext4 /dev/xvdb1 //格式化分区
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
1441792 inodes, 5765543 blocks
288277 blocks (5.00%) reserved for the super user
first data block=0
maximum filesystem blocks=4294967296
176 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
writing inode tables: done
creating journal (32768 blocks): done
writing superblocks and filesystem accounting information: done
this filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. use tune2fs -c or -i to override.
4,创建目录,并挂载分区
复制代码
代码如下:[root@iz94zz3wqciz ~]# mkdir /mnt/fastdfs //挂载目录
[root@iz94zz3wqciz ~]# echo "/dev/xvdb1 /mnt/fastdfs ext4 defaults 0 0" >> /etc/fstab //重启会自动挂载
[root@iz94zz3wqciz ~]# mount -a //挂载所有目录
[root@iz94zz3wqciz ~]# df
filesystem 1k-blocks used available use% mounted on
/dev/xvda1 20641404 14778404 4814476 76% /
tmpfs 509300 0 509300 0% /dev/shm
/dev/xvdb1 22694396 176064 21365516 1% /mnt/fastdfs //新的分区已挂载
到这儿,就把一块独立的硬盘加到系统当中了。这种扩容的方式操作简单,但是扩展性不强,不推荐这种扩容方式。
二,采用lvm的方式,进行硬盘扩容(推荐)
1,取消前面测试的挂载,并删除分区
复制代码
代码如下:[root@iz94zz3wqciz ~]# umount -a //取消挂载
[root@iz94zz3wqciz ~]# fdisk /dev/xvdb //分区
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): d //删除分区
selected partition 1
command (m for help): wq //保存
the partition table has been altered!
2,安装lvm
复制代码
代码如下:[root@iz94zz3wqciz nginx]# uname -a //查看内核信息
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
[root@iz94zz3wqciz ~]# yum install lvm2 device-mapper //安装 lvm2,2.6.9以后版本不用装device-mapper
[root@iz94zz3wqciz nginx]# lsmod | grep dm_mod //是否加载了dm_mod
dm_mod 84337 5 dm_mirror,dm_log
lvm的安装,首先加载device-mapper模块,从linux内核2.6.9开始,device-mapper模块就已经包含在内,所以你只需加载即可。加载mapper模块:modprobe dm_mod。
3,创建lvm分区
复制代码
代码如下:[root@iz94zz3wqciz ~]# fdisk /dev/xvdb //分区
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): l
0 empty 24 nec dos 81 minix / old lin bf solaris
1 fat12 39 plan 9 82 linux swap / so c1 drdos/sec (fat-
2 xenix root 3c partitionmagic 83 linux c4 drdos/sec (fat-
3 xenix usr 40 venix 80286 84 os/2 hidden c: c6 drdos/sec (fat-
4 fat16 <32m 41 ppc prep boot 85 linux extended c7 syrinx
5 extended 42 sfs 86 ntfs volume set da non-fs data
6 fat16 4d qnx4.x 87 ntfs volume set db cp/m / ctos / .
7 hpfs/ntfs 4e qnx4.x 2nd part 88 linux plaintext de dell utility
8 aix 4f qnx4.x 3rd part 8e linux lvm df bootit
9 aix bootable 50 ontrack dm 93 amoeba e1 dos access
a os/2 boot manag 51 ontrack dm6 aux 94 amoeba bbt e3 dos r/o
b w95 fat32 52 cp/m 9f bsd/os e4 speedstor
c w95 fat32 (lba) 53 ontrack dm6 aux a0 ibm thinkpad hi eb beos fs
e w95 fat16 (lba) 54 ontrackdm6 a5 freebsd ee gpt
f w95 ext'd (lba) 55 ez-drive a6 openbsd ef efi (fat-12/16/
10 opus 56 golden bow a7 nextstep f0 linux/pa-risc b
11 hidden fat12 5c priam edisk a8 darwin ufs f1 speedstor
12 compaq diagnost 61 speedstor a9 netbsd f4 speedstor
14 hidden fat16 <3 63 gnu hurd or sys ab darwin boot f2 dos secondary
16 hidden fat16 64 novell netware af hfs / hfs+ fb vmware vmfs
17 hidden hpfs/ntf 65 novell netware b7 bsdi fs fc vmware vmkcore
18 ast smartsleep 70 disksecure mult b8 bsdi swap fd linux raid auto
1b hidden w95 fat3 75 pc/ix bb boot wizard hid fe lanstep
1c hidden w95 fat3 80 old minix be solaris boot ff bbt
1e hidden w95 fat1
command (m for help): n
command action
e extended
p primary partition (1-4)
p
partition number (1-4): 1
first cylinder (1-2871, default 1):
using default value 1
last cylinder, +cylinders or +size{k,m,g} (1-2871, default 2871):
using default value 2871
command (m for help): p
disk /dev/xvdb: 23.6 gb, 23622320128 bytes
255 heads, 63 sectors/track, 2871 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: 0x27cc1f5a
device boot start end blocks id system
/dev/xvdb1 1 2871 23061276 83 linux //不是lvm分区格式
command (m for help): t //转换
selected partition 1
hex code (type l to list codes): 8e //换成8e
changed system type of partition 1 to 8e (linux lvm)
command (m for help): p
disk /dev/xvdb: 23.6 gb, 23622320128 bytes
255 heads, 63 sectors/track, 2871 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: 0x27cc1f5a
device boot start end blocks id system
/dev/xvdb1 1 2871 23061276 8e linux lvm //现在lvm分区格式了
command (m for help): wq
the partition table has been altered!
calling ioctl() to re-read partition table.
syncing disks.
4,创建逻辑卷组,以及逻辑卷等
复制代码
代码如下:[root@iz94zz3wqciz ~]# pvcreate /dev/xvdb1 //创建物理卷
physical volume "/dev/xvdb1" successfully created
[root@iz94zz3wqciz ~]# vgcreate myfiles /dev/xvdb1 //创建逻辑卷组
volume group "myfiles" successfully created
[root@iz94zz3wqciz ~]# vgchange -ay myfiles //激活逻辑卷组
0 logical volume(s) in volume group "myfiles" now active
[root@iz94zz3wqciz ~]# vgdisplay myfiles | grep "total pe" //查看该卷组所有的pe
total pe 5629
[root@iz94zz3wqciz ~]# lvcreate -l 5629 -n fastdfs myfiles //创建逻辑卷
logical volume "fastdfs" created.
5,格式化逻辑卷
复制代码
代码如下:[root@iz94zz3wqciz ~]# mkfs.ext4 /dev/myfiles/fastdfs //格式化逻辑卷
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
1441792 inodes, 5764096 blocks
288204 blocks (5.00%) reserved for the super user
first data block=0
maximum filesystem blocks=4294967296
176 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
writing inode tables: done
creating journal (32768 blocks): done
writing superblocks and filesystem accounting information: done
this filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. use tune2fs -c or -i to override.
6,设置自动挂载,并查看分区
复制代码
代码如下:[root@iz94zz3wqciz ~]# echo '/dev/myfiles/fastdfs /mnt/fastdfs ext4 defaults 0 0' >> /etc/fstab //自动挂载
[root@iz94zz3wqciz ~]# mount -a //手动挂载
[root@iz94zz3wqciz ~]# df
filesystem 1k-blocks used available use% mounted on
/dev/xvda1 20641404 14778608 4814272 76% /
tmpfs 509300 0 509300 0% /dev/shm
/dev/mapper/myfiles-fastdfs
22694396 176064 21365516 1% /mnt/fastdfs //lvm逻辑卷已挂载
[root@iz94zz3wqciz ~]# reboot //操作完最好重启一下
7,再扩容一块硬盘到已有逻辑卷
复制代码
代码如下:[root@iz94zz3wqciz ~]# fdisk /dev/xvdc //详细过程同上,就不详细说明了
[root@iz94zz3wqciz ~]# reboot
[root@iz94zz3wqciz ~]# pvcreate /dev/xvdc1 //创建物理卷
physical volume "/dev/xvdc1" successfully created
[root@iz94zz3wqciz ~]# vgextend myfiles /dev/xvdc1 //将新硬盘加入卷组
volume group "myfiles" successfully extended
[root@iz94zz3wqciz ~]# vgdisplay myfiles | grep "total pe" //查看所有pe
total pe 6907
[root@iz94zz3wqciz ~]# lvresize -l 6907 /dev/myfiles/fastdfs //重新规定大小
size of logical volume myfiles/fastdfs changed from 21.99 gib (5629 extents) to 26.98 gib (6907 extents).
logical volume fastdfs successfully resized
[root@iz94zz3wqciz ~]# resize2fs /dev/myfiles/fastdfs //重新规定大小
resize2fs 1.41.12 (17-may-2010)
filesystem at /dev/myfiles/fastdfs is mounted on /mnt/fastdfs; on-line resizing required
old desc_blocks = 2, new_desc_blocks = 2
performing an on-line resize of /dev/myfiles/fastdfs to 7072768 (4k) blocks.
the filesystem on /dev/myfiles/fastdfs is now 7072768 blocks long.
[root@iz94zz3wqciz ~]# df -h
filesystem size used avail use% mounted on
/dev/xvda1 20g 15g 4.6g 76% /
tmpfs 498m 0 498m 0% /dev/shm
/dev/mapper/myfiles-fastdfs
27g 172m 26g 1% /mnt/fastdfs //新的5g硬盘加上去了
如果是频繁的扩容硬盘的话,lvm是首选,扩展真的很方便。