美文网首页
2019-02-23

2019-02-23

作者: 上岸的海龟 | 来源:发表于2019-02-25 23:57 被阅读0次

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

GPT

    GUID磁盘分区表GUID Partition Table,缩写:GPT)其含义为“全局唯一标识磁盘分区表”,是一个实体硬盘的分区表的结构布局的标准。但在GPT硬盘中,分区表的位置信息储存在GPT头中。但出于兼容性考虑,硬盘的第一个扇区仍然用作MBR,之后才是GPT头。GPT最大的好处就是突破了2.2T最大容量的限制,理论支持容量猛增到当前技术看来近乎无限的18EB(1EB=1024TB=1048576GB),允许无限数量的分区。支持几乎有的64位的win0,win8,win7和Vista,以及所对应的服务器都能从GPT启动。

linux系统中如何使用GPT

1.在虚拟机中添加一块硬盘。

[root@centos7 ~]# fdisk -l

Disk /dev/sda: 214.7 GB, 214748364800 bytes, 419430400 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: 0x000c2d19

  Device Boot      Start        End      Blocks  Id  System

/dev/sda1  *        2048    2099199    1048576  83  Linux

/dev/sda2        2099200  106956799    52428800  83  Linux

/dev/sda3      106956800  169871359    31457280  83  Linux

/dev/sda4      169871360  419430399  124779520    5  Extended

/dev/sda5      169873408  174067711    2097152  82  Linux swap / Solaris

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

2.使用parted命令

[root@centos7 ~]# parted /dev/sdb

GNU Parted 3.1

Using /dev/sdb

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted) mklabel gpt                                                     

(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) mkpart

Partition name?  []? gpttest                                             

File system type?  [ext2]?                                               

Start? 1                                                                 

End? 2047

(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      1049kB  2047MB  2046MB              gpttest

(parted) quit                                                           

Information: You may need to update /etc/fstab.

3.创建文件系统

[root@centos7 ~]# mkfs -t 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 

4.挂载使用

[root@centos7 ~]# mkdir /test

[root@centos7 ~]# mount /dev/sdb /test/

[root@centos7 ~]# df -h

Filesystem      Size  Used Avail Use% Mounted on

/dev/sda2        50G  3.1G  47G  7% /

devtmpfs        523M    0  523M  0% /dev

tmpfs          538M    0  538M  0% /dev/shm

tmpfs          538M  7.7M  530M  2% /run

tmpfs          538M    0  538M  0% /sys/fs/cgroup

/dev/sda3        30G  33M  30G  1% /data

/dev/sda1      1014M  158M  857M  16% /boot

tmpfs          108M  32K  108M  1% /run/user/0

/dev/sr0        8.1G  8.1G    0 100% /run/media/root/CentOS 7 x86_64

/dev/sdb        20G  45M  19G  1% /test

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

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

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

(3)可开机自动挂载。

[root@centos7 ~]# 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 0xd302aeb3.

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

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

[root@centos7 ~]# kpartx -af /dev/sdb1

[root@centos7 ~]# mke2fs -t ext4 /dev/sdb1 -b 2048 -m 20 -L MYDATA

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@centos7 ~]# mount -o noauto,noatime /dev/sdb1 /mydata

利用vi修改/etc/fstab 设置开机自动挂载,修改结果如下:

[root@centos7 ~]# cat /etc/fstab

#

# /etc/fstab

# Created by anaconda on Sun Jan  6 07:13:37 2019

#

# Accessible filesystems, by reference, are maintained under '/dev/disk'

# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info

#

UUID=c152db73-6c10-4c84-8da6-aef97a2d57a6 /                      xfs    defaults        0 0

UUID=9f461386-0c26-4891-b3fe-98c76f67c56c /boot                  xfs    defaults        0 0

UUID=70c14145-383d-4603-a9ed-82c428786cb5 /data                  xfs    defaults        0 0

UUID=f7b435d2-980e-43b6-aa27-7d48d9de84ab swap                    swap    defaults        0 0

UUID=485997fa-0f8c-4b42-b176-e81495750d66 /mydata                ext4    defaults        0 0

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

创建一个swap分区,用t命令将分区系统ID改为82,创建结果如下

[root@centos7 ~]# 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): p

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: 0xd302aeb3

  Device Boot      Start        End      Blocks  Id  System

