Resize Linux partition (command line)

Recent media capacity is getting higher, rather than lower price.

Compared to server storage and memory media several years ago, the price is several times the capacity and the price is a fraction.

This time, I copied the Linux disk to the whole large capacity drive and resized the partition on the command line.

1. Install parted

https://www.gnu.org/software/parted/

It is easy to install with package.

2. Unmount target partition

It was mounted automatically, so unmount it.

#umount / mnt

3. Check the status of the drive.

To check all drives, use the following command.

# fdidsk-l

Specify the target drive because it is mounted on / dev / sda.

# fdisk -l / dev / sda
Disk / dev / sda: 196.9 GB, 196865949696 bytes
255 heads, 63 sectors / track, 23934 cylinders, total 384503808 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical / physical): 512 bytes / 512 bytes
I / O size (minimum / optimal): 512 bytes / 512 bytes
Disk identifier: 0x6c70c806
Device Boot Start End Blocks Id System
/ dev / sda 1 2048 43007 20480 83 Linux
/ dev / sda 2 43008 3970047 1963520 83 Linux
#

Extend the partition of less than 2 GB of / dev / sda 2.

4. Start gparted and check the free space

# parted
GNU Parted 2.3
Using / dev / sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) print free
Model: Generic - USB 3.0 CRW - SD (scsi)
Disk / dev / sda: 197 GB
Sector size (logical / physical): 512 B / 512 B
Partition Table: msdos

Number Start End Size Type File system Flags
31.7 kB 1049 kB 1017 kB Free Space
1 1049 kB 22.0 MB 21.0 MB primary fat 16
2 22.0 MB 2033 MB 2011 MB primary ext 4
2033 MB 197 GB 195 GB Free Space

(parted)

There is 195 GB of free space behind Partition 2.

5. In order to specify exact sectors, change to unit (sector) unit display

(parted) unit s
(parted) print free
Model: Generic - USB 3.0 CRW - SD (scsi)
Disk / dev / sda: 384503808s
Sector size (logical / physical): 512 B / 512 B
Partition Table: msdos
Number Start End Size Type File system Flags
62s 2047s 1986s Free Space
1 2048 s 43007 s 40960 s primary fat 16
2 43008s 3970047s 3927040s primary ext 4
3970048s 384503807s 380533760s Free Space
(parted)

I will make a note as I know that the last sector is 384503807s.

Parted contains a resize command for the partition, but it seems to be incompatible feature as below.

(parted) resize
WARNING: you are attempting to use parted to operations on (resize) a file system.
parted's file system manipulation code is not as robust as what you'll find in
dedicated, file-system-specific packages like e2fsprogs. We recommend
you use parted only to manipulate partition tables, whenever possible.
Support for performing most operations on most types of file systems
will be removed in an upcoming release.
Partition number? 2
Start? [43008s]?
End? [3970047s]? 384503807s
Error: File system has an incompatible feature enabled. Compatible features are
has_journal, dir_index, filetype, sparse_super and large_file. Use tune2fs or
debugfs to remove features.
(parted)

Why it does not correspond is unknown.
Is it compatible only with the ext4 file system?
Let's move on carefully from here.

6. Deletes the second partition to be expanded

(parted) rm 2
(parted)

Since the management table of the partition is only deleted, the actual file is not deleted.

7 Set a new partition for number 2

The file system specifies the same (ext 4) as before.

(parted) mkpart
Partition type? Primary / extended? Primary
File system type? [Ext 2]? Ext 4
Start? 43008s
End? 384460807s
(parted)

The command used here is mkpart.
Although it is similar, if you use mkpartfs, create a file system and overwrite it so do not use it.

Check the partition configuration. There is no empty space behind.

(parted) print free
Model: Generic - USB 3.0 CRW - SD (scsi)
Disk / dev / sda: 384503808s
Sector size (logical / physical): 512 B / 512 B
Partition Table: msdos
Number Start End Size Type File system Flags
62s 2047s 1986s Free Space
1 2048 s 43007 s 40960 s primary fat 16
2 43008s 384503807s 384460800s primary ext 4
(parted)

8. Return to the command line (shell)

(parted) q
Information: You may need to update / etc / fstab.
#

Here you can confirm that the previous file is properly left, but even if you check with the df command etc., the partition capacity is the same as before.

9. I will check the file system

We will check the integrity of the file system of the expanded partition.
If you do not do consistency check, you will not be able to issue a warning with the command in the future.

# fsck.ext 4 - f / dev / sda 2
e2fsck 1.42.5 (29-Jul-2012)
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 / sda 2: 17888/122880 files (0.2% non-contiguous), 242431/490880 blocks
#

There seems to be no problem in particular.

10. Convert file system

Resize the file system corresponding to the expanded partition.

# resize2fs / dev / sda2
resize2fs 1.42.5 (29-Jul-2012)
Resizing the filesystem on / dev / sda 2 to 48057600 (4 k) blocks.
The filesystem on / dev / sda 2 is now 48057600 blocks long.
resize2fs / dev / sda2 30.50 s user 18.80 s system 10% cpu 8: 02.46 total
# mount / dev / sda2 / mnt
# df | grep sda
/ dev / sda2 189213468 942460 178659488 1% / mnt
#

The partition is now expanded.

It was a little troublesome, but it was an extension of the partition on the command line.
Gparted is a staple if it is GUI.

It was a simple ext4 case, but the case of LVM and so on is at a later date.

It was N of Tokyo head office.