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.www58058.com/41749
博客寫得非常的好,32個贊,給出了詳細操作步驟,腳本思路清晰,加油!