第七周作業
1.創建一個10G分區,并格式化為ext4文件系統:
(1)要求甘block大小為2048,預留空間百分比為2,卷標為MYDATA,默認掛載屬性包含acl;
(2)掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@nginx ~]# fdisk /dev/sdd
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xf6cff7e3.
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)
1
Invalid partition number for type `1'
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First cylinder (1-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 10G
Command (m for help): p
Disk /dev/sdd: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 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: 0xf6cff7e3
Device Boot Start End Blocks Id System
/dev/sdd1 1 10 80293+ 83 Linux
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@nginx ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdd1
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
20112 inodes, 40146 blocks
802 blocks (2.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=41943040
3 block groups
16384 blocks per group, 16384 fragments per group
6704 inodes per group
Superblock backups stored on blocks:
16384
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 36 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@nginx ~]#
[root@nginx mydata]# mount -o noexec,nodiratime /dev/sdd1 /data/mydata
2.創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
[root@nginx mydata]# fdisk /dev/sde
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0x81838cae.
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-1305, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): 1G
Command (m for help): t
Selected partition 1
Hex code (type L to list codes): 1
Changed system type of partition 1 to 1 (FAT12)
Command (m for help): t
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): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: If you have created or modified any DOS 6.x
partitions, please see the fdisk manual page for additional
information.
Syncing disks.
[root@nginx mydata]# mkswap /dev/sde1
Setting up swapspace version 1, size = 7996 KiB
no label, UUID=77e8c9dc-d0c3-48d5-8871-01a819d88979
[root@nginx mydata]# swapon
Usage:
swapon -a [-e] [-v] [-f] enable all swaps from /etc/fstab
swapon [-p priority] [-v] [-f] <special> enable given swap
swapon -s display swap usage summary
swapon -h display help
swapon -V display version
The <special> parameter:
{-L label | LABEL=label} LABEL of device to be used
{-U uuid | UUID=uuid} UUID of device to be used
<device> name of device to be used
<file> name of file to be used
[root@nginx mydata]# swapon -a
[root@nginx mydata]# free -m
total used free shared buffers cached
Mem: 980 204 776 0 19 68
-/+ buffers/cache: 117 863
Swap: 1983 0 1983
[root@nginx mydata]#
3.寫一個腳本
(1)獲取并列出當前系統上的所有磁盤設備;
(2)顯示每個磁盤設備上每個分區相關的空間使用信息
#/bin/bash
#
#
fdisk -l | grep "Disk \/dev\/[sh]d[a-z]"
fdisk -l | grep [sh]d[a-z]
4.總結RAID的各個級別及其組合方式和性能的不同
RAID級別 |
RAID0 |
RAID1 |
RAID5 |
RAID6 |
RAID01 |
RAID10 |
容錯 |
無 |
有 |
有 |
有 |
有 |
有 |
冗余 |
無 |
鏡像 |
奇偶校驗 |
奇偶校驗 |
鏡像 |
鏡像 |
讀性能 |
有所提高 |
有所提高 |
有所提高 |
有所提高 |
有所提高 |
有所提高 |
寫性能 |
有所提高 |
稍降 |
有所提高 |
有所提高 |
有所提高 |
有所提高 |
至少磁盤數量 |
2 |
2 |
3 |
4 |
4 |
4 |
允許磁盤最多損壞數量 |
0 |
1 |
1 |
2 |
每組各1 |
每組各1 |
可用容量 |
100% |
50% |
(n-1)/n |
(n-2)/n |
50% |
50% |
5.創建一個大小為10G的RAID1,要求有一個空閑盤,而且chunk大小為128k
[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 3 /dev/sdd /dev/sde /dev/sdf
mdadm: You haven't given enough devices (real or missing) to create this array
[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 3 /dev/sdd1 /dev/sde1 /dev/sdf1
mdadm: You haven't given enough devices (real or missing) to create this array
[root@nginx ~]# mdadm -C /dev/md0 -l 1 -c 128 -x 1 -n 2 /dev/sdd1 /dev/sde1 /dev/sdf1
mdadm: /dev/sdd1 appears to contain an ext2fs file system
size=9446188K mtime=Thu Jan 1 08:00:00 1970
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
mdadm: /dev/sde1 appears to contain an ext2fs file system
size=9446188K mtime=Thu Jan 1 08:00:00 1970
Continue creating array? y
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md0 started.
[root@nginx ~]#
6.創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能
[root@nginx ~]# mdadm -C /dev/md1 -c 256 -n 3 -l 5 /dev/sdb1 /dev/sdc1 /dev/sdd1
mdadm: Defaulting to version 1.2 metadata
mdadm: array /dev/md1 started.
[root@nginx ~]# mke2fs -t etx4 /dev/md1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=64 blocks, Stripe width=128 blocks
525200 inodes, 2098432 blocks
104921 blocks (5.00%) reserved for the super user
First data block=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
Writing inode tables: 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.
#
# /etc/fstab
# Created by anaconda on Fri Dec 25 05-55-39 2015
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
/dev/mapper/vg_centos-lv_root / ext4 defaults 1 1
UUID=19e69dcd-00ea-471e-b368-1e21d77c548f /boot ext4 defaults 1 2
/dev/mapper/vg_centos-lv_swap swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
#UUID=d44bb1f3-d8a552ed-ffd3527d-138ecff8 /backup ext4 defaults,acl 0 0
#UUID=9f0645ef-1a1f8086-ee568336-0c8097d4 /mydata ext4 defaults 0 0
#/dev/md126 /backup ext4 defaults 0 0
/dev/md127 /mydata ext4 defaults 0 0
/dev/md1 /backup ext4 defaults,acl,nodiratime 0 0
7.寫一個腳本
(1)接受一個以上文件路徑作為參數
(2)顯示每個文件擁有的行數
(3)總結說明本次共為幾個文件統計了其行數
#/bin/bash
#
#
declare -i lines
declare -i files
files=0
if [ $# == 0 ]; then
echo "please give a file name for me"
exit 1
fi
for i in $* ; do
lines=0
lines=`wc -l $i | cut -f1 -d" "`
echo "fle $i total lines is: $lines"
files=files+1
done
echo "Total count file: $files"
8.寫一個腳本
(1)傳遞兩個以上字符串當作用戶名
(2)創建這些用戶,且密碼同用戶名
(3)總結說明共創建了幾個用戶
#/bin/bash
#
#
declare -i totalusers
totalusers=0
for i in $* ; do
if ! id $i &> /dev/null ; then
useradd $i
echo $i | passwd $i &> /dev/null
totalusers=totalusers+1
fi
done
echo "TotalUser is : $totalusers"
9.寫一個腳本,新建20個用戶,visitor1-visitor20,計算他們的ID之和
#/bin/bash
#
#
declare -i userno
declare -i totaluid
declare -i iuid
userno=1
totaluid=0
iuid=0
while [ $userno -lt 21 ] ; do
useradd visitor$userno &> /dev/null
iuid=`id visitor$userno -u`
totaluid=$totaluid+$iuid
userno=$userno+1
done
10.寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以
#號開頭的行數之和,以及總的空白行數
#/bin/bash
#
#
declare -i lines
declare -i spacelines
file1=/etc/rc.d/rc.sysinit
file2=/etc/rc.d/init.d/functions
file3=/etc/fstab
lines=0
spacelines=0
lines=`grep ^# $file1 | wc -l`
echo "$file1 include '#' lines total is: $lines "
#echo `grep ^[[:space:]]*$ $file1 | wc -l`
spacelines=$spacelines+`grep ^[[:space:]]*$ $file1 | wc -l`
lines=0
lines=`grep ^# $file2 | wc -l`
echo "$file2 include '#' lines total is: $lines "
spacelines=$spacelines+`grep ^[[:space:]]*$ $file2 | wc -l`
lines=0
lines=`grep ^# $file3 | wc -l`
echo "$file3 include '#' lines total is: $lines "
spacelines=$spacelines+`grep ^[[:space:]]*$ $file3 | wc -l`
echo "Total sapcelines is: $spacelines."
11.寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和
#/bin/bash
#
#
declare -i uids=0
declare -i totaluid=0
#cat /etc/passwd | grep /bin/bash$ | cut -f1 -d:
bashusers=`cat /etc/passwd | grep /bin/bash$ | cut -f1 -d:`
for i in $bashusers; do
uids=`id -u $i`
echo "user name : $i"
echo "uid is : $uids"
totaluid=$totaluid+$uids
echo ""
done
echo "Total UID is: $totaluid."
12.寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名,并說明共有多少個此類用戶
#/bin/bash
#
#
declare -i countusers=0
users=`cat /etc/passwd | cut -f1 -d:`
for u in $users ; do
id $u | cut -f3 -d" " | grep "," &> /dev/null
if [ $? -eq 0 ] ; then
echo "$u"
countusers=$countusers+1
fi
done
echo "Total User for GID is : $countusers."
13.創建一個由至少兩個物理卷組成的大小為20G的卷組,要求:PE大小為8M,而在卷組中創建一個大小為5G的邏輯卷my1v1,
格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl
14.新建用戶magedu,其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄
[root@nginx tmp]# useradd -d /users/magedu magedu
[root@nginx tmp]# su – magedu
Welcom 538,You home is: /users/magedu
[magedu@nginx ~]$ pwd
/users/magedu
[magedu@nginx ~]$ ls
[magedu@nginx ~]$ cp /tmp/* ./
cp: omitting directory `/tmp/install'
cp: omitting directory `/tmp/magedu.com'
cp: omitting directory `/tmp/users'
cp: omitting directory `/tmp/vmware-root'
cp: cannot open `/tmp/yum_save_tx-2016-06-10-16-25Lt9uQC.yumtx' for reading: Permission denied
[magedu@nginx ~]$ ls
countfiles.sh createuser2.sh delusers.sh test.sh
counts.sh createuser.sh guiduser.sh userinfo.sh
[magedu@nginx ~]$
15.擴展mylvl至9G,確保擴展完成后原有數據完全可用
[root@nginx ~]# pvcreate /dev/sde mypv
[root@nginx ~]# vgcreate myvg mypv
[root@nginx ~]# lvcreate -L 5G -n mylv1 myvg
[root@nginx /]# mount /dev/mapper/myvg-mylv1 /data -t ext4
[root@nginx data]# cp /tmp/* ./
cp: omitting directory `/tmp/install'
cp: omitting directory `/tmp/magedu.com'
cp: omitting directory `/tmp/users'
cp: omitting directory `/tmp/VMwareDnD'
cp: omitting directory `/tmp/vmware-root'
[root@nginx data]# ls
countfiles.sh createuser2.sh delusers.sh lost+found userinfo.sh
counts.sh createuser.sh guiduser.sh test.sh
[root@nginx data]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv1 myvg -wi-ao—- 5.00g
lv_root vg_centos -wi-ao—- 17.57g
lv_swap vg_centos -wi-ao—- 1.94g
[root@nginx data]# lvextend -L 9G /dev/myvg/mylv1
Size of logical volume myvg/mylv1 changed from 5.00 GiB (1280 extents) to 9.00 GiB (2304 extents).
Logical volume mylv1 successfully resized
[root@nginx data]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv1 myvg -wi-ao—- 9.00g
lv_root vg_centos -wi-ao—- 17.57g
lv_swap vg_centos -wi-ao—- 1.94g
[root@nginx data]# ls
countfiles.sh createuser2.sh delusers.sh lost+found userinfo.sh
counts.sh createuser.sh guiduser.sh test.sh
[root@nginx data]#
16.縮減mylv1至7G,確保縮減完成后原有數據完全可用
[root@nginx data]# fuser -km /dev/myvg/mylv1
/dev/myvg/mylv1: 2343c
[root@nginx ~]# umount /dev/myvg/mylv1
[root@nginx ~]# 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: 19/327680 files (0.0% non-contiguous), 55910/1310720 blocks
[root@nginx ~]#
[root@nginx ~]# 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 (2304 extents) to 7.00 GiB (1792 extents).
Logical volume mylv1 successfully resized
[root@nginx ~]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
mylv1 myvg -wi-a—– 7.00g
lv_root vg_centos -wi-ao—- 17.57g
lv_swap vg_centos -wi-ao—- 1.94g
[root@nginx ~]# mount /dev/myvg/mylv1 /data
[root@nginx ~]# cd data
-bash: cd: data: No such file or directory
[root@nginx ~]# ls
anaconda-ks.cfg Documents install.log Music Public Videos
Desktop Downloads install.log.syslog Pictures Templates
[root@nginx ~]# cd /data
[root@nginx data]# ls
countfiles.sh createuser2.sh delusers.sh lost+found userinfo.sh
counts.sh createuser.sh guiduser.sh test.sh
[root@nginx data]#
17.對mylv1創快照,并通過備份數據,要深圳市保留原有的屬主屬組等信息.
對mylv1創建快照卷snap-mylv1,并通過其cp內部的數據至/backups/目錄中,要求保留原有屬主屬組等信息;
[root@nginx backup]# lvcreate -L 100M -n snap-mylv1 -p r -s /dev/myvg/mylv1
Logical volume "snap-mylv1" created
[root@nginx backup]# mkdir /snapbackup
[root@nginx backup]# mount /dev/myvg/
mylv1 snap-mylv1
[root@nginx backup]# mount /dev/myvg/snap-mylv1 /snapbackup/
mount: block device /dev/mapper/myvg-snap–mylv1 is write-protected, mounting read-only
[root@nginx backup]# cp -a /snapbackup/ ./
[root@nginx backup]# ls
etc.20151224.tar.gz snapbackup
[root@nginx backup]# cd snapbackup/
[root@nginx snapbackup]# ls
countfiles.sh createuser2.sh delusers.sh lost+found userinfo.sh
counts.sh createuser.sh guiduser.sh test.sh
[root@nginx snapbackup]#
原創文章,作者:馬哥Net19_小斌斌,如若轉載,請注明出處:http://www.www58058.com/19076
寫的很棒,但是有的腳本有點小瑕疵,在仔細測試一下