一、建立一個10G分區,並格式爲ext4文件系統;node
(1) 要求其block大小爲2048, 預留空間百分比爲2, 卷標爲MYDATA, 默認掛載屬性包含acl;linux
# 建立10G分區 [root@CentOS7 ~]# fdisk /dev/sdb 命令(輸入 m 獲取幫助):n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p 分區號 (1-4,默認 1):1 起始 扇區 (2048-104857599,默認爲 2048): 將使用默認值 2048 Last 扇區, +扇區 or +size{K,M,G} (2048-104857599,默認爲 104857599):+10G 分區 1 已設置爲 Linux 類型,大小設爲 10 GiB 命令(輸入 m 獲取幫助):p 磁盤 /dev/sdb:53.7 GB, 53687091200 字節,104857600 個扇區 Units = 扇區 of 1 * 512 = 512 bytes 扇區大小(邏輯/物理):512 字節 / 512 字節 I/O 大小(最小/最佳):512 字節 / 512 字節 磁盤標籤類型:dos 磁盤標識符:0xbde73b0a 設備 Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux 命令(輸入 m 獲取幫助):w The partition table has been altered! Calling ioctl() to r
# 設置文件系統類型並指定參數設置 [root@CentOS7 ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdb1 mke2fs 1.42.9 (28-Dec-2013) 文件系統標籤=MYDATA OS type: Linux 塊大小=2048 (log=1) 分塊大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 655360 inodes, 5242880 blocks 104857 blocks (2.00%) reserved for the super user 第一個數據塊=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: 完成 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 # 設置文件系統的默認掛載屬性包含acl [root@CentOS7 ~]# tune2fs -o acl /dev/sdb1 tune2fs 1.42.9 (28-Dec-2013)
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;git
# 建立掛載文件夾 [root@CentOS7 ~]# mkdir -pv /data/mydata mkdir: 已建立目錄 "/data" mkdir: 已建立目錄 "/data/mydata" [root@CentOS7 ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata [root@CentOS7 ~]# mount /dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime,seclabel,data=ordered)
mount -o options:(掛載文件系統的選項) async:異步模式; sync:同步模式; atime/noatime:是否更新訪問時間戳,包含目錄和文件; diratime/nodiratime:是否更新目錄的訪問時間戳 auto/noauto:是否支持自動掛載 exec/noexec:是否支持應用程序自動運行 dev/nodev:是否支持在此文件系統上使用設備文件; remount:從新掛載 user/nouser:是否容許普通用戶掛載此設備 acl:啓用此文件系統上的acl功能 注意:上述選項可多個同時使用,彼此使用逗號分隔;
二、建立一個大小爲1G的swap分區,並建立好文件系統,並啓用之;shell
[root@CentOS7 ~]# fdisk /dev/sdb 命令(輸入 m 獲取幫助):n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p 分區號 (2-4,默認 2):2 起始 扇區 (20973568-104857599,默認爲 20973568): 將使用默認值 20973568 Last 扇區, +扇區 or +size{K,M,G} (20973568-104857599,默認爲 104857599):+1G 分區 2 已設置爲 Linux 類型,大小設爲 1 GiB 命令(輸入 m 獲取幫助):t 分區號 (1,2,默認 2):2 Hex 代碼(輸入 L 列出全部代碼):82 已將分區「Linux」的類型更改成「Linux swap / Solaris」 命令(輸入 m 獲取幫助):p 磁盤 /dev/sdb:53.7 GB, 53687091200 字節,104857600 個扇區 Units = 扇區 of 1 * 512 = 512 bytes 扇區大小(邏輯/物理):512 字節 / 512 字節 I/O 大小(最小/最佳):512 字節 / 512 字節 磁盤標籤類型:dos 磁盤標識符:0xbde73b0a 設備 Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 83 Linux /dev/sdb2 20973568 23070719 1048576 82 Linux swap / Solaris 命令(輸入 m 獲取幫助):w [root@CentOS7 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@CentOS7 ~]# mkswap /dev/sdb2 正在設置交換空間版本 1,大小 = 1048572 KiB 無標籤,UUID=b30e0963-f82f-4a2f-8fb4-5f763d5f78bd [root@CentOS7 ~]# free -m total used free shared buff/cache available Mem: 481 122 211 4 146 312 Swap: 1023 0 1023
三、寫一個腳本vim
(1)、獲取並列出當前系統上的全部磁盤設備;centos
(2)、顯示每一個磁盤設備上每一個分區相關的空間使用信息;安全
[root@mylinux tmp]# vim diskinfo.sh
#!/bin/bash # 獲取並列出當前系統上的全部磁盤設備 echo "$(fdisk -l /dev/[sh]d[a-z] | grep -o '^Disk /dev/[sh]d[a-z]')" # 顯示分區空間使用信息: diskDev=$(fdisk -l /dev/[sh]d[a-z] | grep -o '^/dev/[sh]d[a-z][[:digit:]]') for i in $diskDev do echo "$(df -lh $i)\n" done
[root@mylinux tmp]# chmod +x diskinfo.sh [root@mylinux tmp]# ./diskinfo.sh Disk /dev/sda Disk /dev/sdb Disk /dev/sdc Disk /dev/sdd Disk /dev/sde Filesystem Size Used Avail Use% Mounted on /dev/sda1 297M 158M 140M 54% /boot\n Filesystem Size Used Avail Use% Mounted on devtmpfs 479M 0 479M 0% /dev\n Filesystem Size Used Avail Use% Mounted on devtmpfs 479M 0 479M 0% /dev\n Filesystem Size Used Avail Use% Mounted on devtmpfs 479M 0 479M 0% /dev\n Filesystem Size Used Avail Use% Mounted on devtmpfs 479M 0 479M 0% /dev\n
四、總結RAID的各個級別及其組合方式和性能的不一樣;bash
RAID 0:將多個磁盤並列起來,成爲一個大硬盤。存放數據時,其將數據分段同時寫入,速度快。app
優勢:並行I/O,讀寫速度最快。dom
缺點:不具備冗餘功能,有一個硬盤壞,則數據所有丟失。
最少磁盤數:2
RAID 1:兩組相同的磁盤系統互做鏡像,在主硬盤上存放數據的同時也在鏡像硬盤上寫相同的數據。
當主硬盤(物理)損壞時,鏡像硬盤則代替主硬盤的工做,至關於備份。
優勢:容許單個磁盤出錯,可靠性最高,數據安全性在全部的RAID級別上來講是最好的。
缺點:速度沒有提升,其磁盤的利用率卻只有50%,是全部RAID上磁盤利用率最低的一個級別。
最少磁盤數:2
RAID 3:以一個硬盤來存放數據的奇偶校驗位,數據則分段存儲於其他硬盤中。
像RAID0同樣以並行的方式來存放數據,但因爲每次存放都須要進行校驗,速度沒有RAID0快。
校驗盤固定爲其中一塊硬盤。
優勢:寫入性能最好,容許單個磁盤出錯,可靠性高,且磁盤利用率高(可用空間爲n-1)
缺點:沒有多任務,做爲存放校驗位的硬盤,工做負荷會很大,由於每次寫操做,都會把生成的校驗信息寫入該磁盤,而其它磁盤的負荷相對較小,這會對性能有必定的影響
最少磁盤數:3
RAID 4:跟Raid3相同,區別在於Raid3只能單任務,Raid4 具有多任務及容錯功能 Parity 磁盤驅動器形成性能瓶頸
RAID 5:在RAID 3的基礎上,進行一些改進,奇偶校驗數據均勻存放在陣列中的各個盤上,
優勢:容許單個磁盤出錯,可靠性高,相對Raid3來講,硬盤負荷相對較小。
缺點:每次存放數據會進行奇偶校驗,增長工做負荷。損失一塊硬盤空間。
最少磁盤數:3
RAID 6:相似於RAID 5,但其奇偶校驗盤爲2個,均勻存放在陣列中的各個盤上。
優勢:容許2塊磁盤出錯,可靠性比RAID更高
缺點:存放數據時增長工做負荷,空間利用率低,爲n-2
最少磁盤數:4
RAID-10:先將硬盤兩兩一組作成RAID-1,而後再將這一組組的RAID-1作成RAID-0。
優勢:讀、寫性能提高,容許同一組內1個硬盤損壞
缺點:可用空間50%
最少磁盤數:4
RAID-01:先將硬盤分爲2組,每一組硬盤單獨作成RAID-0,而後將這兩組硬盤作成RAID-1。
優勢:讀、寫性能提高,有容錯能力,能夠損壞任意一組的硬盤(能夠一組全壞)
缺點:可用空間50%
最少磁盤數:4
五、建立一個大小爲10G的RAID1,要求有一個空閒盤,並且CHUNK大小爲128k;
#對硬盤sde分配3個分區,大小均爲10G,設置分區類型爲fd(Linux raid auto) [root@mylinux ~]# fdisk /dev/sde 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-125829119, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-125829119, default 125829119): +10G Partition 1 of type Linux and of size 10 GiB is set 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-125829119, default 20973568): Using default value 20973568 Last sector, +sectors or +size{K,M,G} (20973568-125829119, default 125829119): +10G Partition 2 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): 3 First sector (41945088-125829119, default 41945088): Using default value 41945088 Last sector, +sectors or +size{K,M,G} (41945088-125829119, default 125829119): +10G Partition 3 of type Linux and of size 10 GiB is set Command (m for help): t Partition number (1-3, default 3): 1 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1-3, default 3): 2 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1-3, default 3): 3 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): p Disk /dev/sde: 64.4 GB, 64424509440 bytes, 125829120 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: 0x1646a2bb Device Boot Start End Blocks Id System /dev/sde1 2048 20973567 10485760 fd Linux raid autodetect /dev/sde2 20973568 41945087 10485760 fd Linux raid autodetect /dev/sde3 41945088 62916607 10485760 fd Linux raid autodetect [root@mylinux ~]# partx -a /dev/sde partx: /dev/sde: error adding partitions 1-2
[root@mylinux ~]# yum install mdadm #個別須要使用yum安裝mdadm軟件包 # 使用mdadm命令建立軟raid [root@mylinux ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 -c 128 /dev/sde{1..3} mdadm: Note: this array has metadata at the start and may not be suitable as a boot device. If you plan to store '/boot' on this device please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started.
PS:語法格式:mdadm [mode] <raiddevice> [options] <component-devices> 建立:-C -n #: 使用#個塊設備來建立此RAID; -l #:指明要建立的RAID的級別,mdadm支持的RAID級別:LINEAR, RAID0, RAID1, RAID4, RAID5, RAID6, RAID10; -a {yes|no}:自動建立目標RAID設備的設備文件; -c CHUNK_SIZE: 指明塊大小; -x #: 指明空閒盤的個數。 裝配: -A 監控: -F 管理: -f:將磁盤人工標記爲損壞(faulty) -r:將塊設備從RAID卷中移除 -a:向RAID卷中添加塊設備 -S, --stop:拆除RAID卷,釋放全部塊設備
# 驗證結果:可以使用mdadm -D 或 cat /proc/mdstat [root@localhost tmp]# mdadm -D /dev/md0
六、建立一個大小爲4G的RAID5設備,chunk大小爲256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,並且不更新訪問時間戳,且支持acl功能;
# 實驗環境搭建,新建三個2GB大小的分區,以下sde{5,6,7},並設置分區格式爲fd [root@localhost tmp]# fdisk /dev/sde Device Boot Start End Blocks Id System /dev/sde1 2048 20973567 10485760 fd Linux raid autodetect /dev/sde2 20973568 41945087 10485760 fd Linux raid autodetect /dev/sde3 41945088 62916607 10485760 fd Linux raid autodetect /dev/sde4 62916608 104859647 20971520 5 Extended /dev/sde5 62918656 67112959 2097152 fd Linux raid autodetect /dev/sde6 67115008 71309311 2097152 fd Linux raid autodetect /dev/sde7 71311360 75505663 2097152 fd Linux raid autodetect
[root@localhost tmp]# mdadm -C /dev/md1 -a yes -l 5 -n 3 -c 256 /dev/sde{5,6,7} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@localhost tmp]# mke2fs -t ext4 -L 'MD1' /dev/md1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label=MD1 OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262144 inodes, 1047552 blocks 52377 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 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 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]# mkdir /backup #建立掛載目錄 [root@localhost ~]# vim /etc/fstab #修改配置文件設置開機自動掛載,在文件最後面添加如下內容。 /dev/md1 /backup ext4 noatime,acl 0 0 # 掛載使用-a [root@localhost ~]# mount -a [root@localhost ~]# mount #查看已掛載狀況 ...(中間省略)... /dev/md1 on /backup type ext4 (rw,noatime,seclabel,stripe=128,data=ordered)
七、寫一個腳本
(1) 接受一個以上文件路徑做爲參數;
(2) 顯示每一個文件擁有的行數;
(3) 總結說明本次共爲幾個文件統計了其行數;
[root@localhost bin]# vim sumfile.sh #!/bin/bash # declare -i sumfile=0 if [ $# -lt 1 ];then echo "Error: Please enter at least one path." exit 1 fi for i in $@; do if [ -f $i ];then echo "Filename:$i lines:`wc -l $i | cut -d" " -f1` " let sumfile++ else echo "Error: $i is not a correct file path." fi done echo "Total files count is: $sumfile"
# 命令執行 [root@localhost bin]# bash -n sumfile.sh # 測試命令是否存在語法錯誤 [root@localhost bin]# bash sumfile.sh /etc/passwd /etc/fstab Filename:/etc/passwd lines:33 Filename:/etc/fstab lines:12 Total files count is: 2 # 錯誤測試: [root@localhost bin]# bash sumfile.sh # 不輸入文件路徑做爲參數時候 Error: Please enter at least one path. [root@localhost bin]# bash sumfile.sh /etc/passwd /etc/ # 其中一個參數不是文件,而是文件夾時 Filename:/etc/passwd lines:33 Error: /etc/ is not a correct file path. Total files count is: 1
八、寫一個腳本
(1) 傳遞兩個以上字符串看成用戶名;
(2) 建立這些用戶;且密碼同用戶名;
(3) 總結說明共建立了幾個用戶;
[root@localhost bin]# vim adduser2.sh #!/bin/bash # declare usercount=0 if [ $# -lt 2 ];then echo "Error: Please enter two or more usernames." exit 1 fi for i in $@; do id $i &> /dev/null if [ $? -eq 0 ] then echo "$i is already exist" else useradd $i echo "$i" | passwd --stdin $i &> /dev/null let usercount++ echo "username: $i add finished. " fi done echo "Total add user counts is $usercount"
# 腳本執行 [root@localhost bin]# bash adduser2.sh terry1 terry2 terry3 username: terry1 add finished. username: terry2 add finished. username: terry3 add finished. Total add user counts is 3 # 錯誤演示 [root@localhost bin]# bash adduser2.sh Error: Please enter two or more usernames. [root@localhost bin]# bash adduser2.sh terry1 te terry1 is already exist username: te add finished. Total add user counts is 1
九、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
# 新建20個用戶並計算他們ID總和的shell腳本: [root@localhost bin]# vim sumid.sh #!/bin/bash # declare -i sumid=0 if [ ! $UID -eq 0 ]; then echo "Only root have permission to do these." exit 1 fi for i in {1..20}; do if id visitor$i &> /dev/null; then echo "visitor$i exists." else useradd visitor$i if [ $? -eq 0 ]; then echo "visitor$i" | passwd --stdin visitor$i &> /dev/null echo "Add visitor$i finished." fi fi ids=$(id -u visitor$i) let sumid+=ids done echo "UID sum is:$sumid"
# 上述shell腳本中統計ID總和的兩條計算命令要放在if條件外,這樣能夠避免因個別用戶ID已經建立致使統計結果不許確。
[root@localhost bin]# bash sumid.sh Add visitor1 finished. ...(中間省略)... Add visitor20 finished. UID sum is:20490
# 刪除用戶腳本:由於上述腳本測試過程當中執行屢次,有部分用戶已經建立好了,爲了更好地測試,寫個腳本刪除 [root@localhost bin]# vim deluser.sh #!/bin/bash # for i in {1..20}; do name=visitor userdel -rf $name$i echo "User: $name$i had been delete completed." done [root@localhost bin]# bash -n deluser.sh [root@localhost bin]# bash deluser.sh User: visitor1 had been delete completed. ...(中間省略)... User: visitor20 had been delete completed.
十、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
[root@localhost bin]# vim sum1.sh #!/bin/bash # declare -i sum1=0 declare -i sum2=0 for file in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab}; do l1=`grep '^#' $file | wc -l` l2=`grep '^[[:space:]]\+$' $file | wc -l` let sum1+=$l1 let sum2+=$l2 done echo "Total # lines: $sum1" echo "Total blank lines: $sum2" [root@localhost bin]# bash sum1.sh grep: /etc/rc.d/rc.sysinit: No such file or directory # CentOS7沒有該文件 grep: /etc/rc.d/rc.sysinit: No such file or directory Total # lines: 38 Total blank lines: 3 [root@localhost bin]# bash sum1.sh # 在CentOS6.5上運行結果 Total # lines: 91 Total blank lines: 6
十一、寫一個腳本,顯示當前系統上全部默認shell爲bash的用戶的用戶名、UID以及此類全部用戶的UID之和;
[root@localhost bin]# vim sumuid.sh #!/bin/bash # 變量user是存放全部符合條件的uid # for循環是統計全部的uid之和,結果傳遞到sum # username_id是存放全部符合條件的username:uid # \n 是換行輸出,目的爲了輸出對齊。 declare -i sum=0 user=$(grep '/bin/bash$' /etc/passwd | cut -d : -f 3) username_id=$(grep '/bin/bash$' /etc/passwd | cut -d : -f 1,3) for i in $user;do username=`echo $i | cut -d : -f 1` uid=`echo $i | cut -d : -f 3` let sum+=$uid done echo -e "當前系統上全部默認shell爲bash的用戶有如下列表:\n$username_id" echo "以上全部的UID之和爲: $sum" ~ [root@localhost bin]# bash sumuid.sh 當前系統上全部默認shell爲bash的用戶有如下列表: root:0 terry:1000 user1:1001 user2:1002 user3:1003 ...(中間省略)... visitor19:1033 visitor20:1034 以上全部的UID之和爲: 35595
十二、寫一個腳本,顯示當前系統上全部,擁有附加組的用戶的用戶名;並說明共有多少個此類用戶;
[root@localhost bin]# vim guserlist1.sh #!/bin/bash declare -i count=0 userlist=$(cat /etc/passwd | cut -d : -f 1) for username in $userlist;do id $username | cut -d ' ' -f 3 |grep [,] &> /dev/null if [ $? -eq 0 ] then echo "$username" count=$[$count+1] fi done echo "擁有附加組的用戶數爲:$count"
[root@localhost bin]# bash guserlist1.sh postfix 擁有附加組的用戶數爲:1
1三、建立一個由至少兩個物理卷組成的大小爲20G的卷組;要求,PE大小爲8M;而在卷組中建立一個大小爲5G的邏輯卷mylv1,格式化爲ext4文件系統,開機自動掛載至/users目錄,支持acl;
[root@localhost ~]# fdisk /dev/sdb # 分別建立兩個10GB的分區sdb1 、sdb2,並將其文件系統更改成LVM Device Boot Start End Blocks Id System /dev/sdb1 2048 20973567 10485760 8e Linux LVM /dev/sdb2 20973568 41943039 10484736 8e Linux LVM # 建立兩個pv [root@localhost ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@localhost ~]# pvcreate /dev/sdb2 Physical volume "/dev/sdb2" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 centos lvm2 a-- 49.51g 44.00m /dev/sdb1 lvm2 --- 10.00g 10.00g /dev/sdb2 lvm2 --- 10.00g 10.00g # 建立vg,卷組名爲myvg,並指定其PE大小爲8M [root@localhost ~]# vgcreate -s 8m myvg /dev/sdb1 Volume group "myvg" successfully created [root@localhost ~]# vgextend myvg /dev/sdb2 Volume group "myvg" successfully extended [root@localhost ~]# vgdisplay --- Volume group --- VG Name myvg System ID Format lvm2 Metadata Areas 2 Metadata Sequence No 2 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 19.98 GiB PE Size 8.00 MiB Total PE 2558 Alloc PE / Size 0 / 0 Free PE / Size 2558 / 19.98 GiB VG UUID OcIY2r-SbI3-W9sk-z9OT-ZUT1-iiGo-X4tVYg # 在VG中建立名爲mylv1,大小爲5G的LV [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID XvtKZT-15g3-Ajc2-yqLm-Wh3j-Pu2X-6MuvF5 LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-09 22:16:52 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 # 設置文件系統類型爲ext4 [root@localhost ~]# mkfs -t ext4 /dev/myvg/mylv1 # 建立目錄/users並將mylv1掛載至該目錄 [root@localhost ~]# mkdir /users # 臨時掛載的方式,該操做重啓後失效,須要從新掛載 [root@localhost ~]# mount /dev/myvg/mylv1 /users [root@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on /dev/mapper/myvg-mylv1 4.8G 20M 4.6G 1% /users [root@localhost ~]# umount /dev/myvg/mylv1 # 爲了測試,先卸載臨時掛載的文件系統 # 編輯/etc/fstab文件實現開機自動掛載,在最後一行加入如下內容: [root@localhost ~]# vim /etc/fstab /dev/myvg/mylv1 /users ext4 acl 0 0 # 驗證結果:重啓機器後查看,其中有一條顯示已掛載 [root@localhost ~]# shutdown -r now [root@localhost ~]# mount /dev/mapper/myvg-mylv1 on /users type ext4 (rw,relatime,seclabel,data=ordered)
1四、新建用戶magedu;其家目錄爲/users/magedu,然後su切換至此用戶,複製多個文件至家目錄;
# 新建用戶magedu,並設置其家目錄 [root@localhost ~]# useradd magedu -d /users/magedu [root@localhost ~]# cat /etc/passwd # 驗證建立結果 magedu:x:1035:1035::/users/magedu:/bin/bash # 切換至magedu用戶並複製文件至該用戶家目錄 [root@localhost ~]# su - magedu [magedu@localhost ~]$ cp -r /etc/pam.d ~ [magedu@localhost ~]$ cp -r /etc/fstab ~ [magedu@localhost ~]$ ll ~ total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d
1五、擴展mylv1至9G,確保擴展完成後原有數據徹底可用;
# 操做前先將已掛載的邏輯卷卸載 [magedu@localhost ~]$ umount /dev/myvg/mylv1 umount: /users: umount failed: Operation not permitted # 提示權限不足須要切換至管理員用戶操做 [magedu@localhost ~]$ su - Password: Last login: Sun Oct 9 22:29:17 CST 2016 from 192.168.178.1 on pts/2 [root@localhost ~]# umount /dev/myvg/mylv1 umount: /users: target is busy. (In some cases useful info about processes that use the device is found by lsof(8) or fuser(1)) # 依然提示掛載目錄繁忙沒法卸載,嘗試logout再登陸後,卸載完成 [root@localhost ~]# umount /dev/myvg/mylv1 # 擴展mylv1至9G,因爲第一步操做時給了7G,不足,能夠再次擴展 # 此處9G前沒有+ 表明總容量擴展至9G,若是前面加+號,會在原來容量基礎上+9G [root@localhost ~]# lvextend -L 7G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 7.00g [root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 7.00 GiB (896 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 9.00g # 擴展好後,從新掛載文件系統,查看裏面文件是否存在且可用 [root@localhost ~]# mount /dev/myvg/mylv1 /users [root@localhost ~]# ll /users/magedu total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d
1六、縮減mylv1至7G,確保縮減完成後原有數據徹底可用;
# 對文件系統屬性進行操做時先進行卸載 [root@localhost ~]# umount /dev/myvg/mylv1 # 使用e2fsck強制進行檢測或修復文件系統 [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.42.9 (28-Dec-2013) Pass 1: Checking inodes, blocks, and sizes Pass 2: Checking directory structure Pass 3: Checking directory connectivity Pass 4: Checking reference counts Pass 5: Checking group summary information /dev/myvg/mylv1: 52/327680 files (0.0% non-contiguous), 58497/1310720 blocks # 先修改文件系統大小爲7G,再把邏輯卷大小修改成7G [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 9.00g # 文件系統大小修改後,lvs顯示依然是9G [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce mylv1? [y/n]: y Size of logical volume myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 7.00g [root@localhost ~]# mount /dev/myvg/mylv1 /users [root@localhost ~]# ll /users/magedu total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d
1七、對mylv1建立快照,並經過備份數據;要求保留原有的屬主屬組等信息;
# 考慮到mylv1大小爲7G,而且數據備份中可能有數據變更,建立10G大小的快照 # -s指明快照,-n是名字,-p是權限,r是隻讀 [root@localhost ~]# lvcreate -L 10G -p r -s -n mylv1-snap /dev/myvg/mylv1 Reducing COW size 10.00 GiB down to maximum usable size 7.03 GiB. Logical volume "mylv1-snap" created. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID XvtKZT-15g3-Ajc2-yqLm-Wh3j-Pu2X-6MuvF5 LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-10-09 22:16:52 +0800 LV snapshot status source of mylv1-snap [active] LV Status available # open 1 LV Size 7.00 GiB Current LE 896 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:2 --- Logical volume --- LV Path /dev/myvg/mylv1-snap LV Name mylv1-snap VG Name myvg LV UUID ynizpC-ftB1-BAmf-csBt-OV6z-aylr-szJMRb LV Write Access read only LV Creation host, time localhost.localdomain, 2016-10-09 23:13:25 +0800 LV snapshot status active destination for mylv1 LV Status available # open 0 LV Size 7.00 GiB Current LE 896 COW-table size 7.03 GiB COW-table LE 900 Allocated to snapshot 0.00% Snapshot chunk size 4.00 KiB Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:5
# 一般建立快照的主要目的是作備份,可寫一個簡單的備份腳原本自動建立快照,而後掛載進行備份, # 備份完成後又自動卸載和刪除。 # 由於快照建立速度很是快,因此使用快照來備份做業不只能夠解決一致性問題, 也可減小備份時間 # 快照須要掛載後才能看到其內容,而快照只是保存修改前那一刻的狀態,不會自動更新, # 當備份操做完成後,應該刪除快照,以節省空間及減輕系統的I/O負擔 # 快照的查看與備份恢復 [root@localhost ~]# mount /dev/myvg/mylv1-snap /backup mount: /dev/mapper/myvg-mylv1--snap is write-protected, mounting read-only [root@localhost ~]# ll /users/magedu # 原文件裏的內容 total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d [root@localhost ~]# ll /backup/magedu/ # 快照裏的內容,跟原文件內容是一直的 total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d [root@localhost ~]# cp -a /backup/magedu/ /tmp #因爲快照只讀,備份快照內容至/tmp/ [root@localhost ~]# ls /tmp/ filetype.sh ks-script-pw2v6q magedu pam.d.tar uidex.sh yum.log [root@localhost users]# rm -rf magedu # 刪除原文件內容並添加一些新文件 [root@localhost users]# mkdir newfolder [root@localhost users]# touch hello.txt [root@localhost users]# ll total 20 -rw-r--r--. 1 root root 0 Oct 9 23:33 hello.txt drwx------. 2 root root 16384 Oct 9 22:19 lost+found drwxr-xr-x. 2 root root 4096 Oct 9 23:33 newfolder [root@localhost ~]# ls /backup/magedu # 不管對原文件如何操做,快照的內容依然不變 fstab pam.d [root@localhost ~]# ll /tmp/magedu/ total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d [root@localhost ~]# cp -a /tmp/magedu/ /users/ # 將快照內容恢復至原文件位置 [root@localhost ~]# ll /users total 24 -rw-r--r--. 1 root root 0 Oct 9 23:33 hello.txt #新建立的文件依然存在 drwx------. 2 root root 16384 Oct 9 22:19 lost+found drwx------. 4 magedu magedu 4096 Oct 9 22:48 magedu #恢復的文件,時間等屬性不變 drwxr-xr-x. 2 root root 4096 Oct 9 23:33 newfolder #新建立的文件依然存在 [root@localhost ~]# ll /users/magedu #從備份恢復回來的原文件,時間等屬性不變 total 8 -rw-r--r--. 1 magedu magedu 542 Oct 9 22:40 fstab drwxr-xr-x. 2 magedu magedu 4096 Oct 9 22:38 pam.d # 備份還原操做完成後,快照不須要存在,進行卸載刪除 [root@localhost ~]# umount /dev/myvg/mylv1-snap [root@localhost ~]# lvremove /dev/myvg/mylv1-snap Do you really want to remove active logical volume mylv1-snap? [y/n]: y Logical volume "mylv1-snap" successfully removed [root@localhost ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos -wi-ao---- 47.46g swap centos -wi-ao---- 2.00g mylv1 myvg -wi-ao---- 7.00g