磁盘管理 fdisk,lvm

本文详细介绍了Linux中MBR和GPT分区的区别,包括各自的特点和适用场景。并通过实例演示了如何创建和格式化分区,挂载及自动挂载分区,创建swap分区,以及LVM的基本操作和分区的扩展与缩减过程。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1、描述GPT是什么,应该怎么使用。

Linux中磁盘分区分为MBR和GPT。

MBR全称为Master Boot Record,为主引导记录,是传统的分区机制,应用于绝大多数使用的BIOS的PC设备。

MBR分区的特点:

1、MBR支持32位和64位的系统

2、MBR支持分区数量有限

 3、MBR只支持不超过2T的硬盘,超过2T的硬盘将只能用2T空间。

GPT(全称为GUID Partition Table)分区为全局唯一分区表,是一个较新的分区机制,解决了MBR很多缺点

GPT分区的特点:

1、支持超过2T的磁盘(64位寻址)。Fdisk最大只能建立2TB大小的分区

2、向后兼容MBR

3、必须在支持UEFI的硬件上才能使用(Intel提出,用于取代BIOS)

4、 必须使用64位系统

2、创建一个10G的分区,并格式化为ext4文件系统。要求:

1)block 大小的为2048,预留空间20%,卷标为MYDATA.

[root@localhost ~]# fdisk /dev/sda

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').

Command (m for help): n
Command action
   l   logical (5 or over)
   p   primary partition (1-4)
l
First cylinder (13119-15665, default 13119): 
Using default value 13119
Last cylinder, +cylinders or +size{K,M,G} (13119-15665, default 15665): +10G

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@localhost ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008d864

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       13119   104857600   8e  Linux LVM
/dev/sda3           13119       15665    20458488+   5  Extended
/dev/sda5           13119       14424    10490124+  83  Linux

