深入淺出話Linux分區管理
譯文
【51CTO.com快譯】創建Linux分區是新Linux用戶最棘手的任務之一。我在本文中將探討有關Linux分區的各個主題,將使用不同的Linux分區管理器來列出、創建、調整、擴展和刪除Linux分區。
Linux分區管理器
我們在本文中將使用各種Linux分區管理器對系統存儲驅動器執行CRUD(創建或克隆、讀取、刪除和更新)等操作。如果你是新的Linux用戶,可能想嘗試一種易于使用的基于GUI的工具。如果你喜歡使用命令行(CLI),我們還會介紹4款基于CLI的分區管理器。
1. 基于GUI的工具
(1)Gparted
1)列出分區
2) 創建分區
3) 調整分區大小
4) 刪除分區
2.基于CLI的工具
(1)cfdisk
1)列出分區
2)創建分區
3)調整分區大小
4)刪除分區
(2)fdisk
1)列出分區
2)創建分區
3) 刪除分區
列出Linux中的驅動器和分區
1. Gparted列出分區
Gparted是最流行最強大的Linux分區管理器。你可以對存儲驅動器輕松執行CRUD。
在Gparted中列出分區只需打開應用程序本身。加載所有系統驅動器和分區需要幾秒鐘。
圖1
2. cfdisk列出分區
cfdisk是一款極易使用的分區管理工具。它基于cli,但提供了易于使用的選項,可以對系統驅動器執行操作。
- sudo cfdisk
只要打開該應用程序,它會列出當前存儲驅動器的所有分區。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- >> /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux
- ┌───────────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- │Attributes: 80
- │Filesystem UUID: 77518fa2-4624-430d-8965-abf7b328165d
- │Filesystem: ext4
- │Mountpoint: / (mounted)
- └───────────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- Quit program without writing changes
3. fdisk列出分區
想用fdisk列出分區,使用fdisk時使用‘l’參數。
- sudo fdisk -l
- sandy@LinuxAndPeppermint ~ $ sudo fdisk -l
- Disk /dev/sda: 30.1 GiB, 32365232128 bytes, 63213344 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: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux
- /dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- /dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- Partition table entries are not in disk order.
創建Linux分區
1. Gparted創建新分區
在Linux中用Gparted創建分區極容易極快捷。只要選擇想要創建分區的驅動器,鼠標右擊,點擊“新建”。確保未掛載驅動器,否則“新建”選項呈灰色禁用。
輸入新分區大小,點擊“添加”按鈕。
圖2
點擊“應用”,開始創建分區。
2. cfdisk創建分區
想使用cfdisk創建新分區,以sudo權限運行cfdisk。
- sudo cfdisk
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> Free space 50782208 63213343 12431136 5.9G
- [ New ] [ Quit ] [ Help ] [ Write ] [ Dump ]
- Create new partition from free space
選擇你想創建分區的閑置空間。選擇“新建”,按回車鍵。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> Free space 50782208 63213343 12431136 5.9G
- Partition size: 5.9G
- May be followed by M for MiB, G for GiB, T for TiB, or S for sectors.
輸入分區大小,按回車鍵。默認情況下,它會創建全部閑置空間大小的分區。在按回車鍵之前暫停,看看你是否真想創建全部閑置空間大小的分區。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux
- ┌──────────────────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- └──────────────────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
一切就緒。現在選擇“寫入”,按回車鍵。它會讓你輸入“Yes”,按回車鍵。這是最后一次確認;出現該提示之后,cfdisk會創建分區。
- ┌────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- └────────────────────────────────────────────────────┘
- Are you sure you want to write the partition table to disk? yes_
- Type "yes" or "no", or press ESC to leave this dialog.
就這樣。分區已創建完畢!
- ┌─────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- └─────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- The partition table has been altered.
3. fdisk創建分區
想使用fdisk創建Linux分區,先列出分區,記下你想創建分區的設備路徑。
- sudo fdisk -l
- sandy@LinuxAndPeppermint ~ $ sudo fdisk -l
- Disk /dev/sda: 30.1 GiB, 32365232128 bytes, 63213344 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: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- /dev/sda3 50782208 63213343 12431136 5.9G 83 Linux
- /dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- /dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- Partition table entries are not in disk order.
我想從/dev/sda3創建一個新分區。然后使用fdisk命令選擇分區。
- sandy@LinuxAndPeppermint ~ $ sudo fdisk /dev/sda3
- Welcome to fdisk (util-linux 2.31.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
- Device does not contain a recognized partition table.
- Created a new DOS disklabel with disk identifier 0x64d7e7ab.
- Command (m for help):
想創建新分區,使用‘n’命令。
- Command (m for help): n
- Partition type
- p primary (0 primary, 0 extended, 4 free)
- e extended (container for logical partitions)
- Select (default p):
選擇分區類型。想創建主分區,使用‘p’命令,使用‘e’代表擴展類型。
- Select (default p): p
- Partition number (1-4, default 1):
下一步,它會要求提供分區號。使用默認值1。按回車鍵。
- First sector (2048-12431135, default 2048): 2048
- Last sector, +sectors or +size{K,M,G,T,P} (2048-12431135, default 12431135): +2000M
- Created a new partition 1 of type 'Linux' and of size 2 GiB.
- Command (m for help):
然后選擇新分區的大小。第一個扇區選擇默認值,最后一個扇區輸入以M為單位的值。比如說,想創建一個2GB大小的分區,輸入+2000M。
最后,它會顯示消息如下:'Created a new partition 1 of type 'Linux' and of size 2 GiB. '
但分區仍未創建。想把變更寫入到磁盤,使用‘w’命令。它會顯示下列消息:"The kernel still uses the old partitions. The new table will be used at the next reboot. Syncing disks."。
若要讓新分區出現,需要重啟系統。
調整Linux分區大小
1. Gparted調整分區大小
Gparted中調整分區大小很簡單。打開該應用程序,選擇想要調整大小的分區,右擊鼠標,選擇“調整大小/移動”。
圖3
輸入新分區大小,點擊“調整大小/移動”按鈕。最后,點擊“應用”按鈕,使剛完成的變更生效。
2. cfdisk調整分區大小
以sudo權限開啟cfdisk。
- sudo cfdisk
選擇想要調整大小的分區。選擇“調整大小”選項,按回車鍵。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux
- ┌──────────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763
- │Filesystem: ext4
- └──────────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- Reduce or enlarge the current partition
輸入新分區大小。
- ┌─────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763
- │ Filesystem: ext4
- └─────────────────────────────────────────────────────┘
- New size: 5.9G_
- May be followed by M for MiB, G for GiB, T for TiB, or S for sectors.
最后,選擇寫入選項,將變更寫入到磁盤。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> /dev/sda3 50782208 57073663 6291456 3G 83 Linux
- Free space 57073664 63213343 6139680 2.9G
- ┌───────────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763
- │Filesystem: ext4
- └───────────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- Write partition table to disk (this might destroy data)
刪除Linux分區
1. Gparted刪除分區
想在Linux中使用GParted刪除分區,只要選擇分區,右擊鼠標,選擇刪除選項,即可刪除分區。
圖4
別忘了刪除分區后使變更生效。
2. cfdisk刪除分區
想使用cfdisk刪除分區,以sudo權限開啟cfdisk。
- sudo cfdisk
高亮選中想刪除的分區,選擇“刪除”選項。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> /dev/sda3 50782208 63211519 12429312 5.9G 83 Linux
- ┌──────────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763
- │Filesystem: ext4
- └──────────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- Delete the current partition
最后,選擇“寫入”選項,將變更寫入到磁盤。
- Disk: /dev/sda
- Size: 30.1 GiB, 32365232128 bytes, 63213344 sectors
- Label: dos, identifier: 0xa51a596a
- Device Boot Start End Sectors Size Id Type
- /dev/sda1 * 2048 19531775 19529728 9.3G 83 Linux
- /dev/sda2 19533822 50782207 31248386 14.9G 5 Extended
- ├─/dev/sda5 19533824 27344895 7811072 3.7G 82 Linux swap / Solaris
- └─/dev/sda6 27346944 50782207 23435264 11.2G 83 Linux
- >> Free space 50782208 63213343 12431136 5.9G
- ┌───────────────────────────────────────────────────────────┐
- │Filesystem UUID: 266f1f9d-5e6e-4d80-a973-4c991cf28763
- │Filesystem: ext4
- └───────────────────────────────────────────────────────────┘
- [ New ] [ Quit ] [ Help ] [ Write ] [ Dump ]
- Write partition table to disk (this might destroy data)
3. fdisk刪除分區
想使用fdisk刪除分區,先運行fdisk,選擇想要刪除的分區。
- sandy@LinuxAndPeppermint ~ $ sudo fdisk /dev/sda3
- Welcome to fdisk (util-linux 2.31.1).
- Changes will remain in memory only, until you decide to write them.
- Be careful before using the write command.
- Command (m for help):
然后使用‘d’命令刪除所選擇的分區。
- Command (m for help): d
刪除后,你可以使用‘n’命令創建一個新分區,最后輸入‘w’命令,將所有變更寫入到磁盤。
結束語
以上就是管理Linux分區的方法。本文介紹了在Linux中使用三大分區管理工具,列出Linux分區、創建Linux分區、調整分區大小以及刪除分區。
原文標題:Linux Partition Management
【51CTO譯稿,合作站點轉載請注明原文譯者和出處為51CTO.com】