我所接觸的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分區大小。