[ubuntu] 透過fdisk擴展你的vm硬碟空間

[ubuntu] 透過fdisk擴展你的vm硬碟空間

因為玩了很多docker的套件,像是hadoop的image就佔了1.7g,當初開的10g硬碟空間已不夠用了,雖然之前在windows的經驗都有一些好用的gui軟體可以做repartition,但是在linux上,我沒有經驗,因此,這次來試著手動擴展vm的硬碟空間吧!~

首先,你要先關閉vm,然後在vm的設定那邊直接加大硬碟空間,但是此舉並不會直接反應到系統內,因為系統一個Partition Table,記錄著哪些硬碟有哪些磁區,他的開始跟結尾的位置在哪,例如我給20g的空間,他的空間長度就是41943039,我們可以在這個長度內分配我們的磁區來達到合適的配置

先來,查看本機磁碟使用狀況,目前還有約18%的空間,我試了很多方法後,再清到剩下這樣,但我認為這樣還遠遠不夠,隨時還是會有爆掉的尾機:

df -h

Filesystem 	Size 		Used 	Avail Use% 	Mounted on
udev 		974M 		0 		974M 	0% 	/dev
tmpfs 		199M 		6.1M 	193M 	4% 	/run
/dev/sda1 	8.8G 		6.8G 	1.6G 	82% /
tmpfs 		992M 		0 		992M 	0% 	/dev/shm
tmpfs 		5.0M 		0 		5.0M 	0% 	/run/lock
tmpfs 		992M 		0 		992M 	0% 	/sys/fs/cgroup
tmpfs 		199M 		0 		199M 	0%	/run/user/1000

接著切換到root帳號

sudo -i

然後啟動fdisk,我們選擇的是作為系統糟的sda

fdisk /dev/sda

畫面會如下呈現

Welcome to fdisk (util-linux 2.27.1).

首先先輸入p,來顯示partition表,這邊就是記錄著每個磁區所使用的空間與類型

Command (m for help): p

Device 		Boot 	Start 		End 		Sectors 	Size 	Id Type
/dev/sda1 	* 		2048 		18874367 	18872320 	9G 		83 Linux
/dev/sda2 			18876414 	20969471 	2093058 	1022M 	5 Extended
/dev/sda5 			18876416 	20969471 	2093056 	1022M 	82 Linux swap / Solaris

先刪除第1個磁區

Command (m for help): d

Partition number (1,2,5, default 5): 1

Partition 1 has been deleted.

Command (m for help): d
Partition number (2,5, default 5): 2

Partition 2 has been deleted.

重新查看目前的partition狀況,空無一物

Command (m for help): p

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xc111612d

new一個partition磁區

Command (m for help): n

Partition type
p primary (0 primary, 0 extended, 4 free)
e extended (container for logical partitions)

建立primary磁區

Select (default p): p

Partition number (1-4, default 1):
First sector (2048-41943039, default 2048): 2048
Last sector, +sectors or +size{K,M,G,T,P} (2048-41943039, default 41943039): 334 10751
Created a new partition 1 of type ‘Linux’ and of size 16 GiB.

再new一個partition磁區

Command (m for help): n

Partition type
p primary (1 primary, 0 extended, 3 free)
e extended (container for logical partitions)

一樣是primary磁區

Select (default p): p

Partition number (2-4, default 2):
First sector (33410752-41943039, default 33411072):
Last sector, +sectors or +size{K,M,G,T,P} (33411072-41943039, default 41943039):

Created a new partition 2 of type 'Linux' and of size 4.1 GiB.

重新show partition table

Command (m for help): p

Disk /dev/sda: 20 GiB, 21474836480 bytes, 41943040 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
Disklabel type: dos
Disk identifier: 0xc111612d

Device Boot Start End Sectors Size Id Type
/dev/sda1 2048 33410751 33408704 16G 83 Linux
/dev/sda2 33411072 41943039 8531968 4.1G 83 Linux

更換磁區的type

Command (m for help): t

Partition number (1,2, default 2): 2
Partition type (type L to list all types): 82

Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’.

寫入partition

Command (m for help): w

The partition table has been altered.
Calling ioctl() to re-read partition table.
Re-reading the partition table failed.: Device or resource busy

The kernel still uses the old table. The new table will be used at the next rebo ot or after you run partprobe(8) or kpartx(8).

重新啟動

shutdown -r now

最後一步,重新resize filesystem資訊

resize2fs /dev/sda1

看起來大功告成!!

df -h

Filesystem      Size  Used Avail Use% Mounted on
udev            974M     0  974M   0% /dev
tmpfs           199M  6.0M  193M   4% /run
/dev/sda1        16G  6.7G  8.2G  46% /
tmpfs           992M     0  992M   0% /dev/shm
tmpfs           5.0M     0  5.0M   0% /run/lock
tmpfs           992M     0  992M   0% /sys/fs/cgroup
tmpfs           199M     0  199M   0% /run/user/1000

發佈留言

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

WP Facebook Auto Publish Powered By : XYZScripts.com