fdisk创建MBR分区,sgdisk创建GPT分区
gdisk软件包中包含sgdisk命令。 需要事先安装gdisk
Debian/Ubuntu: apt-get install gdisk RedHat/CentOS: yum install gdisk OpenSuSE: yast install gdis
1、新建分区
命令: sgdisk --help | grep new
帮助: -n, --new=partnum:start:end create new partition
-n 创建一个分区, -n后的参数分别是: 分区号:起始地址:终止地址
分区号为0:代表使用第一个可用的分区号;
起始地址和终止地址为0:0代表第一个可用地址和最后一个可用地址;
起始地址和终止地址可以为+/-xxx:代表偏移量,+代表在起始地址后的xxx地址,-代表在终止地址前的xxx地址;
①一块盘全部用于创建一个分区:
sgdisk -n 0:0:0 /dev/sdb
查看: lsblk | grep sdb
②创建一个从默认起始地址开始的10G的分区
sgdisk -n 2:0:+10G /dev/sdb
lsblk | grep sdb
③创建分区2,扇区从2048到10485760,type code为8300。
sgdisk -n 2:2048:10485760 -t 2:8300 -p /dev/sdb
例如一块盘创建4个分区
#sgdisk -n 1:0:+5 /dev/sdb Creating new GPT entries. The operation has completed successfully. #sgdisk -n 2:0:+5 /dev/sdb The operation has completed successfully. #sgdisk -n 3:0:+5 /dev/sdb The operation has completed successfully. #sgdisk -n 4:0:0 /dev/sdb The operation has completed successfully. #lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 50G 0 disk ├─sda1 8:1 0 1G 0 part /boot └─sda2 8:2 0 49G 0 part ├─centos-root 253:0 0 44.2G 0 lvm / └─centos-swap 253:1 0 4.8G 0 lvm [SWAP] sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 2.5K 0 part ├─sdb2 8:18 0 2.5K 0 part ├─sdb3 8:19 0 2.5K 0 part └─sdb4 8:20 0 2.5K 0 part
2、查看分区详情
命令:sgdisk --help | grep info
帮助:-i, --info=partnum show detailed information on partition
-i 显示某个分区详情
①查看第一分区详情
sgdisk -i 1 /dev/sdb
sgdisk --info=1 /dev/sdb
②查看所有GPT分区
sgdisk -p /dev/sdb
3、修改分区的 type code
命令:sgdisk --help | grep typecode
帮助:-t, --typecode=partnum:{hexcode|GUID} change partition type code
-t 修改某个分区的type code
①指定第一分区的type code
命令:sgdisk -t 1:0FC63DAF-8483-4772-8E79-3D69D8477DE4 /dev/sdb
查看:sgdisk -i 1 /dev/sdb
4、修改分区名
命令:sgdisk --help | grep change-name
帮助:-c, --change-name=partnum:name change partition's name
-c 修改某个分区的分区名
①指定第一分区的分区名
sgdisk -c 1:"test data" /dev/sdb
查看:sgdisk -i 1 /dev/sdb
实例如下
# sgdisk -n 0:0:+5G -t 0:ef02 -c 0:grub /dev/sdb Creating new GPT entries. Setting name! partNum is 0 REALLY setting name! The operation has completed successfully. # sgdisk -n 0:0:+5G -t 0:ea00 -c 0:boot /dev/sdb # sgdisk -n 0:0:+2G -t 0:8200 -c 0:swap /dev/sdb # sgdisk -n 0:0:0 -t 0:8300 -c 0:root /dev/sdb #lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sdb 8:16 0 20G 0 disk ├─sdb1 8:17 0 5G 0 part ├─sdb2 8:18 0 5G 0 part ├─sdb3 8:19 0 2G 0 part └─sdb4 8:20 0 8G 0 part #sgdisk -p /dev/sdb Disk /dev/sdb: 41943040 sectors, 20.0 GiB ...... Total free space is 2014 sectors (1007.0 KiB) Number Start (sector) End (sector) Size Code Name 1 2048 10487807 5.0 GiB EF02 grub 2 10487808 20973567 5.0 GiB EA00 boot 3 20973568 25167871 2.0 GiB 8200 swap 4 25167872 41943006 8.0 GiB 8300 root
5、清除分区数据
命令:sgdisk --help | grep zap
帮助:-z, --zap zap (destroy) GPT (but not MBR) data structures
①清除第一分区
sgdisk -z /dev/sdb1
6、删除分区
命令:sgdisk --help | grep delete
帮助:-d, --delete=partnum delete a partition
-d 删除一个分区
①删除第一分区
sgdisk --delete=1 /dev/sdb sgdisk -d 1 /dev/sdb lsblk | grep sdb
②删除所有分区
sgdisk --zap-all --clear --mbrtogpt /dev/sdb sgdisk --zap-all /dev/sdb
7、生产环境中,一般会用sgdisk将磁盘的分区方案写入文件,备份存储。
sgdisk --backup=/root/sda.partitiontable /dev/sda
sgdisk --backup=/root/sdb.partitiontable /dev/sdb
如果发生故障,可以使用sgdisk命令的--load-backup选项还原分区表。
从/dev/sda复制分区方案到/dev/sdb。
sgdisk -R /dev/sdb /dev/sda
新的硬盘上的GUID随机化,以确保它们是唯一的。
sgdisk -G /dev/sdb
检查两个硬盘驱动器现在是否具有相同的分区。
sgdisk -p /dev/sda sgdisk -p /dev/sdb
8、列出分区类型代码
#sgdisk --list-types 0700 Microsoft basic data 0c01 Microsoft reserved 2700 Windows RE 3000 ONIE boot 3001 ONIE config 4100 PowerPC PReP boot 4200 Windows LDM data 4201 Windows LDM metadata 7501 IBM GPFS 7f00 ChromeOS kernel 7f01 ChromeOS root 7f02 ChromeOS reserved 8200 Linux swap 8300 Linux filesystem 8301 Linux reserved 8302 Linux /home 8400 Intel Rapid Start 8e00 Linux LVM a500 FreeBSD disklabel a501 FreeBSD boot a502 FreeBSD swap a503 FreeBSD UFS a504 FreeBSD ZFS a505 FreeBSD Vinum/RAID a580 Midnight BSD data a581 Midnight BSD boot a582 Midnight BSD swap a583 Midnight BSD UFS a584 Midnight BSD ZFS a585 Midnight BSD Vinum a800 Apple UFS a901 NetBSD swap a902 NetBSD FFS a903 NetBSD LFS a904 NetBSD concatenated a905 NetBSD encrypted a906 NetBSD RAID ab00 Apple boot af00 Apple HFS/HFS+ af01 Apple RAID af02 Apple RAID offline af03 Apple label af04 AppleTV recovery af05 Apple Core Storage be00 Solaris boot bf00 Solaris root bf01 Solaris /usr & Mac Z bf02 Solaris swap bf03 Solaris backup bf04 Solaris /var bf05 Solaris /home bf06 Solaris alternate se bf07 Solaris Reserved 1 bf08 Solaris Reserved 2 bf09 Solaris Reserved 3 bf0a Solaris Reserved 4 bf0b Solaris Reserved 5 c001 HP-UX data c002 HP-UX service ea00 Freedesktop $BOOT eb00 Haiku BFS ed00 Sony system partitio ed01 Lenovo system partit ef00 EFI System ef01 MBR partition scheme ef02 BIOS boot partition fb00 VMWare VMFS fb01 VMWare reserved fc00 VMWare kcore crash p fd00 Linux RAID
9、ceph中用sgdisk工具进行分区--------固定uuid
ceph通过uuid来区分你是osd分区还是journal分区之后,再决定数据放在哪里
ceph中创建journal分区和data分区:
sgdisk -n {part num}:0:+{part size}G {disk} -t {part num}:4fbd7e29-9d25-41b8-afd0-062c0ceff05d -c {part num}:”ceph data”
sgdisk -n {part num}:0:+{part size}G {disk} -t {part num}:45b0969e-9b03-4f30-b4c6-b4b80ceff106 -c {part num}:”ceph journal”
root@host1:~# sgdisk -n 1:0:+20G /dev/nvme0n1 -t 1:45b0969e-9b03-4f30-b4c6-b4b80ceff106 -c 1:"ceph journal" Creating new GPT entries. The operation has completed successfully. root@host1:~# sgdisk -n 2:0:+20G /dev/nvme0n1 -t 2:45b0969e-9b03-4f30-b4c6-b4b80ceff106 -c 2:"ceph journal" The operation has completed successfully. root@host1:~# sgdisk -n 3:0:+150G /dev/nvme0n1 -t 3:4fbd7e29-9d25-41b8-afd0-062c0ceff05d -c 3:"ceph data" The operation has completed successfully. root@host1:~# sgdisk -n 4:0:+150G /dev/nvme0n1 -t 4:4fbd7e29-9d25-41b8-afd0-062c0ceff05d -c 4:"ceph data" The operation has completed successfully. root@host1:~# ceph-disk list /dev/nvme0n1 : /dev/nvme0n1p1 ceph journal /dev/nvme0n1p2 ceph journal /dev/nvme0n1p3 ceph data, unprepared /dev/nvme0n1p4 ceph data, unprepared
10、格式化、挂载
# partprobe
将磁盘分区表变化信息通知内核,请求操作系统重新加载分区表
格式化
mkfs.xfs /dev/sdb1 mkfs.xfs /dev/sdc1 #SDBUUID=$(blkid | grep -E '/dev/sdb1'| awk '{print $2}' |awk -F""" '{print $2}') #echo $SDBUUID 5ed06824-e1da-475f-9a15-9a89bc5fed30 #SDCUUID=$(blkid | grep -E '/dev/sdc1'| awk '{print $2}' |awk -F""" '{print $2}')
使用UUID挂载
echo -e "${SDBUUID} /mnt/data xfs defaults 0 0" >> /etc/fstab echo -e "${SDCUUID} /mnt/ssd xfs defaults 0 0" >> /etc/fstab