说明fdisk、parted命令进行硬盘分区

时间:2021-11-16 16:25:16

①通过fdisk工具对磁盘进行分区,其格式是:fdisk<硬盘设备名>

[root@FQDN home]# su – root 
Last login: Thu Jan 11 19:11:55 CST 2018 on pts/0
[root@FQDN ~]# fdisk /dev/sdb                 //对/dev/sdb进行分区
Welcome to fdisk (util-linux 2.23.2).
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x30f74b96.
Command (m for help): m
Command action
   a   toggle a bootable flag
   b   edit bsd disklabel
   c   toggle the dos compatibility flag
   d   delete a partition
   g   create a new empty GPT partition table
   G   create an IRIX (SGI) partition table
   l   list known partition types
   m   print this menu
   n   add a new partition
   o   create a new empty DOS partition table
   p   print the partition table
   q   quit without saving changes
   s   create a new empty Sun disklabel
   t   change a partition's system id
   u   change display/entry units
   v   verify the partition table
   w   write table to disk and exit
   x   extra functionality (experts only)
Command (m for help): o               //创建DOS类型的空分区表
Building a new DOS disklabel with disk identifier 0x8e9e909f.
Command (m for help): n              //创建新的分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): 
Using default value 41943039
Partition 1 of type Linux and of size 20 GiB is set
Command (m for help): p             //显示当前分区表
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8e9e909f
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    41943039    20970496   83  Linux
Command (m for help): d              //删除分区
Selected partition 1
Partition 1 is deleted
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8e9e909f
   Device Boot      Start         End      Blocks   Id  System
Command (m for help): n            //创建大小为1G的1号主分区
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): p
Partition number (1-4, default 1): 
First sector (2048-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +1G   
Partition 1 of type Linux and of size 1 GiB is set
Command (m for help): n
Partition type:
   p   primary (1 primary, 0 extended, 3 free)
   e   extended
Select (default p): p
Partition number (2-4, default 2): 
First sector (2099200-41943039, default 2099200): 
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-41943039, default 41943039): +2G  
Partition 2 of type Linux and of size 2 GiB is set
Command (m for help): n
Partition type:
   p   primary (2 primary, 0 extended, 2 free)
   e   extended
Select (default p): e
Partition number (3,4, default 3): 
First sector (6293504-41943039, default 6293504): 
Using default value 6293504
Last sector, +sectors or +size{K,M,G} (6293504-41943039, default 41943039): 
Using default value 41943039
Partition 3 of type Extended and of size 17 GiB is set
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): 1
Invalid partition type `1'
Command (m for help): n          //创建大小为5G的逻辑分区
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 5
First sector (6295552-41943039, default 6295552): 
Using default value 6295552
Last sector, +sectors or +size{K,M,G} (6295552-41943039, default 41943039): +5G
Partition 5 of type Linux and of size 5 GiB is set
Command (m for help): n
Partition type:
   p   primary (2 primary, 1 extended, 1 free)
   l   logical (numbered from 5)
Select (default p): l
Adding logical partition 6
First sector (16783360-41943039, default 16783360): 
Using default value 16783360
Last sector, +sectors or +size{K,M,G} (16783360-41943039, default 41943039): +8G
Partition 6 of type Linux and of size 8 GiB is set
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8e9e909f
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   83  Linux
/dev/sdb2         2099200     6293503     2097152   83  Linux
/dev/sdb3         6293504    41943039    17824768    5  Extended
/dev/sdb5         6295552    16781311     5242880   83  Linux
/dev/sdb6        16783360    33560575     8388608   83  Linux
Command (m for help): t              //将1号分区该为swap类型
Partition number (1-3,5,6, default 6): l
Partition number (1-3,5,6, default 6): 1
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): t
Partition number (1-3,5,6, default 6): 2
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): t
Partition number (1-3,5,6, default 6): 6
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'
Command (m for help): p
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8e9e909f
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   82  Linux swap / Solaris
/dev/sdb2         2099200     6293503     2097152   8e  Linux LVM
/dev/sdb3         6293504    41943039    17824768    5  Extended
/dev/sdb5         6295552    16781311     5242880   83  Linux
/dev/sdb6        16783360    33560575     8388608   8e  Linux LVM
Command (m for help): w              //保存
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@FQDN ~]# partprobe /dev/sdb

②parted命令是由GNU组织开发的一款功能强大的磁盘分区和分区大小调整工具,与fdisk不同,它支持调整分区的大小。命令格式是:parted<选项><参数>

选项

-h:显示帮助信息;

-i:交互式模式;

-s:脚本模式,不提示用户;

-v:显示版本号
[root@FQDN ~]#  parted /dev/sdc
GNU Parted 3.1
Using /dev/sdc
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel gpt                                                      
(parted) print                                                            
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 42.9GB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: 
Number  Start  End  Size  File system  Name  Flags
(parted) mkpart primary 0 512
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? Ignore                                                     
(parted) quit                                                             
Information: You may need to update /etc/fstab.
[root@FQDN ~]# fdisk -l                                                   
Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x00003361
   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *        2048     2099199     1048576   83  Linux
/dev/sda2         2099200    41943039    19921920   8e  Linux LVM
Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: dos
Disk identifier: 0x8e9e909f
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048     2099199     1048576   82  Linux swap / Solaris
/dev/sdb2         2099200     6293503     2097152   8e  Linux LVM
/dev/sdb3         6293504    41943039    17824768    5  Extended
/dev/sdb5         6295552    16781311     5242880   83  Linux
/dev/sdb6        16783360    33560575     8388608   8e  Linux LVM
WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.
Disk /dev/sdc: 42.9 GB, 42949672960 bytes, 83886080 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk label type: gpt
Disk identifier: D011A620-24CD-49E7-86BF-36E4CCD55EBE
#         Start          End    Size  Type            Name
 1           34      1000000  488.3M  Microsoft basic primary
Disk /dev/mapper/centos-root: 18.2 GB, 18249416704 bytes, 35643392 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk /dev/mapper/centos-swap: 2147 MB, 2147483648 bytes, 4194304 sectors
Units = sectors of 1 * 512 = 512 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
[root@FQDN ~]# mkfs.ext3 /dev/sdc1
mke2fs 1.42.9 (28-Dec-2013)
warning: 267 blocks unused.
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
125416 inodes, 499713 blocks
24985 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
61 block groups
8192 blocks per group, 8192 fragments per group
2056 inodes per group
Superblock backups stored on blocks: 
	8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409
Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done 
[root@FQDN ~]# mount /dev/sdb1 /mnt
mount: /dev/sdb1 is write-protected, mounting read-only
mount: wrong fs type, bad option, bad superblock on /dev/sdb1,
       missing codepage or helper program, or other error
       In some cases useful info is found in syslog - try
       dmesg | tail or so.
[root@FQDN ~]# df -h
Filesystem               Size  Used Avail Use% Mounted on
/dev/mapper/centos-root   17G  3.8G   14G  23% /
devtmpfs                 473M     0  473M   0% /dev
tmpfs                    489M     0  489M   0% /dev/shm
tmpfs                    489M  7.2M  481M   2% /run
tmpfs                    489M     0  489M   0% /sys/fs/cgroup
/dev/sda1               1014M  199M  816M  20% /boot
tmpfs                     98M  4.0K   98M   1% /run/user/42
tmpfs                     98M   20K   98M   1% /run/user/1000
/dev/sr0                 4.3G  4.3G     0 100% /run/media/heshuilong/CentOS 7 x86_64