第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: 0x5e963a27Device 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 setCommand (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/sdbDisk /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: 0x5e963a27Device Boot Start End Blocks Id System
/dev/sdb1 2048 20973567 10485760 83 Linux
[root@localhost ~]#2. 格式化(創建文件系統) ```shell [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
-