1. 程式人生 > >Linux下調整ext3分割槽大小【轉】

Linux下調整ext3分割槽大小【轉】

本文轉載自:https://blog.csdn.net/cruise_h/article/details/22403529

本文討論如何再不丟失資料的情況下調整已有ext3分割槽的大小,包括:

  1. 壓縮已有分割槽
  2. 增大已有分割槽
  3. 合併兩個ext3分割槽

這在沒有使用LVM(邏輯卷管理),而已有分割槽規劃不能滿足要求時,非常有用。

前提

1.擁有root許可權

用root登入,或者sudo操作

2.被操作的分割槽的檔案系統必須已被解除安裝(umount)

2.1對於不包含系統重要檔案的分割槽,如/home分割槽,可以直接umount。
2.2對於包含系統重要檔案的分割槽,如/分割槽,往往不可能在使用時umount,則需要藉助Live CD。

如果在生產環境中實施,一定記得備份好你的資料!以防萬一因新分割槽尺寸等問題導致資料意味丟失。

以下以較複雜情況(使用Live CD)為例,說明調整ext3分割槽的步驟。

案例1:壓縮一個ext3分割槽

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.5G  4.1G  4.9G  46% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile

本案例意在壓縮分割槽/dev/sda1。

shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
10016496

接著,我們重啟系統,使用Live CD進入系統(如果你需要修改的分割槽不包含重要系統檔案,則無需此步驟,可以直接操作)。

shell>shutdown -r now

執行Live CD進入系統後,切換成root,進行後續操作:

shell>su 
shell>umount /dev/sda1 #確保需要調整的分割槽被umount
shell>fsck -n /dev/sda1

fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159037/1254176 files, 1095299/2504124 blocks

接下來我們移除/dev/sda1上的檔案系統日誌資訊,將/dev/sda1分割槽上的檔案系統降為ext2

shell>tune2fs -O ^has_journal /dev/sda1

tune2fs 1.38 (30-Jun-2005)

shell>e2fsck -f /dev/sda1

e2fsck 1.38 (30-Jun-2005)
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/sda1: 164178/1254176 files (0.6% non-contiguous), 1051617/2504124 blocks

檢查無誤後,我們使用resize2fs調整檔案系統大小。resize2fs只能調整ext2檔案系統,這就是為什麼我們之前將/dev/sda1轉化成了ext2

shell>resize2fs /dev/sda1 6000M 
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 1536000 (4k) blocks.
The filesystem on /dev/sda1 is now 1536000 blocks long.

需要記錄下這裡的blocks數目1536000和他們的大小4k,我們後面會使用到。
接下來我們刪除/dev/sda1分割槽(不用擔心,不會有資料丟失),且建立一個新的,較小(適合我們調整後的檔案系統)的分割槽。

shell>fdisk /dev/sda   (注意:不是sda1!是針對整個磁碟的操作)
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
#看下幫助
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition’s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)

#刪除分割槽

Command (m for help): d
Partition number (1-5): 1    #指定分割槽號

#建立新分割槽/dev/sda1,它之前是主分割槽,現在依然選擇主分割槽p

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p
Partition number (1-4): 1

#接下來是關鍵的步驟,我們需要指定新分割槽的大小,第一個柱面是確定的(通過fdisk -l可以查到),最後一個柱面需要計算,我們根據resize2fs的輸出進行計算,為了確保分割槽夠大,我們加上%3~5%:1536000 * 4k * 1.03 = 6328320k

First cylinder (1-1305, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): +6328320K #大寫K

我們原有的/dev/sda1有boot標記,即開機啟動,新分割槽也加上:

Command (m for help): a
Partition number (1-5): 1

接著我們將之前變更寫入分割槽表,並退出:

Command (m for help): w
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.
Syncing disks.

重啟系統,再次進入Live CD。

shell>shutdown -r now
shell>su
shell>fsck -n /dev/sda1

 fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1047239/1536000 blocks

shell>tune2fs -j /dev/sda1 #為ext2加上日誌,轉化為ext3
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

#再次重啟,取出Live CD,進入到原系統

shell>shutdown -r now
shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             5.8G  4.1G  1.6G  73% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
6337611

案例2:擴充套件一個ext3分割槽

經過案例1,我們空出了4G的未使用空間,在分割槽/dev/sda1之後。這裡我們期望將這部分空間新增到我們的/dev/sda1分割槽上(這隻有在未使用空間正好緊鄰該分割槽時才可以實現!)。

先檢視下現有分割槽資訊:

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             5.8G  4.1G  1.6G  73% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
6337611

