馬哥教育網絡班21期-第七周課程練習

第七周作業

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

[root@qq ~]# fdisk /dev/sdb         #使用fdisk命令進行分區;
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x61b9ad0b.
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): p         #用p命令查看現有分區,由于是新盤什么都沒;

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x61b9ad0b

   Device Boot      Start         End      Blocks   Id  System

Command (m for help): n         #新建分區,理論上可以建4個主分區或3個主分區1個擴展分區,建完擴展分區后可建多個邏輯分區;
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-26108, default 1): 
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-26108, default 26108): +10G

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x61b9ad0b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux

Command (m for help): w         #分完分區后,w保存寫入;
The partition table has been altered!

[root@qq ~]# partx -a -n 1 /dev/sdb     #使用partx命令在不關機情況下讓內核重讀分區表
BLKPG: Device or resource busy
error adding partition 1
[root@qq ~]# 
[root@qq ~]# 
[root@qq ~]# partx -a -n 1 /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@qq ~]#
  • (1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;

    [root@qq ~]# mke2fs -t ext4 -b 2048 -m 2 -L "MYDATA"  /dev/sdb1
    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 21 mounts or
    180 days, whichever comes first.  Use tune2fs -c or -i to override.
    
    /dev/sdb1: LABEL="MYDATA" UUID="f4c97dcc-3bd4-413c-b56d-a5179398713d" TYPE="ext4" 
    [root@qq ~]# tune2fs -o acl /dev/sdb1       #設置acl功能;
    tune2fs 1.41.12 (17-May-2010)
    [root@qq ~]# echo $?
    0
  • (2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;

    [root@qq ~]# mount -o noexec,noatime,acl /dev/sdb1 /data/mydata/
    [root@qq ~]# 
    [root@qq ~]# more /etc/mtab | grep /dev/sdb1
    /dev/sdb1 /data/mydata ext4 rw,noexec,noatime,acl 0 0
    [root@qq ~]#

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

Command (m for help): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 2
First cylinder (1307-26108, default 1307): 
Using default value 1307
Last cylinder, +cylinders or +size{K,M,G} (1307-26108, default 26108): +1G

Command (m for help): t
Partition number (1-4): 2
Hex code (type L to list codes): 82         #將分區設置為swap格式,可用l(字母)命令查看所有的分區類型;
Changed system type of partition 2 to 82 (Linux swap / Solaris)

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x61b9ad0b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        1438     1060290   82  Linux swap / Solaris    #已經設置swap類型



[root@qq tmp]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
[root@qq tmp]# partx -a /dev/sdb
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2

[root@qq tmp]# mkswap /dev/sdb2             #用mkswap命令設置為swap格式;
Setting up swapspace version 1, size = 1060284 KiB
no label, UUID=5c6c92b7-7896-454a-bcd3-ee9120b7fb51

[root@qq tmp]# swapon /dev/sdb2             #啟動swap分區;

[root@qq tmp]# free -m          #原來是4G的swap,現在已經是5G多了;
             total       used       free     shared    buffers     cached
Mem:           482        175        307          0         20         71
-/+ buffers/cache:         84        398
Swap:         5131          0       5131

3、寫一個腳本

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

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

    [root@qq tmp]# vim disk.sh 
    
    #!/bin/bash
    #
    fdisk -l | grep -o "^/[^[:space:]]\+"
    df -lh

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

常見的RAID如下:

  • RADI0(條帶卷):

    • 由兩塊或兩塊以上硬盤組成;

    • IO讀寫速度是RAID中最快的,但是無容錯能力;

    • 可用空間:N*min(S1,S2,……)

  • RAID1(鏡像卷):

    • 由兩塊或兩塊以上硬盤相互作鏡像組成;

    • 讀速度由于從多個磁盤讀取,所以速度會相應提高;但是寫入數據要拷貝副本到另外的磁盤可能會導致寫入速度略有下降;有容錯能力,由于另外一個磁盤是鏡像卷,當主磁盤故障時,由于鏡像卷磁盤數據一模一樣,所以不會影響到數據的正常性;

    • 可用空間:1*min(S1,S2,……)

  • RADI5:

    • 最少需要三塊硬盤組成;

    • 有容錯能力,寫入數據的同時還要將數據的奇偶校驗信息分別存放在各個磁盤,當其中一個磁盤損壞時,可通過其它磁盤的數據和奇偶信息去恢復損壞的數據;RAID5可以理解成是在RAID0的基礎上加了奇偶檢驗功能,所以寫入速度比單獨寫入一塊硬盤的速度略慢,但卻比RAID1快;要注意的是,數據塊(chunks)和校驗位必須要按順序即默認為左對稱的方式排放;

    • RAID5理論上只能壞一塊磁盤;

    • 可用空間:(N-1)*min(S1,S2,……)

  • RIAD6:

    • 最少需要四塊硬盤組成;

    • 有容錯能力,可以看作是RAID5的升級版,理論上能壞兩塊磁盤后,還能保持數據正常;

    • 可用空間:(N-2)*min(S1,S1,……)

  • RAID10:

    • 最少需要四塊硬盤組成;

    • 它的基本原理是,先4塊硬盤兩兩先做成2個RAID1,然后這兩個RAID1陣列再繼續做出RAID0模式;讀寫性能有提升,有容錯能力,前提是不要一組鏡像盤同時故障,如果兩組鏡像盤各壞一個,不會損壞數據;

    • 可用空間:N*min(S1,S2,……)/2

  • JBOD:

    • 將多塊磁盤的空間合并一個大的連續空間使用,無容錯能力;

    • 可用空間:sum(S1,S2,……)

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

Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x61b9ad0b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1               1        1306    10490413+  83  Linux
/dev/sdb2            1307        1438     1060290   82  Linux swap / Solaris
/dev/sdb3            1439       26108   198161775    5  Extended
/dev/sdb5            1439        2744    10490413+  fd  Linux raid autodetect   RAID分區類型;
/dev/sdb6            2745        4050    10490413+  fd  Linux raid autodetect
/dev/sdb7            4051        4312     2104483+  fd  Linux raid autodetect
/dev/sdb8            4313        4574     2104483+  fd  Linux raid autodetect
/dev/sdb9            4575        4836     2104483+  fd  Linux raid autodetect
/dev/sdb10           4837        6142    10490413+  fd  Linux raid autodetect
/dev/sdb11           6143        7448    10490413+  fd  Linux raid autodetect
/dev/sdb12           7449        8754    10490413+  fd  Linux raid autodetect
/dev/sdb13           8755       10060    10490413+  fd  Linux raid autodetect
/dev/sdb14          10061       11366    10490413+  8e  Linux LVM   LVM文件系統類型;
/dev/sdb15          11367       12672    10490413+  8e  Linux LVM
/dev/sdb16          12673       13587     7349706   8e  Linux LVM


[root@qq tmp]# mdadm /dev/md0 -C -n 2 -x 1 -a yes -c 128 -l 5 /dev/sdb{11,12,13}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@qq tmp]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Mon Aug  1 18:10:12 2016
     Raid Level : raid5
     Array Size : 10482176 (10.00 GiB 10.73 GB)
  Used Dev Size : 10482176 (10.00 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Mon Aug  1 18:10:12 2016
          State : clean, degraded, recovering   #狀態為clean,說明創建完成
 Active Devices : 1
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 2

         Layout : left-symmetric
     Chunk Size : 128K

 Rebuild Status : 0% complete                   #進度條

           Name : qq.localdomain:0  (local to host qq.localdomain)
           UUID : cd6c0b6b:89de1553:3be0084c:68c9aa06
         Events : 1

    Number   Major   Minor   RaidDevice State
       0       8       27        0      active sync   /dev/sdb11
       3       8       28        1      spare rebuilding   /dev/sdb12

       2       8       29        -      spare   /dev/sdb13  #熱備盤

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

[root@qq tmp]# mdadm /dev/md1 -C -n 3 -a yes -c 256 -l 5 /dev/sdb{7,8,9}
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.

[root@qq tmp]# cat /proc/mdstat 
Personalities : [raid1] [raid6] [raid5] [raid4] 
md1 : active raid5 sdb9[3] sdb8[1] sdb7[0]
      4204544 blocks super 1.2 level 5, 256k chunk, algorithm 2 [3/2] [UU_]
      [==>..................]  recovery = 13.6% (287944/2102272) finish=1.7min speed=16937K/sec

[root@qq tmp]# mke2fs -t ext4 /dev/md1

[root@qq tmp]# vim /etc/fstab 
/dev/md1                /backup                 ext4    defaults,acl,noatime              0 0

[root@qq tmp]# mount -a
[root@qq tmp]# df -lh | grep /dev/md1
/dev/md1        3.9G  8.1M  3.7G   1% /backup

7、寫一個腳本

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

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

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

    #!/bin/bash
    #
    declare -i i=0
    
    if [ $# -lt 1 ]; then
        echo "please input file path,dont input empty"
        exit 2
    fi
    
    for file in $*; do
        if [ -f $file ]; then
            echo "$file line number:$(wc -l $file | cut -d" " -f1)"
            let i++
        else
            echo "please input correct file path"
    
        fi
    done
    
    echo "statistical file count=$i"
    
    測試:
    [root@qq shell_scripts]# bash file-line-1.sh 
    please input file path,dont input empty
    [root@qq shell_scripts]# 
    [root@qq shell_scripts]# 
    [root@qq shell_scripts]# bash file-line-1.sh /etc/issue /etc/grub.conf 
    /etc/issue line number:3
    /etc/grub.conf line number:17
    statistical file count=2
    [root@qq shell_scripts]#

8、寫一個腳本

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

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

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

    #!/binbash
    #
    
    declare -i i=0
    
    if [ $# -lt 2 ];then
        echo "last input two username"
        exit 2
    fi
    
    for username in $@; do
        id $username &> /dev/null
        if [ $? -eq 0 ]; then
        echo "$username exist"
        else
        useradd $username
        echo "$username" | passwd --stdin $username &> /dev/null
        let i++
        fi
    done
    
    echo "add user count: $i"
    
    測試:
    
    [root@qq shell_scripts]# bash two-user.sh qq
    last input two username
    
    [root@qq shell_scripts]# bash two-user.sh user10 110
    user10 exist
    add user count: 1
    [root@qq shell_scripts]# 
    [root@qq shell_scripts]# bash two-user.sh user120 user119
    add user count: 2

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

#!/bin/bash
#

declare -i sum=0;

for ((i=1;i<=20;i++)); do
    useradd visitor$i;
    echo "useradd visitor$i success!"
    sum+=$(id -u visitor$i)
done

echo "All user uid sum: $sum"

測試:
[root@qq serven]# bash user1-20.sh 
useradd visitor1 success!
useradd visitor2 success!
useradd visitor3 success!
useradd visitor4 success!
useradd visitor5 success!
useradd visitor6 success!
useradd visitor7 success!
useradd visitor8 success!
useradd visitor9 success!
useradd visitor10 success!
useradd visitor11 success!
useradd visitor12 success!
useradd visitor13 success!
useradd visitor14 success!
useradd visitor15 success!
useradd visitor16 success!
useradd visitor17 success!
useradd visitor18 success!
useradd visitor19 success!
useradd visitor20 success!
All user uid sum: 11110

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

#!/bin/bash
#
declare -i sum1=0;
declare -i sum2=0;

for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab}; do
    sum1+=$( grep -c '^#' $i )
    sum2+=$( grep -c '^[[:space:]]*$' $i )
done

echo "#start line number: $sum1"
echo "space line number: $sum2"

測試:
[root@qq serven]# bash space-line.sh 
#start line number: 91
space line number: 179

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

#!/bin/bash
#

declare -i sum=0;
declare -i i=0;

grep "\/bin\/bash$" /etc/passwd | awk -v FS=":" '{print $1,"\t",$3}'

for i in $(awk -F: '/bash$/{print $3}' /etc/passwd); do
   let sum+=$i 
done

echo "user uid sum= $sum"

測試:
[root@qq serven]# bash -x shell-bash.sh 
+ declare -i sum=0
+ declare -i i=0
+ awk -v FS=: '{print $1,"\t",$3}'
+ grep '\/bin\/bash$' /etc/passwd
root     0
docker   523
qq   524
myuser   525
magedu   545
++ awk -F: '/bash$/{print $3}' /etc/passwd
+ for i in '$(awk -F: '\''/bash$/{print $3}'\'' /etc/passwd)'
+ let sum+=0
+ for i in '$(awk -F: '\''/bash$/{print $3}'\'' /etc/passwd)'
+ let sum+=523
+ for i in '$(awk -F: '\''/bash$/{print $3}'\'' /etc/passwd)'
+ let sum+=524
+ for i in '$(awk -F: '\''/bash$/{print $3}'\'' /etc/passwd)'
+ let sum+=525
+ for i in '$(awk -F: '\''/bash$/{print $3}'\'' /etc/passwd)'
+ let sum+=545
+ echo 'user uid sum= 2117'
user uid sum= 2117

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

