Linux基礎—磁盤管理

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

1)要求其block大小為2048,預留空間百分比2,卷標MYDATA,默認掛載屬性包含acl 2)掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳

#mkfs.ext4 -b 2048 -m 2 -L mydata /dev/sda5
#mount /dev/sda5 /data/mydata -o noatime,noexec

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

#fdisk /dev/sda
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (341858304-976773167, default 341858304): 
Using default value 341858304
Last sector, +sectors or +size{K,M,G} (341858304-976773167, default 976773167): +1G
Partition 6 of type Linux and of size 1 GiB is set\
Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): L
 0  Empty           24  NEC DOS         81  Minix / old Lin bf  Solaris        
 1  FAT12           27  Hidden NTFS Win 82  Linux swap / So c1  DRDOS/sec (FAT-
 2  XENIX root      39  Plan 9          83  Linux           c4  DRDOS/sec (FAT-
 3  XENIX usr       3c  PartitionMagic  84  OS/2 hidden C:  c6  DRDOS/sec (FAT-
 4  FAT16 <32M      40  Venix 80286     85  Linux extended  c7  Syrinx         
 5  Extended        41  PPC PReP Boot   86  NTFS volume set da  Non-FS data    
 6  FAT16           42  SFS             87  NTFS volume set db  CP/M / CTOS / .
 7  HPFS/NTFS/exFAT 4d  QNX4.x          88  Linux plaintext de  Dell Utility   
 8  AIX             4e  QNX4.x 2nd part 8e  Linux LVM       df  BootIt         
 9  AIX bootable    4f  QNX4.x 3rd part 93  Amoeba          e1  DOS access     
 a  OS/2 Boot Manag 50  OnTrack DM      94  Amoeba BBT      e3  DOS R/O        
 b  W95 FAT32       51  OnTrack DM6 Aux 9f  BSD/OS          e4  SpeedStor      
 c  W95 FAT32 (LBA) 52  CP/M            a0  IBM Thinkpad hi eb  BeOS fs        
 e  W95 FAT16 (LBA) 53  OnTrack DM6 Aux a5  FreeBSD         ee  GPT            
 f  W95 Ext'd (LBA) 54  OnTrackDM6      a6  OpenBSD         ef  EFI (FAT-12/16/
10  OPUS            55  EZ-Drive        a7  NeXTSTEP        f0  Linux/PA-RISC b
11  Hidden FAT12    56  Golden Bow      a8  Darwin UFS      f1  SpeedStor      
12  Compaq diagnost 5c  Priam Edisk     a9  NetBSD          f4  SpeedStor      
14  Hidden FAT16 <3 61  SpeedStor       ab  Darwin boot     f2  DOS secondary  
16  Hidden FAT16    63  GNU HURD or Sys af  HFS / HFS+      fb  VMware VMFS    
17  Hidden HPFS/NTF 64  Novell Netware  b7  BSDI fs         fc  VMware VMKCORE 
18  AST SmartSleep  65  Novell Netware  b8  BSDI swap       fd  Linux raid auto
1b  Hidden W95 FAT3 70  DiskSecure Mult bb  Boot Wizard hid fe  LANstep        
1c  Hidden W95 FAT3 75  PC/IX           be  Solaris boot    ff  BBT            
1e  Hidden W95 FAT1 80  Old Minix      
Hex code (type L to list all codes): 82    
Changed type of partition 'Linux' to 'Linux swap / Solaris'

Command (m for help): w
The partition table has been altered!
Hex code (type L to list all codes): 82    
[root@localhost ~]# mkswap /dev/sda
[root@localhost ~]# mkswap /dev/sda6    
[root@localhost ~]# free -h
              total        used        free      shared  buff/cache   available
    Mem:           3.7G         93M        3.4G        8.4M        245M        3.4G
    Swap:          3.0G          0B        3.0G

3、寫一個腳本

1)獲取并列出當前系統上的所有磁盤設備 2)顯示每個磁盤設備上的每個分區相關的空間使用信息 #!/bin/bash # lsblk df -h 4、總結raid的各個級別及其組合方式和性能的不同

5、創建一個大小為10G的raid1,要求一個空閑盤,而且chunk大小為128K

[root@localhost ~]# fdisk /dev/sda
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (341858304-976773167, default 341858304): 
Using default value 341858304
Last sector, +sectors or +size{K,M,G} (341858304-976773167, default 976773167): +10G
Partition 6 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): n  
All primary partitions are in use
Adding logical partition 7
First sector (362831872-976773167, default 362831872): 
Using default value 362831872
Last sector, +sectors or +size{K,M,G} (362831872-976773167, default 976773167): +10G
Partition 7 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): n
All primary partitions are in use
Adding logical partition 8
First sector (383805440-976773167, default 383805440): 
Using default value 383805440
Last sector, +sectors or +size{K,M,G} (383805440-976773167, default 976773167): +10G
Partition 8 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1-8, default 8): 8
Hex code (type L to list all codes): fd
Changed type of partition 'Linux' to 'Linux raid autodetect'

