馬哥教育網絡班20期+第7周課程練習

1、創建一個10G分區,并格式為ext4文件系統;

(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;

[root@localhost ~]# fdisk /dev/sdc    # 創建一個10G分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
 ,
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-121583, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-121583, default 121583): +10G
 
Command (m for help): p
 
Disk /dev/sdc: 1000.1 GB, 1000056291328 bytes
255 heads, 63 sectors/track, 121583 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xe34b702b
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdc1               1        1306    10490413+  83  Linux
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' -O acl /dev/sdc1
mke2fs 1.41.12 (17-May-2010) #其中的-O是啟動指定特性
Invalid filesystem option set: acl # 提示:無效的文件系統選項設置,說明設置失敗
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdc1
# -t指定格式為ext4,-b 2048 指定block大小為2048, -m 2預留空間百分比為2,
# -L 'MYDATA'卷標為MYDATA, 默認掛載屬性包含acl;
mke2fs 1.41.12 (17-May-2010)
Filesystem label=MYDATA
OS type: Linux
Block size=2048 (log=1)
Fragment size=2048 (log=1)
Stride=0 blocks, Stripe width=0 blocks
657408 inodes, 5245206 blocks
104904 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=543162368
321 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
 
Writing inode tables: done                           
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 37 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# tune2fs -o acl /dev/sdc1
# 其中-o acl調整文件系統的默認掛載選項為acl
tune2fs 1.41.12 (17-May-2010)

 (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;

[root@localhost ~]# mount -o noexec,noatime /dev/sdc1 /data/mydata
# 掛載時noexec禁止程序自動運行,noatime不更新文件的訪問時間戳
# 通過查看/etc/mtab文件顯示當前文件已掛載的設備信息
/dev/sdc1 on /data/mydata type ext4 (rw,noexec,noatime)
[root@localhost ~]# cat /proc/mounts | grep '/dev/sdc1'
# 查看內核追蹤到的已掛載的所有設備信息
/dev/sdc1 /data/mydata ext4 rw,seclabel,noexec,noatime,barrier=1,data=ordered 0 0
[root@localhost ~]# mount | grep '/dev/sdc1'

2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;

(動態調整交換分區大?。?/span>

第一種方法:新建分區,將新建的分區增加到交換分區

[root@localhost ~]# fdisk /dev/sdb # 創建一個1G的swap分區
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xadfeb0da.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
#注意:分出一個區做為交換分區,分區類型應為primary,extend不能做為交換分區。
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +1G
 
Command (m for help): t # 轉換為82 (Linux swap / Solaris)
Selected partition 1
Hex code (type L to list codes): 82
Changed system type of partition 1 to 82 (Linux swap / Solaris)
 
Command (m for help): p # 查看分區情況
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xadfeb0da
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         132     1060258+  82  Linux swap / Solaris
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb # 通知內核重新讀取硬盤分區表
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@localhost ~]# mkswap /dev/sdb1 # 創建交換分區
Setting up swapspace version 1, size = 1060252 KiB
no label, UUID=0d83dd2a-248c-4307-a5a0-e0b2a49d4f18
[root@localhost ~]# swapon /dev/sdb1 # 掛載交換分區,啟用
[root@localhost ~]# free –m # 以MB為單位查看內存空間使用狀態
             total       used       free     shared    buffers     cached
Mem:           988        515        473          0         26        194
-/+ buffers/cache:        294        694
Swap:         2059          0       2059
[root@localhost ~]# swapoff /dev/sdb1 #禁用交換分區
[root@localhost ~]# free –m  # 可以看到禁用后swap分區變小了
             total       used       free     shared    buffers     cached
Mem:           988        512        476          0         26        194
-/+ buffers/cache:        292        696
Swap:         1023          0       1023

第二種方法:建立虛擬內存文件

#先創建1G的空文件
[root@localhost ~]# dd if=/dev/zero of=/tmp/swap1 bs=1024 count=1024000
1024000+0 records in
1024000+0 records out
1048576000 bytes (1.0 GB) copied, 3.38308 s, 310 MB/s
[root@localhost ~]# mkswap /tmp/swap1  # 創建交換分區
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=98dc9904-bbb1-40e7-841b-3e86a506324f
[root@localhost ~]# swapon /tmp/swap1  # 掛載交換分區,啟用
[root@localhost ~]# free –m # 以MB為單位查看內存空間使用狀態

3、寫一個腳本

(1)、獲取并列出當前系統上的所有磁盤設備;

(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;

#!/bin/bash
echo "all disk as follows:"
fdisk -l | grep '^Disk[[:space:]]/dev/' | cut -d ' ' -f 2 | cut -d : -f1
for disk in $(fdisk -l | grep '^/dev/sd*' | cut -d ' ' -f 1);do
    echo "$disk partion useages:"
    df -h $disk
done

4、總結RAID的各個級別及其組合方式和性能的不同;

RAID-0:條帶卷,strip;
    讀、寫性能提升;(IO速度最快)
    可用空間:N*min(S1,S2,...)
    無容錯能力
    最少磁盤數:2, 2+(兩個或兩個以上)
RAID-1:  鏡像卷,mirror;
    讀性能提升、寫性能略有下降;
    可用空間:1*min(S1,S2,...)
    有冗余能力(容錯性最好)
     最少磁盤數:2, 2+
RAID-4:
    讀、寫性能提升
    可用空間:(N-1)*min(S1,S2,...)
    有容錯能力:最多壞1塊磁盤
    最少磁盤數:3, 3+        
RAID-5:(性能與數據備份的綜合考慮)
    讀、寫性能提升
    可用空間:(N-1)*min(S1,S2,...)
    有容錯能力:最多壞1塊磁盤
    最少磁盤數:3, 3+
RAID-6:
    讀、寫性能提升
    可用空間:(N-2)*min(S1,S2,...)
    有容錯能力:允許壞2塊磁盤
    最少磁盤數:4, 4+                  
RAID-10:
             讀、寫性能提升
    可用空間:N*min(S1,S2,...)/2
    有容錯能力:每組鏡像最多只能壞1塊磁盤;
    最少磁盤數:4, 4+
RAID-01:
    讀、寫性能提升
    可用空間:N*min(S1,S2,...)/2
    有容錯能力:最多壞1塊
    最少磁盤數:4, 4+
RAID-50:
    RAID 5與RAID 0的組合,先作RAID 5,再作RAID 0。
        讀、寫性能提升
    可用空間:(N-1)*min(S1,S2,...)/2
    有容錯能力:每組鏡像最多只能壞1塊磁盤;
    最少磁盤數:6,6+
JBOD:(Just a Bunch Of Disks)
    將多塊磁盤的空間合并一個大的連續空間使用;
    可用空間:sum(S1,S2,...)

5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;

[root@localhost ~]# fdisk /dev/sdb  # 創建/dev/sdb{1,2,3}分區,都是10G
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-13054, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +10G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (2613-13054, default 2613):
Using default value 2613
Last cylinder, +cylinders or +size{K,M,G} (2613-13054, default 13054): +10G
# 將新建的sdb{1,23}調整為fd( Linux raid autodetect)類型
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)
 
Command (m for help): p  #查看新建的分區
 
Disk /dev/sdb: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7bbe0efa
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  fd  Linux raid autodetect
/dev/sdb2            1307        2612    10490445   fd  Linux raid autodetect
/dev/sdb3            2613        3918    10490445   fd  Linux raid autodetect
 
Command (m for help): w  #保存
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb  # 通知內核重讀硬盤分區表
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# mdadm -C /dev/md0 -a yes -c 128 -n 2 -x 1 -l 1 /dev/sdb{1,2,3}
# 創建/dev/md0,-a自動創建目標RAID設備的設備文件,-c指明塊chunk為128k,-n使用2個 設備來創建此RAID,-x指明1個空閑盤,-l指明要創建的RAID的級別為RAID1
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.
[root@localhost ~]# mdadm -D /dev/md0 # 顯示RAID的詳細信息
/dev/md0:
        Version : 1.2
  Creation Time : Tue Jul 19 11:30:27 2016 #RAID被創建的時間
     Raid Level : raid1 # RAID的等級為raid1
     Array Size : 10489317 (10.00 GiB 10.74 GB) #此RAID可用的磁盤容量
  Used Dev Size : 10489317 (10.00 GiB 10.74 GB) # 每個設備可用容量
   Raid Devices : 2 # 用作raid的可用設備
  Total Devices : 3 # 全部的設備數量
    Persistence : Superblock is persistent
 
    Update Time : Tue Jul 19 11:30:50 2016
          State : clean, resyncing
 Active Devices : 2 # 啟動的設備數量
Working Devices : 3
 Failed Devices : 0
  Spare Devices :1 # 空閑磁盤數量
 
 Rebuild Status : 38% complete # 創建進度
 
           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : cec50dc5:adfd8f8f:573c7429:dd1414f9
         Events : 6
 
    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
 
       2       8       19        -      spare   /dev/sdb3
[root@localhost ~]# cat /proc/mdstat #查看內核是否已經識別新的分區
Personalities : [raid1]
md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0]
      10489317 blocks super 1.2 [2/2] [UU]
      [==================>..]  resync = 92.3% (9684544/10489317) finish=0.0min speed=184684K/sec
     
