N22-第七周作業

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

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

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

創建一個10G的分區:
    [root@localhost ~]# fdisk /dev/sda    
    Welcome to fdisk (util-linux 2.23.2).
    
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Command (m for help): n
    All primary partitions are in use
    Adding logical partition 7
    First sector (196081664-251658239, default 196081664): 
    Using default value 196081664
    Last sector, +sectors or +size{K,M,G} (196081664-251658239, default 251658239): +10G
    Partition 7 of type Linux and of size 10 GiB is set
    
    Command (m for help): w
    The partition table has been altered!
    
    Calling ioctl() to re-read partition table.
    
    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
       
       
    [root@localhost ~]# partx -a /dev/sda
    partx: /dev/sda: error adding partitions 1-6
    [root@localhost ~]# partx -a /dev/sda
    partx: /dev/sda: error adding partitions 1-7  
格式化等操作:    
   ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sda7
   ~]# tune2fs -o acl /dev/sda7
   ~]# mount -o noatime,noexec /dev/sda7 /mnt/sda7

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

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

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

Command (m for help): w  
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-5
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-6


[root@localhost ~]#mkswap /dev/sda6
[root@localhost ~]#swapon /dev/sda6

3、寫一個腳本

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

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

#!/bin/bash
#
echo "disk infomation:"
fdisk -l /dev/[sh]d[z-a]
echo "-----------------------"
echo "partition infomation:"
df -lh

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

raid-0:
	條帶卷,讀寫性能有所提升
	可用空間:N*min(S1,S2...)
	無容錯能力
	最少磁盤數:2
raid-1:
	讀性能提升、寫性能略有下降
	可用空間:1*min(S1,S2...)
	有冗余能力
	最少磁盤數:2
raid-5:
	讀,寫性能提升
	可用空間:(N-1)*min(S1,S2...)
	有容錯能力:1塊磁盤
	最少磁盤數:3
raid-6:
	讀寫性能提升
	可用空間:(N-2)*(S1,S2...)
	有容錯能力:2塊磁盤
	最少磁盤數:4
JBOD:
	將多塊磁盤的空間合并一個大的連續空間使用
	可用空間:sum(S1,S2...)
raid-10:
	讀寫性能提升
	可用空間:N*min(S1,S2...)
	有容錯能力:每組鏡像最多只能壞一塊
	最少磁盤數:4

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

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x9597ecff.

Command (m for help): p

Disk /dev/sdb: 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: 0x9597ecff

   Device Boot      Start         End      Blocks   Id  System

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): p  

Disk /dev/sdb: 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: 0x9597ecff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   83  Linux
/dev/sdb2        20973568    41945087    10485760   83  Linux
/dev/sdb3        41945088    62916607    10485760   83  Linux

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): p

Disk /dev/sdb: 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: 0x9597ecff

   Device Boot      Start         End      Blocks   Id  System
/dev/sdb1            2048    20973567    10485760   fd  Linux raid autodetect
/dev/sdb2        20973568    41945087    10485760   fd  Linux raid autodetect
/dev/sdb3        41945088    62916607    10485760   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
partx: /dev/sdb: error adding partitions 1-3

[root@localhost ~]# mdadm -C /dev/md0 -a yes -c 128K -n 2 -l 1 -x 1 /dev/sdb{1,2,3}
[root@localhost ~]# mdadm -D /dev/md0
/dev/md0:
        Version : 1.2
  Creation Time : Thu Dec  1 16:27:49 2016
     Raid Level : raid1
     Array Size : 10477568 (9.99 GiB 10.73 GB)
  Used Dev Size : 10477568 (9.99 GiB 10.73 GB)
   Raid Devices : 2
  Total Devices : 3
    Persistence : Superblock is persistent

    Update Time : Thu Dec  1 16:28:41 2016
          State : clean 
 Active Devices : 2
Working Devices : 3
 Failed Devices : 0
  Spare Devices : 1

           Name : localhost.localdomain:0  (local to host localhost.localdomain)
           UUID : 76db81c7:812deaf4:a8679005:cd602505
         Events : 17

    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

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

[root@localhost ~]# fdisk /dev/sdb
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.

Command (m for help): n
Partition type:
   p   primary (0 primary, 0 extended, 4 free)
   e   extended
Select (default p): 
Using default response 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): +4G
Partition 1 of type Linux and of size 4 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): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.

[root@localhost ~]# partx -a /dev/sdb

[root@localhost ~]# mdadm -C /dev/md1 -a yes  -c 256k -n 3 -l 5 /dev/sdb{1,2,3}