[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2

[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 5

[root@localhost ~]# cat /proc/partitions 
major minor  #blocks  name

   8        0  125829120 sda
   8        1     512000 sda1
   8        2  104857600 sda2
   8        3         31 sda3
   8        5   10490124 sda5
 253        0   52428800 dm-0
 253        1    4194304 dm-1
 253        2   48230400 dm-2
 
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 20 -L MYDATA /dev/sda5
mke2fs 1.41.12 (17-May-2010)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
657408 inodes, 5245062 blocks
1049012 blocks (20.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=543162368
321 block groups
16384 blocks per group, 16384 fragments per group
2048 inodes per group
Superblock backups stored on blocks: 
	16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 
	2048000, 3981312

Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.

[root@localhost ~]# blkid /dev/sda5
/dev/sda5: LABEL="MYDATA" UUID="20620667-1182-4321-80a8-de671e2f5534" TYPE="ext4" 

2)挂载至/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳。

[root@localhost ~]# mount -o noatime,noexec /dev/sda5 /mydata

3)可开机自动挂载。

[root@localhost ~]# tail -1 /etc/fstab 
/dev/sda5		/mnt			ext4	defaults	0 0

3、创建一个大小1G的swap分区,并启用。

[root@localhost ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008d864

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       13119   104857600   8e  Linux LVM
/dev/sda3           13119       15665    20458488+   5  Extended
/dev/sda5           13119       14424    10490124+  83  Linux
/dev/sda6           14425       14556     1060258+  82  Linux swap / Solaris

[root@localhost ~]# mkswap /dev/sda6
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID=9f74a48d-873e-4a5d-8cd1-3d9a7cac670d

[root@localhost ~]# blkid /dev/sda6
/dev/sda6: UUID="9f74a48d-873e-4a5d-8cd1-3d9a7cac670d" TYPE="swap" 
#查看swap大小
[root@localhost ~]# free -m
         total       used       free     shared    buffers     cached
Mem:          3945        365       3580          1         21        124
-/+ buffers/cache:        219       3726
Swap:         4095          0       4095
[root@localhost ~]# swapon /dev/sda6
[root@localhost ~]# free -m
             total       used       free     shared    buffers     cached
Mem:          3945        365       3580          1         21        124
-/+ buffers/cache:        220       3725
Swap:         5131          0       5131

4、编写脚本计算/etc/passwd文件中第10个用户和第20个用户id号之和。

[root@localhost scripts]# bash userSum.sh 
11
996
The result :1007
[root@localhost scripts]# cat userSum.sh 
#!/bin/bash
#
id1=$(head -10 /etc/passwd|tail -1|cut -d: -f3)
echo $id1
id2=$(head -20 /etc/passwd|tail -1|cut -d: -f3)
echo $id2
userSum=$(($id1 + $id2))
echo "The result :$userSum"

5、将当前主机名保存到hostName变量中,主机名如果为空,或者为localhost.localdomain则将设置为www.magedu.com

[root@localhost scripts]# hostname
www.magedu.com
[root@localhost scripts]# cat hostName.sh 
#!/bin/bash
#

hostName=$(hostname)
[ -z "$hostName" -o "$hostName" == "localhost.localdomain" ] && hostname www.magedu.com

6、编写脚本,通过命令行参数传入一个用户名,判断id号是偶数还是奇数。

[root@localhost scripts]# bash test.sh z
ou shu
[root@localhost scripts]# id -u z
1006
[root@localhost scripts]# cat test.sh 
#!/bin/bash
#
if [ $# -lt 1 ];then
	echo "Please input userName"
	exit 4
fi

userId=$(id -u $1)
let var=$userId%2

if [ $var -eq 0 ];then
	echo "ou shu"
else
	echo "ji shu"
fi

7、lvm基本应用以及扩展缩减实现。

[root@localhost ~]# fdisk -l

Disk /dev/sda: 128.8 GB, 128849018880 bytes
255 heads, 63 sectors/track, 15665 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x0008d864

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64       13119   104857600   8e  Linux LVM
/dev/sda4           13119       15665    20458488+   5  Extended
/dev/sda5           13119       13772     5252934+  8e  Linux LVM
/dev/sda6           13773       14034     2104483+  8e  Linux LVM

[root@localhost ~]# pvcreate /dev/sda5
  Physical volume "/dev/sda5" successfully created
[root@localhost ~]# pvs
  PV         VG       Fmt  Attr PSize   PFree 
  /dev/sda2  VolGroup lvm2 a--u 100.00g     0 
  /dev/sda5           lvm2 ----  10.00g 10.00g
  
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree
  VolGroup   1   3   0 wz--n- 100.00g    0 
[root@localhost ~]# 
[root@localhost ~]# vgcreate vg /dev/sda5
  Volume group "vg" successfully created
[root@localhost ~]# vgs
  VG       #PV #LV #SN Attr   VSize   VFree 
  VolGroup   1   3   0 wz--n- 100.00g     0 
  vg         1   0   0 wz--n-  10.00g 10.00g
 
[root@localhost ~]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_home VolGroup -wi-ao---- 46.00g                                                    
  lv_root VolGroup -wi-ao---- 50.00g                                                    
  lv_swap VolGroup -wi-ao----  4.00g                                                    
[root@localhost ~]# lvcreate -L 6G -n lvt vg
  Logical volume "lvt" created.
[root@localhost ~]# lvs
  LV      VG       Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  lv_home VolGroup -wi-ao---- 46.00g                                                    
  lv_root VolGroup -wi-ao---- 50.00g                                                    
  lv_swap VolGroup -wi-ao----  4.00g                                                    
  lvt     vg       -wi-a-----  6.00g  
  
扩展lv
[root@localhost ~]# mkfs.ext4 /dev/vg/lvt  
[root@localhost ~]# mount /dev/vg/lvt /mnt/
[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  3.4G   44G   8% /
tmpfs                 2.0G   72K  2.0G   1% /dev/shm
/dev/sda1             477M   42M  411M  10% /boot
/dev/mapper/VolGroup-lv_home
                       46G   52M   43G   1% /home
/dev/mapper/vg-lvt    5.8G   12M  5.5G   1% /mnt

[root@localhost ~]# lvextend -L +1G /dev/vg/lvt
  Size of logical volume vg/lvt changed from 6.00 GiB (1536 extents) to 7.00 GiB (1792 extents).
  Logical volume lvt successfully resized.
  
[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  3.4G   44G   8% /
tmpfs                 2.0G   72K  2.0G   1% /dev/shm
/dev/sda1             477M   42M  411M  10% /boot
/dev/mapper/VolGroup-lv_home
                       46G   52M   43G   1% /home
/dev/mapper/vg-lvt    5.8G   12M  5.5G   1% /mnt

[root@localhost ~]# resize2fs /dev/vg/lvt
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/vg/lvt is mounted on /mnt; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/vg/lvt to 1835008 (4k) blocks.
The filesystem on /dev/vg/lvt is now 1835008 blocks long.

[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  3.4G   44G   8% /
tmpfs                 2.0G   72K  2.0G   1% /dev/shm
/dev/sda1             477M   42M  411M  10% /boot
/dev/mapper/VolGroup-lv_home
                       46G   52M   43G   1% /home
/dev/mapper/vg-lvt    6.8G   14M  6.5G   1% /mnt    

缩小
[root@localhost ~]# umount /mnt

[root@localhost ~]# e2fsck -f /dev/vg/lvt
e2fsck 1.41.12 (17-May-2010)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/vg/lvt: 11/458752 files (0.0% non-contiguous), 65023/1835008 blocks

[root@localhost ~]# resize2fs /dev/vg/lvt 5G
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/vg/lvt to 1310720 (4k) blocks.
The filesystem on /dev/vg/lvt is now 1310720 blocks long.

[root@localhost ~]# lvreduce -L 5G /dev/vg/lvt
  WARNING: Reducing active logical volume to 5.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce vg/lvt? [y/n]: y
  Size of logical volume vg/lvt changed from 7.00 GiB (1792 extents) to 5.00 GiB (1280 extents).
  Logical volume lvt successfully resized.

[root@localhost ~]# mount /dev/vg/lvt /mnt

[root@localhost ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       50G  3.4G   44G   8% /
tmpfs                 2.0G   72K  2.0G   1% /dev/shm
/dev/sda1             477M   42M  411M  10% /boot
/dev/mapper/VolGroup-lv_home
                       46G   52M   43G   1% /home
/dev/mapper/vg-lvt    4.8G   12M  4.6G   1% /mnt
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值