Command (m for help): w
The partition table has been altered!
[root@localhost ~]# mdadm --create /dev/md0 --auto=yes --level=1 --chunk=128K \
> --raid-devices=2 --spare-devices=1 /dev/sda{6,7,8}
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/n) y
[root@localhost ~]# mkfs.ext4 /dev/md0 
[root@localhost ~]# mount /dev/md0 /media/
[root@localhost ~]# df -h /media/
Filesystem      Size  Used Avail Use% Mounted on
/dev/md0        9.8G   37M  9.2G   1% /media

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

直接調用5題中的三塊盤配置raid5,大小為20G
[root@localhost ~]# mdadm --create /dev/md1 --auto=yes --level=5 --chunk=256K --raid-devices=3 /dev/sda{6,7,8}
[root@localhost ~]# mkfs.ext4 /dev/md1
[root@localhost ~]# lsblk /dev/md1 
NAME MAJ:MIN RM SIZE RO TYPE  MOUNTPOINT
md1    9:1    0  20G  0 raid5 
#vi /etc/fstab
/dev/md1    /backup     ext4        noatime,acl     0   0

7、寫一個腳本 1)接受一個以上文件路徑作為參數 2)顯示每個文件擁有的行數 3)總結說明本次共為幾個文件統計了其行數;

#!/bin/bash
if [ $# -eq 0 ] ;then
    echo "You must input a file-full-directory at least!"
    else
            SUM=0;
            FILENUM=0;
            for file in $*;
            do
             if [ ! -f $file ];then
                    echo "THE $file is not a file,please input again!"
            else
                    LINE=$(cat $file|wc -l)
                    echo $file has $LINE lines
                    SUM=$(($SUM+$LINE))
                    FILENUM=$(($FILENUM+1))
            fi
            done
            echo the file-num is $FILENUM;
            echo the tatol lines is $SUM;
fi

8、寫一個腳本 1)傳遞兩個以上字符串當做用戶名 2)創建這些用戶,且密碼同用戶名 3)總結說明共創建了幾個用戶

