1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]#mke2fs -t ext4 -b 2048 -m 2 -L "MYDATA" /dev/sdb1 [root@localhost ~]#mkdir -p /MYDATA [root@localhost ~]#mount -o acl /dev/sdb1 /MYDATA [root@localhost ~]# mount ... none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/sdb1 on /MYDATA type ext4 (rw,acl)
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]#mkdir -pv /data/mydata [root@localhost ~]# mount -t ext4 -o noexec,noatime /dev/sdb1 /data/mydata/ [root@localhost ~]# mount ... none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) /dev/sdb1 on /data/mydata type ext4 (rw,noexec,noatime)
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
注:只能把整个盘做为swap分区,不然无法使用
[root@localhost ~]# free -m total used free shared buffers cached Mem: 995 356 639 0 121 78 -/+ buffers/cache: 156 839 Swap: 2015 0 2015 [root@localhost ~]# fdisk /dev/sdb ... Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 2610 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 82 Changed system type of partition 3 to 82 (Linux swap / Solaris) Command (m for help): w .... Syncing disks. [root@localhost ~]# fdisk -l /dev/sdb ... Device Boot Start End Blocks Id System /dev/sdb1 1 2610 20964793+ 82 Linux swap / Solaris [root@localhost ~]# mkswap -L SWAP1 /dev/sdb1 Setting up swapspace version 1, size = 20964788 KiB LABEL=SWAP1, UUID=8910028d-99b2-4ac8-84d8-83ecfdee3af9 [root@localhost ~]# swapon /dev/sdb1 [root@localhost ~]# free -m total used free shared buffers cached Mem: 995 371 624 0 121 78 -/+ buffers/cache: 171 824 Swap: 22489 0 22489
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # echo "disk info:$(fdisk -l | grep -o "^Disk[[:space:]]/dev/s[a-z]\+"|cut -d" " -f2)" for i in $(fdisk -l | grep -o "^Disk[[:space:]]/dev/s[a-z]\+"|cut -d" " -f2);do INFO=`fdisk -l $i` echo "$i Disk INFO $INFO" done
4、总结RAID的各个级别及其组合方式和性能的不同;
级别: RAID-0: 0,条带卷,strip; RAID-1: 1,镜像卷,mirro; RAID-2 ... RAID-5 RADI-6 RAID-01 RAID-10 RAID-0: 读、写性能提升; 可用空间;N*min(S1,S2,...) 无容错能力 最少磁盘数:2,2+ RAID-1: 读性能提升 写性能略有下降; 可用空间 1*min(s1,s2,....) 有冗余能力 至少磁盘数:2,2+ RAID-5 读写性能提升 可用空间:(N-1)*min(S1,S2...) 有容错能力:1块磁盘 至少磁盘数:3,3+ RAID-6: 读、写性能提升 可用空间:(N-2)*min(S1,S2...) 有容错能力:2块磁盘 至少磁盘数:4,4+ 混合 RAID-10: 读、写性能提升 可用空间:N*MIN(S1,S2...)/2 有容错能力:每一组镜像最多只能坏一块; 至少磁盘数:4,4+
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[root@localhost ~]# fdisk /dev/sdb ... Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 1 First cylinder (1-6527, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-6527, default 6527): +10G Command (m for help): N Command action e extended p primary partition (1-4) P Partition number (1-4): 2 First cylinder (1307-6527, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-6527, default 6527): +10G Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (2613-6527, default 2613): Using default value 2613 Last cylinder, +cylinders or +size{K,M,G} (2613-6527, default 6527): +10G Command (m for help): P ...... Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ 83 Linux /dev/sdb2 1307 2612 10490445 83 Linux /dev/sdb3 2613 3918 10490445 83 Linux Command (m for help): W ... Syncing disks. [root@localhost ~]# mdadm -C /dev/md0 -a yes -n 3 -x 1 -l 1 -c 128 /dev/sdb{1,2,3} [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Tue Aug 9 12:27:30 2016 Raid Level : raid1 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 : Tue Aug 9 12:27:38 2016 State : clean, resyncing Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Resync Status : 12% complete Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : 622d8814:e0132731:bbc31b2a:a8168e65 Events : 1 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功能;
注:创建4个以上的分区盘
[root@localhost ~]# mdadm -C /dev/md0 -n 3 -l 5 -a yes -c 256 -x 1 /dev/sdb{1,2,3,4} [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Thu Aug 11 10:48:48 2016 Raid Level : raid5 Array Size : 4204544 (4.01 GiB 4.31 GB) Used Dev Size : 2102272 (2.00 GiB 2.15 GB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Thu Aug 11 10:49:01 2016 State : clean, degraded, recovering Active Devices : 2 Working Devices : 4 Failed Devices : 0 Spare Devices : 2 Layout : left-symmetric Chunk Size : 256K Rebuild Status : 41% complete Name : localhost.localdomain:0 (local to host localhost.localdomain) UUID : 9e115ad8:b5e1867e:d9ec3a2f:17625ac0 Events : 7 Number Major Minor RaidDevice State 0 8 21 0 active sync /dev/sdb5 1 8 22 1 active sync /dev/sdb6 4 8 23 2 spare rebuilding /dev/sdb7 3 8 24 - spare /dev/sdb8 [root@localhost ~]# mke2fs -t ext4 /dev/md0 [root@localhost ~]# mount -a -o noatime,acl /dev/md0 /backup [root@localhost ~]# vim /etc/fstab 末行加入/dev/md0 /backup ext4 rw,noatime,acl 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # declare -i sum=0 if [ $# -lt 1 ];then echo "Useage:bash.sh file1_name file2_name.." exit 1 fi for i in $*;do if [ -d $i ];then echo " $i is a directory,please write a file path." exit 0 else num=`wc -l $i | cut -d" " -f1` sum+=num fi done echo "$# files,total file lines:$sum"
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # declare -i usersum=0 if [ $# -le 1 ];then echo "Usage must two users to bash:bash.sh user1 user2 .." exit 1 fi for i in $*;do if id $i &> /dev/null;then echo "User $i has exist." else useradd $i &> /dev/null result=$? if [ $result -eq 0 ];then echo "$i" | passwd --stdin $i &> /dev/null echo "Add $i finish!" let usersum++ else echo "Add $i fail " exit 1 fi fi done echo "Total add $usersum users!"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i idsum=0; for i in {1..20};do if id visitor$i &> /dev/null ;then echo "visitor$i has exist!" idnum=`cat /etc/passwd | grep visitor$i | head -1| cut -d: -f3` else useradd visitor$i &> /dev/null idnum=`awk -F: '{print $3}' /etc/passwd | tail -1` fi idsum+=$idnum let i++ done echo "idsum=$idsum"
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 lines=`grep "^#" $i | wc -l` spacelines=`grep "^$" $i | wc -l` sum1+=$lines sum2+=$spacelines done echo "Total #lines:$sum1 lines." echo "Total space lines:$sum2 lines."
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash # declare -i uidsum=0 while read line;do grepbash=`echo $line | grep -o "/bin/bash$"` result=$? if [ $? -eq 0 ];then user=`echo $line |cut -d: -f1` uidnum=`echo $line | cut -d: -f3` echo "user is $user,uid is $uidnum" uidsum+=$uidnum fi done < /etc/passwd echo "Uid total sum is $uidsum "
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash # declare -i usersum=0 while read line;do username=`echo $line | cut -d: -f1` user_group=`id $username | cut -d" " -f3|grep -o ","` result=$? if [ $result -eq 0 ];then echo "附加组用户:$username" let usersum++ fi done < /etc/passwd echo "总共有附加组用户$usersum个."
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[root@localhost ~]# fdisk -l /dev/sdb Disk /dev/sdb: 107.4 GB, 107374182400 bytes 255 heads, 63 sectors/track, 13054 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: 0x8000b224 Device Boot Start End Blocks Id System /dev/sdb1 1 13054 104856223+ 5 Extended /dev/sdb5 1 1306 10490382 83 Linux /dev/sdb6 1307 2612 10490413+ 83 Linux [root@localhost ~]# pvcreate /dev/sdb{5,6} Physical volume "/dev/sdb5" successfully created Physical volume "/dev/sdb6" successfully created [root@localhost ~]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a-- 99.51g 0 /dev/sdb5 lvm2 --- 10.00g 10.00g /dev/sdb6 lvm2 --- 10.00g 10.00g [root@localhost ~]# vgcreate -s 8M myvg /dev/sdb5 /dev/sdb6 Volume group "myvg" successfully created [root@localhost ~]# vgs myvg VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 20.00g 15.00g [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 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 cahcpL-xpFk-pLj2-57Fp-Cce9-SeA3-GHEnAE [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg [root@localhost ~]# lvs myvg/mylv1 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 [root@localhost ~]# mkdir -p /users [root@localhost ~]# mount -a -o acl /dev/myvg/mylv1 /users/ [root@localhost ~]# mount ... /dev/mapper/myvg-mylv1 on /users type ext4 (rw,acl)
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost ~]# useradd -d /users/magedu magedu [magedu@localhost root]$ cd /users/magedu/ [magedu@localhost ~]$ cp /etc/fstab /etc/inittab ./ [magedu@localhost ~]$ ll total 8 -rw-r--r--. 1 magedu magedu 860 Aug 9 16:00 fstab -rw-r--r--. 1 magedu magedu 884 Aug 9 16:00 inittab
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost ~]# lvs myvg/mylv1 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-ao---- 5.00g [root@localhost ~]# lvextend -L +4G /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 ~]# resize2fs /dev/myvg/mylv1 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@localhost ~]# df -lh Filesystem Size Used Avail Use% Mounted on ... /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /users [root@localhost ~]# cat /users/magedu/fstab # # /etc/fstab .... proc /proc proc defaults 0 0
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost ~]# umount /dev/myvg/mylv1 [root@localhost ~]# mount ... none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw) [root@localhost ~]# 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: 18/589824 files (0.0% non-contiguous), 72678/2359296 blocks [root@localhost ~]# 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: 18/589824 files (0.0% non-contiguous), 72678/2359296 blocks [root@localhost ~]# lvreduce -L -2G /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 ~]# ll /users/ total 20 drwxr-xr-x. 2 root root 16384 Aug 9 15:50 lost+found drwx------. 2 magedu magedu 4096 Aug 9 16:01 magedu [root@localhost ~]# cat /users/magedu/inittab # inittab is only used by upstart for the default runlevel. ... # id:3:initdefault:
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost ~]# lvcreate -s -L 1M -n lv_magedu -p r /dev/myvg/mylv1 Rounding up size to full physical extent 8.00 MiB Logical volume "lv_magedu" created [root@localhost ~]# mount /dev/myvg/lv_magedu /backup/ mount: block device /dev/mapper/myvg-lv_magedu is write-protected, mounting read-only [root@localhost ~]# cd /backup/magedu/ [root@localhost magedu]# ll total 8 -rw-r--r--. 1 magedu magedu 860 Aug 9 16:00 fstab -rw-r--r--. 1 magedu magedu 884 Aug 9 16:00 inittab
原创文章,作者:Net21_Lion,如若转载,请注明出处:http://www.178linux.com/35053
评论列表(1条)
写的很好,排版也很棒,加油