Home  >  Article  >  System Tutorial  >  Using fdisk to expand partition capacity under Linux

Using fdisk to expand partition capacity under Linux

王林
王林forward
2024-02-11 15:27:23691browse

The servers we manage may suffer from insufficient disk space as the business volume continues to grow. For example, the hard disk space of the shared file server is insufficient. At this time, we need to increase the disk space to meet the online business; and Or when we use Linux, sometimes due to improper partitioning when installing the system, some partitions have insufficient space, and some partitions have excess space. We can use the fdisk partition tool to dynamically adjust the size of the partition;

Expand disk space

The hard disk space is 20G. If you use vSphere Client to increase the disk size, you need to add another 10G space;
Using fdisk to expand partition capacity under LinuxUsing fdisk to expand partition capacity under Linux

After the expansion is completed, restart the system and use fdisk -l to check again. You will find that the hard disk space has become larger;

[root@linuxprobe ~]# fdisk -l
Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   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        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        2611     8952832   83  Linux
[root@linuxprobe ~]# df -hT
Filesystem     Type   Size  Used Avail Use% Mounted on
/dev/sda2      ext4   9.7G  1.5G  7.7G  16% /
tmpfs          tmpfs  939M     0  939M   0% /dev/shm
/dev/sda1      ext4   194M   34M  151M  19% /boot
/dev/sda4      ext4   8.5G  148M  7.9G   2% /data
Using fdisk to expand partition capacity under Linux

Re-create partition and adjust partition information

This experiment mainly expands the /dev/sda4 partition. If it is a production environment, please make a backup in advance and save it to other partitions. Although expanding the partition size will not cause data loss, for safety reasons, please do it in advance. Good backup;
First simulate some data:

[root@linuxprobe data]# mkdir test
[root@linuxprobe data]# echo "we are Linuxer" > linuxprobe
[root@linuxprobe data]# ll
total 24
-rw-r--r--. 1 root root    15 May 23 21:59 linuxprobe
drwx------. 2 root root 16384 May 23 15:07 lost+found
drwxr-xr-x. 2 root root  4096 May 23 21:51 test
[root@linuxprobe ~]# umount /dev/sda4          #卸载磁盘分区

If it prompts that the disk is busy, use fuser to find out the program that is using the disk and end it;

fuser -m -v /data
fuser -m -v -i -k /data

Use the fdisk tool to first delete the /dev/sda4 partition, and then create a new partition. Note that the starting magnetic column number must be consistent with the original one (a key step to ensure that data is not lost), and the ending magnetic column number is the default. Press Enter to use all disks.

[root@linuxprobe ~]# fdisk /dev/sda

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/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   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        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        2611     8952832   83  Linux

Command (m for help): d           #删除分区
Partition number (1-4): 4         #删除第四个

Command (m for help): p       #再次查看分区信息,/dev/sda4已被删除

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   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        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris

Command (m for help): n      #创建新的分区
Command action
   e   extended
   p   primary partition (1-4)
p             #创建为主分区
Selected partition 4
First cylinder (1497-3916, default 1497):          #经对比,正好和上一个磁盘柱一致,默认即可
Using default value 1497
Last cylinder, +cylinders or +size{K,M,G} (1497-3916, default 3916): 
Using default value 3916              #直接默认就可以

Command (m for help): p               #查看分区表信息

Disk /dev/sda: 32.2 GB, 32212254720 bytes
255 heads, 63 sectors/track, 3916 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: 0x0005210c

   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        1301    10240000   83  Linux
/dev/sda3            1301        1497     1572864   82  Linux swap / Solaris
/dev/sda4            1497        3916    19436582   83  Linux

Command (m for help): wp       #保存并退出,如果创建有误,直接退出不要保存即可
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.

Using fdisk to expand partition capacity under LinuxUsing fdisk to expand partition capacity under Linux
After re-creating the partition, you need to restart it;

[root@linuxprobe ~]# init 6
[root@linuxprobe ~]# e2fsck -f /dev/sda4                #检查分区信息
[root@linuxprobe ~]# resize2fs -p /dev/sda4             #调整分区大小

Remount, view partition size, data

[root@linuxprobe ~]# mount /dev/sda4 /data
[root@linuxprobe ~]# df -hT
[root@linuxprobe ~]# cat /data/linuxprobe
we are  Linuxer
Using fdisk to expand partition capacity under Linux

The above is the detailed content of Using fdisk to expand partition capacity under Linux. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lxlinux.net. If there is any infringement, please contact admin@php.cn delete