N21_第7周_磁盤及文件系統管理
作業題目:
1、創建一個10G分區,并格式為ext4文件系統;
(1) 要求其block大小為2048, 預留空間百分比為2, 卷標為MYDATA, 默認掛載屬性包含acl;
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
[root@localhost ~]# fdisk /dev/sda Command (m for help): p 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: 0x00048a22 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 548 4194304 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 548 2611 16571392 83 Linux Command (m for help): d Partition number (1-4): 3 Command (m for help): p 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: 0x00048a22 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 548 4194304 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. Command (m for help): n Command action e extended p primary partition (1-4) p Partition number (1-4): 3 First cylinder (548-2610, default 548): Using default value 548 Last cylinder, +cylinders or +size{K,M,G} (548-2610, default 2610): +10G Command (m for help): wq [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: 0x00048a22 Device Boot Start End Blocks Id System /dev/sda1 * 1 26 204800 83 Linux Partition 1 does not end on cylinder boundary. /dev/sda2 26 548 4194304 82 Linux swap / Solaris Partition 2 does not end on cylinder boundary. /dev/sda3 548 1853 10484094+ 83 Linux
# partx -a /dev/sda # mke2fs -t ext4 -b 2048 -m 2 -L "MYDATA" /dev/sda3 # mkdir -p /data/mydata # mount -t ext4 -o noexec,noatime,acl /dev/sda3 /data/mydata/
2、創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
[root@localhost data]# fdisk /dev/sda 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 (1854-2610, default 1854): Using default value 1854 Last cylinder, +cylinders or +size{K,M,G} (1854-2610, default 2610): +1000M Command (m for help): t Partition number (1-4): 1 Hex code (type L to list codes): 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 Hex code (type L to list codes): 82 Changed system type of partition 1 to 82 (Linux swap / Solaris) Command (m for help): wq The partition table has been altered! [root@localhost data]# partx -a /dev/sda [root@localhost data]# mkswap /dev/sdb4 [root@localhost data]# swapon /dev/sda5
3、寫一個腳本
(1)、獲取并列出當前系統上的所有磁盤設備;
(2)、顯示每個磁盤設備上每個分區相關的空間使用信息;
[root@localhost ~]# vi getFdisk.sh #!/bin/bash Disk_name=$(fdisk -l | awk -F'[ :]+' '/^Disk \/dev\/sd?/ {print $2}') for i in ${Disk_name}; do fdisk -l $i done
4、總結RAID的各個級別及其組合方式和性能的不同;
RAID:磁盤陣列(Redundant Arrays of Inexpensive Disks,RAID),獨立冗余磁盤陣列。
5、創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
[root@localhost ~]# mdadm -C /dev/md0 -a yes -l 1 -c 128 -n 2 -x 1 /dev/sdb{6,7,8}
6、創建一個大小為4G的RAID5設備,chunk大小為256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
# mdadm -C /dev/md0 -a yes -n 3 -c 256 -l 5 /dev/sdb{1,2,3} # mke2fs -t ext4 /dev/md0
7、寫一個腳本
(1) 接受一個以上文件路徑作為參數;
(2) 顯示每個文件擁有的行數;
(3) 總結說明本次共為幾個文件統計了其行數;
#!/bin/bash if [[ $# -eq 0 ]]; then echo -e "\nusage: sh $0 <FILE...>\n" exit 1 else for i in "$@"; do wc -l ${i} done echo "Total number of files: $#" fi
[root@localhost ~]# sh getFileMessage.sh /home/test1.txt /home/test2.txt 3 /home/test1.txt 0 /home/test2.txt Total number of files: 2
8、寫一個腳本
(1) 傳遞兩個以上字符串當作用戶名;
(2) 創建這些用戶;且密碼同用戶名;
(3) 總結說明共創建了幾個用戶;
#!/bin/bash Num_user=0 if [[ $# -le 1 ]]; then echo -e "\nusage: sh $0 <arg1> <arg2>..." echo -e "Pls enter at least two strings.\n" exit 1 else for i in "$@"; do useradd ${i} &>/dev/null if [[ $? -eq 0 ]]; then let Num_user+=1 echo "${i}" | passwd --stdin ${i} &>/dev/null fi done echo "The number of users created: ${Num_user}" echo -e "User lists:" echo "$*" | sed -r 's/[[:space:]]+/\n/g' fi
[root@localhost ~]# sh createUser.sh abc bcd cde The number of users created: 3 User lists: abc bcd cde
9、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
[root@localhost ~]# vi create20users.sh #!/bin/bash sum=0 for i in visitor{1..20}; do useradd $i let sum+=$(id -u $i) done echo "The sum of UID is: $sum"
[root@localhost ~]# sh create20users.sh The sum of UID is: 10250
10、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
#!/bin/bash if [[ $# -ne 1 ]]; then echo 'Pls enter only one argument.' else echo "The number of #: $(grep '^#' $1 | wc -l)" echo "The number of blank lines: $(grep '^$' $1 | wc -l)" fi
11、寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名、UID以及此類所有用戶的UID之和;
#!/bin/bash Sum_uid=0 echo "UserName:UID lists" for i in $(grep '\<bash\>$' /etc/passwd | cut -d : -f1); do echo "${i}:$(id -u ${i})" let Sum_uid+=$(id -u ${i}) done echo "The sum of UID: $Sum_uid"
12、寫一個腳本,顯示當前系統上所有,擁有附加組的用戶的用戶名;并說明共有多少個此類用戶;
#!/bin/bash Sum_user=0 echo "User list:" while read line; do User_name=$(echo ${line} | cut -d: -f1) Num=$(id -G ${User_name} | wc -w) if [[ ${Num} -eq 2 ]]; then echo "${User_name}" let Sum_user+=1 fi done < /etc/passwd echo "The number of user: ${Sum_user}"
13、創建一個由至少兩個物理卷組成的大小為20G的卷組;要求,PE大小為8M;而在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
# pvcreate /dev/sdb /dev/sdc # vgcreate -s 8M myvg /dev/sd{b,c} # lvcreate -L 5G -n mylv1 /dev/myvg # mkfs.ext4 /dev/myvg/mylv1 # echo "/dev/myvg/mylv1 /users ext4 defaults,acl,nodiratime 0 0" >> /etc/fstab
14、新建用戶magedu;其家目錄為/users/magedu,而后su切換至此用戶,復制多個文件至家目錄;
# useradd -d /users/magedu magedu # su - magedu $ pwd /users/magedu $ cp /etc/fstab /etc/issue ./
15、擴展mylv1至9G,確保擴展完成后原有數據完全可用;
# cp /etc/fstab ./ # lvextend -L +4G -n /dev/myvg/mylv1 # resize2fs /dev/myvg/mylv1
16、縮減mylv1至7G,確??s減完成后原有數據完全可用;
# umount /users # e2fsck -f /dev/myvg/mylv1 # resize2fs /dev/myvg/mylv1 7G # lvreduce -L 7G -n /dev/myvg/mylv1
17、對mylv1創建快照,并通過備份數據;要求保留原有的屬主屬組等信息;
# lvcreate -L 3G -p r -s -n mylv1_bak /dev/myvg/mylv1
其它,fdisk分區命令
Command action a toggle a bootable flag b edit bsd disklabel c toggle the dos compatibility flag d delete a partition l list known partition types m print this menu n add a new partition o create a new empty DOS partition table p print the partition table q quit without saving changes s create a new empty Sun disklabel t change a partition's system id u change display/entry units v verify the partition table w write table to disk and exit x extra functionality (experts only)
原創文章,作者:365,如若轉載,請注明出處:http://www.www58058.com/48827
寫的很好,如果圖是自己畫的話,就完美了