1、創建一個10G分區,并格式為ext4文件系統;
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
(2) 掛載至datamydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳
[root@ns1 ~]# mkfs.ext4 -b 2048 -m 2 -L MYDATA /dev/sdc1 [root@ns1 ~]# tune2fs -o acl /dev/sdc1 tune2fs 1.41.12 (17-May-2010) [root@ns1 ~]# mount -o noexec,noatime,acl /dev/sdc1 /datamydata/ [root@ns1 ~]# cat /etc/mtab | grep sdc1 /dev/sdc1 /datamydata ext4 rw,noexec,noatime,acl 0 0 [root@ns1 ~]# echo "/dev/sdc1 /datamydata ext4 defaults,noexec,noatime,acl 0 0">>/etc/fstab [root@ns1 ~]# mount -a [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
方法1:(利用磁盤分區做swap分區) # fdisk /dev/sdb Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x972e4c2d. 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//添加一個分區 Command action e extended p primary partition (1-4) p Partition number (1-4): 1//分區號,輸入1 First cylinder (1-2610, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): Using default value 2610 Command (m for help): w//保存配置 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. 格式化分區設備: # mkswap /dev/sdb1 Setting up swapspace version 1, size = 20964788 KiB no label, UUID=01896901-c843-4b2c-b627-3ab208325ece 啟用swap分區: # swapon /dev/sdb1 # echo "/dev/sdb1 swap swap defaults 0 0">>/etc/fstab 查看swap分區: # cat /proc/swaps FilenameTypeSizeUsedPriority /dev/sda2 partition41943000-1 /dev/sdb1 partition209647880 -2 # free -m total used free shared buffers cached Mem: 3818 425 3393 1 72 129 -/+ buffers/cache: 223 3594 Swap: 24569 0 24569 方法2:(利用磁盤文件做swap分區) # dd if=/dev/zero of=/swapfile1 bs=1M count=512 記錄了512+0 的讀入 記錄了512+0 的寫出 536870912字節(537 MB)已復制,29.1873 秒,18.4 MB/秒 # mkswap /swapfile1 Setting up swapspace version 1, size = 524284 KiB no label, UUID=3cba4d1a-d846-4e5c-b5b2-7d638bb9c345 You have new mail in /var/spool/mail/root # swapon /swapfile1 # cat /proc/swaps FilenameTypeSizeUsedPriority /dev/sda2 partition41943000-1 /dev/sdb1 partition209647880-2 /swapfile1 file5242840-3 # echo "/swapfile1 swap swap defaults 0 0">>/etc/fstab
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/bin/bash #created by molewan # show the disks names and the space fdisk -l | grep -o "^/[^[:space:]]\+" >/disk.txt df -h>/space.txt # fdisk -l | grep -o "^/[^[:space:]]\+" /dev/sda1 /dev/sda2 /dev/sda3 /dev/sda4 /dev/sda5 # df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.4G 4.5G 66% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp
4、總結RAID的各個級別及其組合方式和性能的不同;
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至backup目錄,而且不更新訪問時間戳,且支持acl功能;
說明:關于RAID,因為生成環境肯定會接觸到,但用到軟RAID的情況真心不太多,所以重點精力還是投在對概念的理解以及應用場景的理解,比如說服務器出廠時默認的RAID通常為RAID1(2塊盤),大于2塊盤一般會用RAID5,一般還會用一塊熱備盤,已便出現故障后,能夠頂上去。
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
# cat collet.sh #!/bin/bash #creted by molewan # declare -i i=0 if [ $# -lt 1 ];then echo "please input file path,for example:/etc/passwd" exit 2 fi for file in $*;do if [ -f $file ] ;then echo "$file:$(wc -l $file | awk '{print $1}')" let i++ else echo "please input file path,for example:/etc/passwd" fi done echo "file counts:$i" 驗證結果: [root@ns1 ~]# bash collet.sh 1 please input file path,for example:/etc/passwd file counts:0 [root@ns1 ~]# bash collet.sh passwd please input file path,for example:/etc/passwd file counts:0 [root@ns1 ~]# bash collet.sh /etc/pass please input file path,for example:/etc/passwd file counts:0 [root@ns1 ~]# file /etc/pass /etc/pass: cannot open `/etc/pass' (No such file or directory) [root@ns1 ~]# bash collet.sh /etc/passwd /etc/passwd:76 file counts:1 [root@ns1 ~]# bash collet.sh /etc/passwd /etc/filesystems /etc/passwd:76 /etc/filesystems:9 file counts:2
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash # declare -i i=0 if [ $# -lt 2 ];then echo "please input two username,for example:zhang3 li4" exit 2 fi for username in $@; do id $username &> /dev/null if [ $? -eq 0 ]; then echo "$username exist" else useradd $username echo "$username" | passwd --stdin $username &> /dev/null let i++ fi done echo "add user count: $i"
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
#!/bin/bash # declare -i sum=0; for ((i=1;i<=20;i++)); do useradd visitor$i; echo "useradd visitor$i success!" sum+=$(id -u visitor$i) done echo "All user uid sum: $sum"
10、寫一腳本,分別統計/etc/rc.d /rc/sysinit、/etc/rc.d /init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash # declare -i sum1=0; declare -i sum2=0; for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab}; do sum1+=$( grep -c '^#' $i ) sum2+=$( grep -c '^[[:space:]]*$' $i ) done echo "#start line number: $sum1" echo "space line number: $sum2"
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash # declare -i sum=0; declare -i i=0; SHELLUSER=$(grep "/bin/bash" /etc/passwd | awk -F ":" '{print $1}'\t) USERUID=$(grep "/bin/bash" /etc/passwd | awk -F ":" '{print $3}'\t) for i in $USERUID;do let sum+=$i done echo "user id sum=$sum" ##################################### echo "users is $SHELLUSER" ##################################### echo "userid is $USERUID"
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
實際我們是想找出帶有,隔開的
[root@ns1 ~]# id adm uid=3(adm) gid=4(adm) 組=4(adm),3(sys) #!/bin/bash # declare -i i=0; for user in `cut -d: -f1 /etc/passwd`; do group=$(id $user | cut -d" " -f3 | awk -F, '{print $2}') if [ -n "$group" ]; then echo $user let i++ fi done echo "user number: $i" 測試結果: [root@ns1 ~]# bash 12.sh bin daemon adm postfix amandabackup user number: 5
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至users目錄,支持acl;
思路:
a.將磁盤分區轉換為8e模式的(LVM卷)
b.磁盤卷轉換為物理卷(pvcreate)
c.創建vg(vgcreate)
d.基于vg創建lv
e.格式化分區,并編寫/etc/fstab,配置好掛載的屬性
分區并打好標簽: [root@ns1 usermagedu]# fdisk /dev/sdd Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x9eb30a74. 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 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 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@ns1 usermagedu]# fdisk /dev/sde Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel Building a new DOS disklabel with disk identifier 0x7cb5d846. 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 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 Command (m for help): t Selected partition 1 Hex code (type L to list codes): 8e Changed system type of partition 1 to 8e (Linux LVM) Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@ns1 usermagedu]# partx /dev/sdd # 1: 63- 20980889 ( 20980827 sectors, 10742 MB) # 2: 0- -1 ( 0 sectors, 0 MB) # 3: 0- -1 ( 0 sectors, 0 MB) # 4: 0- -1 ( 0 sectors, 0 MB) [root@ns1 usermagedu]# partx /dev/sde # 1: 63- 20980889 ( 20980827 sectors, 10742 MB) # 2: 0- -1 ( 0 sectors, 0 MB) # 3: 0- -1 ( 0 sectors, 0 MB) # 4: 0- -1 ( 0 sectors, 0 MB) 轉化為pv卷 [root@ns1 usermagedu]# pvcreate /dev/sdd1 Physical volume "/dev/sdd1" successfully created [root@ns1 usermagedu]# pvcreate /dev/sde1 Physical volume "/dev/sde1" successfully created [root@ns1 usermagedu]# pvs PV VG Fmt Attr PSize PFree /dev/sdd1 lvm2 --- 10.00g 10.00g /dev/sde1 lvm2 --- 10.00g 10.00g 創建VG參數: [root@ns1 usermagedu]# vgcreate myvg -s 8M /dev/sdd1 /dev/sde1 Volume group "myvg" successfully created [root@ns1 usermagedu]# 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 20.00 GiB PE Size 8.00 MiB Total PE 2560 Alloc PE / Size 0 / 0 Free PE / Size 2560 / 20.00 GiB VG UUID 6SRviy-OTzG-SyjB-9d2m-ZNgQ-y1kn-zLAfWy 創建LV: [root@ns1 usermagedu]# lvcreate -L 5G -n myvlv1 myvg Logical volume "myvlv1" created. [root@ns1 usermagedu]# lvdisplay --- Logical volume --- LV Path /dev/myvg/myvlv1 LV Name myvlv1 VG Name myvg LV UUID JGiFW6-8hI9-7BqR-srof-2UdS-i2mG-x9ZKrQ LV Write Access read/write LV Creation host, time ns1.example.com, 2016-08-22 10:40:48 +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 [root@ns1 usermagedu]# mkfs.ext4 /dev/myvg/myvlv1 [root@ns1 ~]# mkdir -p /users [root@ns1 ~]# echo "/dev/myvg/myvlv1 /users ext4 defaults,acl 0 0">>/etc/fstab [root@ns1 ~]# mount -a [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 4.8G 10M 4.6G 1% /users
14、新建用戶magedu;其家目錄為usersmagedu,而后su切換至此用戶,復制多個文件至家目錄;
[root@ns1 ~]# adduser magedu -d /usermagedu [root@ns1 ~]# cd /usermagedu/ [root@ns1 usermagedu]# ls -ld /usermagedu/ drwx------ 4 magedu magedu 4096 Aug 22 10:30 /usermagedu/ [root@ns1 usermagedu]# su - magedu [magedu@ns1 ~]$ cp /etc/sysconfig/network . [magedu@ns1 ~]$ ls network [magedu@ns1 ~]$ pwd /usermagedu
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
[root@ns1 users]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert myvlv1 myvg -wi-ao---- 5.00g [root@ns1 users]# vgs VG #PV #LV #SN Attr VSize VFree myvg 2 1 0 wz--n- 20.00g 15.00g [root@ns1 users]# lvextend -L 9G /dev/myvg/myvlv1 [root@ns1 users]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert myvlv1 myvg -wi-ao---- 9.00g [root@ns1 users]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 4.8G 11M 4.6G 1% /users [root@ns1 users]# resize2fs /dev/myvg/myvlv1 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/myvlv1 is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/myvlv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/myvlv1 is now 2359296 blocks long. [root@ns1 users]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 8.8G 12M 8.3G 1% /users
16、縮減mylv1至7G,確??s減完成后原有數據完全可用;
[root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 8.8G 21M 8.3G 1% /users 卸載掛載的LV(與AIX不同,縮小LV存在風險,必須先卸載分區) [root@ns1 ~]# umount /dev/mapper/myvg-myvlv1 [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata 檢查文件系統是否存在壞塊: [root@ns1 ~]# e2fsck -f /dev/mapper/myvg-myvlv1 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/mapper/myvg-myvlv1: 11/589824 files (0.0% non-contiguous), 74975/2359296 blocks 縮小文件系統到7G: [root@ns1 ~]# resize2fs /dev/mapper/myvg-myvlv1 7G resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/mapper/myvg-myvlv1 to 1835008 (4k) blocks. The filesystem on /dev/mapper/myvg-myvlv1 is now 1835008 blocks long. 縮小LV: [root@ns1 ~]# lvreduce -L 7G /dev/myvg/myvlv1 WARNING: Reducing active logical volume to 7.00 GiB THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvlv1? [y/n]: y//根據提示進行操作 Size of logical volume myvg/myvlv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume myvlv1 successfully resized 重新進行分區掛載: [root@ns1 ~]# mount -a [root@ns1 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda5 14G 8.9G 4.0G 70% / tmpfs 1.9G 72K 1.9G 1% /dev/shm /dev/sda1 190M 42M 139M 23% /boot /dev/sda3 2.0G 3.1M 1.9G 1% /tmp /dev/sdc1 9.8G 13M 9.6G 1% /datamydata /dev/mapper/myvg-myvlv1 6.8G 21M 6.4G 1% /users
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;
原理:LVM 快照利用一種稱為“寫時復制(COW – Copy-On-Write)”的技術來跟蹤和維持其數據的一致性。它的原理比較簡單,就是跟蹤原始卷上塊的改變, 在這些數據被改變之前將其復制到快照自己的預留空間里(顧名思義稱為寫時復制)。 當對快照進行讀取的時候,被修改的數據從快照的預留空間中讀取,未修改的數據則重定向到原始卷上去讀取,因此在快照的文件系統與設備之間多了一層COW設備。
[root@ns1 ~]# lvcreate -L 1G -n mylv1_snapshot -p r -s /dev/myvg/myvlv1 Logical volume "mylv1_snapshot" created. [root@ns1 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1_snapshot myvg sri-a-s--- 1.00g myvlv1 0.00 myvlv1 myvg owi-aos--- 7.00g [root@ns1 ~]# mount /dev/myvg/mylv1_snapshot /lvmsnapshot/ mount: block device /dev/mapper/myvg-mylv1_snapshot is write-protected, mounting read-only [root@ns1 ~]# ls -lrt /lvmsnapshot/ total 16 drwx------ 2 root root 16384 Aug 22 10:57 lost+found [root@ns1 users]# ls -lrt total 16 drwx------ 2 root root 16384 Aug 22 10:57 lost+found 關于LVM快照的應用,后面我會寫一篇文章補充
原創文章,作者:Net21-冰凍vs西瓜,如若轉載,請注明出處:http://www.www58058.com/38880
有方法,有思路,有過程,有結果,很棒