#!/bin/bash
#

declare -i i=0;

for user in `cut -d: -f1 /etc/passwd`; do
    group=$(id $user | cut -d" " -f3 | awk -F, '{print $2}')
    if [ -n "$group" ]; then
    echo $user
    let i++
    fi
done

echo "user number: $i"

測試:
[root@qq serven]# bash sub-group.sh 
bin
daemon
adm
postfix
magedu
user number: 5

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

    Command (m for help): p

Disk /dev/sdb: 214.7 GB, 214748364800 bytes
255 heads, 63 sectors/track, 26108 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: 0x61b9ad0b

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb14          10061       11366    10490413+  8e  Linux LVM
/dev/sdb15          11367       12672    10490413+  8e  Linux LVM
/dev/sdb16          12673       13587     7349706   8e  Linux LVM

[root@qq tmp]# pvcreate /dev/sdb14
  Physical volume "/dev/sdb14" successfully created
[root@qq tmp]# pvcreate /dev/sdb15
  Physical volume "/dev/sdb15" successfully created

[root@qq tmp]# 
[root@qq tmp]# pvs          #顯示pvs信息
  PV         VG   Fmt  Attr PSize  PFree 
  /dev/sdb14      lvm2 ---  10.00g 10.00g
  /dev/sdb15      lvm2 ---  10.00g 10.00g

