fdisk 常用的磁碟分區工具,受mbr分區表的限制,只能給小於2TB的磁碟劃分分區,如果使用fdisk對大於2TB的磁碟進行分區,雖然可以分區,但只能識別2T的空間,一般使用parted分區工具 -l # 顯示所有磁碟分區信息 Device # 分區名稱 Boot # 啟動分區,用*表示的是啟動分 ...
fdisk
常用的磁碟分區工具,受mbr分區表的限制,只能給小於2TB的磁碟劃分分區,如果使用fdisk對大於2TB的磁碟進行分區,雖然可以分區,但只能識別2T的空間,一般使用parted分區工具
-l # 顯示所有磁碟分區信息
[root@puppet105 ~]# fdisk -l Disk /dev/sda: 64.4 GB, 64424509440 bytes, 125829120 sectors # 磁碟/dev/sda 的大小 Units = sectors of 1 * 512 = 512 bytes # 每個柱面大小512位元組 Sector size (logical/physical): 512 bytes / 512 bytes # 每個扇區的位元組數 I/O size (minimum/optimal): 512 bytes / 512 bytes # 每次讀寫的位元組數 Disk label type: dos Disk identifier: 0x000a9f20
Device Boot Start End Blocks Id System /dev/sda1 * 2048 2099199 1048576 83 Linux /dev/sda2 2099200 4196351 1048576 82 Linux swap / Solaris /dev/sda3 4196352 125829119 60816384 8e Linux LVM Disk /dev/mapper/centos_centos7-root: 62.3 GB, 62272831488 bytes, 121626624 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
Device # 分區名稱
Boot # 啟動分區,用*表示的是啟動分區
Start # 表示開始的柱面
End # 表示結束的柱面
Blocks # block塊數量
Id # 分區類型ID
System # 分區類型
磁碟分區步驟
# 加了一塊20G的磁碟 [root@centos7 ~]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 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 # 查看分區前設備狀態 [root@centos7 ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb # 開始分區 [root@centos7 ~]# fdisk /dev/sdb # 後面直接跟設備文件,或者-cu參數
Welcome to fdisk (util-linux 2.23.2). 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 Building a new DOS disklabel with disk identifier 0xfe05c2f7. Command (m for help): m # 輸入m是幫助菜單 Command action a toggle a bootable flag # 設置引導扇區 b edit bsd disklabel # 編輯bsd捲標 c toggle the dos compatibility flag # 設置dos相容扇區 d delete a partition # 刪除一個分區 g create a new empty GPT partition table # 創建空白gpt分區表 G create an IRIX (SGI) partition table # 創建IRIX分區表 l list known partition types # 查看分區類型對應列表編號 m print this menu # 列印幫助菜單 n add a new partition # 添加一個新分區 o create a new empty DOS partition table # 創建新的DOS分區表 p print the partition table # 列印分區表 q quit without saving changes # 不保存退出 s create a new empty Sun disklabel # 創建新的空sun捲標 t change a partition's system id # 更改分區系統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): n # 創建一個新分區 Partition type: p primary (0 primary, 0 extended, 4 free) # 主分區,只能創建4個,第五個必須為擴展分區 e extended # 擴展分區 Select (default p): p # 創建主分區標識 Partition number (1-4, default 1): 1 #編號(1-4) First sector (2048-41943039, default 2048): # 起始位置,直接回車,預設是1 Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +5G # 設置主分區大小 Partition 1 of type Linux and of size 5 GiB is set Command (m for help): n # 創建一個新分區 Partition type: p primary (1 primary, 0 extended, 3 free) e extended # 創建一個擴展分區 Select (default p): e Partition number (2-4, default 2): 2 # 編號2 First sector (10487808-41943039, default 10487808): Using default value 10487808 Last sector, +sectors or +size{K,M,G} (10487808-41943039, default 41943039): +5G # 也是5G的容量 Partition 2 of type Extended and of size 5 GiB is set Command (m for help): n # 創建一個新分區 Partition type: p primary (1 primary, 1 extended, 2 free) # 此時因為創建了擴展分區,所以只剩下主分區和邏輯分區了 l logical (numbered from 5) Select (default p): l # 創建邏輯分區 Adding logical partition 5 # 邏輯分區編號必須是從5開始,預設的就是5 First sector (10489856-20973567, default 10489856): Using default value 10489856 Last sector, +sectors or +size{K,M,G} (10489856-20973567, default 20973567): +2G # 給2G的容量 Partition 5 of type Linux and of size 2 GiB is set Command (m for help): p # 列印創建的分區列表 Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 5 Extended /dev/sdb5 10489856 14684159 2097152 83 Linux Command (m for help): w # 保存 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@centos7 ~]# ls /dev/sdb* # 查看設備狀態 /dev/sdb /dev/sdb1 /dev/sdb2 /dev/sdb5 [root@centos7 ~]# partprobe /dev/sdb # 通知內核分區表已經更改 [root@centos7 ~]# mkfs.ext4 /dev/sdb1 #格式化文件系統 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1342177280 40 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 Allocating group tables: done Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done [root@centos7 ~]# tune2fs -c 0 -i 0 /dev/sdb1 # 此命令可以米面磁碟過載自動檢查 tune2fs 1.42.9 (28-Dec-2013) Setting maximal mount count to 1 [root@centos7 ~]# df -h # 格式化後的磁碟需要掛載才能使用,此處未顯示已經創建的磁碟 Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 [root@centos7 ~]# mount /dev/sdb1 /mnt # 掛載磁碟分區 [root@centos7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 98G 1.7G 97G 2% / devtmpfs 901M 0 901M 0% /dev tmpfs 911M 0 911M 0% /dev/shm tmpfs 911M 9.5M 902M 2% /run tmpfs 911M 0 911M 0% /sys/fs/cgroup /dev/sda1 1014M 135M 880M 14% /boot tmpfs 183M 0 183M 0% /run/user/0 /dev/sdb1 4.8G 20M 4.6G 1% /mnt # 磁碟已經掛載上去了 [root@centos7 ~]# vi /etc/fstab # 開機啟動有兩種方式,此為第一種,另外一種 echo "mount /dev/sdb1 /mnt" >>/etc/rc.local # # /etc/fstab # Created by anaconda on Fri Oct 26 21:32:51 2018 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # UUID=fc662da3-4f5b-480b-bae9-cc9c8698742a / xfs defaults 0 0 UUID=e1b67cc9-08ff-47e7-b1e9-3ce2d9ee5eb6 /boot xfs defaults 0 0 UUID=47b26d55-1602-48e1-adba-91fe4f420b03 swap swap defaults 0 0 /dev/sdb1 /mnt ext4 defaults 0 0
# 關於LVM進行分區,會單獨寫博文
fdisk非互動式分區(批量創建)
[root@centos7 ~]# fdisk /dev/sdb<<EOF > n > p > 3 > > +2G > p > w > EOF Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): Partition type: p primary (1 primary, 1 extended, 2 free) l logical (numbered from 5) Select (default p): Partition number (3,4, default 3): First sector (20973568-41943039, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): Partition 3 of type Linux and of size 2 GiB is set Command (m for help): Disk /dev/sdb: 21.5 GB, 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 Disk label type: dos Disk identifier: 0xfe05c2f7 Device Boot Start End Blocks Id System /dev/sdb1 2048 10487807 5242880 83 Linux /dev/sdb2 10487808 20973567 5242880 5 Extended /dev/sdb3 20973568 25167871 2097152 83 Linux /dev/sdb5 10489856 14684159 2097152 83 Linux Command (m for help): 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 or after you run partprobe(8) or kpartx(8) Syncing disks.
partprobe
在硬碟分區發生改變時,更新Linux內核中的硬碟分區表數據。不需要重啟系統就能生效
tnue2fs
調整或者查看ex2/ex3/3x4文件系統的參數,如可以調整文件系統開機自檢周期,用的較少
-c # 設置強制自檢的掛載次數,沒掛載一次計數就會加1,超過次數就會強制自檢,設置0或-1,此功能關閉
-i # 強制自檢的時間間隔
-l # 查案文件系統信息
parted
大於2TB磁碟的分區命令,需要將磁碟轉換為GPT格式
-l # 小時需哦有磁碟分區的信息
-h # 查看幫助
[root@centos7 ~]# parted GNU Parted 3.1 # 版本號 Using /dev/sda Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) h # 查看幫助
align-check TYPE N #檢查磁碟分區,type分為min/opt,N表示磁碟分區號
mklabel,mktable LABEL-TYPE # 創建分區表 table) mkpart PART-TYPE [FS-TYPE] START END # 創建分區 name NUMBER NAME # 為分區命名 print [devices|free|list,all|NUMBER] # 顯示分區表信息,簡寫p quit # 退出 resizepart NUMBER END # 重設分區大小 rm NUMBER # 刪除編號number的分區 select DEVICE #選擇要編輯的設備 set NUMBER FLAG STATE # 改變分區標誌 toggle [NUMBER [FLAG]] # 設置分區標誌 unit UNIT # 設置預設單位 copyright information of GNU Parted (parted) quit
分區步驟,本次使用20GB來做演示
[root@centos7 ~]# ls /dev/sd* /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb [root@centos7 ~]# parted /dev/sdb # 開始分區,互動式 GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) mklabel gpt # 為sdb磁碟創建GPT分區表,大於2T必須執行此步驟,fdisk 是mdos分區表 (parted) mkpart primary 0 5G # 創建主分區 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? Ignore # 忽略掉 (parted) p #顯示分區表 Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 5000MB 5000MB primary #主分區創建完畢,創建完成之後系統會自動識別, (parted) mkpart logical 5G 10G # 創建邏輯分區 (parted) p Model: VMware, VMware Virtual S (scsi) Disk /dev/sdb: 10.7GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 5000MB 5000MB primary 2 5001MB 10.0GB 5000MB logical (parted) quit Information: You may need to update /etc/fstab. [root@centos7 ~]# ls /dev/sd* # 查看分區的設備 /dev/sda /dev/sda1 /dev/sda2 /dev/sda3 /dev/sdb /dev/sdb1 /dev/sdb2# fdisk非互動式分區(批量創建)
# 後面的格式化分區,掛載,開機自動掛載和fdisk分區步驟一樣,因為parted在創建分區時,會自動讓內核識別,所以不用使用partprobe命令
parted /dev/sdb mklabel gpt yes
parted /dev/sdb mkpart primary 0 5G
parted /dev/sdb p
mkfs
創建Linux文件系統,只有格式化創建文件系統後,才能存儲數據
-t #指定要創建的文件系統類型
-c # 創建文件系統時檢查磁碟壞塊
-v # 顯示詳細信息
[root@centos7 ~]# mkfs # 此命令是一個前端命令,會通過-t參數調用mkfs.fstype 因此可以直接使用mkfs.ext4創建
mkfs mkfs.cramfs mkfs.ext3 mkfs.minix
mkfs.btrfs mkfs.ext2 mkfs.ext4 mkfs.xfs
dumpe2fs
用於導出ext2/ext3/ext4文件系統內部的相關信息,如,文件系統的組成bao包含超級塊,塊組,inode,bock等信息
-b # 列印文件系統中預留的塊信息
-h # 進現實超級塊信息
-i # 從指定的文件系統影像文件中讀取文件的系統信息
-x # 以16進位格式列印信息塊成員
resize2fs
用於擴容或收縮未掛載的ext2,3,4文件系統,在2.6內核版本或者更高,還支持線上擴容已掛在的文件系統,常用開針對LVM擴容後的分區使用,對於fdisk和parted應事先規劃好
-p # 列印完成任務的進度條
-r # 強制執行操作