学习总结第四期

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位系统

5、 Mac、Linux、Windows7/8 64bit、windows Server2008 64bits系统都能支持GPT分区格式

GPT磁盘分区是使用parted工具实现的:

Parted用法个常用选项:

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

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

帮助选项:

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

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

    -I , --interactive 在必要是,提示用户

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

    -v , --version 显示版本

     操作命令:

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

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

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

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

        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 的分区的标志

示例:

步骤1、使用parted 工具在设备/dev/sdb上创建磁盘分区

复制代码
 1 [root@localhost ~]# parted /dev/sdb
 2 
 3 GNU Parted 3.1
 4 
 5 Using /dev/sdb
 6 
 7 Welcome to GNU Parted! Type 'help' to view a list of commands.
 8 
 9 (parted)
10 
11  
复制代码

步骤2、接下来需要创建一个分区表(在parted中可以使用help命令打印帮助信息):

复制代码
 (parted) mklabel  

New disk label type? gpt        #我们要正确分区大于2TB的磁盘,应该使用gpt方式的分区表,输入gpt后回车)                                           

(parted) mkpart

Partition name?  []? myNewGPT         #输入创建新区的名字

File system type?  [ext2]? ext4           #输入创建信息的文件系统的类型,如果为ext2类型可直接按回车键             

Start? 0                              #输入分区的起始点

End? 5G                              #输入分区的终止点;GPT分区和MBR分区不同,这里可直接输入起始点如从0开始到5G大小

Warning: The resulting partition is not properly aligned for best performance.         #这里警告新建分区会格式化分区内容

Ignore/Cancel? I                                                          #忽略/取消                                                      

(parted)   
复制代码

步骤3、我们已经创建好了一个分区,下面使用print命令打印出来看                                                    

复制代码
(parted) print                                                            

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

Number  Start   End     Size    File system  Name      Flags

 1      17.4kB  5000MB  5000MB               myNewGPT

 

(parted)
复制代码

步骤4、如果分区错了,可以使用rm命令删除分区,后面跟分区的编号,比如我们要删除上面的分区1,然后打印删除后的结果

复制代码
(parted) rm 1

(parted) print                                                           

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdb: 21.5GB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Disk Flags:

Number  Start  End  Size  File system  Name  Flags

(parted)  

复制代码

步骤5、由于parted内建的mkfs还不够完善,所以完成以后我们可以使用quit命令退出parted并使用 系统的mkfs命令对分区进行格式化了,此时如果使用fdisk -l命令打印分区表会出现警告信息,这是正常的 

复制代码
[root@localhost ~]# fdisk -l /dev/sdb                                    

WARNING: fdisk GPT support is currently new, and therefore in an experimental phase. Use at your own discretion.

 

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: gpt

Disk identifier: E53CF9CC-6556-41C1-B188-25735A0928F7

 

 

#         Start          End    Size  Type            Name

1                 34      9765625    4.7G  Microsoft basic myFirstGPT
复制代码

步骤6、格式化文件系统

复制代码
[root@localhost ~]# mkfs.ext4 /dev/sdb

mke2fs 1.42.9 (28-Dec-2013)

/dev/sdb is entire device, not just one partition!

Proceed anyway? (y,n) y

Filesystem label=

OS type: Linux

Block size=4096 (log=2)

Fragment size=4096 (log=2)

Stride=0 blocks, Stripe width=0 blocks

1310720 inodes, 5242880 blocks

262144 blocks (5.00%) reserved for the super user

First data block=0

Maximum filesystem blocks=2153775104

160 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, 2654208,

         4096000

 

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done  

 

[root@localhost ~]#
复制代码

步骤7、挂载

[root@localhost ~]# mkdir /mnt/myFirstGPT

[root@localhost ~]# mount /dev/sdb /mnt/myFirstGPT/

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

复制代码
[root@localhost ~]# fdisk /dev/sdb

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.

 

Device does not contain a recognized partition table

Building a new DOS disklabel with disk identifier 0x3a339f3a.

 

Command (m for help): n

Partition type:

   p   primary (0 primary, 0 extended, 4 free)

   e   extended

Select (default p): p

Partition number (1-4, default 1): 1

First sector (2048-41943039, default 2048):

Using default value 2048

Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G

Partition 1 of type Linux and of size 10 GiB is set

 

Command (m for help): w
复制代码

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

复制代码
[root@localhost ~]# mkfs -t ext4 -b 2048 -m 20 -L MYDATA /dev/sdb1

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

First data block=0

Maximum filesystem blocks=273678336

320 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

 

Allocating group tables: done                           

Writing inode tables: done                           

Creating journal (32768 blocks): done

Writing superblocks and filesystem accounting information: done  

 

[root@localhost ~]#
复制代码

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

[root@localhost ~]# mkdir /mydata

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

(3)     可开机自动挂载

复制代码
[root@localhost ~]# blkid /dev/sdb1

/dev/sdb1: LABEL="MYDATA" UUID="af467f45-8b5c-426f-9aa6-6327e0a4e064" TYPE="ext4"

[root@localhost ~]# vim /etc/fstab

UUID=af467f45-8b5c-426f-9aa6-6327e0a4e064 /mydata                 ext4    defaults        0 0

[root@localhost ~]# mount –a
复制代码

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

复制代码
[root@localhost ~]# fdisk /dev/sdb

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): n

Partition type:

   p   primary (1 primary, 0 extended, 3 free)

   e   extended

Select (default p): p

Partition number (2-4, default 2): 2

First sector (20973568-41943039, default 20973568):

Using default value 20973568

Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): +1G

Partition 2 of type Linux and of size 1 GiB is set

 

Command (m for help): t

Partition number (1,2, default 2): 2

Hex code (type L to list all codes): 82

Changed type of partition 'Linux' to 'Linux swap / Solaris'

 

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 /dev/sdb

 

Disk /dev/sdb: 21.5 GB, 21474836480 bytes, 41943040 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: 0x3a339f3a

 

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048    20973567    10485760   83  Linux

/dev/sdb2        20973568    23070719     1048576   82  Linux swap / Solaris

[root@localhost ~]# mkswap /dev/sdb2

[root@localhost ~]# swapon /dev/sdb3

 
复制代码

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

复制代码
#!/bin/bash

#fileName addId.sh

#author gongxu

#data 2019/12/9

id1=$(head -10  /etc/passwd | tail -1  | cut  -d:  -f3)
id2=$(head -20   /etc/passwd | tail -1  | cut  -d:  -f3)

id_sum=$[$id1+$id2]

echo “id_sum=$id_sum”

执行脚本:

 [root@localhost ~]# bash .addId.sh

“id_sum=1008”
复制代码

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

hostName=$(hostname)

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

                                

                                           

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

复制代码
#!/bin/bash

#
If ![ $# -eq 1 ] ;then

  echo “please input at lessest one userName”

  exit 2

fi

$uid = grep “^$1\>” /etc/passwd | cut –d: -f3

$uid_yu = $uid % 2

If $uid_yu –eq 0 ; then

  echo “$1的ID号为偶数”

else

  echo “$1的ID号为奇数”

fi         
复制代码

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

LVM基本运用参考此博文LVM

猜你喜欢

转载自www.cnblogs.com/yurong001122/p/11222913.html
今日推荐