Linux command – parted / (resize , resizepart) + resize2fs

Loading

有時候系統不是你建置的,到你接手的時候 Linux 出了問題,問題居然是 root 磁區空間不夠,那該怎麼辦呢!!下面幾種方式可以解決.

LVM

如果 root 磁區採用的是 LVM 那感謝主,這個稍微用指令或是圖形介面操作放大 root 空間 就可以解決.

LVM (Logical Volume Manager)是一種可以動態變更 volume (由 LVM 所切割出來的空間被稱為 volume) 大小的方式,可以讓你能更容易利用管理你的硬碟.傳統的硬碟空間在切割完時就決定了大小,如果要重新規劃必須將資料先備份下來,硬碟磁區重新切割才可能有改變原來的硬碟磁區環境.不過 LVM(Logical Volume Manager) 就不同了,如果磁區空間不夠了,你可以隨意將空間放大或是磁區的使用率太低也可以將空間縮小.當我們在放大縮小時這都不影響原先在硬碟上的資料.

請參考 – https://benjr.tw/101365

parted / resize

可以透過 parted 的 resize 功能來變更硬碟大小,但需要將該磁區 #umount 卸載下來才能修改,如果是在 / (root) 時必須先透過 live cd 或 live usb 開機,關於 Ubuntu Live USB 請參考 – https://benjr.tw/1550 ,就可以直接透過 #parted 的 resize 功能來修改,請參考 https://benjr.tw/94833.

不習慣文字介面還可以用 #GParted 圖形介面來做 磁碟分割 (底層應該還是用 parted 來做).

不知道為什麼在 Ubuntu 14.04 下面 resize 沒有辦法使用,上網查說明 resize 將不再支援,建議用戶用 parted (resizepart) + resize2fs 做放大縮小磁區,後面介紹.

[root@localhost ~]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p                                                                
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size    Type     File system  Flags
 1      32.3kB  3233MB  3233MB  primary  ext3

(parted) resize                                                           
WARNING: you are attempting to use parted to operate 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? 1                                                       
Start?  [32.3kB]?                                                         
End?  [3233MB]? 5000M                                                     
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) q   

RHEL 7 下進入 parted 裡面一樣找不到 resize 的功能 (顯示 parted 3.0 已經此功能移除了)

[root@localhost ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) resize                                                           
Error: The resize command has been removed in parted 3.0

parted / resizepart + resize2fs

resize2fs 含在 e2fsprogs 套件 (Ubuntu 預設有安裝).

root@benjr:~# apt-get install e2fsprogs
Reading package lists... Done
Building dependency tree
Reading state information... Done
e2fsprogs is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 35 not upgraded.

e2fsprogs 套件安裝了哪一些檔案,可以透過 apt-file 來查詢,不過他非預設安裝,需要額外安裝.

root@ubuntu:~# apt-get install apt-file

安裝後的第一步就是先更新資料庫.

root@ubuntu:~# apt-file update

現在就可以來搜尋套件安裝了哪一些檔案,而這些套件存放的地方在哪裡,都會顯示出來.

root@ubuntu:~# apt-file list e2fsprogs
e2fsprogs: /etc/mke2fs.conf
e2fsprogs: /sbin/badblocks
e2fsprogs: /sbin/debugfs
e2fsprogs: /sbin/dumpe2fs
e2fsprogs: /sbin/e2fsck
..............
e2fsprogs: /sbin/resize2fs
..............

需無掛載的硬碟, umount 後才能使用 parted / resizepart + resize2fs 來處理.

root@ubuntu:~# parted /dev/sdb
GNU Parted 2.3
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End    Size   Type     File system  Flags
 1      1049kB  525MB  524MB  primary  ext2

(parted) resizepart
Partition number? 1
End?  [525MB]? 1G
(parted) p
Model: ATA VBOX HARDDISK (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: msdos

Number  Start   End     Size   Type     File system  Flags
 1      1049kB  1000MB  999MB  primary  ext2
root@ubuntu:~# resize2fs /dev/sdb1
resize2fs 1.42.9 (4-Feb-2014)
Please run 'e2fsck -f /dev/sdb1' first.
root@ubuntu:~# e2fsck -f /dev/sdb1
e2fsck 1.42.9 (4-Feb-2014)
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/122400 files (0.0% non-contiguous), 17766/488280 blocks
root@ubuntu:~# resize2fs /dev/sdb1
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/sdb1 to 976560 (1k) blocks.
The filesystem on /dev/sdb1 is now 976560 blocks long.

再重新掛載硬碟磁區後,透過 #df 可以發現硬碟磁區空間調整過了.

fdisk + resize2fs

放大一個磁區也可以透過下面步驟 (這是一個很奇怪的方式不建議使用).

  1. 確定該放大磁區後面有空間,而且必須透過手動的方式先刪除磁區後再重建磁區.
  2. 在新擴大的磁區執行 #resize2fs 來調整 使用所有的空間.
root@ubuntu:~# dumpe2fs /dev/sdb1
dumpe2fs 1.42.9 (4-Feb-2014)
Filesystem volume name:   <none>
Last mounted on:          <not available>
Filesystem UUID:          cd440e00-c52e-4bc7-b1e6-986b0f21ca1d
Filesystem magic number:  0xEF53
Filesystem revision #:    1 (dynamic)
Filesystem features:      has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
..............

/dev/sdb 空間有 2147 MB ,我想要放大 /dev/sda1 (200M) 到 2147 MB.

root@ubuntu:~# fdisk -l /dev/sdb

Disk /dev/sdb: 2147 MB, 2147483648 bytes
160 heads, 6 sectors/track, 4369 cylinders, total 4194304 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: 0x00020c4f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048      411647      204800   83  Linux
root@ubuntu:~# fdisk /dev/sdb

Command (m for help): d
Selected partition 1

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 1
First sector (2048-4194303, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-4194303, default 4194303):
Using default value 4194303

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

接下來放大/dev/sda1 (200M) 到 2147 MB.而且資料都會保留下來.

root@ubuntu:~# resize2fs -f /dev/sdb1
resize2fs 1.42.9 (4-Feb-2014)
Resizing the filesystem on /dev/sdb1 to 2096128 (1k) blocks.
The filesystem on /dev/sdb1 is now 2096128 blocks long.
root@ubuntu:~# fdisk -l /dev/sdb

Disk /dev/sdb: 2147 MB, 2147483648 bytes
11 heads, 4 sectors/track, 95325 cylinders, total 4194304 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: 0x00020c4f

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     4194303     2096128   83  Linux
root@ubuntu:~# e2fsck -f /dev/sdb1
e2fsck 1.42.9 (4-Feb-2014)
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/524288 files (0.0% non-contiguous), 73272/2096128 blocks
沒有解決問題,試試搜尋本站其他內容

4 thoughts on “Linux command – parted / (resize , resizepart) + resize2fs

  1. 你好, 請問在fdisk內使用”D” delete partition. 之後再用”N”. 來加大partition.
    那partition內的資料是否會全被刪除呢?
    謝謝

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

這個網站採用 Akismet 服務減少垃圾留言。進一步了解 Akismet 如何處理網站訪客的留言資料