linux分区命令parted的用法
parted的适用场景
- 创建操作大于2T的分区
一般情况下,我们都是选择使用fdisk工具来进行分区,但是目前在实际生产环境中使用的磁盘空间越来越大,呈TiB级别增长;而常用的fdisk这个工具对分区是有大小限制的,它只能划分小于2T的磁盘,所以在划大于2T磁盘分区的时候fdisk就无法满足要求了;这个时候有2个方法,其一是通过卷管理来实现,其二就是通过parted工具来实现对GPT磁盘进行分区操作;这里我们采用parted的方法来实现管理。
parted的2种使用方式
-
交互式
手动按序交互式的创建; -
非交互式
可将命令行写在脚本中,运行脚本实现一键创建;适用于远程批量管理多台主机的场景。 -
两种方式比较
两种方法的使用和原理其实是一样的;要想实现非交互式创建,无非是在每一个具体的交互式命令前加上parted DEVICES_NAME
即可。 -
Parted命令相关参数
root@simon:~# > parted --help Usage: parted [OPTION]... [DEVICE [COMMAND [PARAMETERS]...]...] Apply COMMANDs with PARAMETERS to DEVICE. If no COMMAND(s) are given, run in interactive mode. OPTIONs: -h, --help displays this help message -l, --list lists partition layout on all block devices -m, --machine displays machine parseable output -s, --script never prompts for user intervention -v, --version displays the version -a, --align=[none|cyl|min|opt] alignment for new partitions COMMANDs: align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END resizepart NUMBER END resize partition NUMBER rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted Report bugs to [email protected]
parted实现分区管理实践
实验环境
操作系统
CentOS 7.5
磁盘信息
- 待管理磁盘
/dev/sdb - 磁盘总大小
18T - 分区需求
将整个/dev/sdb划分到同一个分区里,并挂载到**/gfsdata01目录下。
交互式方式实现分区步骤
选择操作磁盘
parted命令后跟上欲操作磁盘的名字即可选择此设备进行操作。
[root@kvm ~]# parted /dev/sdb
GNU Parted 3.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
新建磁盘标签类型为GPT
因为parted命令只能针对gpt格式的磁盘进行操作,所以这里必须将新建的磁盘标签格式设为gpt。
(parted) mklabel gpt
分区
命令格式
mkpart PART-TYPE [FS-TYPE] START END
-
PART-TYPE(分区类型)
- primary
主分区 - logical
逻辑分区 - extended
扩展分区
- primary
-
FS-TYPE(文件系统类型)
- ext4
- ext3
- ext2
- xfs
- 其他......
-
START
设定磁盘分区起始点;可以为0,numberMiB/GiB/TiB;
- 0
设定当前分区的起始点为磁盘的第一个扇区; - 1G
设定当前分区的起始点为磁盘的1G处开始;
- 0
-
END
设定磁盘分区结束点;
- -1
设定当前分区的结束点为磁盘的最后一个扇区; - 10G
设定当前分区的结束点为磁盘的10G处;
- -1
将/dev/sdb整个空间分给同一个分区
(parted) mkpart primary 0 -1
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) p
Model: AVAGO AVAGO (scsi)
Disk /dev/sdb: 18.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 18.0TB 18.0TB primary
(parted) q
Information: You may need to update /etc/fstab.
格式化分区
因为整个/dev/sdb只分了一个区,则这个分区名默认会分配为/dev/sdb1;使用mkfs命令将/dev/sdb1分区格式化为ext4。
[root@kvm ~]# mkfs -t ext4 /dev/sdb1
mke2fs 1.42.9 (28-Dec-2013)
/dev/sdb1 alignment is offset by 244736 bytes.
This may result in very poor performance, (re)-partitioning suggested.
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=64 blocks
274659328 inodes, 4394530311 blocks
219726515 blocks (5.00%) reserved for the super user
First data block=0
134111 block groups
32768 blocks per group, 32768 fragments per group
2048 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632, 2654208,
4096000, 7962624, 11239424, 20480000, 23887872, 71663616, 78675968,
102400000, 214990848, 512000000, 550731776, 644972544, 1934917632,
2560000000, 3855122432
Allocating group tables: done
Writing inode tables: done
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
设定分区label(非必要)
[root@kvm ~]# e2label /dev/sdb1 /gfsdata01
创建挂载目录
[root@kvm ~]# mkdir /gfsdata01
临时挂载分区
[root@kvm ~]# mount /dev/sdb1 /gfsdata01
[root@kvm ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/root_vg-lv_root 89G 2.6G 82G 4% /
devtmpfs 126G 0 126G 0% /dev
tmpfs 126G 0 126G 0% /dev/shm
tmpfs 126G 2.0M 126G 1% /run
tmpfs 126G 0 126G 0% /sys/fs/cgroup
/dev/sda1 976M 216M 694M 24% /boot
/dev/sda7 99G 61M 94G 1% /home
/dev/sda8 62G 53M 59G 1% /tmp
/dev/sda6 99G 61M 94G 1% /app
tmpfs 26G 0 26G 0% /run/user/1014
tmpfs 26G 0 26G 0% /run/user/0
/dev/sdb1 17T 20K 16T 1% /gfsdata01
开机自动挂载(永久挂载)
即修改/etc/fstab文件。
echo '/dev/sdb1 /gfsdata01 ext4 defaults 0 0' >>/etc/fstab
非交互式方式实现脚本
适用于需要在多台主机上进行同样的分区操作,可以通过ansible工具调用脚本实现批量分区配置挂载等操作。
[root@kvm ~]# cat ./auto_partition.sh
#!/bin/bash
#Used to fomat 6 disks
PATH=/bin:/sbin:/usr/bin:/usr/sbin
export PATH
disk_to_parted="$1"
if [ $# == 0 ];then
echo "no disk to parted,example /dev/sdb"
exit
else
echo -e "\n\n****** ${disk_to_parted} __was Fdisked! Waiting For 3 Second*****\n\n"
fi
/sbin/parted ${disk_to_parted} <<ESXU
mklabel gpt
mkpart primary 0 -1
ignore
quit
ESXU
sleep 3s
/sbin/mkfs.xfs ${disk_to_parted}1
if [ "$?" = "0" ];then
echo -e "\n\n*****${disk_to_parted}1 _________________was Formated, Waiting For 1 Second****\n\n"
fi
/sbin/blkid ${disk_to_parted}1
sleep 1s
parted的其他常用子命令
删除分区(rm子命令)
rm NUMBER:NUMBER即为parted子命令p回显的Number对应的数字;
此命令慎用!!!如果要删除的分区内已有数据,那么此命令会删除其中的所有数据,并且是不可逆的。
(parted) p
Model: AVAGO AVAGO (scsi)
Disk /dev/sdb: 18.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
1 17.4kB 4096GB 4096GB primary
(parted) rm 1
(parted) p
Model: AVAGO AVAGO (scsi)
Disk /dev/sdb: 18.0TB
Sector size (logical/physical): 512B/4096B
Partition Table: gpt
Disk Flags:
Number Start End Size File system Name Flags
(parted)
标签:parted,分区,partition,NUMBER,dev,Linux,磁盘
From: https://www.cnblogs.com/oldSimon/p/17004249.html