fdisk与parted分区

时间:2023-03-10 00:06:03
fdisk与parted分区

    我所接触的linux分区分为两种,一种是使用fdisk分区,另外一种是parted分区。前者是针对MBR模式分区的,后者是针对GPT模式分区的

    fdisk分区:    

fdisk -l  ---->查看磁盘信息
fdisk /dev/sdc --->选择需要操作的磁盘(eg:/dev/sdc)

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             ---->分区ID (逻辑分区为8e)
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): n
Partition type:
p primary ( primary, extended, free)
e extended
Select (default p): p
Partition number (-, default ): 1
First sector (-, default ):
Using default value
Last sector, +sectors or +size{K,M,G} (-, default ):
Using default value
Partition of type Linux and of size GiB is set Command (m for help): t
Selected partition
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

创建及扩展逻辑卷

[root@localhost ~]# pvcreate /dev/sdc1                ---->创建pv
Physical volume "/dev/sdc1" successfully created
[root@localhost ~]# vgextend rhel /dev/sdc1 ---->扩展vg卷组
Volume group "rhel" successfully extended
[root@localhost ~]# vgreduce rhel /dev/sdc1 --->如果不想再加入到已有的vg里面,想自己建一个vg卷组,将刚刚扩展进去的pv给移除出来,
Removed "/dev/sdc1" from volume group "rhel"
[root@localhost ~]# vgcreate vg2 /dev/sdc1 --->创建新的vg,并设置vg名为vg2
Volume group "vg2" successfully created
[root@localhost ~]# lvextend /dev/rhel/root /dev/sdc1
Physical Volume "/dev/sdc1" not found in Volume Group "rhel". --->没添加进vg组,直接扩展到逻辑卷会报错,如要扩展需,先vgextend到相应的vg组
[root@localhost ~]# pvremove /dev/sdc1 ---->移除pv
Labels on physical volume "/dev/sdc1" successfully wiped
如lvextend加入错入则如下解决
[root@localhost ~]# lvextend /dev/rhel/root /dev/sdc1                         ---->将扩展的/dev/sdc1扩展至/dev/rhe1/root中,随后感觉扩展错误,怎么退出,
Size of logical volume rhel/root changed from 90.46 GiB (23158 extents) to 92.46 GiB (23669 extents). --->记住该extents大小,是从23158变为了23669
Logical volume root successfully resized
[root@localhost ~]# lvreduce -l /dev/rhel/root ---->这里是将扩展进去的/dev/sdc1给退出来,-l参数 指定extents大小 ,指定缩减后/dev/rhel/root的大小
WARNING: Reducing active and open logical volume to 90.46 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce root? [y/n]: y
Size of logical volume rhel/root changed from 92.46 GiB ( extents) to 90.46 GiB ( extents).
Logical volume root successfully resized ---->成功,之后可重新扩展

[root@localhost ~]# lvcreate -l +100%Free rhel /dev/sdc1 -n lv2                 ---->重新创建一个lv卷,-n参数 指定创建后的卷名字 rhel是卷组 , 
Logical volume "lv2" created.

parted分区:

[root@localhost ~]# parted
GNU Parted 3.1
Using /dev/sda
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) select /dev/sdc ---->选择要操作的分区,一定要注意所执行的磁盘
Using /dev/sdc
(parted) p ---->查看当前分区信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags
1049kB 2146MB 2145MB p lvm
(parted) mklabel GPT ---->磁盘格式化为GPT模式,之前是则不需要格式化
Warning: The existing disk label on /dev/sdc will be destroyed and all data on
this disk will be lost. Do you want to continue?
Yes/No? yes
(parted) mkpart name % % ---->name为自定义为该分区取名 % % 是磁盘/dev/sdc的区间作为该分区的大小
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Disk Flags: Number Start End Size File system Name Flags
1049kB 2146MB 2145MB name (parted) set lvm on ---->依据分区号1,将分区1设置为逻辑卷
(parted) rm 1 ---->删除分区号为1的分区
(parted) q ---->退出即保存

