第04周作业
- 描述GPT是什么,应该怎么使用
- 答:
- 什么是GPT:GPT即全局唯一标识磁盘分区表(GUID Partition Table,GUID磁盘分区表),是EFI标准的一部分。GPT的产生主要用于解决MBR分区表支持磁盘容量达到上限等问题。关于GPT,有以下几个要点:
- 由于GPT使用64bit数据来存储逻辑块地址,所以使得GPT支持的磁盘容量大大高于使用32bit数据来存储逻辑块地址的MBR分区表
- 由于GPT使用至少16,384Byte的空间来存储分区信息,且每个分区表项的信息使用128Byte的空间进行存储,所以使用GPT的磁盘至少可以划分为最多128个分区。而相对应的,MBR磁盘只能划分为四个主分区/扩展分区
- GPT分区表项使用GUID做为分区类型标识
- GPT分区表项中可存储36个UTF-16字符的分区名,这在MBR分区表中是没有的
- GPT分区表头及分区表项中使用LBA编号来定位磁盘扇区
- GPT分区表头中分别存储有分区表头和分区表项序列的CRC32校验值,用于验证GPT分区表的有效性
- GPT磁盘中存储有备份的分区表头(通常位于磁盘的最后一个扇区)及备份分区表,可用于在必要时恢复错误的分区表头和分区表
- 应该怎么使用:
- 首先,需要确定是否需要使用GPT,通常在以下几种情况下需要使用GPT
- 磁盘容量超过2TB,且最后一个分区起始位置超过2.2TB的位置
- 磁盘容量超过4TB,且不允许存在容量浪费
- 磁盘分区的容量需要超过2TB
- 使用EFI/UEFI而非BIOS来初始化系统(目前,为提高兼容性,主板厂商会为UEFI加入对MBR磁盘的支持功能,但是EFI/UEFI标准本身是不支持MBR的)
- 其次,需要确定所使用的操作系统是否支持GPT
- 大多数的Linux发行版完全支持GPT磁盘
- 32位的Windows操作系统从Windows Server 2003 SP1开始支持GPT,但是从Windows 8开始才能从GPT磁盘启动
- 64位的Windows操作系统从Windows XP Professional x64 Edition / Windows Server 2003开始支持GPT,但是从 Windows Vista开始才能从GPT磁盘启动
- 如何使用GPT
- 在使用
fdisk
工具对磁盘进行分区操作的过程中,可以(在建立分区前,否则会丢失已经有分区信息)使用g
命令将磁盘初始化为GPT磁盘 - 将磁盘初始化为GPT磁盘后,后续的分区、格式化等操作与MBR磁盘的相应操作类似
- 注意:如果要在GPT磁盘上安装并引导Linux操作系统,如果主板固件为BIOS,则磁盘上第一个分区类型必须为
biosboot
,用于存储原本存储于MBR前446个字节空间内的bootloader,其容量不小于1M即可;如果主板固件国UEFI,则磁盘上第一个分区文件系统类型必须为EFI Partition
,且其挂载点必须为/boot/efi
,用于存储EFI引导程序,初始安装时其容量仅数兆即可,一般100M足够使用
- 在使用
- 首先,需要确定是否需要使用GPT,通常在以下几种情况下需要使用GPT
- 什么是GPT:GPT即全局唯一标识磁盘分区表(GUID Partition Table,GUID磁盘分区表),是EFI标准的一部分。GPT的产生主要用于解决MBR分区表支持磁盘容量达到上限等问题。关于GPT,有以下几个要点:
- 答:
-
创建一个10G的分区,并格式化为ext4文件系统,要求:
- block大小为2048,预留空间为20%,卷标为MYDATA
- 挂载至/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳
- 可以开机自动挂载
- 答:假设初始状态下系统内存在未使用的磁盘
/dev/sdb
,其容量大于10G,且其中不存在分区。以下是操作步骤:
- 答:假设初始状态下系统内存在未使用的磁盘
-
分区:
[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: 0x5e963a27 Device Boot Start End Blocks Id System [root@localhost ~]# [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 0x5e963a27. Command (m for help): 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): 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! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# [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: 0x5e963a27 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux [root@localhost ~]#
-
格式化(创建文件系统)
[root@localhost ~]# mke2fs -t ext4 -b 2048 -L MYDATA -m 20 /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 ~]#
- 设置挂载:
- 准备挂载点
[root@localhost ~]# mkdir /mydata [root@localhost ~]#
- 添加以下内容到
/etc/fstab
文件/dev/sdb1 /mydata ext4 defaults,noexec,noatime 0 0
**说明**:defaults选项内含了auto,即自动挂载功能,所以无需另外添加auto选项
- 测试挂载
[root@localhost ~]# mount /mydata [root@localhost ~]# [root@localhost ~]# mount | grep /dev/sdb1 /dev/sdb1 on /mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered) [root@localhost ~]#
- 准备挂载点
-
创建一个大小为1G的swap分区,并启用
-
答:延用上题预设环境,操作步骤如下:
-
分区
[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: 0x5e963a27 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux [root@localhost ~]# [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): 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): 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@MageStudy3 /]# [root@MageStudy3 /]# 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: 0x5e963a27 Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 23070719 1048576 83 Linux [root@MageStudy3 /]# [root@MageStudy3 /]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@MageStudy3 /]# partx -a /dev/sdb partx: /dev/sdb: error adding partitions 1-2 [root@MageStudy3 /]#
- 创建swap文件系统
[root@MageStudy3 /]# mkswap /dev/sdb2 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=014dcc37-05c8-49a3-988e-4dc4f43ead38 [root@MageStudy3 /]#
- 启用swap分区并查看效果
[root@MageStudy3 /]# free -h total used free shared buff/cache available Mem: 1.9G 175M 1.6G 9.4M 130M 1.6G Swap: 2.0G 0B 2.0G [root@MageStudy3 /]# [root@MageStudy3 /]# swapon /dev/sdb2 [root@MageStudy3 /]# [root@MageStudy3 /]# free -h total used free shared buff/cache available Mem: 1.9G 176M 1.6G 9.4M 130M 1.6G Swap: 3.0G 0B 3.0G [root@MageStudy3 /]#
- 注意:
swapon
命令启用交换分区是临时的,重启后即失效,如果需要持久化此设置,需要在/etc/fstab
文件中加入相关条目,如本例中的情况可在/etc/fstab
文件中加入以下行/dev/sdb2 swap swap defaults 0 0
- 注意:
-
-
-
编写脚本计算/etc/passwd文件中第10个用户和第20个用户id号之和
-
答:
#!/bin/bash ################################################################### # File Name : idsum.sh # Author : Tearfalls # Description: Calculate the sum of ids of the 10th user and # 20th user in file /etc/passwd ################################################################### id10=$(cat /etc/passwd | head -10 | tail -1 | cut -d: -f3) id20=$(cat /etc/passwd | head -20 | tail -1 | cut -d: -f3) let idsum=$id10+$id20 echo $idsum
-
-
将当前主机名保存至hostName变量中,主机名如果为空或者为:localhost.localdomain,则将其设置为:www.magedu.com
-
答:
#!/bin/bash ################################################################### # File Name : sethostname1.sh # Author : Tearfalls # Description: Get the HOSTNAME of current system, if HOSTNAME is # null or is equal to "localhost" or # "localhost.localdomain", then set HOSTNAME to # "www.magedu.com". ################################################################### hostName=$(hostname) [ -z "$hostName" -o "$hostName" == "localhost" -o "$hostName" == "localhost.localdomain" ] && hostname www.magedu.com
#!/bin/bash ################################################################### # File Name : sethostname2.sh # Author : Tearfalls # Description: Get the HOSTNAME of current system, if HOSTNAME is # null or is equal to "localhost" or # "localhost.localdomain", then set HOSTNAME to # "www.magedu.com". ################################################################### hostName=$(hostname) if [ -z "$hostName" -o "$hostName" == "localhost" -o "$hostName" == "localhost.localdomain" ]; then hostname www.magedu.com fi
-
-
编写脚本,通过命令行参数传入一个用户名,判断id号是偶数还是奇数
-
答:
#!/bin/bash ################################################################### # File Name : sethostname2.sh # Author : Tearfalls # Description: Get a parameter from command line. # Use the parameter as a username, and # print out whether the id of the user # is odd or not. ################################################################### if [ ! $# -eq 1 ]; then echo "Please give me ONE username. " exit 2 fi userid=$(id -u $1 2> /dev/null) if [ $? -ne 0 ]; then echo "Can't found user by given username. " exit 1 fi if [ $[$userid%2] -eq 0 ]; then echo "The id of given username is EVEN. " else echo "The id of given username is ODD. " fi
-
来源:oschina
链接:https://my.oschina.net/u/4375893/blog/4275558