Linux 基础知识(四)

GPT是什么?

GUID磁盘分区表(GUID Partition Table,缩写:GPT)其含义为“全局唯一标识磁盘分区表”,是一个实体硬盘的分区表的结构布局的标准。它是可扩展固件接口(EFI)标准(被Intel用于替代个人计算机的BIOS)的一部分,被用于替代BIOS系统中的一64bits来存储逻辑块地址和大小信息的主开机纪录(MBR)分区表

 
 
GPT有哪些功能?

  • 在MBR硬盘中,分区信息直接存储于主引导记录(MBR)中(主引导记录中还存储着系统的引导程序)。但在GPT硬盘中,分区表的位置信息储存在GPT头中。但出于兼容性考虑,硬盘的第一个扇区仍然用作MBR,之后才是GPT头。
  • 跟现代的MBR一样,GPT也使用逻辑区块位址(LBA)取代了早期的CHS寻址方式。传统MBR信息存储于LBA 0,GPT头存储于LBA 1,接下来才是分区表本身。64位Windows操作系统使用16,384字节(或32扇区)作为GPT分区表,接下来的LBA 34是硬盘上第一个分区的开始。
  • 苹果公司曾经警告说:“不要假定所有设备的块大小都是512字节。”一些现代的存储设备如固态硬盘可能使用1024字节的块,一些磁光盘(MO)可能使用2048字节的扇区(但是磁光盘通常是不进行分区的)。一些硬盘生产商在计划生产4096字节一个扇区的硬盘,但截至2010年初,这种新硬盘使用固件对操作系统伪装成512字节一个扇区。
  • 使用英特尔架构的苹果机也使用GPT。
  • 为了减少分区表损坏的风险,GPT在硬盘最后保存了一份分区表的副本

 
 
Parted用法个常用选项:

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

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

帮助选项:

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

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

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

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

-v , --version 显示版本

创建一个10G分区,并格式为ext4文件系统

  • 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA,默认挂载属性包含acl;
  • 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
  • 可开机自动挂载

创建分区

fdisk  /dev/sdc
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
The partition table has been altered!

重读磁盘分区表

kpartx -af /dev/sdc

显示分区

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20973567    10485760   83  Linux

创建文件系统

方法一

mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1

方法二

mkfs.ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1

创建挂载目录

mkdir -pv /data/mydaa

配置自动挂载

vim /etc/fstab
/dev/sdb1   /data/mydata  ext4   defaults,noexec,noatime  0 0

 
 

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

创建分区

fdisk  /dev/sdc   
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): 
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): 
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): p

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

   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1            2048    20973567    10485760   83  Linux
/dev/sdc2        20973568    23070719     1048576   82  Linux swap / Solaris

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

重读磁盘分区表

kpartx -af /dev/sdc

创建交换分区

mkswap /dev/sdb2

创建自动挂载

vim /etc/fstab
/dev/sdb2    swap    swap    defaults        0 0

显示交换分区

swapon --show

方法二

dd if=/dev/zero of=/swapfile bs=1G count=1
chmod 600 /swapfile
mkswap /swapfile
vim /etc/fstab
/swapfile         swap       swap    defaults      0 0

 
 

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

#!/bin/bash
#
id1=$(head -10 /etc/passwd | tail -1 | awk -F : '{print $3}')
id2=$(head -20 /etc/passwd | tail -1 | awk -F : '{print $3}')
echo sum=$[$id1+$id2]

 
 

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

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

 
 

编写脚本,通过命令行参数给定一个用户名,判断其ID号是偶数还是奇数

#!/bin/bash
#
if  [ $[ $(id -u $1)%2 ] -eq 0 ]
then
echo $1 ID is  Even numbers
else
echo $1 ID is  Odd numbers
fi

方法二

#!/bin/bash
#
[ $[ $(id -u $1)%2 ] -eq 0 ] && echo $1 ID is  Even numbers || echo $1 ID is  Odd numbers

猜你喜欢

转载自blog.51cto.com/14074807/2336789