Linux学习之磁盘管理实例

1、描述GPT,应该怎么使用
1)GPT描述

  • GPT——全局唯一标识分区表(GUID Partition Table,缩写:GPT)是一个实体硬盘的分区结构。它是EFI(可扩展固件接口标准)的一部分,用来替代BIOS中的主引导记录分区表。但因为MBR分区表不支持容量大于2.2TB(2.2 × 1012字节)的分区,所以也有一些BIOS系统为了支持大容量硬盘而用GPT分区表取代MBR分区表。

  • GPT硬盘中,分区表的位置信息储存在GPT头中。

  • -GPT磁盘分区样式支持最大卷为18 EB(Exabytes)并且每磁盘的分区数没有上限,只受到操作系统限制(由于分区表本身需要占用一定空间,最初规划硬盘分区时,留给分区表的空间决定了最多可以有多少个分区,IA-64版Windows限制最多有128个分区,这也是EFI标准规定的分区表的最小尺寸)。与MBR分区的磁盘不同,至关重要的平台操作数据位于分区,而不是位于非分区或隐藏扇区。另外GPT分区磁盘有备份分区表来提高分区数据结构的完整性。
    2)GPT如何使用
    (1)parted用法和常用选项:

  • 用法:parted [选项]… [设备 [命令 [参数]…]…]

将带有“参数”的命令应用于“设备”。如果没有给出“命令”,则以交互模式运行.

  • 帮助选项:

-h, --help 显示此求助信息

-l, --list 列出所有设别的分区信息

-i, --interactive 在必要时,提示用户

-s, --script从不提示用户

-v, --version显示版本

扫描二维码关注公众号,回复: 4792145 查看本文章

(2)操作命令:

检查 MINOR #对文件系统进行一个简单的检查

cp [FROM-DEVICE] FROM-MINOR TO-MINOR #将文件系统复制到另一个分区

help [COMMAND] #打印通用求助信息,或关于 COMMAND 的信息

mklabel 标签类型 #创建新的磁盘标签 (分区表)

mkfs MINOR 文件系统类型 #在 MINOR 创建类型为“文件系统类型”的文件系统

mkpart 分区类型 [文件系统类型] 起始点 终止点 #创建一个分区

mkpartfs 分区类型 文件系统类型 起始点 终止点 #创建一个带有文件系统的分区

move MINOR 起始点 终止点 #移动编号为 MINOR 的分区

name MINOR 名称 #将编号为 MINOR 的分区命名为“名称”

print [MINOR] #打印分区表,或者分区

quit #退出程序

rescue 起始点 终止点 #挽救临近“起始点”、“终止点”的遗失的分区

resize MINOR 起始点 终止点 #改变位于编号为 MINOR 的分区中文件系统的大小

rm MINOR #删除编号为 MINOR 的分区

select 设备 #选择要编辑的设备

set MINOR 标志 状态 #改变编号为 MINOR 的分区的标志
2、创建一个10G的分区,并格式化为ext4文件系统

1、创建分区
	[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): p

	Disk /dev/sda: 85.9 GB, 85899345920 bytes
	255 heads, 63 sectors/track, 10443 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: 0x000e6a12
	
	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *           1          26      204800   83  Linux
	Partition 1 does not end on cylinder boundary.
	/dev/sda2              26         287     2097152   82  Linux swap / Solaris
	Command (m for help): n
	Command action
	   e   extended
	   p   primary partition (1-4)
	e
	Selected partition 4
	First cylinder (2611-10443, default 2611): 
	Using default value 2611
	Last cylinder, +cylinders or +size{K,M,G} (2611-10443, default 10443): +10G
	
	Command (m for help): p
	
	Disk /dev/sda: 85.9 GB, 85899345920 bytes
	255 heads, 63 sectors/track, 10443 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: 0x000e6a12
	
	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *           1          26      204800   83  Linux
	Partition 1 does not end on cylinder boundary.
	/dev/sda2              26         287     2097152   82  Linux swap / Solaris
	Partition 1 does not end on cylinder boundary.
	/dev/sda3             287        2611    18668544   83  Linux
	/dev/sda4            2611        3916    10483750

2、格式化
	[root@hu ~]# mke2fs -t ext4 -b 2048 -L "MYDATA" -m 20 /dev/sda4
	mke2fs 1.42.9 (28-Dec-2013)
	Filesystem label=MYDATA
	OS type: Linux
	Block size=2048 (log=1)
	Fragment size=2048 (log=1)
	Stride=0 blocks, Stripe width=0 blocks
	655360 inodes, 5242880 blocks
	1048576 blocks (20.00%) reserved for the super user

3、设置开启自动挂载
	[root@hu ~]# vim /etc/fstab
	UUID=9fdfdb9a-6d45-4806-87ec-8510e0c551d6 /mydata ext4 defaults,noatime,noexec 1 1

4、测试开机自动挂载
	[root@hu ~]# mount
	/dev/sr0 on /run/media/hu/CentOS 7 x86_64 type iso9660 (ro,nosuid,nodev,relatime,uid=1000,gid=1000,iocharset=utf8,mode=0400,dmode=0500,uhelper=udisks2)
	tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=100136k,mode=700)
	[root@hu ~]# mount -a
	[root@hu ~]# mount
	tmpfs on /run/user/0 type tmpfs (rw,nosuid,nodev,relatime,seclabel,size=100136k,mode=700)
	/dev/sda3 on /mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered)

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

