1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[[email protected] ~]# fdisk /dev/sdb 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-13054, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-13054, default 13054): +10G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [[email protected] ~]# mkfs.ext4 -b 2048 -m 2 -L ‘MYDATA‘ /dev/sdb1 [[email protected] ~]# tune2fs -o acl /dev/sdb1 tune2fs 1.41.12 (17-May-2010)
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[[email protected] ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata [[email protected] ~]# mount | grep ‘sdb1‘
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[[email protected] ~]# fdisk /dev/sdb 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): 2 First cylinder (1307-13054, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-13054, default 13054): +1G Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): 82 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): p 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: 0xb103db18 Device Boot Start End Blocks Id System /dev/sdb1 1 1306 10490413+ 83 Linux /dev/sdb2 1307 1438 1060290 82 Linux swap / Solaris Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [[email protected] ~]# mkswap /dev/sdb2 #创建sdb2的文件系统为swap# mkswap: /dev/sdb2: warning: don‘t erase bootbits sectors (dos partition table detected). Use -f to force. Setting up swapspace version 1, size = 1060284 KiB no label, UUID=d2d5c88e-94ca-45e7-9185-f80e83a8fea5 [[email protected] ~]# mkswap -f /dev/sdb2 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=d1194b43-1074-4d17-8ea7-3d94f323ba64 [[email protected] ~]# swapon /dev/sdb2
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash disk=$(fdisk -l | grep -o ‘^Disk /dev/[a-z]d[a-z]\>‘ | cut -d‘ ‘ -f2) echo -e "all the disk infomation:\n${disk}\n" echo ‘all the partition infomation:‘ for i in `fdisk -l | grep -o ‘^/dev/[a-z]d[a-z][1-9]‘` do df -h $i | grep -C2 ‘^/dev/‘ done
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID 0 数据切分存在两块盘,读写性能提升,空间每块硬盘直接叠加,无容错能力,最少需要2块盘组合。增加需要2的倍数。
RAID 1 数据写时每块硬盘各保存一份,写性能下降,读取性能提升,空间减半,有容错能力,一块坏,不影响另一块使用,最少2块盘,增加需要2的倍数。
RAID 5 分布式奇偶校验独立磁盘结构,数据交叉存储,读写性能提升,空间磁盘数量N-1,有容错能力,只能坏1块盘,最少需要3块磁盘组合。
RAID 10 先进行raid1,再做raid0,具有两者特性,读写性能提升,有容错能力,磁盘利用率50%。
其他RAID级别一般使用较少。
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[[email protected] /]# mdadm -C /dev/md1 -n 2 -l 1 -c 128 -x 1 /dev/sdb3 /dev/sd4 /dev/sdb5
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[[email protected] /]# mdadm -C /dev/md2 -n 3 -l 5 -c 256 /dev/sdb5 /dev/sdb6 /dev/sdb7 [[email protected] /]# mkfs.ext4 /dev/md2 [[email protected] /]# vim /etc/fstab /dev/md2 /backup ext4 noatime,acl,defaults 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # declare -i sum=0 if [ $# -lt 1 ];then echo “Please in put at least file path” && exit 1 fi for i in [email protected];do if [ -f $i ];then echo "$i file lines is: `wc -l $i | cut -d" " -f1` " let sum++ else echo "$i is not right file path" fi done echo "this sum is file number is: $sum"
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # declare -i name=0 if [ $# -lt 2 ] ;then echo "Please give at least two need to create a username" && exit 3 fi for i in [email protected];do id $i &> /dev/null if [ $? -eq 0 ];then echo "This $i exists" else useradd $i && echo "$i" | passwd --stdin $i let name++ fi done echo "Create user number:$name"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i sum=0 for i in {1..20};do useradd visitor$i && echo "visitor$i UID is: `id -u visitor$i`" name=$(id -u visitor$i) let sum+=$name done echo "the user UID sum is : $sum"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash # declare -i m=0 declare -i n=0 for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do fu=`grep "^#" $i | wc -l` ng=`grep "^[[:space:]]\+$" $i | wc -l` let m+=$fu let n+=$ng done echo "The total number of # for the beginning :$m" echo "Total number of blank lines:$n"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash # declare -i sum=0 user=$(grep "/bin/bash$" /etc/passwd) for i in $user;do uid=`echo $i | cut -d: -f3` user=`echo $i | cut -d: -f1` echo "$user UID is $uid" let sum+=$uid done echo "默认shell为bash的用户UID之和为:$sum"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash declare -i user=0 name=`cut -d: -f1 /etc/passwd` for i in $name;do group=`id $i|cut -d" " -f3|awk -F"," ‘{print $2}‘` if [ -n "$group" ];then echo "$i 拥有附加组" let user++ fi done echo "拥有附加组的用户总数是: $user"
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[[email protected] ~]# fdisk /dev/sdb Command (m for help): n First cylinder (2869-15665, default 2869): Using default value 2869 Last cylinder, +cylinders or +size{K,M,G} (2869-15665, default 15665): +10G Command (m for help): n First cylinder (4175-15665, default 4175): Using default value 4175 Last cylinder, +cylinders or +size{K,M,G} (4175-15665, default 15665): +10G Command (m for help): t Partition number (1-6): 5 Hex code (type L to list codes): 8e Changed system type of partition 5 to 8e (Linux LVM) Command (m for help): t Partition number (1-6): 6 Hex code (type L to list codes): 8e Changed system type of partition 6 to 8e (Linux LVM) Command (m for help): p Disk /dev/sdb: 128.8 GB, 128849018880 bytes 255 heads, 63 sectors/track, 15665 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: 0x000d5ced Device Boot Start End Blocks Id System /dev/sda1 * 1 64 512000 83 Linux Partition 1 does not end on cylinder boundary. /dev/sdb4 2869 15665 102788088+ 5 Extended /dev/sdb5 2869 4174 10486599+ 8e Linux LVM /dev/sdb6 4175 5480 10490413+ 8e 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. [[email protected] ~]# pvcreate /dev/sdb5 Physical volume "/dev/sda5" successfully created [[email protected] ~]# pvcreate /dev/sdb6 Physical volume "/dev/sda6" successfully created [[email protected] ~]# pvdisplay PV VG Fmt Attr PSize PFree /dev/sdb5 lvm2 --- 10.00g 10.00g /dev/sdb6 lvm2 --- 10.00g 10.00g [[email protected] ~]# vgcreate -s 8M myvg /dev/sdb{5,6} Volume group "myvg" successfully created [[email protected] ~]# vgdisplay --- 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.99 GiB PE Size 8.00 MiB Total PE 2559 Alloc PE / Size 0 / 0 Free PE / Size 2559 / 19.99 GiB VG UUID NnCyy1-kevE-pul3-aGpH-GySi-GOQT-S4DABZ [[email protected] ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [[email protected] ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 5sVS3Y-xers-mlnv-c32W-pOnb-shBo-xLCxmD LV Write Access read/write LV Creation host, time linuxnode03, 2016-08-26 23:31:39 +0800 LV Status available # open 0 LV Size 5.00 GiB Current LE 640 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [[email protected] ~]# mkfs.ext4 /dev/myvg/mylv mke2fs 1.41.12 (17-May-2010) 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 Writing inode tables: done Creating journal (32768 blocks): done Writing superblocks and filesystem accounting information: done This filesystem will be automatically checked every 23 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [[email protected] ~]# blkid /dev/sdb5: UUID="hXii7i-t1yU-k5ZA-3C6q-67iZ-2Rz0-JkZF2e" TYPE="LVM2_member" /dev/sdb6: UUID="CLhY2q-9K77-she6-G7Mr-w4nw-9ZLv-J7ccsH" TYPE="LVM2_member" /dev/mapper/myvg-mylv1: UUID="1e6bda45-524f-403e-9f52-5fa0d0c4f56b" TYPE="ext4" 编辑/etc/fstab,在文末行添加此行内容 UUID=1e6bda45-524f-403e-9f52-5fa0d0c4f56b /users ext4 defaults,acl 0 0
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[[email protected] ~]# mkdir -p /users/magedu [[email protected] ~]# useradd -d /users/magedu magedu [[email protected] ~]# su - magedu [[email protected] ~]$ cp -ar /etc/rc.d/* ~
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[[email protected] ~]# lvextend -L +4G /dev/myvg/mylv1 Size of logical volume myvg/mylv changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized [[email protected] ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 5sVS3Y-xers-mlnv-c32W-pOnb-shBo-xLCxmD LV Write Access read/write LV Creation host, time linuxnode03, 2016-08-26 23:31:39 +0800 LV Status available # open 0 LV Size 9.00 GiB Current LE 1152 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [[email protected] ~]# resize2fs /dev/myvg/mylv1 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long.
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[[email protected] ~]# umount /dev/myvg/mylv1 [[email protected] ~]# 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: 11/589824 files (0.0% non-contiguous), 72671/2359296 blocks [[email protected] ~]# resize2fs /dev/myvg/mylv1 [[email protected] ~]# 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 mylv successfully resized [[email protected] ~]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID 5sVS3Y-xers-mlnv-c32W-pOnb-shBo-xLCxmD LV Write Access read/write LV Creation host, time linuxnode03, 2016-08-26 23:31:39 +0800 LV Status available # open 0 LV Size 7.00 GiB Current LE 896 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 256 Block device 253:0 [[email protected] ~]# mount /dev/myvg/mylv1 /users
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[[email protected] users]# lvcreate -L 1G -n snapmylv1 -p r -s /dev/myvg/mylv1 [[email protected] users]# cd /dev/myvg/ [[email protected] myvg]# ls mylv1 snapmylv1