1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@wlm ~]# fdisk /dev/sdb #将/dev/sdb进行分区操作 Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x2f796e2b. 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 #n代表创建新分区 Command action e extended p primary partition (1-4) p #主分区 Partition number (1-4): 1 #分区NO.为1 First cylinder (1-2610, default 1): #默认从第一个柱面开始 Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G #大小为10G+ Command (m for help): w #保存退出 The partition table has been altered! [root@wlm ~]# mke2fs -t ext4 -b 2048 -L MYDATA -m 2 /dev/sdb1 #block大小为2048, 预留空间百分比为2, 卷标为MYDATA [root@wlm ~]# tune2fs -o acl /dev/sdb1 #默认挂载属性包含acl
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@wlm ~]# mount -o noexec,noatime /dev/sdb1 /data/mydata
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[root@wlm ~]# fdisk /dev/sdb #从/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-2610, default 1307): Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-2610, default 2610): +1G #大小为1G Command (m for help): t #更改分区类型,默认为83 Partition number (1-4): 2 #对第二块磁盘进行操作 Hex code (type L to list codes): 82 #分区类型为82(swap) Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): w #保存退出 The partition table has been altered! [root@wlm ~]# partx -a /dev/sdb #强制内核重读磁盘分区信息 [root@wlm ~]# mkswap /dev/sdb2 [root@wlm ~]# swapon /dev/sdb2 #并创建好文件系统,并启用
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
#!/bin/bash # fdisk -l /dev/s[dh][a-z]|grep -o "Disk /dev/s[hd][a-z]"|cut -d" " -f2 echo -e "\n" df -h
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID0 读、写性能提升; 可用空间:N*min(S1,S2,...) 无容错能力 最少磁盘数:2, 2+ RAID1 读性能提升、写性能略有下降; 可用空间: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+ RAID-01: 读、写能力提升 可用空间:N*min(S1,S2,...)/2 有容错能力:每组镜像最多可坏三块; 最少磁盘数:4, 4+ JBOD:Just a Bunch Of Disks 功能:将多块磁盘的空间合并一个大的连续空间使用; 可用空间:sum(S1,S2,...)
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
# 首先创建这三个分区,/dev/sdb1 /dev/sdc1 /dev/sdd1 # mdadm -C /dev/md0 -n 2 -l 1 -c 128 -x 1 /dev/sdb1 /dev/sdc1 /dev/sdd1
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
首先创建3个2G分区,/dev/sdb1 /dev/sdc1 /dev/sdd1 # mdadm -C /dev/md1 -n 3 -l 5 -c 256 /dev/sdb2 /dev/sdc2 /dev/sdd2mke2fs -t ext4 /dev/md1 # vim /etc/fstab # /dev/md1 /backup ext4 noatime,acl 0 0
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash # [ $# -eq 0 ] && echo "At least one path!" && exit 1 for i in $*; do echo "$i has $(wc -l $i | cut -d" " -f1) lines." done echo echo $#
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash # if [ $# -eq 0 ]; then echo "At least one user." exit 1 fi declare -a users declare -i sum=0 for i in $*; do if id $i &>/dev/null; then echo "user $i has existed!" else useradd $i echo "$i" | passwd --stdin $i &>/dev/null users[${#users[*]}]=$i sum+=1 fi done [ ${#users[*]} -gt 0 ] && echo -e "You have add $sum users,\nThey are ${users[*]}"
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash # declare -i sum=0 for i in {1..20}; do id visitor$i &>/dev/null && echo "visitor$i has existed!" || useradd visitor$i &>/dev/null && echo "Add visitor$i success!" let sum+=$(id -u visitor$i) let i++ done echo "SumID:$sum"
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash # declare -i lines_sum1=0 declare -i lines_sum2=0 lines_sum1=$(($(grep "^#" /etc/rc.d/rc.sysinit | wc -l)+$(grep "^#" /etc/rc.d/init.d/functions | wc -l)+$(grep "^#" /etc/fstab | wc -l))) lines_sum2=$(($(grep "^$" /etc/rc.d/rc.sysinit | wc -l)+$(grep "^$" /etc/rc.d/init.d/functions | wc -l)+$(grep "^$" /etc/fstab | wc -l))) echo "以#号开头的行数之和为:$lines_sum1" echo "以#号开头的行数之和为:$lines_sum2"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
使用awk来切割
#!/bin/bash declare -i id_sum=0 grep "bash$" /etc/passwd | awk -F: '{printf "Username: %-10s UID: %d\n",$1,$3}' for i in $(grep "bash$" /etc/passwd | awk -F: '{print $3}');do id_sum+=$i done echo "默认shell为bash的用户的UID之和为:$id_sum"
使用cut切割
#!/bin/bash # declare -i sum=0 for i in $(egrep "(/bin/bash)$" /etc/passwd | cut -d: -f3); do let sum+=$i done echo "当前系统上所有默认shell为bash的用户的用户名、UID:$(egrep "(/bin/bash)$" /etc/passwd | cut -d: -f1,3)" echo "此类所有用户的UID之和:$sum"
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash # declare -i count=0 for i in $(cat /etc/passwd | cut -d: -f1); do sum_groups=$(id -G $i | wc -w) if [ $sum_groups -gt 1 ]; then echo "$i has appand group." let count++ fi done echo "There are $count users has appand group."
13、创建一个由至少两个物理卷组成的大小为20G的卷组VG;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
先在块磁盘上分区,分别为/dev/sdb3和/dev/sdc3,然后创建成连个PV
以/dev/sdc3为例:
[root@wlm script_test]# fdisk /dev/sdc #对/dev/sdc磁盘进行操作 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): 3 #分区号 First cylinder (12-2610, default 12): Using default value 12 Last cylinder, +cylinders or +size{K,M,G} (12-2610, default 2610): +15G #15G大小 Command (m for help): w
将/dev/sdb3和/dev/sdc3创建成PV
[root@wlm script_test]# pvcreate /dev/sdb3 Physical volume "/dev/sdb3" successfully created [root@wlm script_test]# pvcreate /dev/sdc3 Physical volume "/dev/sdc3" successfully created [root@wlm script_test]# pvs PV VG Fmt Attr PSize PFree /dev/sda2 vg_wlm lvm2 a-- 77.80g 0 /dev/sdb3 lvm2 --- 5.01g 5.01g /dev/sdc3 lvm2 --- 15.01g 15.01g
将两个PV组成一个VG,PE大小为8M:
vg_wlm 1 1 0 wz--n- 77.80g 0 [root@wlm script_test]# vgcreate -s 8M myvg /dev/sd{b3,c3} Volume group "myvg" successfully created [root@wlm script_test]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 20.01g 20.01g vg_wlm 1 1 0 wz--n- 77.80g 0
在myvg中创建逻辑卷LV为5G的mylv1:
[root@wlm script_test]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@wlm script_test]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 5.00g LogVol00 vg_wlm -wi-ao---- 77.80g [root@wlm script_test]# lvdisplay --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID H37byP-lYyu-muah-DrZF-VpPk-h9Ed-mNO7Gf LV Write Access read/write LV Creation host, time wlm, 2016-11-08 02:38:02 +0800 LV Status available # open 0 LV Size 5.00 GiB
对可用分区mylv1格式化并设置开机自动挂载:
[root@wlm script_test]# mkfs.ext4 /dev/myvg/mylv1 #在/etc/initab里添加: /dev/myvg/mylv1 /users ext4 defaults,acl 0 0 [root@wlm script_test]# mount -a [root@wlm script_test]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_wlm-LogVol00 77G 9.8G 63G 14% / tmpfs 431M 72K 431M 1% /dev/shm /dev/sda1 190M 90M 91M 50% /boot /dev/sdb1 9.8G 13M 9.6G 1% /data/mydata /dev/mapper/myvg-mylv1 4.8G 10M 4.6G 1% /mylv1 /dev/mapper/myvg-mylv1 4.8G 10M 4.6G 1% /users
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@wlm script_test]# useradd -d /users/magedu magedu [root@wlm script_test]# su - magedu [magedu@wlm ~]$ cp /etc/networks /etc/my.cnf ./
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
vg的物理边界足够大,可以直接扩展;
[root@wlm script_test]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_wlm-LogVol00 77G 9.8G 63G 14% / tmpfs 431M 72K 431M 1% /dev/shm /dev/sda1 190M 90M 91M 50% /boot /dev/sdb1 9.8G 13M 9.6G 1% /data/mydata /dev/mapper/myvg-mylv1 4.8G 11M 4.6G 1% /mylv1 /dev/mapper/myvg-mylv1 4.8G 11M 4.6G 1% /users [root@wlm script_test]# lvextend -L 9G /dev/myvg/mylv1 #注意,9G是扩展后的空间 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@wlm script_test]# resize2fs /dev/myvg/mylv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv1 is mounted on /mylv1; 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@wlm script_test]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/vg_wlm-LogVol00 77G 9.8G 63G 14% / tmpfs 431M 72K 431M 1% /dev/shm /dev/sda1 190M 90M 91M 50% /boot /dev/sdb1 9.8G 13M 9.6G 1% /data/mydata /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /mylv1 /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /users [magedu@wlm ~]$ ls my.cnf networks
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@wlm script_test]# umount /dev/myvg/mylv1 #卸载 [root@wlm script_test]# e2fsck -f /dev/myvg/mylv1 [root@wlm script_test]# resize2fs /dev/myvg/mylv1 7G #强制检测文件系统并先缩减文件系统的大小 [root@wlm script_test]# lvreduce -L 7G /dev/myvg/mylv1 [root@wlm script_test]# mount -a #缩小lv的大小并重新挂载
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
快照卷和原卷必须在同一个VG上:
[root@wlm script_test]# lvcreate -L 20M -s -p r -n mylv1-snap /dev/myvg/mylv1 Rounding up size to full physical extent 24.00 MiB Logical volume "mylv1-snap" created.
挂载快照卷并查看:
[root@wlm magedu]# mount /dev/myvg/mylv1-snap /mnt/ [root@wlm ~]# cd /mnt/ [root@wlm mnt]# ls lost+found magedu
原创文章,作者:dawning,如若转载,请注明出处:http://www.178linux.com/58278
评论列表(2条)
可以加个qq吗
@Old_Driver:可以,2986158667