[root@localhost ~]# mke2fs -t ext4 /dev/md1
mke2fs 1.42.9 (28-Dec-2013)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=128 blocks, Stripe width=256 blocks
524288 inodes, 2095104 blocks
104755 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=2145386496
64 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, 1605632

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done
[root@localhost ~]# echo "/dev/md1        /backup ext4    defaults,acl,noatime    0 0" >>/etc/fstab

[root@localhost ~]# tail -1 /etc/fstab 
/dev/md1	/backup	ext4	defaults,acl,noatime	0 0

7、寫一個腳本

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

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

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

#!/bin/bash
#
#define a value of all_line
declare -i linesum=0
if [ $# -lt 1 ] ;then
	 echo "At least one file." && exit 1
fi

for i in $*;do
	if ! [ -a $i ];then
		echo "file $i not exists." && exit 2
	fi
	line=$(cat $i |wc -l)
	echo "$i file have $line line."
	let linesum+=$line
done

echo "the bash count $# files,all the lines is $linesum."

8、寫一個腳本

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

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

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

#!/bin/bash
#
declare -i count=0

if [ $# -le 0 ] ;then
	echo "At least one username." && exit 1
fi

for username in $@ ;do
	if ! id $username &>/dev/null;then
		let count+=1
		useradd $username 
		echo "$username" | passwd --stdin $username
		echo "$username add finished."
	else
		echo "$username exist."
	fi
done

echo "Finally,add user $count."

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

#!/bin/bash
#
declare -i useridsum=0

for i in {1..20};do
	if id visitor$i &>/dev/null;then
		echo "visitor$i exist."
	else
		useradd visitor$i
	fi

	userid=$(id -u visitor$i)
	let useridsum+=$userid
done
echo $useridsum

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

#!/bin/bash
#
declare -i jinlinesum=0
declare -i splinesum=0

for value in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab ;do
	if ! [ -a $value ] ;then
		echo "$value not exist."
	else
		jinline=$(grep "^#.*" $value | wc -l )
		spline=$(grep  "^[[:space:]]*$" $value| wc -l)
		
		let jinlinesum+=$jinline
		let splinesum+=$spline
	fi
done
echo "jinhao lines are $jinlinesum"
echo "space lines are $splinesum"

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

#!/bin/bash
#
declare -i useridsum=0

for value in `grep "/bin/bash$" /etc/passwd`;do

	userid=$(echo $value | cut -d: -f3)
	username=$(echo $value |cut -d: -f1)
	
	let useridsum+=$userid

	echo "$username   $userid"
done


echo "all the shell is bash user's id_sum:$useridsum."

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

#!/bin/bash
#
declare groupsum=0

for username in $(cut -d: -f1 /etc/passwd);do
#判斷tmp是否大于等于2,如果是則證明此user擁有附加組
	tmp=$(id -G $username | wc -w)
	if [ $tmp -ge 2 ];then
		let groupsum+=1
	fi
done
echo $groupsum

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

[root@localhost ~]# fdisk /dev/sda
Welcome to fdisk (util-linux 2.23.2).

Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (193982464-251658239, default 193982464): 
Using default value 193982464
Last sector, +sectors or +size{K,M,G} (193982464-251658239, default 251658239): +10G
Partition 6 of type Linux and of size 10 GiB is set

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

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

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

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.

WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
The kernel still uses the old table. The new table will be used at
the next reboot or after you run partprobe(8) or kpartx(8)
Syncing disks.

[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-5
[root@localhost ~]# partx -a /dev/sda
partx: /dev/sda: error adding partitions 1-7

[root@localhost ~]# pvcreate /dev/sda{6,7}
  Physical volume "/dev/sda6" successfully created
  Physical volume "/dev/sda7" successfully created
  
 [root@localhost ~]# vgcreate myvg -s 8M /dev/sda{6,7}
  Volume group "myvg" successfully created
[root@localhost ~]# vgdisplay myvg
  --- 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               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               rx2dvs-hc2o-y0Za-w4oa-jaQk-AM7G-VLXkqn 
  
 [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg
  Logical volume "mylv1" created. 
  
  [root@localhost ~]# lvs
  LV    VG     Attr       LSize Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
                                                  
  mylv1 myvg   -wi-a----- 5.00g      
  
[root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1
mke2fs 1.42.9 (28-Dec-2013)
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

Allocating group tables: done                            
Writing inode tables: done                            
Creating journal (32768 blocks): done
Writing superblocks and filesystem accounting information: done 



[root@localhost ~]# mkdir /users
[root@localhost ~]# echo "/dev/myvg/mylv1 /users  ext4    defaults,acl    0 0">>/etc/fstab

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

[root@localhost users]# useradd -d /users/magedu magedu
[root@localhost users]# su - magedu
[magedu@localhost ~]$ pwd
/users/magedu
[magedu@localhost ~]$ cp /etc/fstab /etc/inittab ./
[magedu@localhost ~]$ ls
fstab  inittab

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

[root@localhost users]# lvextend -L 9G /dev/myvg/mylv1
  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 users]# resize2fs /dev/myvg/mylv1
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 2
The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.

[root@localhost users]# df -h 
Filesystem              Size  Used Avail Use% Mounted on
/dev/sda5                50G  5.0G   46G  10% /
devtmpfs                481M     0  481M   0% /dev
tmpfs                   490M   84K  490M   1% /dev/shm
tmpfs                   490M   14M  477M   3% /run
tmpfs                   490M     0  490M   0% /sys/fs/cgroup
/dev/sda2                40G   36M   40G   1% /home
/dev/sda1               497M  124M  373M  25% /boot
/dev/mapper/myvg-mylv1  8.8G   23M  8.3G   1% /users

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

[root@localhost /]# umount /users
[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: 26/589824 files (0.0% non-contiguous), 75566/2359296 blocks

[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 /]# 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 /]# mount /dev/myvg/mylv1 /users
[root@localhost /]# cd /users
[root@localhost users]# ls
lost+found  magedu

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

[root@localhost users]# lvcreate -s -L 512M -n mylv1-snap -pr /dev/myvg/mylv1
  Logical volume "mylv1-snap" created.
  
  [root@localhost users]# mkdir /mnt/mylv1-snap

[root@localhost mnt]# mount /dev/myvg/mylv1-snap /mnt/mylv1-snap/
mount: /dev/mapper/myvg-mylv1--snap is write-protected, mounting read-only

原創文章,作者:N22-成都-stephen,如若轉載,請注明出處:http://www.www58058.com/63860

(0)
N22-成都-stephenN22-成都-stephen
上一篇 2016-12-18
下一篇 2016-12-18

相關推薦

  • 探索這個“男人”

    一、前言 正所謂了解一個命令就得了解他的用法,正好是要了解一個人就得了解他的興趣愛好,處事態度以及為人是怎么樣的。 二、man是什么 man – an interface to the on-line reference manuals Man是manual(手冊)的縮寫,使用權限是所有用戶,man命令提供為linux系統在線提供了很好的幫助手冊…

    Linux干貨 2016-05-03
  • man 命令簡單介紹

    man n command man手冊頁分為下面幾個部分: 1 普通命令2 內核提供的系統調用3 庫調用4 設備文件5 文件格式規范6 游戲7 雜項8 系統管理命令

    Linux干貨 2018-03-03
  • 馬哥教育網絡第21期-第十一周課程練習

    1、詳細描述一次加密通訊的過程,結合圖示最佳。 A和B進行通信 A發送數據:A使用單向加密算法獲得數據的特征碼,把特征碼附加到數據的尾部,A使用對稱加密算法對數據進行加密。A使用B發過來的公鑰對A的對稱加 密算法加密密鑰進行加密,附加到數據后面。 B接收方:B使用自己的私鑰對數據進行解密。得到A的對稱密鑰,對對稱密鑰解密獲得數據的特征碼,B使用單向加密算法獲…

    Linux干貨 2016-12-26
  • CentOS啟動流程排錯

    grub legacy配置文件:/boot/grub/grub.conf     default=#: 設定默認啟動的菜單項;落單項(title)編號從0開始      timeout=#:指定菜單項等待選項選擇的時長     &…

    Linux干貨 2016-09-13
  • Linux基礎:計算機的組成及其功能

    1. 描述計算機的組成及其功能。
    2. 按系列羅列Linux的發行版,并描述不同發行版之間的聯系與區別。
    3. 描述Linux的哲學思想,按照自己的理解對其解釋性描述。
    4. 說明Linux系統上命令的使用格式;詳細介紹ifconfig、echo、tty、startx、export、pwd、history、shutdown、poweroff、reboot、hwclock、date等命令使用,配合相應實例闡述。
    5. 如何獲取幫助信息,描述man文檔章節劃分。
    6. 羅列發行版基礎目錄名稱命名法則及功用規定。

    2017-12-07
  • Linux–各種常用表達式整理收集

    文字顏色設置IP地址獲取隨機口令查找替代等

    Linux干貨 2017-12-10

評論列表(1條)

  • 馬哥教育
    馬哥教育 2016-12-23 00:27

    贊,總結的很認真~~整體看下來很舒服~繼續加油~

欧美性久久久久