欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  科技

fdisk与parted分区

程序员文章站 2022-09-04 18:24:47
我所接触的linux分区分为两种,一种是使用fdisk分区,另外一种是parted分区。前者是针对MBR模式分区的,后者是针对GPT模式分区的 fdisk分区: a toggle a bootable flag b edit bsd disklabel c toggle the dos compat ......

    我所接触的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)               

 1 创建分区示例:
 2 command (m for help): n
 3 partition type:
 4    p   primary (0 primary, 0 extended, 4 free)
 5    e   extended
 6 select (default p): p
 7 partition number (1-4, default 1): 1
 8 first sector (2048-4194303, default 2048):
 9 using default value 2048
10 last sector, +sectors or +size{k,m,g} (2048-4194303, default 4194303):
11 using default value 4194303
12 partition 1 of type linux and of size 2 gib is set
13 
14 command (m for help): t
15 selected partition 1
16 hex code (type l to list all codes): 8e
17 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 23158  /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 (23669 extents) to 90.46 gib (23158 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
 1      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 0% 100%                                             ---->name为自定义为该分区取名  0% 100% 是磁盘/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
 1      1049kb  2146mb  2145mb               name

(parted) set 1 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 16% / devtmpfs devtmpfs 904m 0 904m 0% /dev tmpfs tmpfs 914m 80k 914m 1% /dev/shm tmpfs tmpfs 914m 8.9m 905m 1% /run tmpfs tmpfs 914m 0 914m 0% /sys/fs/cgroup /dev/sda1 xfs 497m 124m 373m 25% /boot /dev/mapper/rhel-home xfs 7.0g 65m 7.0g 1% /home [root@localhost ~]# mkfs.ext4 /dev/mapper/rhel-home ---->缩减分区大小需要先将格式变为ext4格式,xfs格式不支持通过resize2fs缩减 mke2fs 1.42.9 (28-dec-2013) filesystem label= os type: linux block size=4096 (log=2) fragment size=4096 (log=2) stride=0 blocks, stripe width=0 blocks 458752 inodes, 1835008 blocks 91750 blocks (5.00%) reserved for the super user first data block=0 maximum filesystem blocks=1879048192 56 block groups 32768 blocks per group, 32768 fragments per group 8192 inodes per group superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632 allocating group tables: done writing inode tables: done creating journal (32768 blocks): done writing superblocks and filesystem accounting information: done [root@localhost ~]# resize2fs /dev/mapper/rhel-home 2g ---->重新指定/rhel-home卷的大小,需要加单位,不加单位则默认为block块 resize2fs 1.42.9 (28-dec-2013) resizing the filesystem on /dev/mapper/rhel-home to 524288 (4k) blocks. the filesystem on /dev/mapper/rhel-home is now 524288 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 (1792 extents) to 2.00 gib (512 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 16% / devtmpfs 904m 0 904m 0% /dev tmpfs 914m 80k 914m 1% /dev/shm tmpfs 914m 8.9m 905m 1% /run tmpfs 914m 0 914m 0% /sys/fs/cgroup /dev/sda1 497m 124m 373m 25% /boot /dev/mapper/rhel-home 1.9g 21m 1.8g 2% /home
缩减容量:方式二(注:该系统卷root与卷home都为xfs文件系统,与上不同,但该操作与文件系统无关联)
[root@localhost ~]# df -h
filesystem             size  used avail use% mounted on
/dev/mapper/rhel-root   91g   15g   77g  16% /
devtmpfs               904m     0  904m   0% /dev
tmpfs                  914m  276k  914m   1% /dev/shm
tmpfs                  914m  9.1m  905m   1% /run
tmpfs                  914m     0  914m   0% /sys/fs/cgroup
/dev/sda1              497m  124m  373m  25% /boot
/dev/mapper/rhel-home  7.0g   65m  7.0g   1% /home
/dev/sr0               3.7g  3.7g     0 100% /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 (23158 extents) to 88.00 gib (22528 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 (1792 extents) to 9.46 gib (2422 extents).
  logical volume home successfully resized
[root@localhost ~]# xfs_growfs /dev/mapper/rhel-home                ---->xfs文件系统扩容,ext4使用resize2fs扩容
meta-data=/dev/mapper/rhel-home  isize=256    agcount=4, agsize=458752 blks
         =                       sectsz=512   attr=2, projid32bit=1
         =                       crc=0        finobt=0
data     =                       bsize=4096   blocks=1835008, imaxpct=25
         =                       sunit=0      swidth=0 blks
naming   =version 2              bsize=4096   ascii-ci=0 ftype=0
log      =internal               bsize=4096   blocks=2560, version=2
         =                       sectsz=512   sunit=0 blks, lazy-count=1
realtime =none                   extsz=4096   blocks=0, rtextents=0
data blocks changed from 1835008 to 2480128
[root@localhost ~]# df -h
filesystem             size  used avail use% mounted on
/dev/mapper/rhel-root   91g   15g   77g  16% /
devtmpfs               904m     0  904m   0% /dev
tmpfs                  914m  276k  914m   1% /dev/shm
tmpfs                  914m  9.1m  905m   1% /run
tmpfs                  914m     0  914m   0% /sys/fs/cgroup
/dev/sda1              497m  124m  373m  25% /boot
/dev/mapper/rhel-home  9.5g   66m  9.4g   1% /home        ---->这里扩容了2.5g
/dev/sr0               3.7g  3.7g     0 100% /media
添加新卷
[root@localhost ~]# lvcreate -l +100%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=256 agcount=4, agsize=327680 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 finobt=0 data = bsize=4096 blocks=1310720, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=0 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0 [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 16% / devtmpfs 904m 0 904m 0% /dev tmpfs 914m 80k 914m 1% /dev/shm tmpfs 914m 8.9m 905m 1% /run tmpfs 914m 0 914m 0% /sys/fs/cgroup /dev/sda1 497m 124m 373m 25% /boot /dev/mapper/rhel-home 2.0g 33m 2.0g 2% /home /dev/mapper/rhel-lv2 5.0g 33m 5.0g 1% /lv2
扩展容量 [root@localhost ~]#xfs_growfs /dev/mapper/rhel-root ---->扩展xfs分区大小,这是扩展到root根下
[root@localhost ~]#resize2fs /dev/mapper/rhel-home 7g ---->扩展ext4分区大小。