Linux運維-第7周作業


本週作業內容:

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

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


root@ubuntu:~# fdisk /dev/sda


Command (m for help): p


Disk /dev/sda: 1000.2 GB, 1000204886016 bytes

255 heads, 63 sectors/track, 121601 cylinders, total 1953525168 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 identifier: 0x0001404d


   Device Boot      Start         End      Blocks   Id  System

/dev/sda1   *        2048  1886507007   943252480   83  Linux

/dev/sda2      1886509054  1953523711    33507329    5  Extended

/dev/sda5      1886509056  1953523711    33507328   82  Linux swap / Solaris


Command (m for help): n

Partition type:

   p   primary (1 primary, 1 extended, 2 free)

   l   logical (numbered from 5)

Select (default p): 



Select (default p): p

分區號 (1-4,默認 1):1

起始 扇區 (2048-83886079,默認爲 2048):

將使用默認值 2048

Last 扇區, +扇區 or +size{K,M,G} (2048-83886079,默認爲 83886079):+10G

分區 3 已設置爲 Linux 類型,大小設爲 10 GiB

 

命令(輸入 m 獲取幫助):w

The partition table has been altered!

 

Calling ioctl() to re-read partition table.

正在同步磁盤。

 

 

# mkfs.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: 完成 

 

 

# tune2fs -o acl /dev/sdb1

tune2fs 1.42.9 (28-Dec-2013)


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


   # mkdir -p /data/mydata

   # mount -o noatime,noexec /dev/sdb1 /data/mydata



2、創建一個大小爲1G的swap分區,並創建好文件系統,並啓用之;


# mkswap /dev/sdb2

正在設置交換空間版本 1,大小 = 1048572 KiB

無標籤,UUID=a5c89d64-5fda-4ebd-9b00-92231cd57bae

 

# swapon /dev/sdb2


3、寫一個腳本

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

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

#!/bin/bash
#
fdisk -l | grep -o "/dev/sd[[:alnum:]]\{2\}"
echo 
df -h


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


RAID:Redundant Arrays of Inexpensive Disks廉價磁盤冗餘陣列 主要爲了提高I/O能力和容錯率


常用級別分爲RAID-0,RADI-1,RAID-2…RAID-5,RAID-6,組合方式RAID10,RAID01


下面具體介紹一下具體實現和性能差異:




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

# mdadm -C /dev/md1 -a yes -c 128 -x 1 -l 1 -n 2 /dev/sdb{1,2,3}



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


# mdadm -C /dev/md5 -n 3 -l 5 -a yes /dev/sdb{1,2,3} -c 256

# mkfs.ext4 /dev/md5

# echo "/dev/md5 /backup ext4 noatime,acl 0 0" &>> /etc/fstab



7、寫一個腳本

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

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

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

#!/bin/bash
#
if [ $# -eq 0 ];then
        echo "Please input in least two file."
        exit 2
fi
 
for i in $*;do
       if [ -e $i ];then
          echo "$i total lines : $(cat $i | wc -l)"
          echo "File total:$#"
       else
          echo "No such file"
       fi
done
 
echo " the file number is:" $#



8、寫一個腳本

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

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

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

#!/bin/bash
#
if [ $# -lt 2 ];then
        echo "Please input two username!"
        exit 1
fi
 
for i in $@; do
    if cat /etc/passwd | grep $i &> /dev/null; then
        echo "user $i already exits"
    else
        useradd $i
        echo $i | passwd --stdin $i &> /dev/null
        echo "$i added to current system"
        let createdUser+=1
 
    fi
done
 
echo "number of users created: $createdUser"



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

#!/bin/bash
#
declare -i sum=0
U=visitor
for i in {1..20};do
        useradd $U$i
        uid=`id -u $U$i`
        let sum+=$uid
done
echo "User ID sum:$sum"



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

#!/bin/bash
#
declare -i count
count1=`cat /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab|grep "^#" |wc -l`
count2=`cat /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab|grep "^$" |wc -l`
echo -e "The sum of lines is $count1\nThe sum of black lines is $count2"



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

#!/bin/bash
#
name=`cat /etc/passwd | grep "/bin/bash" | cut -d: -f1`
echo -e "The default shell is bash user:\n$name"
namesum=`cat /etc/passwd | grep "/bin/bash" | cut -d: -f1 | wc -l`
echo "The default shell is bash user sum:$namesum"
declare -i uidsum=0
uid=`cat /etc/passwd | grep "/bin/bash" | cut -d: -f3`
for i in $uid;do
      let uidsum+=$i
done
echo -e "The default shell is bash UID:\n$uid"
echo "The default shell is bash UID sum:$uidsum"



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

#!/bin/bash
declare -i count=0
for i in `awk -F: '{print $1}' /etc/passwd`;do
    [ -z `id $i|awk -F',' '{print $2}'|awk -F'(' '{print $2}'|awk -F')' '{print $1}'` ] || echo -e "$i\n" && let count=$count+1
done


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


# fdisk -l /dev/sdb

# pvcreate  /dev/sdb{1,2}

# vgcreate -s 8 vg0 /dev/sdb1 /dev/sdb2

# lvcreate -L 5g -n mylv1 /dev/vg0

# mke2fs -t ext4 /dev/vg0//mylv1 

# echo "/dev/vg0/mylv1  /users  ext4  defaults,acl  0 0 " &>> /etc/fstab


14、新建用戶magedu;其家目錄爲/users/magedu,而後su切換至此用戶,複製多個文件至家目錄;

# useradd -d /users/magedu magedu

# su magedu

# cp -r /tmp/ /users/magedu



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

#lvexpand -L 9G /dev/myvg/mylv1



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


# umount /dev/vgtest/mylv1

# e2fsck -f /dev/myvg/mylv1

# resize2fs /dev/myvg/mylv1 7G

# lvreduce -L 7G /dev/myvg/mylv1

# mount -a


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

# lvcreate -s -L 7G -p r -n snapmylv1 /dev/myvg/mylv


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章