#增大分割槽依然要求分割槽被umount,與案例1類似,重啟,使用Live CD,進入後切換為root後執行

shell>umount /dev/sda1
shell>fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/765536 files, 1080014/1536000 blocks
shell>tune2fs -O ^has_journal /dev/sda1  #轉化為ext2檔案系統

tune2fs 1.38 (30-Jun-2005)

接下來刪除分割槽(資料並不會丟失),並建立較大的新分割槽(包含要擴充套件的部分)
shell>fdisk /dev/sda
The number of cylinders for this disk is set to 1305.
There is nothing wrong with that, but this is larger than 1024,
and could in certain setups cause problems with:
1) software that runs at boot time (e.g., old versions of LILO)
2) booting and partitioning software from other OSs
   (e.g., DOS FDISK, OS/2 FDISK)
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition’s system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): p

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1         789     6337611   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
Command (m for help): d   #刪除分割槽
Partition number (1-5): 1    
Command (m for help): n   #新建分割槽
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
p   #設定為主分割槽
Partition number (1-4): 1
First cylinder (1-1305, default 1): 1
Last cylinder or +size or +sizeM or +sizeK (1-1247, default 1247): 1247   #這裡fdisk檢測到能夠到達的最大值,使用該值即可
Command (m for help): p    

Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1               1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
Command (m for help): a  #新增boot標記
Command (m for help): w
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.
Syncing disks.

然後我們重啟系統,繼續進入Live CD,切換為root:

shell>e2fsck -f /dev/sda1
shell>resize2fs /dev/sda1  #不指定大小,就會最大的可用空間,所以可以不計算
resize2fs 1.38 (30-Jun-2005)
Resizing the filesystem on /dev/sda1 to 2504124 (4k) blocks.
The filesystem on /dev/sda1 is now 2504124 blocks long.
shell>fsck -n /dev/sda1
fsck 1.38 (30-Jun-2005)
e2fsck 1.38 (30-Jun-2005)
/dev/sda1: clean, 159036/1254176 files, 1062544/2504124 blocks
shell>tune2fs -j /dev/sda1  #新增日誌,再次轉化為ext3
tune2fs 1.38 (30-Jun-2005)
Creating journal inode: done
This filesystem will be automatically checked every 30 mounts or
0 days, whichever comes first. Use tune2fs -c or -i to override.

之後就可以重啟進入原系統,檢視新的分割槽資訊

shell>df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda1             9.5G  4.1G  5.0G  45% /
varrun                 94M  132K   94M   1% /var/run
varlock                94M     0   94M   0% /var/lock
udev                   10M   52K   10M   1% /dev
devshm                 94M     0   94M   0% /dev/shm
lrm                    94M   18M   77M  19% /lib/modules/2.6.17-10-generic/volatile
shell>fdisk -l
Disk /dev/sda: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1        1247    10016496   83  Linux
/dev/sda2            1248        1305      465885    5  Extended
/dev/sda5            1248        1305      465853+  82  Linux swap / Solaris
shell>fdisk -s /dev/sda1
10016496

小結

經過上述案例,我們可以瞭解到調整ext3分割槽的基本思路:

步驟1)umount 需要調整的分割槽

步驟2)將ext3降為ext2,刪除日誌功能

步驟3)牢記,檔案系統是建立在分割槽之上的。所以,如果要縮小分割槽,先縮小檔案系統,再縮小分割槽;要擴充套件分割槽,先擴充套件分割槽,再擴充套件檔案系統!

調整檔案系統步驟:先使用tune2f將ext3降為ext2,然後使用resize2fs調整ext2檔案系統大小,最終tune2f再將ext2升級為ext3;

調整分割槽步驟:刪除原分割槽,建立新分割槽,注意分割槽大小的指定,以及是否boot,是否主分割槽等。

案例3:合併兩個ext3分割槽

有了上述兩個案例做鋪墊,我們可以延伸操作本案例。首先前提,兩個分割槽是相鄰分割槽才能夠進行合併。

基本思路:

  1. 將後一個分割槽的檔案系統umount,且刪除該分割槽(因資料會被清除,如果需要保留,提前備份!),這樣就有了空閒空間,形成於案例2一樣的情況。
  2. 按照案例2的操作步驟依次處理即可。

主要注意的是:

1)如果被刪除的分割槽,在/etc/fstab上有記錄,刪除之!
2)被刪除分割槽的資料也會被刪除,再次提醒,如果需要,備份!

具體操作不再詳述,參考前兩個案例。

 

參考:http://www.howtoforge.com/linux_resizing_ext3_partitions