/dev/sdb1            2048    20973567    10485760  83  Linux

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

重载分区表

[root@centos7 ~]# partprobe 

Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.

[root@centos7 ~]# cat /proc/partitions

major minor  #blocks  name

  8        0  209715200 sda

  8        1    1048576 sda1

  8        2  52428800 sda2

  8        3  31457280 sda3

  8        4          0 sda4

  8        5    2097152 sda5

  8      16  20971520 sdb

  8      17  10485760 sdb1

  8      18    1048576 sdb2

  11        0    8491008 sr0

命名分区并激活

[root@centos7 ~]# mkswap /dev/sdb2 -L swap2

Setting up swapspace version 1, size = 1048572 KiB

LABEL=swap2, UUID=ab5749e8-d776-4a62-bcab-334a7be1fd10

[root@centos7 ~]# swapon -a

[root@centos7 ~]# swapon -s

Filename Type Size Used Priority

/dev/sda5                              partition 2097148 8 -1

/dev/sdb2                              partition 1048572 0 -2

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

#!/bin/bash

usera=$(cat -n /etc/passwd | head | tail -n1 | cut -d":" -f3)

userb=$(cat -n /etc/passwd | head -n20| tail -n1 | cut -d":" -f3)

let user=$usera+$userb

echo $user

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

#!/bin/bash

hostName=$(hostname)

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

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

#!/bin/bash

userid=$(id -u $1)

user=$[${userid}%2]

if [ $user -eq 0 ];then

    echo "$1 ID is even"

else

    echo "$1 ID is odd"

fi

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

LVM(Logical Volume Manager,逻辑卷管理器)可以弹性的调整filesystem的容量。

LVM的基本架构

基本应用:

创建LVM系统

1.创建一个15G的卷组,取名给myvg,PE大小为16MB

[root@centos7 ~]# fdisk -l /dev/sdc

Disk /dev/sdc: 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: 0x762d9c3d

  Device Boot      Start        End      Blocks  Id  System

/dev/sdc1            2048    20973567    10485760  8e  Linux LVM

/dev/sdc2        20973568    31459327    5242880  8e  Linux LVM

[root@centos7 ~]# pvcreate /dev/sdc{1,2}

  Physical volume "/dev/sdc1" successfully created.

  Physical volume "/dev/sdc2" successfully created.

[root@centos7 ~]# vgcreate -s 16M myvg /dev/sdc{1,2}

  Volume group "myvg" successfully created

[root@centos7 ~]# vgdisplay myvg

  --- Volume group ---

  VG Name              myvg

  System ID           

  Format                lvm2

  Metadata Areas        2

  Metadata Sequence No  1

  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.97 GiB

  PE Size              16.00 MiB

  Total PE              958

  Alloc PE / Size      0 / 0 

  Free  PE / Size      958 / <14.97 GiB

  VG UUID              Bz4HY9-J9MY-ZeEN-zb9b-U3Oc-zz3s-d2M0l0

2、创建一个逻辑卷,取名为mylv,大小为5G。

[root@centos7 ~]# lvcreate -L 5G -n mylv myvg

  Logical volume "mylv" created.

3.扩容LV+5G

[root@centos7 ~]# lvextend -L +5G /dev/myvg/mylv

  Size of logical volume myvg/mylv changed from 5.00 GiB (320 extents) to 10.00 GiB (640 extents).

  Logical volume myvg/mylv successfully resized.

4.缩减LV

[root@centos7 ~]# resize2fs /dev/myvg/mylv 5G

resize2fs 1.42.9 (28-Dec-2013)

Resizing the filesystem on /dev/myvg/mylv to 1310720 (4k) blocks.

The filesystem on /dev/myvg/mylv is now 1310720 blocks long.

[root@centos7 ~]# lvreduce -L 5G /dev/myvg/mylv

  WARNING: Reducing active logical volume to 5.00 GiB.

  THIS MAY DESTROY YOUR DATA (filesystem etc.)

Do you really want to reduce myvg/mylv? [y/n]: y

  Size of logical volume myvg/mylv changed from 10.00 GiB (640 extents) to 5.00 GiB (320 extents).

  Logical volume myvg/mylv successfully resized.

相关文章

网友评论

      本文标题:2019-02-23

      本文链接:https://www.haomeiwen.com/subject/vprmyqtx.html