1、创建一个10G分区,并格式为ext4文件系统;
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
[root@localhost ~]# 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): 3 First cylinder (787-2610, default 787): Using default value 787 Last cylinder, +cylinders or +size{K,M,G} (787-2610, default 2610): +10G Command (m for help): w [root@localhost ~]# mke2fs -t ext4 /dev/sdb3 -l 'MYDATA' -b 2048 -m 2 [root@localhost ~]# tune2fs -o acl /dev/sdb3 tune2fs 1.41.12 (17-May-2010) [root@localhost ~]# tune2fs -l /dev/sdb3 tune2fs 1.41.12 (17-May-2010) Filesystem volume name: MYDATA Last mounted on: <not available> Filesystem UUID: 34e3ab57-e858-4a16-96aa-596b10e550cc Filesystem magic number: 0xEF53 Filesystem revision #: 1 (dynamic) Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize Filesystem flags: signed_directory_hash Default mount options: acl Filesystem state: clean Errors behavior: Continue Filesystem OS type: Linux Inode count: 657408 Block count: 5245222 Reserved block count: 104904 Free blocks: 5123398 Free inodes: 657397 First block: 0 Block size: 2048 Fragment size: 2048 Reserved GDT blocks: 512 Blocks per group: 16384 Fragments per group: 16384 Inodes per group: 2048 Inode blocks per group: 256 Flex block group size: 16 Filesystem created: Tue Sep 6 01:52:06 2016 Last mount time: n/a Last write time: Tue Sep 6 02:18:14 2016 Mount count: 0 Maximum mount count: 38 Last checked: Tue Sep 6 01:52:06 2016 Check interval: 15552000 (6 months) Next check after: Sun Mar 5 00:52:06 2017 Lifetime writes: 226 MB Reserved blocks uid: 0 (user root) Reserved blocks gid: 0 (group root) First inode: 11 Inode size: 256 Required extra isize: 28 Desired extra isize: 28 Journal inode: 8 Default directory hash: half_md4 Directory Hash Seed: 0ec2f874-96b9-4d4f-99f9-210c998de1b2 Journal backup: inode blocks
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[root@localhost ~]# mkdir /data/mydata [root@localhost ~]# fdisk /dev/sdb [root@localhost ~]# mount -o noexec,noatime /data/mydata
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
第一步: [root@localhost ~]# 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 Selected partition 4 First cylinder (2093-2610, default 2093): Using default value 2093 Last cylinder, +cylinders or +size{K,M,G} (2093-2610, default 2610): +1G Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. You have new mail in /var/spool/mail/root 第二步: [root@localhost ~]# 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): t Partition number (1-4): 4 Hex code (type L to list codes): 82 Changed system type of partition 4 to 82 (Linux swap / Solaris) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. You have new mail in /var/spool/mail/root [root@localhost ~]# mkswap /dev/sdb4 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=8676884c-2a29-4cf5-a6f9-465f333f1ba3 [root@localhost ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x0001bbdc Device Boot Start End Blocks Id System /dev/sda1 * 1 39 307200 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 39 2354 18598912 83 Linux /dev/sda3 2354 2358 33499 83 Linux /dev/sda4 2359 2610 2024190 83 Linux Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x4a01ccd9 Device Boot Start End Blocks Id System /dev/sdb1 1 393 3156741 83 Linux /dev/sdb2 394 786 3156772+ 83 Linux /dev/sdb3 787 2092 10490445 83 Linux /dev/sdb4 2093 2224 1060290 82 Linux swap / Solaris [root@localhost ~]#
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上的每个分区相关的空间使用信息
#!/bin/bash for i in $(fdisk -l | grep "^/dev/sd.*" | cut -d " " -f1) do echo $i df $i done ~
4、总结RAID的各个级别及其组合方式和性能的不同;
RAID-0: (把数据切成块,把不同的块分别存放到不同磁盘里面)提高了读写能力,即提升I/O性能,空间增加,无容错能力,最少需要两块磁盘:2,2+ RAID-1: (把完整的数据分别存放到不同的磁盘里面。)提升了读的性能,写的性能没有提升略有下降,可用空间:1*min(s1,s2,s3),有冗余能力,最少磁盘数两个 RAID-4: 专门用一块盘作为校验码,校验码盘的压力会非常大,能容易造成性能瓶颈 ,能允许坏一块盘的情况下奖级允许 。至少三块硬盘, RAID-5: 轮流做校验码的机制 。读写性能提升,可用空间:(n-1)*min(s1,s2....),有容错能力,最多一块磁盘,最少磁盘上三块及以上。 RAID-6: 用两块盘存放校验码 。最少需要四块磁盘,可用空间n-2*min(s1,s2..),有容错能力最多允许坏两块磁盘。 混合类型的RAID: RAID-10: 读写性能有提升,可能空间n*min(s1,s2..) ,每组镜像最多只能坏一个块,最少磁盘数为4,
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
第一步创建三个5G分区: [root@localhost ~]# 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +5G Command (m for help): n Command action e extended p primary partition (1-4) 2 Invalid partition number for type `2' Command action e extended p primary partition (1-4) p Partition number (1-4): 2 First cylinder (655-2610, default 655): Using default value 655 Last cylinder, +cylinders or +size{K,M,G} (655-2610, default 2610): +5G Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 3c PartitionMagic 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 40 Venix 80286 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 41 PPC PReP Boot 85 Linux extended c7 Syrinx 5 Extended 42 SFS 86 NTFS volume set da Non-FS data 6 FAT16 4d QNX4.x 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS 4e QNX4.x 2nd part 88 Linux plaintext de Dell Utility 8 AIX 4f QNX4.x 3rd part 8e Linux LVM df BootIt 9 AIX bootable 50 OnTrack DM 93 Amoeba e1 DOS access a OS/2 Boot Manag 51 OnTrack DM6 Aux 94 Amoeba BBT e3 DOS R/O b W95 FAT32 52 CP/M 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 53 OnTrack DM6 Aux a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 54 OnTrackDM6 a5 FreeBSD ee GPT f W95 Ext'd (LBA) 55 EZ-Drive a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 56 Golden Bow a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 5c Priam Edisk a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 61 SpeedStor a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 63 GNU HURD or Sys ab Darwin boot f2 DOS secondary 16 Hidden FAT16 64 Novell Netware af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 65 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 70 DiskSecure Mult b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 75 PC/IX bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 80 Old Minix be Solaris boot ff BBT 1e Hidden W95 FAT1 Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): fd Changed system type of partition 1 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-4): 2 Hex code (type L to list codes): fd Changed system type of partition 2 to fd (Linux raid autodetect) Command (m for help): w 第二步创建RIAD1: [root@localhost ~]# mdadm -C /dev/md0 -a yes -c 128 -n 2 -x 1 -l 1 /dev/sdb{1,2,3} [root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sun Sep 25 06:49:27 2016 Raid Level : raid1 Array Size : 5249088 (5.01 GiB 5.38 GB) Used Dev Size : 5249088 (5.01 GiB 5.38 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Sun Sep 25 06:49:55 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 : 57c0cb9f:52fcaa66:5165156a:1feb61a7 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功能;
第一步创建三个2G大小的分区: [root@localhost /]#fdisk /dev/sdb [root@localhost /]# fdisk -l /dev/sdb Disk /dev/sdb: 21.5 GB, 21474836480 bytes 255 heads, 63 sectors/track, 2610 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: 0x69e93d89 Device Boot Start End Blocks Id System /dev/sdb1 1 262 2104483+ fd Linux raid autodetect /dev/sdb2 263 524 2104515 fd Linux raid autodetect /dev/sdb3 525 786 2104515 fd Linux raid autodetect 第二步创建RAID5: [root@localhost /]# mdadm -C /dev/md1 -a yes -n 3 -l 5 -c 256 /dev/sdb{1,2,3} [root@localhost /]# mdadm -D /dev/md1 /dev/md1: Version : 1.2 Creation Time : Sun Sep 25 07:45:47 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 : 3 Persistence : Superblock is persistent Update Time : Sun Sep 25 07:47:52 2016 State : clean Active Devices : 3 Working Devices : 3 Failed Devices : 0 Spare Devices : 0 Layout : left-symmetric Chunk Size : 256K Name : localhost.localdomain:1 (local to host localhost.localdomain) UUID : dff82c2c:98dab1ce:c740d7c2:eada91f4 Events : 26 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 18 1 active sync /dev/sdb2 3 8 19 2 active sync /dev/sdb3
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
#!/bin/bash while : do read -p "input file path(end input q):" fileline if [ -d $fileline ];then echo "not file is dirctory,plice input file" elif [ -e $fileline ];then echo $(wc -l $fileline) let a+=1 elif [ $fileline == "q" ];then echo $a break else echo "file not exit" fi done ~
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
#!/bin/bash i=q while : do read -p "plise input name(end input q):" nusname sname=$( echo $nusname | cut -d " " -f1) if [ "$sname" == "$i" ];then break echo "total user:$a" elif [ -z "$sname" ];then echo "username not empty!" continue else if [ $(grep "^$sname" /etc/passwd | wc -l) -ge 1 ] ;then echo "user name exit!" continue else useradd $sname echo $sname | passwd --stdin $sname let a+=1 fi fi done
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
#!/bin/bash for i in {1..20};do id visitor$i >& /dev/null if [ echo $? -eq 0 ];then echo "user exit" else useradd visitor$i >> /dev/null echo "visitor$i" | passwd --stdin visitor$i >> /dev/null idvalue=$(grep "^visitor$i" /etc/passwd | cut -d ":" -f3) idvalue1=$[ idvalue1 + idvalue ] fi done echo $idvalue1
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
#!/bin/bash i=$(grep "^#" /etc/rc.d/rc.sysinit | wc -l) b=$(grep "^[:space:]\{1,\}" /etc/rc.d/rc.sysinit| wc -l) echo "/etc/rc.d/rc.sysinit #:$i spce:$b" c=$(grep "^#" /etc/rc.d/init.d/functions | wc -l) d=$(grep "^[:space:]\{1,\}" /etc/rc.d/init.d/functions| wc -l) echo "/etc/rc.d/init.d/functions #:$c space:$d" e=$(grep "^#" /etc/fstab | wc -l) f=$(grep "^[:space:]\{1,\}" /etc/fstab| wc -l) echo "/etc/fstab #:$e space:$f"
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
#!/bin/bash for i in $(grep "bash$" /etc/passwd);do svalue=$(echo $i | cut -d ":" -f4) svalue1=$[ $svalue1 + $svalue ] done echo $(grep "bash$" /etc/passwd | cut -d ":" -f1,4) echo "total:$svalue1" ~ ~
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
#!/bin/bash for i in $(cat /etc/passwd | cut -d ":" -f1) do groups $i >> tets.txt done grep -v "^.*[[:space:]]:[[:space:]][[:alnum:]]*[[:punct:]]*[[:alnum:]]*$" tets.txt >> test3.txt echo "total:$(wc -l test3.txt)" echo "$(cat test3.txt)" rm tets.txt rm test3.txt ~
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
第一步创建连个10G分区: [root@localhost /]# 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-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): +10G 第二步创建pv: [root@localhost /]# pvcreate /dev/sdb2 /dev/sdb1 Physical volume "/dev/sdb2" successfully created WARNING: software RAID md superblock detected on /dev/sdb1. Wipe it? [y/n] y Wiping software RAID md superblock on /dev/sdb1. Physical volume "/dev/sdb1" successfully created 第三步创建vg: root@localhost /]# vgcreate vgtest -s 8M /dev/sdb1 /dev/sdb2 Volume group "vgtest" successfully created [root@localhost /]# vgdisplay --- Volume group --- VG Name vgtest 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.00 GiB PE Size 8.00 MiB Total PE 2432 Alloc PE / Size 0 / 0 Free PE / Size 2432 / 19.00 GiB VG UUID 4i4y5I-3iFc-T9sf-yWzj-ooCX-6Awh-hev0Uh 第四步创建lv: [root@localhost /]# lvcreate -L 5G -n mylv1 vgtest Logical volume "mylv1" created [root@localhost /]# lvdisplay --- Logical volume --- LV Path /dev/vgtest/mylv1 LV Name mylv1 VG Name vgtest LV UUID XgtGsv-VUBT-CZz3-blhk-NMUX-24Z3-wQFhED LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-25 08:27:28 -0700 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 第五步格式化: [root@localhost /]# mke2fs -t ext4 /dev/vgtest/mylv1 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 39 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. 第六步自动挂载: /dev/vgtest/mylv1 /users ext4 default,noatime,cal 0 0
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[root@localhost /]# mkdir users [root@localhost /]# useradd -md /users/magedu mage [mage@localhost ~]$ cp /proc/net/{anycast6,arp,connector,dev} /users/magedu
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[root@localhost /]# lvextend -L 9G /dev/vgtest/mylv1 Extending logical volume mylv1 to 9.00 GiB Logical volume mylv1 successfully resized [root@localhost /]# lvdisplay --- Logical volume --- LV Path /dev/vgtest/mylv1 LV Name mylv1 VG Name vgtest LV UUID XgtGsv-VUBT-CZz3-blhk-NMUX-24Z3-wQFhED LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-09-25 08:27:28 -0700 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
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[root@localhost /]# resize2fs /dev/vgtest/mylv1 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/vgtest/mylv1 to 2621440 (4k) blocks. The filesystem on /dev/vgtest/mylv1 is now 2621440 blocks long. [root@localhost /]# lvre lvreduce lvremove lvrename lvresize [root@localhost /]# lvre lvreduce lvremove lvrename lvresize [root@localhost /]# lvreduce -L 7G /dev/vgtest/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 Reducing logical volume mylv1 to 7.00 GiB Logical volume mylv1 successfully resized
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[root@localhost /]# lvcreate -L 7G -p r -s -n snaphot_lv_mylv1 /dev/vgtest/mylv1 Logical volume "snaphot_lv_mylv1" created 复制: [root@localhost vgtest]# cp -a /dev/vgtest/snaphot_lv_mylv1 /tmp/
原创文章,作者:liliangming,如若转载,请注明出处:http://www.178linux.com/41749
评论列表(1条)
博客写得非常的好,32个赞,给出了详细操作步骤,脚本思路清晰,加油!