我所接触的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文件系统,与上不同,但该操作与文件系统无关联)一定要慎用,因为操作几次都把home逻辑卷下的用户给搞坏了,后来还得重新建用户。 [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分区大小。