一般用於分大於2TB以上的硬盤
列出所有設備的分區表信息
# parted -l
列出/dev/sdb/分區表信息
# parted /dev/sdb print
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos //分區表類型
Disk Flags:
Number Start End Size Type File system 標志
1 1049kB 1075MB 1074MB primary xfs
2 1075MB 3222MB 2147MB primary
3 3222MB 6443MB 3221MB extended
5 3223MB 4297MB 1074MB logical
6 4298MB 5372MB 1074MB logical
分區編號 起始 結束 分區大小 分區類型 文件系統類型
-------------------------------------------------
一、分區
將之前實驗的分區刪掉
# fdisk /dev/sdb
歡迎使用 fdisk (util-linux 2.23.2)。
更改將停留在內存中,直到您決定將更改寫入磁盤。
使用寫入命令前請三思。
命令(輸入 m 獲取幫助):p
磁盤 /dev/sdb:10.7 GB, 10737418240 字節,20971520 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x2656627c
設備 Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 6293503 2097152 83 Linux
/dev/sdb3 6293504 12584959 3145728 5 Extended
/dev/sdb5 6295552 8392703 1048576 83 Linux
/dev/sdb6 8394752 10491903 1048576 83 Linux
命令(輸入 m 獲取幫助):d
分區號 (1-3,5,6,默認 6):1
分區 1 已刪除
命令(輸入 m 獲取幫助):d
分區號 (2,3,5,6,默認 6):2
分區 2 已刪除
命令(輸入 m 獲取幫助):d
分區號 (3,5,6,默認 6):3
分區 3 已刪除
命令(輸入 m 獲取幫助):p
磁盤 /dev/sdb:10.7 GB, 10737418240 字節,20971520 個扇區
Units = 扇區 of 1 * 512 = 512 bytes
扇區大小(邏輯/物理):512 字節 / 512 字節
I/O 大小(最小/最佳):512 字節 / 512 字節
磁盤標簽類型:dos
磁盤標識符:0x2656627c
設備 Boot Start End Blocks Id System
命令(輸入 m 獲取幫助):w
The partition table has been altered!
Calling ioctl() to re-read partition table.
正在同步磁盤。
# ll /dev/sd
sda sda1 sda2 sdb
通知內核重讀分區表
# partx -d /dev/sdb //刪除分區使用-d 添加分區使用-a
開始新的實驗
# parted /dev/sdb print //打印分區表
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 10.7GB
Sector size (logical/physical): 512B/512B
Partition Table: msdos
Disk Flags:
Number Start End Size Type File system 標志
# parted /dev/sdb //分區
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) help
help [COMMAND] //顯示命令的幫助信息
mklabel,mktable LABEL-TYPE //創建一個新的分區表類型
mkpart PART-TYPE [FS-TYPE] START END //創建一個新分區
print [devices|free|list,all|NUMBER] //打印分區表信息
quit //退出
rescue START END //找回分區
rm NUMBER //刪除分區
(parted) mklabel gpt //創建gpt類型分區表
警告: The existing disk label on /dev/sdb will be destroyed and all data on this
disk will be lost. Do you want to continue?
是/Yes/否/No? y
(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 標志
mkpart 分區類型 開始位置(大小) 結束位置(大小)
(parted) mkpart primary 1 1G
(parted) mkpart primary 1G 2G
(parted) mkpart primary 2G 3G
(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 標志
1 17.4kB 1000MB 1000MB primary
2 1000MB 2000MB 999MB primary
3 2000MB 3000MB 1000MB primary
(parted) rm 3 //刪除分區
(parted) q
信息: You may need to update /etc/fstab.
二、通知內核重讀分區表
查看分區文件是否存在
# ls /dev/sd
sda sda1 sda2 sdb sdb1 sdb2
如果不存在,通知內核重讀分區表
# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
檢驗分區是否被內核識別
1)查看分區表
# parted -l
2)看dev目錄下設備
# ls /dev/sd
sda sda1 sda2 sdb sdb1 sdb2
三、創建文件系統
# mkfs.xfs /dev/sdb1 //命令后接分區
四、掛載使用
1.創建掛載點
# mkdir /mnt/sdb1
2.將分區掛載到本地目錄
# mount /dev/sdb1 /mnt/sdb1
# df -hsdb1
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/cl-root 17G 3.5G 14G 21% /
devtmpfs 473M 0 473M 0% /dev
tmpfs 489M 144K 489M 1% /dev/shm
tmpfs 489M 7.0M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sda1 1014M 155M 860M 16% /boot
/dev/sr0 4.1G 4.1G 0 100% /mnt/test
tmpfs 98M 16K 98M 1% /run/user/0
/dev/sdb1 1014M 33M 982M 4% /mnt/sdb1
====================================================
mklabel gpt|msdos //會清空所有數據
p 打印分區表
mkpart 類型 起始位置 結束位置 創建分區
rm 分區編號 刪除分區
# parted -s /dev/sdb mklabel {gpt|msdos} //制作新的分區表 -s 不提示
# parted /dev/sdb p //打印分區表
# parted /dev/sdb mkpart primary 1 2G //創建分區
# parted /dev/sdb mkpart primary 2G 4G
# parted /dev/sdb mkpart primary '4G -1' //創建分區使用全部剩余空間
# parted /dev/sdb rm 3 //刪除分區
# parted /dev/sdb recure 1 2G //找回分區
實驗:刪除分區,通過recure找回分區和存放在分區內的數據 不是絕對能找回數據
1.在/dev/sdb1分區內創建一個文件hello.txt 並寫入內容 hello world
[root@localhost ~]# cd /mnt/sdb1
[root@localhost sdb1]# ls
[root@localhost sdb1]# echo "hello world" > hello.txt
[root@localhost sdb1]# cat hello.txt
hello world
[root@localhost sdb1]# cd ..
2.卸載/dev/sdb1分區並查找是否已經卸載
[root@localhost mnt]# umount /dev/sdb1
[root@localhost mnt]# df -h
文件系統 容量 已用 可用 已用% 掛載點
/dev/mapper/cl-root 17G 3.5G 14G 21% /
devtmpfs 473M 0 473M 0% /dev
tmpfs 489M 84K 489M 1% /dev/shm
tmpfs 489M 7.0M 482M 2% /run
tmpfs 489M 0 489M 0% /sys/fs/cgroup
/dev/sr0 4.1G 4.1G 0 100% /mnt/test
/dev/sda1 1014M 155M 860M 16% /boot
tmpfs 98M 16K 98M 1% /run/user/42
tmpfs 98M 0 98M 0% /run/user/0
3.刪除/dev/sdb1分區 並檢查是否刪除
[root@localhost mnt]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of
commands.
(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 標志
1 17.4kB 1000MB 1000MB xfs primary
2 1000MB 2000MB 999MB primary
(parted) rm 1
(parted) q
信息: You may need to update /etc/fstab.
[root@localhost mnt]# ll /dev/sd
sda sda1 sda2 sdb sdb2
4. 找回分區 前提需要知道分區表
[root@localhost mnt]# parted /dev/sdb
GNU Parted 3.1
使用 /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of
commands.
(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 標志
2 1000MB 2000MB 999MB primary
(parte(parted) rescue 0 1G
信息: A xfs primary partition was found at 17.4kB ->
1000MB. Do you want to add it to the partition
table?
是/Yes/否/No/放棄/Cancel? Y
(parted) q
信息: You may need to update /etc/fstab.
5.將找回的分區掛載 查找數據是否找回
[root@localhost mnt]# mount /dev/sdb1 /mnt/sdb1
[root@localhost mnt]# cd /mnt/sdb1
[root@localhost sdb1]# ls
hello.txt
[root@localhost sdb1]# cat hello.txt
hello world
新添加的硬盤直接使用parted是不能分區的
解決:parted /dev/sdc mklabel gpt //設置分區表類型