磁盘格式化后并挂载

 容量缩减,方式一
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/rhel-root xfs 93G 15G 78G % /
devtmpfs devtmpfs 904M 904M % /dev
tmpfs tmpfs 914M 80K 914M % /dev/shm
tmpfs tmpfs 914M 8.9M 905M % /run
tmpfs tmpfs 914M 914M % /sys/fs/cgroup
/dev/sda1 xfs 497M 124M 373M % /boot
/dev/mapper/rhel-home xfs .0G 65M .0G % /home
[root@localhost ~]# mkfs.ext4 /dev/mapper/rhel-home ---->缩减分区大小需要先将格式变为ext4格式,xfs格式不支持通过resize2fs缩减
mke2fs 1.42. (-Dec-)
Filesystem label=
OS type: Linux
Block size= (log=)
Fragment size= (log=)
Stride= blocks, Stripe width= blocks
inodes, blocks
blocks (5.00%) reserved for the super user
First data block=
Maximum filesystem blocks=
block groups
blocks per group, fragments per group
inodes per group
Superblock backups stored on blocks:
, , , , , , , Allocating group tables: done
Writing inode tables: done
Creating journal ( blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# resize2fs /dev/mapper/rhel-home 2G ---->重新指定/rhel-home卷的大小,需要加单位,不加单位则默认为block块
resize2fs 1.42. (-Dec-)
Resizing the filesystem on /dev/mapper/rhel-home to (4k) blocks.
The filesystem on /dev/mapper/rhel-home is now blocks long.
[root@localhost ~]# lvreduce -L 2G /dev/rhel/home ---->2G与上相同
WARNING: Reducing active logical volume to 2.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce home? [y/n]: y
Size of logical volume rhel/home changed from 7.00 GiB ( extents) to 2.00 GiB ( extents).
Logical volume home successfully resized
[root@localhost ~]# mount /dev/mapper/rhel-home /home/
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 93G 15G 78G % /
devtmpfs 904M 904M % /dev
tmpfs 914M 80K 914M % /dev/shm
tmpfs 914M 8.9M 905M % /run
tmpfs 914M 914M % /sys/fs/cgroup
/dev/sda1 497M 124M 373M % /boot
/dev/mapper/rhel-home .9G 21M .8G % /home
缩减容量:方式二(注:该系统卷root与卷home都为xfs文件系统,与上不同,但该操作与文件系统无关联)一定要慎用,因为操作几次都把home逻辑卷下的用户给搞坏了,后来还得重新建用户。
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 91G 15G 77G % /
devtmpfs 904M 904M % /dev
tmpfs 914M 276K 914M % /dev/shm
tmpfs 914M 9.1M 905M % /run
tmpfs 914M 914M % /sys/fs/cgroup
/dev/sda1 497M 124M 373M % /boot
/dev/mapper/rhel-home .0G 65M .0G % /home
/dev/sr0 .7G .7G % /media
[root@localhost ~]# lvreduce -L 88G /dev/rhel/root ---->对根目录操作一定要谨慎再谨慎
WARNING: Reducing active and open logical volume to 88.00 GiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce root? [y/n]: y
Size of logical volume rhel/root changed from 90.46 GiB ( extents) to 88.00 GiB ( extents).
Logical volume root successfully resized
[root@localhost ~]# lvextend /dev/rhel/home /dev/sda2 /dev/sdb1 /dev/sd
/dev/sda2 /dev/sdb1
[root@localhost ~]# lvextend /dev/rhel/home /dev/sda2 /dev/sdb1 ---->将释放的空间扩展到home卷下
No free extents on physical volume "/dev/sda2".
Size of logical volume rhel/home changed from 7.00 GiB ( extents) to 9.46 GiB ( extents).
Logical volume home successfully resized
[root@localhost ~]# xfs_growfs /dev/mapper/rhel-home ---->xfs文件系统扩容,ext4使用resize2fs扩容
meta-data=/dev/mapper/rhel-home isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
data blocks changed from to
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 91G 15G 77G % /
devtmpfs 904M 904M % /dev
tmpfs 914M 276K 914M % /dev/shm
tmpfs 914M 9.1M 905M % /run
tmpfs 914M 914M % /sys/fs/cgroup
/dev/sda1 497M 124M 373M % /boot
/dev/mapper/rhel-home .5G 66M .4G % /home ---->这里扩容了2.5G
/dev/sr0 .7G .7G % /media
添加新卷
[root@localhost ~]# lvcreate -l +%Free rhel /dev/sda2 -n lv2 --->创建新的lv卷
Logical volume "lv2" created.
[root@localhost ~]# mkfs.xfs /dev/mapper/rhel-lv2 ---->将新的lv2卷格式化为xfs格式
meta-data=/dev/mapper/rhel-lv2 isize= agcount=, agsize= blks
= sectsz= attr=, projid32bit=
= crc= finobt=
data = bsize= blocks=, imaxpct=
= sunit= swidth= blks
naming =version bsize= ascii-ci= ftype=
log =internal log bsize= blocks=, version=
= sectsz= sunit= blks, lazy-count=
realtime =none extsz= blocks=, rtextents=
[root@localhost ~]# mkdir /lv2 ---->创建目录
[root@localhost ~]# mount /dev/mapper/rhel-lv2 /lv2/ ---->挂载
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/mapper/rhel-root 93G 15G 78G % /
devtmpfs 904M 904M % /dev
tmpfs 914M 80K 914M % /dev/shm
tmpfs 914M 8.9M 905M % /run
tmpfs 914M 914M % /sys/fs/cgroup
/dev/sda1 497M 124M 373M % /boot
/dev/mapper/rhel-home .0G 33M .0G % /home
/dev/mapper/rhel-lv2 .0G 33M .0G % /lv2
扩展容量
[root@localhost ~]#xfs_growfs /dev/mapper/rhel-root ---->扩展xfs分区大小,这是扩展到root根下
[root@localhost ~]#resize2fs /dev/mapper/rhel-home 7G ---->扩展ext4分区大小。