[root@qq tmp]# vgcreate myvg -s 8 /dev/sdb{14,15}       #創建vg,-s選項設置PM大小;
  Volume group "myvg" successfully created
[root@qq tmp]# 
[root@qq tmp]# vgs          #顯示vg信息
  VG   #PV #LV #SN Attr   VSize  VFree 
  myvg   2   0   0 wz--n- 20.00g 20.00g
[root@qq tmp]# vgdisplay myvg       #顯示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               scLSYH-yaX4-R16R-ZqI2-BrnX-VYnS-4czFEA

[root@qq tmp]# lvcreate -L 5G -n mylv1 myvg     #創建lv
  Logical volume "mylv1" created
[root@qq tmp]# lvs
  LV    VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv1 myvg -wi-a----- 5.00g                                                    
[root@qq tmp]# lvdisplay                        #顯示lv詳細信息;
  --- Logical volume ---
  LV Path                /dev/myvg/mylv1
  LV Name                mylv1
  VG Name                myvg
  LV UUID                xZqrTJ-SIhP-DKhe-yY1o-aspI-mD1G-EWtQu0
  LV Write Access        read/write
  LV Creation host, time qq.localdomain, 2016-08-01 23:10:48 +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     256
  Block device           253:0


[root@qq tmp]# mke2fs -t ext4 /dev/myvg/mylv1       #格式為ext4格式; 
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