unused devices: <none>
[root@localhost ~]# cat /proc/mdstat # 查看目前磁盤陣列的狀態
Personalities : [raid1]
md0 : active raid1 sdb3[2](S) sdb2[1] sdb1[0]
      10489317 blocks super 1.2 [2/2] [UU]
     
unused devices: <none>

6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;

[root@localhost ~]# fdisk /dev/sdb # 創建/dev/sdb{1,2,3}分區,都是4G
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xca63c7a1.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-2610, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +4G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (524-2610, default 524):
Using default value 524
Last cylinder, +cylinders or +size{K,M,G} (524-2610, default 2610): +4G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1047-2610, default 1047):
Using default value 1047
Last cylinder, +cylinders or +size{K,M,G} (1047-2610, default 2610): +4G
# 將新建的sdb{1,23}調整為fd( Linux raid autodetect)類型
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): fd
Changed system type of partition 1 to fd (Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): fd
Changed system type of partition 2 to fd (Linux raid autodetect)
 
Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): fd
Changed system type of partition 3 to fd (Linux raid autodetect)
 
Command (m for help): p
 
Disk /dev/sdb: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xca63c7a1
 
   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1         523     4200966   fd  Linux raid autodetect
/dev/sdb2             524        1046     4200997+  fd  Linux raid autodetect
/dev/sdb3            1047        1569     4200997+  fd  Linux raid autodetect
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
 
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb #   # 通知內核重讀硬盤分區表
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# mdadm -C /dev/md0 -a yes -c 256 -n 3 -l 5 /dev/sdb{1,2,3}
# 創建/dev/md0,-a自動創建目標RAID設備的設備文件,-c指明塊chunk為256k,-n使用3個設備來創建此RAID, -l指明要創建的RAID的級別為RAID5
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@localhost ~]# mdadm -D /dev/md0 # 顯示RAID的詳細信息
/dev/md0:
        Version : 1.2
  Creation Time : Thu Jul 21 16:14:48 2016
     Raid Level : raid5
     Array Size : 8393216 (8.00 GiB 8.59 GB)
  Used Dev Size : 4196608 (4.00 GiB 4.30 GB)
   Raid Devices : 3
  Total Devices : 3
    Persistence : Superblock is persistent
 
    Update Time : Thu Jul 21 16:18:12 2016
          State : clean
 Active Devices : 3
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 0
 
         Layout : left-symmetric
     Chunk Size : 256K
 
           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 1f2c8e69:2a3725f0:a194712e:5d6ddf6c
         Events : 22
 
    Number   Major   Minor   RaidDevice State
       0       8       17        0      active sync   /dev/sdb1
       1       8       18        1      active sync   /dev/sdb2
       3       8       19        2      active sync   /dev/sdb3
