有時候系統不是你建置的,到你接手的時候 Linux 出了問題,問題居然是 root 磁區空間不夠,那該怎麼辦呢!!下面幾種方式可以解決.
- LVM
如果 root 磁區採用的是 LVM 那感謝主,這個稍微用指令或是圖形介面操作放大 root 空間 就可以解決.LVM(Logical Volume Manager)是一種可以動態變更 volume (由 LVM 所切割出來的空間被稱為 volume)大小的方式,可以讓你能更容易利用管理你的硬碟.傳統的硬碟空間在切割完時就決定了大小,如果要重新規劃必須將資料先備份下來,硬碟磁區重新切割才可能有改變原來的硬碟磁區環境.不過 LVM(Logical Volume Manager) 就不同了,如果磁區空間不夠了,你可以隨意將空間放大或是磁區的使用率太低也可以將空間縮小.當我們在放大縮小時這都不影響原先在硬碟上的資料.
可以參考
LVM(Logical Volume Manager) – http://benjr.tw/174
LVM-工具 – http://benjr.tw/7362
RHEL5 – LVM(Logical Volume Manager) – http://benjr.tw/18720 - parted
要修改磁區大小第一步需要將該磁區 #umount 卸載下來才能修改,所以必須先透過 live cd 或 live usb 開機(關於 Ubuntu Live USB 可以參考 http://benjr.tw/1550),就可以直接透過 #parted 來修改 (請參考 http://benjr.tw/94833),需先 #umount 然後 resize.不習慣文字介面還可以用 #GParted 圖形介面來做 磁碟分割 (底層應該還是用 parted 來做)
不過不知道為什麼在 Ubuntu 14.04 (RHEL 7) 下面 resize 沒有辦法使用 (仔細一看說明,resize 將不再支援,建議用戶用 e2fsprogs 套件做放大縮小磁區 ),但在 RedHat (RHEL) 6.5 下使用就沒有問題.
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.
- 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 ..............
透過 resize2fs 可以修改磁區大小,第一步需要將該磁區 #umount 卸載下來才能修改,所以必須先透過 live cd 或 live usb 開機(關於 Ubuntu Live USB 可以參考 http://benjr.tw/1550)
在 #man resize2fs 裡面有提到 http://linux.die.net/man/8/resize2fs 可以 on-line resize for filesystems mounted using ext3 and ext4, 我猜應該是使用 LVM 的分割才有辦法做到.
The resize2fs program will resize ext2, ext3, or ext4 file systems. It can be used to enlarge or shrink an unmounted file system located on device. If the filesystem is mounted, it can be used to expand the size of the mounted filesystem, assuming the kernel supports on-line resizing. (As of this writing, the Linux 2.6 kernel supports on-line resize for filesystems mounted using ext3 and ext4.).放大磁區
如果你想放大一個磁區, 必須做下面的- 確定該放大磁區後面有空間,而且必須透過手動的方式先刪除磁區後再重建磁區.
- 在新擴大的磁區執行 #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
縮小磁區
縮小磁區比放大容易多了, 透過 #resize2fs -Mroot@ubuntu:~# resize2fs -M /dev/sdb1 resize2fs 1.42.9 (4-Feb-2014) Resizing the filesystem on /dev/sdb1 to 4645 (1k) blocks. The filesystem on /dev/sdb1 is now 4645 blocks long. 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/2048 files (0.0% non-contiguous), 4644/4645 blocks 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:~# mount /dev/sdb1 /media/ root@ubuntu:~# df -h Filesystem Size Used Avail Use% Mounted on udev 361M 4.0K 361M 1% /dev tmpfs 75M 912K 74M 2% /run /dev/sda1 11G 4.0G 6.5G 38% / none 4.0K 0 4.0K 0% /sys/fs/cgroup none 5.0M 0 5.0M 0% /run/lock none 371M 144K 371M 1% /run/shm none 100M 44K 100M 1% /run/user /dev/sdb1 288K 287K 0 100% /media
透過 -M 縮小感覺起來怪怪的,硬碟分割沒有影響,看樣子並不是真正的縮小,下次在 Linux 環境下還是記得用 LVM