[root@qq tmp]# vim /etc/fstab           #更新分區表; 

/dev/md1                /backup                 ext4    defaults,acl,noatime              0 0
/dev/myvg/mylv1         /users                  ext4    defaults,acl                      0 0

[root@qq tmp]# df -lh                   #查看磁盤空間使用情況,lv已經掛載上去;
Filesystem            Size  Used Avail Use% Mounted on
/dev/md1              3.9G  8.1M  3.7G   1% /backup
/dev/mapper/myvg-mylv1
                      4.8G   10M  4.6G   1% /users

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

[root@qq tmp]# useradd -d /users/magedu magedu
[root@qq tmp]# cat /etc/passwd | grep magedu
magedu:x:545:545::/users/magedu:/bin/bash
[root@qq tmp]# 
[root@qq tmp]# su - magedu
[magedu@qq ~]$ pwd
/users/magedu
[magedu@qq ~]$ cp /etc/fstab .
[magedu@qq ~]$ cp -a /etc/ .

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

[root@qq tmp]# lvextend -L 9G /dev/myvg/mylv1       #lv擴展至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@qq tmp]# lvdisplay /dev/myvg/mylv1 
  --- Logical volume ---
  LV Path                /dev/myvg/mylv1
  LV Name                mylv1
  VG Name                myvg
  LV UUID                xZqrTJ-SIhP-DKhe-yY1o-aspI-mD1G-EWtQu0
  LV Write Access        read/write
  LV Creation host, time qq.localdomain, 2016-08-01 23:10:48 +0800
  LV Status              available
  # open                 1
  LV Size                9.00 GiB                   #容量已經增加
  Current LE             1152
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

