深入淺出話Linux分區(qū)管理
譯文
【51CTO.com快譯】創(chuàng)建Linux分區(qū)是新Linux用戶最棘手的任務之一。我在本文中將探討有關Linux分區(qū)的各個主題,將使用不同的Linux分區(qū)管理器來列出、創(chuàng)建、調整、擴展和刪除Linux分區(qū)。
Linux分區(qū)管理器
我們在本文中將使用各種Linux分區(qū)管理器對系統(tǒng)存儲驅動器執(zhí)行CRUD(創(chuàng)建或克隆、讀取、刪除和更新)等操作。如果你是新的Linux用戶,可能想嘗試一種易于使用的基于GUI的工具。如果你喜歡使用命令行(CLI),我們還會介紹4款基于CLI的分區(qū)管理器。
1. 基于GUI的工具
(1)Gparted
1)列出分區(qū)
2) 創(chuàng)建分區(qū)
3) 調整分區(qū)大小
4) 刪除分區(qū)
2.基于CLI的工具
(1)cfdisk
1)列出分區(qū)
2)創(chuàng)建分區(qū)
3)調整分區(qū)大小
4)刪除分區(qū)
(2)fdisk
1)列出分區(qū)
2)創(chuàng)建分區(qū)
3) 刪除分區(qū)
列出Linux中的驅動器和分區(qū)
1. Gparted列出分區(qū)
Gparted是最流行最強大的Linux分區(qū)管理器。你可以對存儲驅動器輕松執(zhí)行CRUD。
在Gparted中列出分區(qū)只需打開應用程序本身。加載所有系統(tǒng)驅動器和分區(qū)需要幾秒鐘。
圖1
2. cfdisk列出分區(qū)
cfdisk是一款極易使用的分區(qū)管理工具。它基于cli,但提供了易于使用的選項,可以對系統(tǒng)驅動器執(zhí)行操作。
- sudo cfdisk
只要打開該應用程序,它會列出當前存儲驅動器的所有分區(qū)。
- 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列出分區(qū)
想用fdisk列出分區(qū),使用fdisk時使用‘l’參數(shù)。
- 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.
創(chuàng)建Linux分區(qū)
1. Gparted創(chuàng)建新分區(qū)
在Linux中用Gparted創(chuàng)建分區(qū)極容易極快捷。只要選擇想要創(chuàng)建分區(qū)的驅動器,鼠標右擊,點擊“新建”。確保未掛載驅動器,否則“新建”選項呈灰色禁用。
輸入新分區(qū)大小,點擊“添加”按鈕。
圖2
點擊“應用”,開始創(chuàng)建分區(qū)。
2. cfdisk創(chuàng)建分區(qū)
想使用cfdisk創(chuàng)建新分區(qū),以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
選擇你想創(chuàng)建分區(qū)的閑置空間。選擇“新建”,按回車鍵。
- 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.
輸入分區(qū)大小,按回車鍵。默認情況下,它會創(chuàng)建全部閑置空間大小的分區(qū)。在按回車鍵之前暫停,看看你是否真想創(chuàng)建全部閑置空間大小的分區(qū)。
- 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 ]
一切就緒?,F(xiàn)在選擇“寫入”,按回車鍵。它會讓你輸入“Yes”,按回車鍵。這是最后一次確認;出現(xiàn)該提示之后,cfdisk會創(chuàng)建分區(qū)。
- ┌────────────────────────────────────────────────────┐
- │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.
就這樣。分區(qū)已創(chuàng)建完畢!
- ┌─────────────────────────────────────────────────────┐
- │Partition type: Linux (83)
- └─────────────────────────────────────────────────────┘
- [Bootable] [ Delete ] [ Resize ] [ Quit ] [ Type ] [ Help ] [ Write ] [ Dump ]
- The partition table has been altered.
3. fdisk創(chuàng)建分區(qū)
想使用fdisk創(chuàng)建Linux分區(qū),先列出分區(qū),記下你想創(chuàng)建分區(qū)的設備路徑。
- 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創(chuàng)建一個新分區(qū)。然后使用fdisk命令選擇分區(qū)。
- 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):
想創(chuàng)建新分區(qū),使用‘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):
選擇分區(qū)類型。想創(chuàng)建主分區(qū),使用‘p’命令,使用‘e’代表擴展類型。
- Select (default p): p
- Partition number (1-4, default 1):
下一步,它會要求提供分區(qū)號。使用默認值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):
然后選擇新分區(qū)的大小。第一個扇區(qū)選擇默認值,最后一個扇區(qū)輸入以M為單位的值。比如說,想創(chuàng)建一個2GB大小的分區(qū),輸入+2000M。
最后,它會顯示消息如下:'Created a new partition 1 of type 'Linux' and of size 2 GiB. '
但分區(qū)仍未創(chuàng)建。想把變更寫入到磁盤,使用‘w’命令。它會顯示下列消息:"The kernel still uses the old partitions. The new table will be used at the next reboot. Syncing disks."。
若要讓新分區(qū)出現(xiàn),需要重啟系統(tǒng)。
調整Linux分區(qū)大小
1. Gparted調整分區(qū)大小
Gparted中調整分區(qū)大小很簡單。打開該應用程序,選擇想要調整大小的分區(qū),右擊鼠標,選擇“調整大小/移動”。
圖3
輸入新分區(qū)大小,點擊“調整大小/移動”按鈕。最后,點擊“應用”按鈕,使剛完成的變更生效。
2. cfdisk調整分區(qū)大小
以sudo權限開啟cfdisk。
- sudo cfdisk
選擇想要調整大小的分區(qū)。選擇“調整大小”選項,按回車鍵。
- 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
輸入新分區(qū)大小。
- ┌─────────────────────────────────────────────────────┐
- │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分區(qū)
1. Gparted刪除分區(qū)
想在Linux中使用GParted刪除分區(qū),只要選擇分區(qū),右擊鼠標,選擇刪除選項,即可刪除分區(qū)。
圖4
別忘了刪除分區(qū)后使變更生效。
2. cfdisk刪除分區(qū)
想使用cfdisk刪除分區(qū),以sudo權限開啟cfdisk。
- sudo cfdisk
高亮選中想刪除的分區(qū),選擇“刪除”選項。
- 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刪除分區(qū)
想使用fdisk刪除分區(qū),先運行fdisk,選擇想要刪除的分區(qū)。
- 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’命令刪除所選擇的分區(qū)。
- Command (m for help): d
刪除后,你可以使用‘n’命令創(chuàng)建一個新分區(qū),最后輸入‘w’命令,將所有變更寫入到磁盤。
結束語
以上就是管理Linux分區(qū)的方法。本文介紹了在Linux中使用三大分區(qū)管理工具,列出Linux分區(qū)、創(chuàng)建Linux分區(qū)、調整分區(qū)大小以及刪除分區(qū)。
原文標題:Linux Partition Management
【51CTO譯稿,合作站點轉載請注明原文譯者和出處為51CTO.com】