一.了解常用的2款分区工具
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 ''' 8 fdisk 【只支持MSDOS分区布局】 9 parted 【支持MSDOS、GPT分区布局】 10 '''
俗话说:工欲善其事,必先利其器。想对磁盘进行分区,就得了解常用的分区工具,知道他们各自的长处,然后选择适合自己的分区工具。这就是我们今天要学习的parted命令,其实用法很简单,之前我有写过关于fdisk分区工具的用法,大家可以参考:http://www.cnblogs.com/yinzhengjie/p/6840563.html
好了,废话不多说,让我们直接开始分区吧,分区有三个步骤,第一个步骤就是用分区工具进行分区,第二个步骤就是创建文件系统(也就是格式化),因为只有创建文件系统了,才能往里面存取数据,第三个步骤就是挂载,光分完区不对其进行挂载的话照样是白扯。
一.用parted命令对一块设备进行分区。
1 #!/usr/bin/env python 2 #_*_coding:utf-8_*_ 3 #@author :yinzhengjie 4 #blog:http://www.cnblogs.com/yinzhengjie/tag/python%E8%87%AA%E5%8A%A8%E5%8C%96%E8%BF%90%E7%BB%B4%E4%B9%8B%E8%B7%AF/ 5 #EMAIL:y1053419035@qq.com 6 7 ''' 8 [root@yinzhengjie ~]# parted /dev/sdb #进入/dev/sdb进行分区 9 GNU Parted 2.1 10 Using /dev/sdb 11 Welcome to GNU Parted! Type 'help' to view a list of commands. 12 (parted) mktable 13 New disk label type? 14 New disk label type? gpt #选择分区格式为gpt 15 Warning: The existing disk label on /dev/sdb will be destroyed and all data on 16 this disk will be lost. Do you want to continue? #这是一个警告,说是如果对这个这边进行分区,数据将会被覆盖而且数据会丢失,问你是否继续? 17 Yes/No? y #输入“y”,表示确定,进行下一步 18 (parted) mkpart #在命令行输出分区指令 19 Partition name? []? primary #分区类型为主分区,这是给分区起个名字,你可以自定义的,可以不用写我这里的参数,这里没有扩展分区一说,都是主分区。 20 File system type? [ext2]? ext4 #设置文件系统为ext4 21 Start? 0 #设置起始分配位置 22 End? 2G #设置结束分配位置,-1表示,最后,即整块盘 23 Warning: The resulting partition is not properly aligned for best performance. 24 Ignore/Cancel? i #输入i,忽略此提示即可 25 (parted) mkpart #继续分区 26 Partition name? []? primary #自定义名称 27 File system type? [ext2]? ext4 #指定文件系统格式为ext4 28 Start? 2G #由于上块磁盘分区的结束位置是2G,所以我们从最2G开始分配即可 29 End? 5G #定义结束位置为5G 30 (parted) mkpart #继续分区 31 Partition name? []? primary #自己自定义一个名字 32 File system type? [ext2]? ext4 #设置文件系统为ext4、xfs,可通过mkfs.格式化成需要的格式 33 Start? 5G #设置卷标的起始位置 34 End? 100% #输入100%,表示将剩下的所有容量都分配给该分区. 35 (parted) p #打印分区结果 36 Model: VMware, VMware Virtual S (scsi) 37 Disk /dev/sdb: 21.5GB 38 Sector size (logical/physical): 512B/512B 39 Partition Table: gpt 40 41 Number Start End Size File system Name Flags 42 1 17.4kB 2000MB 2000MB primary 43 2 2001MB 5000MB 2999MB primary 44 3 5000MB 21.5GB 16.5GB primary 45 46 (parted) quit #退出分区模式,不需要输入保存的按钮,退出来它会自动保存分区表信息。 47 Information: You may need to update /etc/fstab. 48 49 [root@yinzhengjie ~]# 50 51 52 53 '''
命令
|
解释
|
# parted /dev/sdb
|
对/dev/sdb进行分区或管理操作
|
(parted) rm
|
rm删除命令
(删除之前必须确保分区没有被挂载)
|
Partition number? 1
|
删除第一个分区
|
(parted) print
|
查看当前分区情况
|
Model: ATA VBOX HARDDISK (scsi) Disk /dev/sda: 21.5GB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size File system Name Flags |
系统返回值
|
在格式化几个TB的磁盘的时候,时间会非常的长,格式化6T的磁盘时间大概在一个半小时左右。(据硬盘实际情况而定)
三、 partd分区实例
fdisk工具虽然很好用,但对于大于2T以上的硬盘分区特别慢,可能一部分容量识别不了,也不支持非交互模式。
用parted就非常方便了,对大硬盘支持很好,也可以实现脚本分区。
默认一般都安装过了,没有的话install it!
yum install parted
parted有个不提示用户参数选项,就是通过这个选项来实现非交互
-s, --script never prompts for user intervention
下面我们通过一个一块硬盘来说明它的具体操作:
第一个主分区3G 剩余分区都给扩展分区 第一个逻辑卷分区2G 第二个逻辑源用剩余空间
第一个主分区3G
parted -s /dev/sdb mklabel msdos parted -s /dev/sdb mkpart primary 0 3G
剩余空间给扩展分区
parted -s /dev/sdb mkpart entended 3 100%
在扩展分区上创建第一个逻辑分区
parted -s /dev/sdb mkpart logic 3G 5G
创建第二个逻辑分区
parted -s /dev/sdb mkpart logic 5G 100% #100%代表使用剩余的所有空间
查看分区大小
parted -s /dev/sdb print Model: ATA QEMU HARDDISK (scsi) Disk /dev/sdb: 8590MB Sector size (logical/physical): 512B/512B Partition Table: msdos Number Start End Size Type File system 标志 1 512B 3000MB 3000MB primary 2 3001MB 8590MB 5589MB extended lba 5 5000MB 8590MB 3590MB logical
删除分区
parted -s /dev/sdb rm 5 #rm后面跟的事分区的编号,print出的Number parted -s /dev/sdb print Number Start End Size Type File system 标志 1 512B 3000MB 3000MB primary 2 3001MB 8590MB 5589MB extended lba
对/dev/sdc分一个主分区,类型为swap
parted -s /dev/sdc mklabel msdos parted -s /dev/sdc -- mkpartfs primary linux-swap 0 -1 #从使用所有空间
例子:
[root@cs172-16-4-60 ~]# parted /dev/sdb GNU Parted 3.1 Using /dev/sdb Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help 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 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 (parted) mklabel gpt (parted) p Model: VMware Virtual disk (scsi) Disk /dev/sdb: 3299GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags (parted) mkpart p1 xfs 0 -1 Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? I (parted) p Model: VMware Virtual disk (scsi) Disk /dev/sdb: 3299GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 3299GB 3299GB p1 (parted) q Information: You may need to update /etc/fstab. [root@cs172-16-4-60 ~]# pvdisplay --- Physical volume --- PV Name /dev/sda2 VG Name centos_cs-t4 PV Size <79.00 GiB / not usable 3.00 MiB Allocatable yes (but full) PE Size 4.00 MiB Total PE 20223 Free PE 0 Allocated PE 20223 PV UUID B0pzzX-tmJm-6OPx-11gQ-856u-V3NM-C8QG18 [root@cs172-16-4-60 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created. [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# vgdisplay --- Volume group --- VG Name centos_cs-t4 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <79.00 GiB PE Size 4.00 MiB Total PE 20223 Alloc PE / Size 20223 / <79.00 GiB Free PE / Size 0 / 0 VG UUID Q5Xdy7-13RF-TW83-AuHP-y8f6-U6SX-Wm8RJd [root@cs172-16-4-60 ~]# vgcreate data /dev/sdb1 Volume group "data" successfully created [root@cs172-16-4-60 ~]# vgdisplay --- Volume group --- VG Name centos_cs-t4 System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size <79.00 GiB PE Size 4.00 MiB Total PE 20223 Alloc PE / Size 20223 / <79.00 GiB Free PE / Size 0 / 0 VG UUID Q5Xdy7-13RF-TW83-AuHP-y8f6-U6SX-Wm8RJd --- Volume group --- VG Name data System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 1 VG Access read/write VG Status resizable MAX LV 0 Cur LV 0 Open LV 0 Max PV 0 Cur PV 1 Act PV 1 VG Size <3.00 TiB PE Size 4.00 MiB Total PE 786431 Alloc PE / Size 0 / 0 Free PE / Size 786431 / <3.00 TiB VG UUID VOrBbr-RAUX-P8rt-to6Z-ECKe-4X9Q-XDBAr1 [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# lvdisplay --- Logical volume --- LV Path /dev/centos_cs-t4/root LV Name root VG Name centos_cs-t4 LV UUID 2YlxIX-AIHr-hsEQ-Pcqk-s7Nf-VShl-GFS52r LV Write Access read/write LV Creation host, time localhost, 2017-11-14 10:50:31 +0800 LV Status available # open 1 LV Size <71.00 GiB Current LE 18175 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:0 --- Logical volume --- LV Path /dev/centos_cs-t4/swap LV Name swap VG Name centos_cs-t4 LV UUID sjN3Kr-eiIy-tfMz-7DsQ-VdH5-kENd-aytb50 LV Write Access read/write LV Creation host, time localhost, 2017-11-14 10:50:32 +0800 LV Status available # open 2 LV Size 8.00 GiB Current LE 2048 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:1 [root@cs172-16-4-60 ~]# lvcreate -l +786431 -n data data Logical volume "data" created. [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# mkfs.xfs /dev/mapper/data-data meta-data=/dev/mapper/data-data isize=512 agcount=4, agsize=201326336 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=805305344, imaxpct=5 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=393215, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# mkdir /data [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# cat /etc/fstab # # /etc/fstab # Created by anaconda on Tue Nov 14 10:50:32 2017 # # 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 # /dev/mapper/centos_cs--t4-root / xfs defaults 0 0 UUID=0e1181c8-f493-4a16-8b0a-eba15f72c951 /boot xfs defaults 0 0 /dev/mapper/centos_cs--t4-swap swap swap defaults 0 0 /dev/mapper/data-data /data xfs defaults 0 0 [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos_cs--t4-root xfs 71G 2.0G 69G 3% / devtmpfs devtmpfs 40G 0 40G 0% /dev tmpfs tmpfs 40G 0 40G 0% /dev/shm tmpfs tmpfs 40G 8.5M 40G 1% /run tmpfs tmpfs 40G 0 40G 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 189M 826M 19% /boot tmpfs tmpfs 7.9G 0 7.9G 0% /run/user/1001 [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# mount -a [root@cs172-16-4-60 ~]# [root@cs172-16-4-60 ~]# df -hT Filesystem Type Size Used Avail Use% Mounted on /dev/mapper/centos_cs--t4-root xfs 71G 2.0G 69G 3% / devtmpfs devtmpfs 40G 0 40G 0% /dev tmpfs tmpfs 40G 0 40G 0% /dev/shm tmpfs tmpfs 40G 8.5M 40G 1% /run tmpfs tmpfs 40G 0 40G 0% /sys/fs/cgroup /dev/sda1 xfs 1014M 189M 826M 19% /boot tmpfs tmpfs 7.9G 0 7.9G 0% /run/user/1001 /dev/mapper/data-data xfs 3.0T 33M 3.0T 1% /data [root@cs172-16-4-60 ~]#