[root@qq magedu]# resize2fs /dev/myvg/mylv1         #刷新lv
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@qq magedu]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/md1              3.9G  8.1M  3.7G   1% /backup
/dev/mapper/myvg-mylv1
                      8.8G   24M  8.3G   1% /users      #df空間使用情況已經更新;

[root@qq magedu]# pwd
/users/magedu
[root@qq magedu]# cat /etc/fstab                        #可以正常讀取文件

#
# /etc/fstab
# Created by anaconda on Thu Jul 28 00:37:31 2016
……

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

操作須得按步驟來,有一定風險;

1、首先的取消分區掛載;
[root@qq ~]# umount /dev/myvg/mylv1 

2、檢查分區是否正常;
[root@qq ~]# 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: 11/589824 files (0.0% non-contiguous), 74975/2359296 blocks

3、刷新lv;
[root@qq ~]# resize2fs /dev/myvg/mylv1 
resize2fs 1.41.12 (17-May-2010)
The filesystem is already 2359296 blocks long.  Nothing to do!

4、lv縮減至7G;
[root@qq ~]# lvcreate -L 7G /dev/myvg/mylv1 
  Volume group name expected (no slash)
  Run `lvcreate --help' for more information.
[root@qq ~]# 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

5、重新掛載分區;
[root@qq ~]# mount /dev/myvg/mylv1 /users/
[root@qq ~]# df -lh
Filesystem            Size  Used Avail Use% Mounted on
/dev/md1              3.9G  8.1M  3.7G   1% /backup
/dev/mapper/myvg-mylv1
                      6.8G   16M  6.4G   1% /users

6、驗證lv的分區使用情況;正常能否正常讀取,寫入;
[root@qq users]# vim /users/fstab 

iiiii
#
# /etc/fstab
# Created by anaconda on Thu Jul 28 00:37:31 2016
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
……

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

[root@qq users]# lvcreate -L 1GB -n mylv1.snapshot.bak -p r -s /dev/myvg/mylv1  #創建鏡像
  Logical volume "mylv1.snapshot.bak" created
[root@qq users]# lvs
  LV                 VG   Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  mylv1              myvg owi-aos--- 7.00g                                                    
  mylv1.snapshot.bak myvg sri-a-s--- 1.00g      mylv1  0.00   

[root@qq /]# mkdir /snapshot
[root@qq /]# mount /dev/myvg/mylv1.snapshot.bak /snapshot/  掛載鏡像;
mount: block device /dev/mapper/myvg-mylv1.snapshot.bak is write-protected, mounting read-only
[root@qq /]# ls /snapshot/          #查看鏡像內容
fstab  lost+found

原創文章,作者:Net21_仲樂,如若轉載,請注明出處:http://www.www58058.com/31609

(0)
Net21_仲樂Net21_仲樂
上一篇 2016-08-08
下一篇 2016-08-08

相關推薦

  • Linux基礎之安全加密及CA、證書實戰應用

    一、建立私有CA并讓客戶端申請請求,需要兩臺虛擬機完成 1、建立私有CA,生成私鑰 2、CA公司給自己,生成自簽證書 3、為CA提供所需要的目錄及文件(如果事先存在,此步驟可以跳過) 二、CA公司已建立,需要一臺客戶機配合CA簽署證書 1、客戶端用到證書,因此生成私鑰 2、客戶端生成證書簽署請求 3、將請求通過可靠方式發送給CA主機,這里作為測試,因此使用s…

    Linux干貨 2016-10-04
  • RAID and LVM

    RAID          Redundant Arrays of  inexpensive(Independent)Disks,RAID:容錯式廉價磁盤陣列,容錯式獨立磁盤陣列,簡稱RAID,由加利福尼亞大學伯克利分校(University of California-Berkeley)在1988…

    Linux干貨 2016-09-02
  • sed命令及vim編輯器

    sed[option]… 'script' inputfile…選項:-n:不輸出模式空間內容的自動打印-e: 多點編輯-f /PATH/TO/SCRIPT_FILE: 從指定文件中讀取編輯腳本-r: 支持使用擴展正則表達式-i: 原處編輯script:'地址命令' 1.地址定界:(1) 不給地…

    Linux干貨 2016-08-11
  • shell腳本小練習..

    剛剛接觸vim和shell腳本,用幾個小腳本練練手。 1、編寫腳本/root/bin/systeminfo.sh,顯示當前主機系統信息,包括主機名,IPv4地址,操作系統版本,內核版本,CPU型號,內存大小,硬盤大小。 2、編寫腳本/root/bin/backup.sh,可實現每日將/etc/目錄備份到/root/etcYYYY-mm-dd中。 3、編寫腳本…

    2017-04-13
  • N_28 正則表達式的一些基本用法

    1、復制/etc/skel目錄為/home/tuser1,要求/home/tuser1及其內部文件的屬組和其它用戶均沒有任何訪問權限。 ~]# cp -r /etc/skel /home/tuser1 ~]# chmod -R -g— -o— /home/tuser1 2、編輯/etc/group文件,添加組hadoop。 ~]#vi…

    Linux干貨 2017-12-23
  • 網絡接口配置-bonding

    網絡接口配置-bonding ?Bonding 就是將多塊網卡綁定同一IP地址對外提供服務,可以實現高可用或者負載均衡。當然,直接給兩塊網卡設置同一IP地址是不可能的。通過bonding,虛擬一塊網卡對外提供連接,物理網卡的被修改為相同的MAC地址。 Bonding的工作模式 ?Mode 0 (balance-rr) 輪轉(Round-robin)策略:從頭…

    Linux干貨 2016-09-05

評論列表(2條)

  • 馬哥教育
    馬哥教育 2016-08-12 14:16

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

欧美性久久久久