#!/bin/bash
declare sum=0;
if [ $# -lt 2 ];then
    echo -e "\033[031m you must insert more string !\033[0m"
else
        for name in $*
           do
             id $name>>/dev/null 2>&1
             if [ $? -eq 0 ]; then
                   echo -e "\033[031m the user $name is exist!\033[0m"
             else
                   useradd $name && echo $name |passwd --stdin $name
                   sum=$(($sum+1))
        fi
        done
        echo "$sum users is created!"
fi

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

#!/bin/bash
#       create 20 users and count the sum of users-id
#       20161108    
declare sum=0;  
for (( i=1;i<=20;i++ ))
    do
    id visitor$i >> /dev/null 2>&1
    if [ $? -eq 0 ];then
        echo "The visitor$i is exist!"
    else
        useradd visitor$i && echo visitor$i |passwd --stdin visitor$i >>/dev/null 2>&1
        uid=$(id -u visitor$i)
        sum=$(($sum+$uid))
     fi
done
echo "the sum of user_id is $sum."

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

#!/bin/bash
#
#       count the lines in /etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions、/etc/fstab
#       20161109
declare SUM1=0;
declare SUM2=0;
for file in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab
        do
        if [ -f $file ];then
                line1=$(grep "^#" $file|wc -l) >> /dev/null 2>&1
                line2=$(grep "[[:space:]]\+" $file|wc -l) >> /dev/null 2>&1
        echo "the $file the lines head by # is $line1,the space lines is $line2"             
                SUM1=$(($SUM1+$line1))
                SUM2=$(($SUM2+$line2))
        else echo -e "\033[031m the $file is not exist or wrong \033[0m"
        fi
        done
                echo "the tatol of lines head by # is $SUM1,the tatol space lines is $SUM2"

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

#!/bin/bash
#       Display the user whose shell is bash,and its uid and the sum of uid
#       20161108

declare sum=0;
for USER_NAME in $(grep "bash$" /etc/passwd|cut -d":" -f 1)
    do
        Uid=$(id -u $USER_NAME)
        echo "$USER_NAME's uid is $Uid"
        sum=$(($sum+$Uid))
done
        echo "The sum of user_id is $sum"

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

#!/bin/bash
#       Display the user who have two subordinate-group
#       20161108

USER=$(cat /etc/passwd|cut -d':' -f 1)
declare SUM=0;
for NAME in $USER
   do
        if [ `id -G $NAME |wc -w` -ge 2 ] ;then
           SUM=$(($SUM+1))
           GROUP=$(id $NAME|cut -d' ' -f3)
           echo "The user $NAME groups is $GROUP"
        fi
done
        echo "these are $SUM users"

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

#pvcreate /dev/sd{c1,d1,e1}
#vgcreate -s 8M  testvg /dev/sd{c1,d1,e1}
#lvcreate -L +5G -n mylv1 testvg
#mkfs.ext4 /dev/testvg/mylv1
#vi /etc/fstab
/dev/testvg/mylv1       /users  ext4        default,acl 0  0
#mount /dev/testvg/mylv1  /users
#df -h /users 
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  4.8G   21M  4.6G   1% /users

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

#useradd -d /users/magedu magedu
#cp -r /var/* /users
#ls /users
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

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

[root@localhost ~]# df -h /users/
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  4.8G  314M  4.3G   7% /users
[root@localhost ~]# umount /users/

[root@localhost ~]# e2fsck /dev/testvg/mylv1 
e2fsck 1.42.9 (28-Dec-2013)
/dev/testvg/mylv1: clean, 8488/327680 files, 133517/1310720 blocks

[root@localhost ~]# lvextend -L 6G /dev/testvg/mylv1 
  Size of logical volume testvg/mylv1 changed from 5.00 GiB (640 extents) to 6.00 GiB (768 extents).
  Logical volume mylv1 successfully resized.

[root@localhost ~]# e2fsck -f /dev/testvg/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/testvg/mylv1: 8488/327680 files (0.2% non-contiguous), 133517/1310720 blocks
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# resize2fs /dev/testvg/mylv1 
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/mylv1 to 1572864 (4k) blocks.
The filesystem on /dev/testvg/mylv1 is now 1572864 blocks long.

[root@localhost ~]# mount /dev/testvg/mylv1 /users/
[root@localhost ~]# df -h /users/
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  5.8G  314M  5.2G   6% /users

[root@localhost ~]# ls /users/
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

16、縮減mylv1至4G,確??s減完成后數據可用

[root@localhost ~]#umount /users
[root@localhost ~]# e2fsck /dev/testvg/mylv1            /*檢查下數據完整性
e2fsck 1.42.9 (28-Dec-2013)

[root@localhost ~]# resize2fs /dev/testvg/mylv1 4G        /*如果是縮小空間,應先縮小其數據空間
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/testvg/mylv1 to 1048576 (4k) blocks.
The filesystem on /dev/testvg/mylv1 is now 1048576 blocks long.

lvreduce -L 4G /dev/testvg/mylv1                    /*然后再縮小其lv邏輯空間
  WARNING: Reducing active logical volume to 4.00 GiB
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce mylv1? [y/n]: y
  Size of logical volume testvg/mylv1 changed from 6.00 GiB (768 extents) to 4.00 GiB (512 extents).
  Logical volume mylv1 successfully resized.
[root@localhost ~]# mount /dev/testvg/mylv1 /users/ /*掛載

[root@localhost ~]# df -h /users/                   /*查看空間利用率,依然是314M
Filesystem                Size  Used Avail Use% Mounted on
/dev/mapper/testvg-mylv1  3.9G  314M  3.3G   9% /users
[root@localhost ~]# ls /users/                      /*查看內容依然還在
adm    crash  empty  games   kerberos  local  log         mail  opt       run    tmp  yp
cache  db     ftp    gopher  lib       lock   lost+found  nis   preserve  spool  var

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

[root@localhost ~]# lvcreate -L 3.5G -n mylv1_snap -s /dev/testvg/mylv1 
  Logical volume "mylv1_snap" created.
[root@localhost ~]# lvs
  LV         VG     Attr       LSize  Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  root       centos -wi-ao---- 27.46g                                                    
  swap       centos -wi-ao----  2.00g                                                    
  mylv1      testvg owi-aos---  4.00g                                                    
  mylv1_snap testvg swi-a-s---  3.50g      mylv1  0.00   

[root@localhost ~]# mount /dev/testvg/mylv1_snap /mnt
[root@localhost ~]# ls /mnt/
adm    db     games     lib    log         nis       run    var
cache  empty  gopher    local  lost+found  opt       spool  yp
crash  ftp    kerberos  lock   mail        preserve  tmp

原創文章,作者:396064847,如若轉載,請注明出處:http://www.www58058.com/59480

(0)
396064847396064847
上一篇 2016-11-16
下一篇 2016-11-17

相關推薦

  • N26-博客作業-week15

    1、總結sed和awk的詳細用法 sed: 語法結構 sed [OPTION]…’script’ [input-file]…[action] -r:支持擴展正則表達式 -n:不輸出模式空間中的內容至屏幕 -e script1 -e script2 -e script3:指定多腳本運行 -f /path/to/script_file:從指定的文件中讀取…

    Linux干貨 2017-07-14
  • N22-第6周作業-冥界之王

    請詳細總結vim編輯器的使用并完成以下練習題1、復制/etc/rc.d/rc.sysinit文件至/tmp目錄,將/tmp/rc.sysinit文件中的以至少一個空白字符開頭的行的行首加#;    [root@CentOS6 /]# cp /etc/rc.d/rc.sysinit /tmp…

    Linux干貨 2016-09-19
  • N27_第六周作業

    請詳細總結vim編輯器的使用并完成以下練習題 基本模式:編輯模式(命令模式),輸入模式,末行模式 模式轉換: 編輯模式–輸入模式: i:insert,在光標所在處前方輸入 a:append,在光標所在處后方輸入 o:在光標所在處的下方新建一行 I:在光標所在行行首輸入 A:在光標所在處行尾輸入 O:在光標所在處的上方打開一個新行 輸入模式&#82…

    Linux干貨 2017-10-26
  • 網絡班第27期第一周作業

    1、         描述計算機的組成及其功能 CPU:主要由控制器和運算器組成,其他還有寄存器和緩存等。 控制器:其功能是對程序規定的控制信息進行解釋,根據其要求進行控制,調度程序、數據、地址,協調計算機各部分工作及內存與外設的訪問等。 運算器:功能是對數據進行各種算術運算和邏…

    2017-07-29
  • LVS調度方法

    lvs scheduler:     根據其調度時是否考慮后端主機的當前負載,可分為靜態方法和動態方法     靜態方法:僅根據算法本身進行調度:         RR:Round Ronin 輪詢         WRR:Weighted…

    Linux干貨 2017-01-10
  • 整型、浮點型在內存中的存儲方式

      在學習C語言的過程中,有時候會想,數據到底是以什么樣的方式在內存中存儲的呢?經過一段時間的查閱資料,小編終于整理了一些…… 以VC6.0編譯器為測試環境(int型為4Byte) 先簡介一下大小端模式,具體資料可參考 http://blog.csdn.net/ce123_zhouwei/article/details/6971544  …

    Linux干貨 2016-01-14

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-11-17 10:01

    寫的很好,排版也很漂亮,期待你的下一次佳作,完全可以當作模版博客來推薦了

欧美性久久久久