[FreeBSD 10]_新增以及擴充磁碟分割區

因為一開始接觸FBSD以為他跟Linux一樣精簡,殊不知裝了幾個套件以及port-tree的緣故

一開始切給根目錄的10G空間已經被消耗殆盡了…以前都在玩Ubuntu,從來沒遇過這種情況…

 

不過我是在VM裡面,所以磁碟新增以及擴充的方式是比較自由一些,先來看看我還沒處理的分割區狀況吧:

[root@ntl_freebsd01_for_ncku ~]# df
Filesystem 1K-blocks    Used   Avail Capacity  Mounted on
/dev/da0p2  10143420 9210820  121128    99%    /
devfs              1       1       0   100%    /dev
/dev/da0p3   9135164   73948 8330404     1%    /home

可以看到只有一顆硬碟(da0)且上面的跟目錄已經呈現爆滿狀況…

 

我這邊的操作是GPT的硬碟,所以並不會使用fdisk,而是使用gpart來做接下來我要做的事情

這點需要特別注意!

 

首先先安裝新的60G硬碟上去,我這裡是用虛擬機器,所以直接在vmware裡面調整,調整方法我就不細數了

以及我也擴充原先只有20G的da0變成40G,比較要注意的是擴充磁區有個條件就是必須要刪光之前的snapshot

 

處理ok,開機之後,使用gpart show指令檢視一下,沒意外的話會發現多了一顆硬碟,這裡是da1,舊的那顆是da0

[root@ntl_freebsd01_for_ncku ~]# gpart show
=>      34  83886013  da0  GPT  (40G)
        34       128    1  freebsd-boot  (64K)
       162  20971392    2  freebsd-ufs  (10G)
  20971554  18874368    3  freebsd-ufs  (9.0G)
  39845922   2097084    4  freebsd-swap  (1.0G)
  41943006  41943041       - free -  (20G)

=>       34  125829053  da1  GPT  (60G)
         34  125829053       - free -  (60G)

 

接下來本文要做以下操作:

  1. 初始化da1成GPT Disk
  2. 新增一個新的SWAP磁區大小為10G,da1p1
  3. 新增一個新的HOME磁區大小為20G,da1p2
  4. 關掉在da0p4現有的SWAP,並且在da1p1重新打開
  5. 複製在da0p3的home到新的da1p2磁區
  6. 修改fstab,讓home目錄以及swap mount point指向新的位置
  7. 刪除da0p3,p4兩個磁區
  8. 擴充da0p2磁區
  9. 讓分割區長滿da0p2磁區

 

i.初始化da1成GPT Disk:

[root@ntl_freebsd01_for_ncku ~]# gpart create -s gpt
da1 created

 

ii.新增一個新的SWAP磁區大小為10G,da1p1

[root@ntl_freebsd01_for_ncku ~]# gpart add -t freebsd-swap -s 10G da1
da1p1 created

另外add下面可以接-l 磁碟標籤,

例如gpart add -t freebsd-swap -l FBSD_SWAP -s 10G da1

 

 iii.新增一個HOME磁區大小為20G,da1p2

[root@ntl_freebsd01_for_ncku ~]# gpart add -t freebsd-ufs -s 20G da1
da1p1 added

 

 iv.關掉在da0p4現有的SWAP,並且在da1p1重新打開

[root@ntl_freebsd01_for_ncku ~]# swapoff /dev/da0p4
[root@ntl_freebsd01_for_ncku ~]# swapon /dev/da1p1

 

v.複製在da0p3的home到新的da1p2磁區

[root@ntl_freebsd01_for_ncku ~]# mount /dev/da1p2 /mnt
[root@ntl_freebsd01_for_ncku ~]# rsync-avP /home/* /mnt/
sending incremental file list
...下略...

 

這時候下gpart show,將會得到:

gpart show
=>      34  83886013  da0  GPT  (40G)
        34       128    1  freebsd-boot  (64K)
       162  20971392    2  freebsd-ufs  (10G)
  20971554  18874368    3  freebsd-ufs  (9.0G)
  39845922   2097084    4  freebsd-swap  (1.0G)
  41943006  41943041       - free -  (20G)

=>       34  125829053  da1  GPT  (60G)
         34   20971520    1  freebsd-swap  (10G)
   20971554   41943040    2  freebsd-ufs  (20G)
   62914594   62914493       - free -  (30G)

 

 vi.修改fstab,讓home目錄指向新的位置

[root@ntl_freebsd01_for_ncku ~]# cat /etc/fstab
# Device    Mountpoint  FStype  Options Dump    Pass#
/dev/da0p2  /       ufs rw,userquota    1   1
#/dev/da0p3    /home       ufs rw,userquota    2   2
/dev/da1p2  /home       ufs rw,userquota    2   2
#/dev/da0p4    none        swap    sw  0   0
/dev/da1p1  none        swap    sw  0   0

 

 vii.刪除da0p3,p4兩個磁區

[root@ntl_freebsd01_for_ncku /home]# gpart delete -i 3 da0
da0p3 deleted
[root@ntl_freebsd01_for_ncku /home]# gpart delete -i 4
da0p4 deleted
[root@ntl_freebsd01_for_ncku /home]# gpshow
=>      34  83886013  da0  GPT  (40G)
        34       128    1  freebsd-boot  (64K)
       162  20971392    2  freebsd-ufs  (10G)
  20971554  62914493       - free -  (30G)

=>       34  125829053  da1  GPT  (60G)
         34   20971520    1  freebsd-swap  (10G)
   20971554   41943040    2  freebsd-ufs  (20G)
   62914594   62914493       - free -  (30G)

 

 

viii.擴充da0p2磁區

[root@ntl_freebsd01_for_ncku /home]# gpart resize -i 2 -a 4k -s 39G
da0p2 resized
[root@ntl_freebsd01show
=>      34  83886013  da0  GPT  (40G)
        34       128    1  freebsd-boot  (64K)
       162  81788926    2  freebsd-ufs  (39G)
  81789088   2096959       - free -  (1.0G)

=>       34  125829053  da1  GPT  (60G)
         34   20971520    1  freebsd-swap  (10G)
   20971554   41943040    2  freebsd-ufs  (20G)
   62914594   62914493       - free -  (30G)

 

-a 4k是要讓磁區對齊的意思,這裡部長滿40G的緣故是因為雖然vii步驟有30G,但事實上只是接近而已,所以通常還要小一點

除非你是指定磁區數量,就可以完全的擴充滿

 

ix.讓分割區長滿da0p2磁區

注意,這個動作最好在單人模式下做,我在正常模式下做系統會不回應..也不會成功

[root@ntl_freebsd01_for_ncku /home]# growfs /dev/da0p2
Device is mounted read-write; resizing will result in temporary write suspension for /.
It's strongly recommended to make a backup before growing the file system.
OK to grow file system on /dev/ada0p2, mounted on /, from 10GB to 39GB? [Yes/No] Yes
super-block backups (for fsck -b #) at:
..下略...

 

最後重開機之後,下df -h就會看到擴充成功啦!

[root@ntl_freebsd01_for_ncku ~]# df -h
Filesystem    Size    Used   Avail Capacity  Mounted on
/dev/da0p2     38G    9.7G     25G    28%    /
devfs         1.0K    1.0K      0B   100%    /dev
/dev/da1p2     19G     40M     18G     0%    /home

 

 

thx for:

http://www.wonkity.com/~wblock/docs/html/disksetup.html

https://www.freebsd.org/doc/handbook/disks-growing.html

http://www.unibia.com/unibianet/freebsd/resize-your-existing-freebsd-root-partitionslice-safely-without-re-installing

Leave a comment

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