[root@localhost ~]# cat /proc/mdstat # #查看內核是否已經識別新的分區
 
Personalities : [raid6] [raid5] [raid4]
md0 : active raid5 sdb3[3] sdb2[1] sdb1[0]
      8393216 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/3] [UUU]
     
unused devices: <none>
[root@localhost ~]# mke2fs -t ext4 /dev/md0 # 將/dev/md0格式化為ext4
mke2fs 1.41.12 (17-May-2010)
文件系統標簽=
操作系統:Linux
塊大小=4096 (log=2)
分塊大小=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
525200 inodes, 2098304 blocks
104915 blocks (5.00%) reserved for the super user
第一個數據塊=0
Maximum filesystem blocks=2151677952
65 block groups
32768 blocks per group, 32768 fragments per group
8080 inodes per group
Superblock backups stored on blocks:
    32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632
 
正在寫入inode表: 完成                           
Creating journal (32768 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
 
This filesystem will be automatically checked every 33 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# vim /etc/fstab # 設置開機自動掛載,noatime不更新訪問時間戳
添加:/dev/md0 /backup ext4 acl,noatime 0 0
[root@localhost ~]# mkdir /backup
[root@localhost ~]# mount -a
[root@localhost ~]# df -lh | grep '/dev/md0' # 查看掛載信息
/dev/md0              7.9G  147M  7.4G   2% /backup

7、寫一個腳本

(1) 接受一個以上文件路徑作為參數;

(2) 顯示每個文件擁有的行數;

(3) 總結說明本次共為幾個文件統計了其行數;

#!/bin/bash
#
read -p "please input the file path: " -a line
 
if [ ${#line[@]} -lt 1 ];then
   echo "please input file path, but now you haven't input anything!"
else       
   for i in $(seq 0 $[${#line[*]}-1]);do
      if [ -f ${line[$i]} ];then
        echo "${line[$i]} lines is:`wc -l ${line[$i]} | cut -d ' ' -f 1`"
      else
        echo "please input a true file path!"
        exit 1
      fi
   done
fi
echo "file total number:${#line[@]}"

8、寫一個腳本

(1) 傳遞兩個以上字符串當作用戶名;

(2) 創建這些用戶;且密碼同用戶名;

(3) 總結說明共創建了幾個用戶;

#!/bin/bash
for i in $@;do
    useradd $i
    echo $i | passwd --stdin $i
done
echo "create total users: $#"

9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;

#!/bin/sbin
for i in {1..20};do
    useradd visitor$i
    uid=`grep "\<visitor$i\>" /etc/passwd | cut -d: -f3`
    sumid=$[ $sumid + $uid ]
done
echo "ID sum:$sumid"

10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;

#!/bin/bash
for i in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do
    line1=$(grep "^#" $i | wc -l)
    line2=$(grep "^[[:space:]]" $i | wc -l)
    echo "$i # start line total: $line1"
    echo "$i space line total: $line2"
done

11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;

#!/bin/bash
echo "The users and UID:"
grep '/bin/bash$' /etc/passwd | cut -d: -f1,3
for i in $(grep '/bin/bash$' /etc/passwd | cut -d: -f3)
do
   let sumuid+=$i
done
echo "The uid sum is $sumuid ."

12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;

#!/bin/bash
declare i=1
userlist=`grep "[^:]$" /etc/group | cut -d: -f4`
for username in `cut -d : -f1 /etc/passwd`;do
    echo "$userlist" | grep -o "$username" &> /dev/null
    if [ $? == 0 ];then
        let sum+=i
    fi
    echo "$userlist" | grep -o "$username" | sort -u
done
echo "total: $sum"

13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;

[root@localhost ~]# fdisk /dev/sdb
# 先創建2個分區大小都為10G的PV
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x1358b443.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
 
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
 
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
         switch off the mode (command 'c') and change display units to
         sectors (command 'u').
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-13054, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G
 
Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-13054, default 1307):
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +10G
 
Command (m for help): t
Partition number (1-4): 1
Hex code (type L to list codes): 8e
Changed system type of partition 1 to 8e (Linux LVM)
 
Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 8e
Changed system type of partition 2 to 8e (Linux LVM)
 
Command (m for help): w
The partition table has been altered!
 
Calling ioctl() to re-read partition table.
Syncing disks.
[root@localhost ~]# partx -a /dev/sdb # 通知內核重新讀取硬盤分區表
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# partx -a /dev/sdb # 通知內核重新讀取硬盤分區表
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
[root@localhost ~]# pvcreate /dev/sdb{1,2}  # 將分區創建為物理卷
  Physical volume "/dev/sdb1" successfully created
  Physical volume "/dev/sdb2" successfully created
[root@localhost ~]# pvs # # 簡要查看創建的物理卷  pvdisplay 可查看詳細PV信息
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1       lvm2 a--  10.00g 10.00g
  /dev/sdb2       lvm2 a--  10.00g 10.00g
[root@localhost ~]# vgcreate -s 8M myvg /dev/sdb{1,2}
  Volume group "myvg" successfully created
# 將物理卷創建為卷組名為myvg,PE大小為8M
[root@localhost ~]# vgdisplay # # 查看卷組vg的詳細信息
  --- 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               20.00 GiB
  PE Size               8.00 MiB
  Total PE              2560
  Alloc PE / Size       0 / 0   
  Free  PE / Size       2560 / 20.00 GiB
  VG UUID               3UWOY0-Vm57-4ges-tnX1-Q98N-6haM-sEbIVe
[root@localhost ~]# lvcreate -L 5G -n mylv1 myvg
  Logical volume "mylv1" created
# 在卷組中創建大小為5G的邏輯卷mylv1
[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 # 將mylv1格式化
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 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
 
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
 
This filesystem will be automatically checked every 21 mounts or
180 days, whichever comes first.  Use tune2fs -c or -i to override.
[root@localhost ~]# mkdir /users # 創建/users目錄
[root@localhost ~]# vim /etc/fstab # 設為開機自動掛載至/users
添加  /dev/myvg/mylv1 /users ext4 acl 0 0
[root@localhost ~]# mount -a 
[root@localhost ~]# mount | grep '^/dev/mapper/myvg-mylv1' # 查看掛載情況
/dev/mapper/myvg-mylv1 on /users type ext4 (rw,acl) # 說明掛載成功
[root@localhost ~]# df -lh
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda2                18G  2.5G   15G  16% /
tmpfs                   504M   72K  504M   1% /dev/shm
/dev/sda1               291M   33M  244M  12% /boot
/dev/mapper/myvg-mylv1  5.0G  138M  4.6G   3% /users

14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;

[root@localhost ~]# useradd -d /usr/magedu/ magedu
[root@localhost ~]# su - magedu
[magedu@localhost root]$ cp -r /etc/pam.d /users/magedu
#復制/etc/pam.d目錄至用戶magedu的家目錄

15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;

[magedu@localhost root]$ su - # 切換至root用戶
Password:
[root@localhost ~]# pvs   # 查看pv信息,看物理卷是否能擴展
  PV         VG   Fmt  Attr PSize  PFree
  /dev/sdb1  myvg lvm2 a--u 10.00g  5.00g
  /dev/sdb2  myvg lvm2 a--u 10.00g 10.00g
[root@localhost ~]# vgs   # 查看vg信息,看卷組是否能擴展
  VG   #PV #LV #SN Attr   VSize  VFree
  myvg   2   1   0 wz--n- 20.00g 15.00g
[root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 #  擴展mylv1至9G
  Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# df -lh # #  查看硬盤信息,/dev/mapper/myvg-mylv1還是5G
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              18G  3.0G   14G  19% /
tmpfs                 504M  232K  503M   1% /dev/shm
/dev/sda1             291M   59M  217M  22% /boot
/dev/mapper/myvg-mylv1
                      5.0G  138M  4.6G   3% /users
[root@localhost ~]# resize2fs -p /dev/myvg/mylv1
#通過resize2fs工具來修復文件系統,把新添加的文件系統更新,相當于卸載后重新掛載
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 1
Performing an on-line resize of /dev/myvg/mylv1 to 2359296 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.
[root@localhost ~]# df -lh  # 查看硬件信息
Filesystem            Size  Used Avail Use% Mounted on
/dev/sda2              18G  3.0G   14G  19% /
tmpfs                 504M  232K  503M   1% /dev/shm
/dev/sda1             291M   59M  217M  22% /boot
/dev/mapper/myvg-mylv1 # 已更新,但不一定真正達,9G的10%以內是可以接受的
                      8.9G  140M  8.3G   2% /users
[root@localhost ~]# ls /users/magedu/pam.d/ ## 查看megedu用戶文件是否丟失

16、縮減mylv1至7G,確??s減完成后原有數據完全可用;

[root@localhost ~]# umount /dev/myvg/mylv1  # 卸載分區
[root@localhost ~]# resize2fs /dev/myvg/mylv1 7G # 縮減邏輯卷大小
resize2fs 1.41.12 (17-May-2010)
Please run 'e2fsck -f /dev/myvg/mylv1' first.
[root@localhost ~]# e2fsck -f /dev/myvg/mylv1
e2fsck 1.41.12 (17-May-2010)
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: 121/589824 files (0.0% non-contiguous), 72769/2359296 blocks
[root@localhost ~]# resize2fs /dev/myvg/mylv1 7G
# 縮減邏輯卷大小
resize2fs 1.41.12 (17-May-2010)
Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks.
The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long.
[root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 
# 縮減邏輯卷lv物理邊界大小至3G
  WARNING: Reducing active logical volume to 7.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce myvg/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/magedu/ # 查看邏輯卷
[root@localhost ~]# cd !$  # 查看magedu用戶文件是否丟失
cd /users/magedu/
[root@localhost magedu]# ls
lost+found  magedu
[root@localhost magedu]# cd magedu
[root@localhost magedu]# ls pam.d

17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;

[root@localhost magedu]# cp /etc/issue /users
[root@localhost ~]# lvcreate -s -L 30M -n snaplv -p r /dev/myvg/mylv1
# 創建快照 –s,-L指定快照大小為30M,-n指定快照名字為snaplv,-p r只讀權限
  Rounding up size to full physical extent 32.00 MiB
  Logical volume "snaplv" created.
[root@localhost ~]# mount /dev/myvg/snaplv /mnt # 將快照文件掛載至/mnt
mount: block device /dev/mapper/myvg-snaplv is write-protected, mounting read-only
[root@localhost ~]# cd /mnt # /查看mnt下掛載的issue
[root@localhost mnt]# ls
hgfs  issue  sdb  sdc  sdd
[root@localhost mnt]# cat issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
 
[root@localhost mnt]# vim /users/issue # 編輯源卷的issue文件
[root@localhost mnt]# cat !$ # 查看修改后的issue文件
cat /users/issue
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
New line. # 這一句是添加的。
[root@localhost mnt]# pwd # 再查看/mnt下快照里面的issue
/mnt
[root@localhost mnt]# cat issue # 下面并沒有變化,還是原來的文件內容
CentOS Linux release 6.0 (Final)
Kernel \r on an \m
 
[root@localhost mnt]# cp /etc/rc.d/init.d/functions /users # 復制某文件至/users
[root@localhost mnt]# cd /users/
[root@localhost users]# ls # 發現/users源卷仍可以新增文件等操作
magedu  functions  issue  lost+found

原創文章,作者:二極管,如若轉載,請注明出處:http://www.www58058.com/25876

(0)
二極管二極管
上一篇 2016-07-26
下一篇 2016-07-26

相關推薦

  • linux入門及基本命令

    Linux的入門及基本命令     一.   磁盤分區 1.       分區類型 主分區:1-4,一個硬盤最多四個,最少可以沒有,前提有別的硬盤存在,啟動系統或存數據,不要再分小區,只有個活動狀態。 2.    &nb…

    Linux干貨 2017-07-15
  • N26_第四周

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 [root@node1 skel]# cp -r /etc/skel/home/tuser1 [root@node1 skel]# chmod go-wrx/home/tuser1/   2、編輯/etc/grou…

    Linux干貨 2017-05-26
  • N22-第十周作業

    第十周    1、請詳細描述CentOS系統的啟動流程(詳細到每個過程系統做了哪些事情)    POST–>BootSequence(BOIS)–>BootLoader(MBR)–>Kernel(ramfs或者ramdisk)–&gt…

    Linux干貨 2016-11-07
  • bash特性及用戶和組的管理相關練習

    1、列出當前系統上所有已經登錄的用戶的用戶名,注意同一個用戶登錄多次,則只顯示一次即可: 2、取出最后登錄到系統的用戶的相關信息: 3、取出當前系統上被用戶當做默認shell最多的那個shell: 4、將/etc/passwd中的第三個字段數值最大的后10個用戶的信息全部改為大寫后保存至/tmp/maxuser.txt文件中: 5、取出當前主機的IP地址: …

    2017-10-16
  • N25-第10周博客作業

    請詳細描述CentOS的啟動流程     啟動第一步–加載BIOS         當打開計算機電源,計算機會首先加載BIOS信息,BIOS信息是如此的重要,以至于計算機必須在最開始就找到它,這是因為BIOS中包含了CPU的相關信息、設備啟動順序信息、硬盤信息、內存信息…

    Linux干貨 2017-03-10
  • linux 系統基礎(三)–用戶和組命令使用總結

    1、列出當前系統上所有已經登錄的用戶名,注意:同一個用戶登錄多次,則只顯示一次即可。 [root@node2 ~]# who   root     pts/0        2016-10-…

    Linux干貨 2016-10-08

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-07-27 11:33

    寫的很好,排版也很棒,加油

欧美性久久久久