Linux磁盘扩容三种方式

 

Hlroliu

于 2020-11-18 10:43:14 发布

7708
收藏 78
分类专栏: Linux磁盘扩容 磁盘扩容 文章标签: linux
版权

Linux磁盘扩容
同时被 2 个专栏收录
1 篇文章0 订阅
订阅专栏

磁盘扩容
1 篇文章0 订阅
订阅专栏
Linux在使用过程中由于数据量不断增大,导致磁盘空间不足,需要增加磁盘空间,主要有以下三种方式
1、直接给 / 分区(或者某一分区)扩容,直接在原有磁盘上增大空间
2、给虚拟机新增一块磁盘,为这块磁盘新建一个分区
3、给虚拟机新增一块磁盘,并把磁盘空间扩容到原有分区

1、给 / 分区扩容
查看磁盘空间大小,目前 / 分区大小为50G,并且只有一块磁盘

给/dev/vda 增加50G的空间,并把这50G扩容到 / 分区

/dev/vda 目前为100G,接下来对磁盘进行分区,创建物理卷,把新增的物理卷加到卷组里

对磁盘分区,使用fdisk /dev/vda ,输入n 新建分区,然后一直回车,最后输入 w 保存配置;创建完成之后,可输入 p 查看;/dev/vda3 即为新建的分区

输入partprobe 让系统识别新增的分区,然后创建物理卷,使用pvcreate /dev/vda3
扩展卷组,使用vgextend centos /dev/vda3命令,卷组信息可通过 vgdisplay 查看

扩展 / 的大小,增加50G,使用lvresize -L +30G /dev/mapper/centos-root命令,其中

重新识别 / 分区的大小,而ext4格式磁盘使用resize2fs /dev/mapper/centos-root

2、新增一块磁盘并划分独立分区,新增一块100G的磁盘,划分为/data

对磁盘 /dev/vdb 进行分区,格式化,挂载
磁盘分区

格式化

创建挂载点 /data,挂载分区

实现开机自动挂载(编辑 /etc/fstab 文件),保存退出后,执行 mount -a 无报错则配置成功

3、新增一块磁盘,并扩容到原有分区
新增一块磁盘 /dev/vdc 大小为50G,并把空间扩容至 / 分区

对 /dev/vdc 进行分区,并设置分区属性
t: 修改分区文件系统id,选择8e,LVM(与原有分区一致)
L:列出所有Hex代码

输入partprobe 让系统识别新增的分区,然后创建物理卷,使用pvcreate /dev/vdc1;pvdisplay查看

扩展卷组,扩展分区

查看磁盘大小

 

————————————————
版权声明:本文为CSDN博主「Hlroliu」的原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/Hlroliu/article/details/109764269

Linux下使用fdisk扩大分区容量(不丢失数据)

原创

sandshell博主文章分类:Linux©著作权

文章标签Linux使用fdisk扩大文章分类Linux系统/运维阅读数4.4万

环境:

模拟新加一块磁盘,并分区,挂载,永久挂载,然后写入数据,然后对分区扩容,测试扩容后分区里面的内容是否丢失。

查看所有分区:

[root@backup ~]# fdisk -l
Disk /dev/sda: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0x00040b96
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1070     8388608   82  Linux swap / Solaris
/dev/sda3            1070       13055    96263168   83  Linux
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0x00000000
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.

开始分区:

[root@backup ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xc6e44e94.
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): p            #p是打印当前磁盘所有分区

Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0xc6e44e94

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n            #n是新建分区
Command action
   e   extended
   p   primary partition (1-4)
p                                  #新建主分区
Partition number (1-4): 1          #给主分区编号为1,从1开始编号
First cylinder (1-130541, default 1):     #回车,默认从1开始,也可以手动输入1
Using default value 1                        #起始分区的位置
Last cylinder, +cylinders or +size{K,M,G} (1-130541, default 130541):     #直接回车,默认把磁盘所有空间分配到此分区。这里支持K,M,G,如果想给此分区分配100M空间,就输入+100M;如果要分配100G那么就输入+100G
Using default value 130541                    #结束分区的位置

Command (m for help): w                #w为写入分区信息并保存,然后退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.
  • 39.

查看刚刚分区的信息:/dev/sdb1

[root@backup ~]# fdisk -l
Disk /dev/sda: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0x00040b96
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          26      204800   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              26        1070     8388608   82  Linux swap / Solaris
/dev/sda3            1070       13055    96263168   83  Linux
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.

格式化分区:mkfs.ext4 /dev/sdb1

[root@backup ~]# mkfs.ext4 /dev/sdb1
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
65536000 inodes, 262142637 blocks
13107131 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=4294967296
8000 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, 78675968, 
        102400000, 214990848
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.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.

挂载分区:

