#介绍
parted命令为大于2T硬盘分区 一般来说我们对磁盘分区都是用fdisk命令,但是对于硬盘容量大于2T时,这个命令就不管用了,因为MBR分区表只支持2T磁盘;所以大于2T的磁盘必须使用GPT分区表,这时我们就需要用到parted命令。
#一、分区
shell
# parted /dev/sdb #使用parted来对GPT磁盘操作,进入交互式模式
(parted) mklabel gpt # 将MBR磁盘格式化为GPT
(parted) mkpart primary xfs 0% 100% 将所有容量分为一个主分区
(parted) p #打印当前分区
(parted) q #退出
# parted /dev/sdb #使用parted来对GPT磁盘操作,进入交互式模式
(parted) mklabel gpt # 将MBR磁盘格式化为GPT
(parted) mkpart primary xfs 0% 100% 将所有容量分为一个主分区
(parted) p #打印当前分区
(parted) q #退出
- 或者
shell
#创建分区
ansible ceph -m shell -a 'parted /dev/sde mklabel gpt'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 2048s 12%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 12% 24%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 24% 36%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 36% 48%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 48% 60%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 60% 72%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 72% 84%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 84% 100%'
#创建分区
ansible ceph -m shell -a 'parted /dev/sde mklabel gpt'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 2048s 12%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 12% 24%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 24% 36%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 36% 48%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 48% 60%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 60% 72%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 72% 84%'
ansible ceph -m shell -a 'parted /dev/sde mkpart primary 84% 100%'
#二、格式化
shell
# mkfs.xfs /dev/sdb1
# mkfs.xfs /dev/sdb1
1、查看uuid
shell
第一种:
[root@jenkins01 ~]# blkid
/dev/vda1: UUID="eb448abb-3012-4d8d-bcde-94434d586a31" TYPE="ext4"
/dev/vdb: UUID="5ab28fa6-21d7-47de-9cf2-f458f2dc4add" TYPE="xfs"
第二种:
[root@jenkins01 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 eb448abb-3012-4d8d-bcde-94434d586a31 /
vdb xfs 5ab28fa6-21d7-47de-9cf2-f458f2dc4add /data
第三种:
[root@localhost ~]# ll /dev/disk/by-uuid/
第一种:
[root@jenkins01 ~]# blkid
/dev/vda1: UUID="eb448abb-3012-4d8d-bcde-94434d586a31" TYPE="ext4"
/dev/vdb: UUID="5ab28fa6-21d7-47de-9cf2-f458f2dc4add" TYPE="xfs"
第二种:
[root@jenkins01 ~]# lsblk -f
NAME FSTYPE LABEL UUID MOUNTPOINT
vda
└─vda1 ext4 eb448abb-3012-4d8d-bcde-94434d586a31 /
vdb xfs 5ab28fa6-21d7-47de-9cf2-f458f2dc4add /data
第三种:
[root@localhost ~]# ll /dev/disk/by-uuid/
shell
#!/bin/bash
#2016-11-10
#Author
disk=$1
swap=$2
dir_path=/data
if [ $# -ne 2 ];then
echo "Usage:$0 Disk SwapSize"
else
echo "正在创建磁盘分区请稍等"
/sbin/fdisk $disk >&/dev/null << End
n
p
1
+$2
n
p
2
t
1
82
w
End
mkswap ${disk}1 >& /dev/null
sleep 3
swapon ${disk}1 >& /dev/null
echo -e "${disk}1 \t\tswap \t\t\t swap \t defaults \t 0 0" >>/etc/fstab
if [ ! -x "$dir_path" ];then
mkdir $dir_path
fi
mkfs -t ext4 ${disk}2 >& /dev/null
sleep 5
mount ${disk}2 /data >&/dev/null
echo -e "${disk}2 \t\t /data \t\t\t ext4 \tdefaults \t 0 0" >>/etc/fstab
echo "磁盘分区创建已完成!"
fi
#!/bin/bash
#2016-11-10
#Author
disk=$1
swap=$2
dir_path=/data
if [ $# -ne 2 ];then
echo "Usage:$0 Disk SwapSize"
else
echo "正在创建磁盘分区请稍等"
/sbin/fdisk $disk >&/dev/null << End
n
p
1
+$2
n
p
2
t
1
82
w
End
mkswap ${disk}1 >& /dev/null
sleep 3
swapon ${disk}1 >& /dev/null
echo -e "${disk}1 \t\tswap \t\t\t swap \t defaults \t 0 0" >>/etc/fstab
if [ ! -x "$dir_path" ];then
mkdir $dir_path
fi
mkfs -t ext4 ${disk}2 >& /dev/null
sleep 5
mount ${disk}2 /data >&/dev/null
echo -e "${disk}2 \t\t /data \t\t\t ext4 \tdefaults \t 0 0" >>/etc/fstab
echo "磁盘分区创建已完成!"
fi
2.分区扩容
1.首先查看一下当前分区状态
bash
root@localserver:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 394M 6.6M 387M 2% /run
/dev/mmcblk0p2 ext4 59G 20G 40G 33% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 2.0G 20K 2.0G 1% /tmp
/dev/mmcblk1p1 ext4 223M 79M 140M 37% /boot
/dev/sda1 ext4 125G 82G 37G 69% /root/wxedge
/dev/sda2 ext4 343G 28K 325G 1% /root/userdata0
/dev/zram1 ext4 49M 24M 22M 53% /var/log
tmpfs tmpfs 394M 8.0K 394M 1% /run/user/0
root@localserver:~# df -Th
Filesystem Type Size Used Avail Use% Mounted on
udev devtmpfs 1.8G 0 1.8G 0% /dev
tmpfs tmpfs 394M 6.6M 387M 2% /run
/dev/mmcblk0p2 ext4 59G 20G 40G 33% /
tmpfs tmpfs 2.0G 0 2.0G 0% /dev/shm
tmpfs tmpfs 5.0M 0 5.0M 0% /run/lock
tmpfs tmpfs 2.0G 20K 2.0G 1% /tmp
/dev/mmcblk1p1 ext4 223M 79M 140M 37% /boot
/dev/sda1 ext4 125G 82G 37G 69% /root/wxedge
/dev/sda2 ext4 343G 28K 325G 1% /root/userdata0
/dev/zram1 ext4 49M 24M 22M 53% /var/log
tmpfs tmpfs 394M 8.0K 394M 1% /run/user/0
在本次示例中,由于dev/sda1
分区容量较小;而/dev/sda2
业务较为不重要,因此想将/dev/sda2
的容量转移到/dev/sda1
中
2.停用欲扩容分区业务,卸载欲扩容分区
bash
umount /dev/sda1
umount /dev/sda2
umount /dev/sda1
umount /dev/sda2
3.使用parted进入磁盘操作模式:
bash
parted /dev/sda
parted /dev/sda
此时应有返回:
bash
root@armbian:~# parted /dev/sda
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
root@armbian:~# parted /dev/sda
GNU Parted 3.4
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted)
4.查看当前分区表:
bash
(parted) p
Model: NVME USB 3.2 (scsi)
Disk /dev/sda: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 137GB 137GB ext4
2 137GB 512GB 375GB ext4
(parted) p
Model: NVME USB 3.2 (scsi)
Disk /dev/sda: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 1049kB 137GB 137GB ext4
2 137GB 512GB 375GB ext4
记下当前输出的所有信息,后面会用到,最好是复制出来到另外的地方
5.删除2号分区和1号分区:
bash
(parted) rm 2
(parted) rm 1
(parted) p
Model: NVME USB 3.2 (scsi)
Disk /dev/sda: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted) rm 2
(parted) rm 1
(parted) p
Model: NVME USB 3.2 (scsi)
Disk /dev/sda: 512GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
6.重建分区:
bash
(parted) mkpart
# 输入上面记下的1号分区的Name值,如果输出的时候就没有就直接回车
Partition name? []?
# 输入上面记下的1号分区的分区格式值
File system type? [ext2]? ext4
# 输入上面记下的起始点
Start? 1049KB
# 输入终点,注意,这个值只能比上面记下的End值大
End? 240GB
(parted) mkpart
# 输入上面记下的1号分区的Name值,如果输出的时候就没有就直接回车
Partition name? []?
# 输入上面记下的1号分区的分区格式值
File system type? [ext2]? ext4
# 输入上面记下的起始点
Start? 1049KB
# 输入终点,注意,这个值只能比上面记下的End值大
End? 240GB
7.退出parted,命令是quit
8.resize一下扩容的分区
bash
root@localserver:~# resize2fs -f /dev/sda1
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/sda1 to 58593536 (4k) blocks.
The filesystem on /dev/sda1 is now 58593536 (4k) blocks long.
root@localserver:~# resize2fs -f /dev/sda1
resize2fs 1.46.2 (28-Feb-2021)
Resizing the filesystem on /dev/sda1 to 58593536 (4k) blocks.
The filesystem on /dev/sda1 is now 58593536 (4k) blocks long.
9.重新挂载分区