Linux-扩容已有分区的磁盘空间

曾经这个小短文解决了我很多实操的问题

针对:磁盘已有在用盘,再扩容空间用于增加新的MBR分区。

  1. 分区挂载准备

$ fdisk -lu /dev/vda  #运行以下命令确认数据盘的分区表格式,,本例是MBR分区
  #如果"System"="Linux",说明数据盘使用的是MBR分区表格式。
  #如果"System"="GPT",说明数据盘使用的是GPT分区表格式。
$ blkid /dev/vda1  #确认已有分区的文件系统类型。此文件系统类型为ext4。
  # (如未分区,无信息显示)
  /dev/vdb1: UUID="e97bf1e2-fc84-4c11-9652-73********24" TYPE="ext4"
$ e2fsck -n /dev/vda1   #ext4,查看文件系统状态 clean代表是正常的
  #xfs_repair -n        #xfs文件系统用这个命令

2. 新增并格式化MBR分区

场景:新增空间用于增加新的MBR分区

$ fdisk -u /dev/vda  #命令新建分区
  Command (m for help): p  #打印分区情况
  Command (m for help): n  #创建分我
  Partition type:
     p   primary (1 primary, 0 extended, 3 free)
     e   extended
  Select (default p): p #主分区
  Partition number (2-4, default 2): 2
  First sector (41943040-83886079, default 41943040):  回车
  Last sector, +sectors or +size{K,M,G} (41943040-83886079, default 83886079): 回车

  Command (m for help): w  #
  The partition table has been altered!
  Calling ioctl() to re-read partition table.
      WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
      The kernel still uses the old table. The new table will be used at
      the next reboot or after you run partprobe(8) or kpartx(8)
      Syncing disks.

$  fdisk -lu /dev/vda         #查看分区

3. 等待重启连接

[root@*** ~]#   reboot

4. mkfs.ext4 /dev/vda2 #创建文件系统

[root@***~]#  fdisk -lu /dev/vda          #查看分区
[root@***~]# mkfs.ext4 /dev/vda2
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
1310720 inodes, 5242880 blocks
262144 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2153775104
160 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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

5. 备份数据,清空/data用户分区

[root@***~]#  cp /etc/fstab /etc/fstab.bak
[root@*** ~]#  mkdir /bak
[root@*** ~]#  tar -zcvf /bak/data.tar.gz /data/
[root@*** ~]#  cd /data & cd /data
[root@*** ~]#  rm -rf * &&  cd ..

6. 向/etc/fstab写入新分区信息,启动开机自动挂载分区。

#向/etc/fstab里写入新分区信息。如只是挂载到 /mnt,将/data 改为/mnt 
[root@*** ~]#  echo `blkid /dev/vda2 | awk '{print $2}' | sed 's/\"//g'` /data ext4 defaults 0 0 >> /etc/fstab
[root@*** ~]# cat /etc/fstab #查看信息
[root@*** ~]# mount /dev/vda2 /data   #立即生效的mount

7. df -h #查看结果情况

"/data挂载成功结果检查

[root@*** /]# df -h
Filesystem      Size  Used Avail Use% Mounted on
devtmpfs        1.9G     0  1.9G   0% /dev
tmpfs           1.9G     0  1.9G   0% /dev/shm
tmpfs           1.9G  460K  1.9G   1% /run
tmpfs           1.9G     0  1.9G   0% /sys/fs/cgroup
/dev/vda1        20G   15G  3.7G  81% /
tmpfs           379M     0  379M   0% /run/user/0
/dev/vda2        20G   45M   19G   1% /data

8. 恢复数据

$ tar -zxvf /bak/data.tar.gz -C /data/
$ mv /data/data/* /data/
$ rmdir /data/data
$ reboot

相关推荐

  1. Linux-扩容磁盘空间

    2023-12-14 07:42:01       58 阅读
  2. linux磁盘存储空间不足磁盘扩容

    2023-12-14 07:42:01       41 阅读

最近更新

  1. docker php8.1+nginx base 镜像 dockerfile 配置

    2023-12-14 07:42:01       94 阅读
  2. Could not load dynamic library ‘cudart64_100.dll‘

    2023-12-14 07:42:01       100 阅读
  3. 在Django里面运行非项目文件

    2023-12-14 07:42:01       82 阅读
  4. Python语言-面向对象

    2023-12-14 07:42:01       91 阅读

热门阅读

  1. 百度智能云解析DNS python版api ddns实现2023最新

    2023-12-14 07:42:01       59 阅读
  2. PyTorch : torch.cuda.amp: 自动混合精度详解

    2023-12-14 07:42:01       62 阅读
  3. wordpress打包小程序并能分享转发

    2023-12-14 07:42:01       67 阅读
  4. 深入了解 Scrapy 中的 Pipelines 和 Item

    2023-12-14 07:42:01       58 阅读
  5. 简单的WEB服务器

    2023-12-14 07:42:01       52 阅读
  6. loki memberlist

    2023-12-14 07:42:01       52 阅读