[root@backup backup]# mkdir /backup
[root@backup backup]# mount /dev/sdb1 /backup/
[root@backup backup]# df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda3      ext4    91G  2.2G   84G   3% /
tmpfs          tmpfs  1.9G     0  1.9G   0% /dev/shm
/dev/sda1      ext4   190M   38M  142M  21% /boot
/dev/sdb1      ext4   985G   72M  935G   1% /backup
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.

挂载磁盘:(永久挂载)

通过vi编辑器编辑/etc/fstab文件,内容如下,(最后一行是刚添加上去的):

 

[root@backup ~]# cat /etc/fstab 
#
# /etc/fstab
# Created by anaconda on Mon May 14 02:01:11 2018
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=7d223255-1b77-4f58-bd72-262928547fcf /                       ext4    defaults        1 1
UUID=284d6b31-8723-497b-bc7b-3c12ede23ee6 /boot                   ext4    defaults        1 2
UUID=5d36c1b3-df6e-480d-80ca-72befc66f5e9 swap                    swap    defaults        0 0
tmpfs                   /dev/shm                tmpfs   defaults        0 0
devpts                  /dev/pts                devpts  gid=5,mode=620  0 0
sysfs                   /sys                    sysfs   defaults        0 0
proc                    /proc                   proc    defaults        0 0
/dev/sdb1                   /backup                 ext4    defaults        0 0
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.

 

第四列挂载参数:通过查看man mount 来查看

第五列是否要备份:(0为不备份,1为要备份,一般情况下不用做备份)

第六列自检程序  (0为不自检,1或2为要自检,如果是根分区要设置1,其它分区只能是2)

[root@backup ~]# echo backup >/backup/backup.txt
[root@backup ~]# cat /backup/backup.txt 
backup
  • 1.
  • 2.
  • 3.

此时机器不关机,用vmware vspere client扩容磁盘大小。

[root@backup ~]# fdisk -l /dev/sdb
Disk /dev/sdb: 1073.7 GB, 1073741824000 bytes
255 heads, 63 sectors/track, 130541 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: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.

此时发现扩容的磁盘容量看不到,所以还是重启一下吧,建议关机时候再扩容。

[root@backup ~]# umount /dev/sdb1    #取消挂载
[root@backup ~]# fdisk /dev/sdb    
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): p
Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes
255 heads, 63 sectors/track, 143595 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: 0xc6e44e94
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1      130541  1048570551   83  Linux
Command (m for help): d        #因为此磁盘只有一个分区sdb1,所以按d删除时候默认不会让选择要删除的分区,如果有多个分区会提示要删除的分区。
Selected partition 1
Command (m for help): p        #打印当前分区,发现分区已删除
Disk /dev/sdb: 1181.1 GB, 1181116006400 bytes
255 heads, 63 sectors/track, 143595 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: 0xc6e44e94
   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-143595, default 1):     #默认是1,比如要和扩容前的分区其实点一样,扩容前的分区起始也是1,这个是扩容的重点地方。
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-143595, default 143595): #终止的位置是默认是硬盘柱体的尾部,即使最大值,这个是要扩容的点,回车。
Using default value 143595
Command (m for help): w                           #w报错,并退出。
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.
  • 18.
  • 19.
  • 20.
  • 21.
  • 22.
  • 23.
  • 24.
  • 25.
  • 26.
  • 27.
  • 28.
  • 29.
  • 30.
  • 31.
  • 32.
  • 33.
  • 34.
  • 35.
  • 36.
  • 37.
  • 38.

3、调整分区

e2fsck -f /dev/sdb1 #检查分区信息

resize2fs /dev/sdb1 #调整分区大小

 

[root@backup ~]# e2fsck -f /dev/sdb1    #由于磁盘容量很大,数据多的情况下需要等待一段时间
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/sdb1: 12/65536000 files (0.0% non-contiguous), 4164250/262142637 blocks
[root@backup ~]# resize2fs /dev/sdb1     #由于磁盘容量很大,数据多的情况下需要等待一段时间
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/sdb1 to 288356701 (4k) blocks.
The filesystem on /dev/sdb1 is now 288356701 blocks long.
[root@backup ~]# mount /dev/sdb1 /backup/
[root@backup ~]# cat /backup/
backup.txt  lost+found/ 
[root@backup ~]# cat /backup/backup.txt 
backup
  • 1.
  • 2.
  • 3.
  • 4.
  • 5.
  • 6.
  • 7.
  • 8.
  • 9.
  • 10.
  • 11.
  • 12.
  • 13.
  • 14.
  • 15.
  • 16.
  • 17.

发现扩容成功,扩容前的内容没有丢失。

参考内容:

https://blog.csdn.net/wuweilong/article/details/7538634

https://blog.csdn.net/jackyu651/article/details/53070607

https://blog.csdn.net/a746742897/article/details/52689285