Home  >  Article  >  Operation and Maintenance  >  How to resize partition in linux

How to resize partition in linux

WBOY
WBOYforward
2023-05-15 08:31:055332browse

(1) Backup

First, backup the files in the partition to be changed. Operations on the disk may cause data loss. It is best to copy important files before starting work. Save the data to another partition.

(2) Obtain relevant information

1, run $ df, view the file system information. Note down the partition you want to adjust The corresponding mount point and device file.

This step is to check which device file in /usr/local corresponds to /dev/. This article takes /dev/hda7 as an example.

2 , run # sudo fdisk /dev/sda. Enter fdisk, press p, and view the disk partition information. Note down the starting cylinder number and ending cylinder number of /dev/sda8. Let them be start and end respectively. And note down How many bytes are included in the next "unit".

Note that the file system size and the disk partition size are not the same: at least their measurement units are different. In the file system, kb, mb, and gb are generally used. Unit. But in fdisk, it is calculated based on the "unit" value of the disk (that is, the number of bytes contained in a cylinder). After we run # fdisk /dev/sda, fdisk displays the following information:

disk /dev/sda: 163.9 gb, 163928604672 bytes
255 heads, 63 sectors/track, 19929 cylinders
units = cylinders of 16065 * 512 = 8225280 bytes

(3) Calculate the ending cylinder number

e.g: The original size of /usr/local is 5000 mb, the starting cylinder number is 2550, and the ending cylinder number is 3157 . Then I want to change its size to 1000mb now, and the ending cylinder number should be calculated like this ([3.2] = 4):

Ending cylinder number = 2550 [(1000 * 1024 * 1024) / 8225280 ] = 2678

Use fdisk to delete the original partition (the data of the previous partition will not be lost!). Then create a new partition. The only difference between the new partition and the old partition is that the partition size is different. This is reflected in their The ending cylinder number is different.

Increase the partition size: The ending cylinder number is set to the last cylinder number of the free space. Decrease the partition size: The ending cylinder number is calculated.

Start adjusting

Before starting the adjustment, it is best to take a piece of paper and write down the following information about the partition to be adjusted:

Size before adjustment, size after adjustment. In which directory it is mounted , corresponds to which device in /dev/. Starting, ending cylinder

e.g: before adjustment: 5g, after adjustment: 1g, mount point: /usr/local, corresponding to /dev/hda7 . The starting and ending cylinders are 2550 and 3157 respectively

1, uninstall the partition

It is best to uninstall it with runlevel=1.

#umount/dev/

2, check the file System consistency

#fsck-n/dev/

3. If it is an ext3 system, you must also remove the log

#tune2fs-o^has_journal/dev/

Note: The ext3 system cannot be mounted after removing the log. Use #tune2fs-j/ dev/rebuild log.

4, adjust the file system size

#resize_reiserfs-s1000m/dev/hda7

After adjusting the file system size, use the following command to conduct a comprehensive check on the file system:

#reiserfsck--check--fix-fixable/dev/hda7

5, enter fdisk and adjust the partition size

#fdisk/dev/hda

Since the kernel still uses the old partition table, restart the next steps before proceeding.

Through the above steps, we have completed the hard disk partition size Adjustment operation, next back to the problem of resizing the file system. Since hard disk partitions are planned according to cylinders, it is difficult for us to adjust the file system size to be exactly equal to the hard disk partition size in one step in the resize_reiserfs step. Therefore, it is generally necessary to adjust the hard disk partition to be a little larger than the file system, and then synchronize it through the following command (of course, if you don’t care about the existence of some space in the hard disk partition, you can ignore the following steps):

#umount/home/
#resize_reiserfs/dev/hda6

In this way, Just adjust the size of /usr/local to 1g, leaving 4g of free disk space.

The above is the detailed content of How to resize partition in linux. For more information, please follow other related articles on the PHP Chinese website!

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