1、创建分区
	[root@hu ~]# fdisk /dev/sda
	Welcome to fdisk (util-linux 2.23.2).
	
	Changes will remain in memory only, until you decide to write them.
	Be careful before using the write command.
	
	
	Command (m for help): p
	
	Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
	Units = sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 512 bytes
	I/O size (minimum/optimal): 512 bytes / 512 bytes
	Disk label type: dos
	Disk identifier: 0x0000a242
	
	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *        2048      411647      204800   83  Linux
	/dev/sda2          411648    41943039    20765696   8e  Linux LVM
	/dev/sda3        41943040    62914559    10485760   83  Linux
	/dev/sda4        62914560   104857599    20971520    5  Extended
	/dev/sda5        62916608    83888127    10485760   83  Linux
	
	Command (m for help): n
	All primary partitions are in use
	Adding logical partition 5
	First sector (62916608-104857599, default 62916608): 
	Using default value 62916608
	Last sector, +sectors or +size{K,M,G} (62916608-104857599, default 104857599): +1G
	Partition 5 of type Linux and of size 1 GiB is set
	
	Command (m for help): p
	
	Disk /dev/sda: 53.7 GB, 53687091200 bytes, 104857600 sectors
	Units = sectors of 1 * 512 = 512 bytes
	Sector size (logical/physical): 512 bytes / 512 bytes
	I/O size (minimum/optimal): 512 bytes / 512 bytes
	Disk label type: dos
	Disk identifier: 0x0000a242
	
	   Device Boot      Start         End      Blocks   Id  System
	/dev/sda1   *        2048      411647      204800   83  Linux
	/dev/sda2          411648    41943039    20765696   8e  Linux LVM
	/dev/sda3        41943040    62914559    10485760   83  Linux
	/dev/sda4        62914560   104857599    20971520    5  Extended
	/dev/sda5        62916608    65013759     1048576   83  Linux

2、设置为swap并启动
	[root@hu ~]# mkswap /dev/sda5
	Setting up swapspace version 1, size = 1048572 KiB
	no label, UUID=484e9289-1974-48eb-ad1e-aea9d52d5dfc
	[root@hu ~]# free -m
	              total        used        free      shared  buff/cache   available
	Mem:            977         274         227           7         475         521
	Swap:          2047           0        2047
	[root@hu ~]# swapon /dev/sda5
	[root@hu ~]# free -m
	              total        used        free      shared  buff/cache   available
	Mem:            977         275         227           7         475         520
	Swap:          3071           0        3071

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

id10=$(cat /etc/passwd | head -10 | tail -1 | cut -d: -f3)

id20=$(cat /etc/passwd | head -20 | tail -1 | cut -d: -f3)

echo "idsum is $[$id10 + $id20]"                      

2)、运算结果

[root@localhost shfile]# bash idsum.sh
idsum is 70

5、当前主机名保存到hostNAME变量中,若主机名为空,或为localhost,或为localhost.localdomain,则设置为www.magedu.com
1)、脚本

hostNAME=$(hostname)

echo "hostname is $hostNAME"

if [ -z "$hostNAME" -o "$hostNAME"=="localhost" -o "$hostNAME"=="localhost.domain" ];then
        hostname www.magedu.com
fi

echo "hostname is $(hostname)"

2)、结果

[root@localhost shfile]# bash ckname.sh
hostname is localhost.localdomain
hostname is www.magedu.com

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

user=$1

id=$(id -u $1)

echo "usernae is $user"

echo "id is $id"

if [ "$[$id%2]" -eq 0 ];then
        echo "id is 偶数"
else
        echo "id is 奇数"
fi

2)、结果

[root@localhost shfile]# bash jiou.sh hu
usernae is hu
id is 1000
id is 偶数

7、lvm基本应用以及扩展和缩减实现
1)、基本应用
LVM是逻辑盘卷管理(LogicalVolumeManager)的简称,它是Linux环境下对磁盘分区进行管理的一种机制,LVM是建立在硬盘和 分区之上的一个逻辑层,来提高磁盘分区管理的灵活性。通过LVM系统管理员可以轻松管理磁盘分区
2)、扩展与缩减的实现

[root@localhost shfile]# vgextend testvg /dev/sda6			==>扩展
  Volume group "testvg" successfully extended
[root@localhost shfile]# vgdisplay
  --- Volume group ---
  VG Name               testvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  2
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               14.99 GiB
  PE Size               4.00 MiB
  Total PE              3838
  Alloc PE / Size       0 / 0   
  Free  PE / Size       3838 / 14.99 GiB
  VG UUID               eA0ura-onyT-1QNO-ZBlC-C51p-s6Vk-KkUGls	
  			
[root@localhost shfile]# vgreduce testvg /dev/sda6					==>缩减
  Removed "/dev/sda6" from volume group "testvg"
[root@localhost shfile]# vgdesplay
bash: vgdesplay: command not found...
Similar command is: 'vgdisplay'
[root@localhost shfile]# vgdisplay
  --- Volume group ---
  VG Name               testvg
  System ID             
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               10.00 GiB
  PE Size               4.00 MiB
  Total PE              2559
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2559 / 10.00 GiB
  VG UUID               eA0ura-onyT-1QNO-ZBlC-C51p-s6Vk-KkUGls

猜你喜欢

转载自blog.csdn.net/qq_29954175/article/details/84892689