1、創建一個10G分區,并格式為ext4文件系統;
[root@localhost ~]# fdisk /dev/sda 歡迎使用 fdisk (util-linux 2.23.2)。 更改將停留在內存中,直到您決定將更改寫入磁盤。 使用寫入命令前請三思。 命令(輸入 m 獲取幫助):n Partition type: p primary (3 primary, 0 extended, 1 free) e extended Select (default e): p 已選擇分區 4 起始 扇區 (102586368-209715199,默認為 102586368): 將使用默認值 102586368 Last 扇區, +扇區 or +size{K,M,G} (102586368-209715199,默認為 209715199):+10G 分區 4 已設置為 Linux 類型,大小設為 10 GiB 命令(輸入 m 獲取幫助):w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 設備或資源忙. 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) 正在同步磁盤。 [root@localhost ~]#
[root@localhost ~]# partx -a /dev/sda
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
[root@localhost ~]# mke2fs -t ext4 -b 2048 -L 'MYDATA' -m 2 /dev/sda5 mke2fs 1.42.9 (28-Dec-2013) 文件系統標簽=MYDATA OS type: Linux 塊大小=2048 (log=1) 分塊大小=2048 (log=1) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 2621440 blocks 52428 blocks (2.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=540016640 160 block groups 16384 blocks per group, 16384 fragments per group 2048 inodes per group Superblock backups stored on blocks: 16384, 49152, 81920, 114688, 147456, 409600, 442368, 802816, 1327104, 2048000 Allocating group tables: 完成 正在寫入inode表: 完成 Writing superblocks and filesystem accounting information: 完成
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@localhost ~]# mount -o nodiratime,noexec /dev/sda5 /data/mydata/
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
[root@localhost ~]# fdisk /dev/sda 歡迎使用 fdisk (util-linux 2.23.2)。 更改將停留在內存中,直到您決定將更改寫入磁盤。 使用寫入命令前請三思。 命令(輸入 m 獲取幫助):n All primary partitions are in use 添加邏輯分區 7 起始 扇區 (115175424-123557887,默認為 115175424): 將使用默認值 115175424 Last 扇區, +扇區 or +size{K,M,G} (115175424-123557887,默認為 123557887):+1G 分區 7 已設置為 Linux 類型,大小設為 1 GiB 命令(輸入 m 獲取幫助):t 分區號 (1-7,默認 7):7 Hex 代碼(輸入 L 列出所有代碼):82 已將分區“Linux”的類型更改為“Linux swap / Solaris” 命令(輸入 m 獲取幫助):w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 設備或資源忙. 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) 正在同步磁盤。
[root@localhost ~]# partx -a 7 /dev/sda
[root@localhost ~]# mkswap /dev/sda7 正在設置交換空間版本 1,大小 = 1048572 KiB 無標簽,UUID=6151d94b-d2e7-473a-80c5-9085822aa01e
[root@localhost ~]# swapon /dev/sda7
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/bin/bash echo "Print all disks information" echo " " diskinfo=$(fdisk -l /dev/sd[a-z] | grep '^Disk[[:space:]]/'| awk '{print $1,$2,$3,$4}') echo $diskinfo echo " " echo "Print partations space information" echo " " for i in $(fdisk -l | egrep "^/dev/sd[a-z][[:digit:]]" | awk '{print $1}' ) do df -h $i done
4、總結RAID的各個級別及其組合方式和性能的不同;
RAID0:無容錯能力,提升讀、寫性能,可用空間為N,最少磁盤數為2 RAID1:有容錯能力,提升讀性能,寫性能略有下降,可用空間為N/2,最少磁盤數為2 RAID5:提供一塊磁盤的容錯能力,提升讀、寫性能,可用空間為N-1,最少磁盤數為3 RAID6:提供兩塊磁盤的容錯能力,提升讀、寫性能,可用空間為N-2,最少磁盤數為4 RAID10:有容錯能力,但每組鏡像最多只能壞一塊磁盤,提升讀、寫性能,可用空間為N/2,最少磁盤數為4
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
添加三塊10G大小的磁盤,
[root@localhost ~]# fdisk /dev/sdb Welcome to fdisk (util-linux 2.23.2). Changes will remain in memory only, until you decide to write them. Be careful before using the write command. Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 5 First sector (4096-207620095, default 4096): Using default value 4096 Last sector, +sectors or +size{K,M,G} (4096-207620095, default 207620095): +10G Partition 5 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 6 First sector (20977664-207620095, default 20977664): Using default value 20977664 Last sector, +sectors or +size{K,M,G} (20977664-207620095, default 207620095): +10G Partition 6 of type Linux and of size 10 GiB is set Command (m for help): n Partition type: p primary (0 primary, 1 extended, 3 free) l logical (numbered from 5) Select (default p): l Adding logical partition 7 First sector (41951232-207620095, default 41951232): Using default value 41951232 Last sector, +sectors or +size{K,M,G} (41951232-207620095, default 207620095): +10G Partition 7 of type Linux and of size 10 GiB is set Command (m for help): t Partition number (1,5-7, default 7): 5 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1,5-7, default 7): 6 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): t Partition number (1,5-7, default 7): 7 Hex code (type L to list all codes): fd Changed type of partition 'Linux' to 'Linux raid autodetect' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@localhost ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 partx: /dev/sdb: error adding partitions 5-7
[root@localhost ~]# mdadm -C /dev/md0 -n2 -l1 -ayes -c128 -x1 /dev/sdb5 /dev/sdb6 /dev/sdb7
[root@localhost ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Fri Aug 5 16:00:06 2016 Raid Level : raid1 Array Size : 10477568 (9.99 GiB 10.73 GB) Used Dev Size : 10477568 (9.99 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Fri Aug 5 16:00:58 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 : e14beb8f:691289a4:23e23350:2ee02f88 Events : 17 Number Major Minor RaidDevice State 0 8 21 0 active sync /dev/sdb5 1 8 22 1 active sync /dev/sdb6 2 8 23 - spare /dev/sdb7 [root@localhost ~]#
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
添加磁盤3塊大小為2G的磁盤
步驟略。。。。
創建RAID5設備
[root@localhost ~]# mdadm -C /dev/md1 -n3 -l5 -c256 -ayes /dev/sdb8 /dev/sdb9 /dev/sdb10 mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started.
格式化設備,開機自動掛載至/backup,不更新訪問時間戳,支持acl功能
[root@localhost ~]# mke2fs -t ext4 /dev/md1 mke2fs 1.42.9 (28-Dec-2013) Filesystem label= OS type: Linux Block size=4096 (log=2) Fragment size=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262144 inodes, 1047552 blocks 52377 blocks (5.00%) reserved for the super user First data block=0 Maximum filesystem blocks=1073741824 32 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 Allocating group tables: done Writing inode tables: done Creating journal (16384 blocks): done Writing superblocks and filesystem accounting information: done [root@localhost ~]#
[root@localhost ~]# mount -o noatime,acl /dev/md1 /backup
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Number of statistics file echo "Please input the filename,I will help you statistics the file lines." read -p "Enter the filename:" filename i=0 for file in $filename;do if [ ! -e $file ];then echo "No such file." exit 3 fi echo "The number of rows in $file is:$(cat $file | wc -l)" let i=i+1 done echo "Now, I help you statistics $i file lines."
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Add users. echo "Now, I will help you add users, the password as well as username." read -p "Please input the username:" username i=0 for users in $username;do if id $users &> /dev/null then echo "$users is exist." else useradd $users echo "$users" | passwd --stdin $users fi let i=i+1 done echo "Now, I help you add $i users."
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Add twenty users visitor1 to visitor20 and calculate their UID sum. x=0 y=0 for i in {1..20};do if id visitor$i &> /dev/null;then echo "The visitor$i is exist." uid1=$(cat /etc/passwd | grep "visitor$i" | cut -d: -f3) let y=y+$uid1 else useradd visitor$i uid2=$(cat /etc/passwd | grep "visitor$i" | cut -d: -f3) let x=x+$uid2 fi done sum=$[ x+y ] echo "The UID sum of user visitor1 to user visitor 20 is: $sum"
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Statistics of the /etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions and /etc/fstab file to # number at the beginning of the line number # and the total number of blank lines. for i in /etc/rc.d/rc.sysinit /etc/rc.d/init.d/functions /etc/fstab;do if [ ! -e $i ];then echo "No such file: $i" continue else x=0 y=0 x=$(cat $i | grep -o "^[#]*" | wc -l) y=$(cat $i | grep "^$" | wc -l) echo "The file of $i to # number at the beginning of the line number is $x." echo "The file of $i to total numer of blank number is $y." fi done
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Show the users and uid of this system that the default shell as wall as username,and calculate their uid sum. cat /etc/passwd | grep "bash$" | cut -d: -f1,3 i=0 for sum in $(cat /etc/passwd | grep "bash$" | cut -d: -f3);do let i=$sum+i done echo "The UID sum is:$i"
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
#!/bin/bash # Version: 0.0.1 # Auther: Anaconda # Description: Show this system which user have additional group and statistics it. for users in $(cat /etc/passwd | cut -d: -f3);do if [ -n "$users" ];then unames=$(cat /etc/passwd | grep "$users" | cut -d: -f1) echo $unames let i=i+1 fi done echo "A total of such users is: $i"
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
[root@localhost ~]# pvcreate /dev/sdc1 /dev/sdc2 Physical volume "/dev/sdc1" successfully created Physical volume "/dev/sdc2" successfully created [root@localhost ~]# vgcreate -s 8M testvg /dev/sdc1 /dev/sdc2 Volume group "testvg" successfully created [root@localhost ~]# lvcreate -L 5G -n mylv1 testvg Logical volume "mylv1" created. [root@localhost ~]# mke2fs -t ext4 /dev/testvg/mylv1 [root@localhost ~]# mkdir /users [root@localhost ~]# mount -o auto,acl /dev/testvg/mylv1 /users
14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;
[root@localhost ~]# useradd -d /users/magedu magedu [root@localhost ~]# cp /etc/fstab /etc/inittab /var/log/anaconda/ifcfg.log /users/magedu/
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
[root@localhost ~]# lvextend -L +4G /dev/testvg/mylv1 Size of logical volume testvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# resize2fs /dev/testvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/testvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/testvg/mylv1 is now 2359296 blocks long. [root@localhost ~]# lvdisplay --- Logical volume --- LV Path /dev/testvg/mylv1 LV Name mylv1 VG Name testvg LV UUID nb0bjj-RDOC-Lnjo-PDKE-CwIK-Peta-DeLnSN LV Write Access read/write LV Creation host, time localhost.localdomain, 2016-08-15 11:31:10 +0800 LV Status available # open 1 LV Size 9.00 GiB Current LE 1152 Segments 1 Allocation inherit Read ahead sectors auto - currently set to 8192 Block device 253:3 [root@localhost ~]# cat /users/magedu/fstab # # /etc/fstab # Created by anaconda on Fri Mar 11 22:14:07 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=73822afe-bc00-431b-8851-0151d8054a67 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
16、縮減mylv1至7G,確保縮減完成后原有數據完全可用;
[root@localhost ~]# umount /dev/testvg/mylv1 [root@localhost ~]# e2fsck -f /dev/testvg/mylv1 e2fsck 1.42.9 (28-Dec-2013) 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/testvg/mylv1: 22/589824 files (0.0% non-contiguous), 75562/2359296 blocks [root@localhost ~]# resize2fs /dev/testvg/mylv1 7G resize2fs 1.42.9 (28-Dec-2013) Resizing the filesystem on /dev/testvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/testvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# lvreduce -L -2G /dev/testvg/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 testvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. [root@localhost ~]# mount -o auto,acl /dev/testvg/mylv1 /users [root@localhost ~]# cat /users/magedu/fstab # # /etc/fstab # Created by anaconda on Fri Mar 11 22:14:07 2016 # # Accessible filesystems, by reference, are maintained under '/dev/disk' # See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info # /dev/mapper/centos-root / xfs defaults 0 0 UUID=73822afe-bc00-431b-8851-0151d8054a67 /boot xfs defaults 0 0 /dev/mapper/centos-home /home xfs defaults 0 0 /dev/mapper/centos-swap swap swap defaults 0 0
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;
[root@localhost ~]# lvcreate -L 7G -p r -s -n mylv1_snapshot /dev/testvg/mylv1 Logical volume "mylv1_snapshot" created.
原創文章,作者:Anaconda,如若轉載,請注明出處:http://www.www58058.com/24717