馬哥教育網絡班21期+第7周課程練習
-
創建一個10G分區,并格式為ext4文件系統;
a.要求block大小為2048,預留空間百分比為2,卷標為MYDATA,默認掛載屬性包含acl;
b.掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不能更新文件的訪問時間戳;
# 創建10G的分區 [root@CentOS-7 ~]# fdisk /dev/sda 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 (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): 3 First sector (25591808-251658239, default 25591808): Using default value 25591808 Last sector, +sectors or +size{K,M,G} (25591808-251658239, default 251658239): +10G Partition 3 of type Linux and of size 10 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 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) Syncing disks. [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-2 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 10485760 sda3 11 0 1048575 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 格式化為ext4,block大小為2048,預留空間百分比為2,卷標為MYDATA; [root@CentOS-7 ~]# mke2fs -t ext4 -b 2048 -m 2 -L "MYDATA" /dev/sda3 # 創建掛載目錄 [root@CentOS-7 ~]# mkdir -p /data/mydata # 默認掛載屬性包含acl,掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不能更新文件的訪問時間戳; [root@CentOS-7 ~]# mount -t ext4 -o noexec,noatime,acl /dev/sda3 /data/mydata/
2. 創建一個大小為1G的swap分區,并創建好文件系統,并啟用之;
# 首先新建1G的swap類型分區 #先創建邏輯分區,使用所有空間,然后在邏輯分區上創建1G swap分區; [root@CentOS-7 ~]# fdisk /dev/sda 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 (3 primary, 0 extended, 1 free) e extended Select (default e): e Selected partition 4 First sector (46563328-251658239, default 46563328): Using default value 46563328 Last sector, +sectors or +size{K,M,G} (46563328-251658239, default 251658239): Using default value 251658239 Partition 4 of type Extended and of size 97.8 GiB is set Command (m for help): n All primary partitions are in use Adding logical partition 5 First sector (46565376-251658239, default 46565376): Using default value 46565376 Last sector, +sectors or +size{K,M,G} (46565376-251658239, default 251658239): +1G Partition 5 of type Linux and of size 1 GiB is set Command (m for help): l 0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris 1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT- 2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT- 3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT- 4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx 5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data 6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / . 7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility 8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt 9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/ 10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b 11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor 12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor 14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary 16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS 17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE 18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto 1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep 1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT 1e Hidden W95 FAT1 80 Old Minix Command (m for help): t Partition number (1-5, default 5): 5 Hex code (type L to list all codes): 82 Changed type of partition 'Linux' to 'Linux swap / Solaris' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: Device or resource busy. 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) Syncing disks. [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-5 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 10485760 sda3 8 4 1 sda4 8 5 1048576 sda5 11 0 1048575 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 創建swap文件系統并啟用 [root@CentOS-7 ~]# free -m total used free shared buff/cache available Mem: 977 552 96 16 328 197 Swap: 2047 57 1990 [root@CentOS-7 ~]# [root@CentOS-7 ~]# mkswap /dev/sda5 Setting up swapspace version 1, size = 1048572 KiB no label, UUID=cd5c8885-e09e-478d-8915-275ff2ba2d0f [root@CentOS-7 ~]# swapon /dev/sda5 [root@CentOS-7 ~]# free -m total used free shared buff/cache available Mem: 977 553 95 16 328 196 Swap: 3071 57 3014
3. 寫一個腳本
a. 獲取并列出當前系統上所有磁盤設備;
b.顯示每個磁盤設備上每個分區相關的空間使用信息;
#!/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各個級別及其組合方式和性能的不同;
5. 創建一個大小為10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k;
# 首先在虛擬機添加3塊10G的磁盤 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-d] | grep '^Disk /dev' Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Disk /dev/sdd: 10.7 GB, 10737418240 bytes, 20971520 sectors # 創建3個新分區 [root@CentOS-7 ~]# 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, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@CentOS-7 ~]# fdisk /dev/sdc [root@CentOS-7 ~]# fdisk /dev/sdd # 10G的RAID1,要求有一個空閑盤,而且CHUNK大小為128k; [root@CentOS-7 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdc partx: /dev/sdc: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdd partx: /dev/sdd: error adding partition 1 [root@CentOS-7 ~]# mdadm -C /dev/md0 -n 2 -x 1 -l 1 -a yes -c 128 /dev/sd{b1,c1,d1} mdadm: Note: this array has metadata at the start and may not be suitable as a boot device. If you plan to store '/boot' on this device please ensure that your boot-loader understands md/v1.x metadata, or use --metadata=0.90 Continue creating array? y mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md0 started. [root@CentOS-7 ~]# mdadm -D /dev/md0 /dev/md0: Version : 1.2 Creation Time : Sun Aug 21 17:24:21 2016 Raid Level : raid1 Array Size : 10476544 (9.99 GiB 10.73 GB) Used Dev Size : 10476544 (9.99 GiB 10.73 GB) Raid Devices : 2 Total Devices : 3 Persistence : Superblock is persistent Update Time : Sun Aug 21 17:25:14 2016 State : clean Active Devices : 2 Working Devices : 3 Failed Devices : 0 Spare Devices : 1 Name : CentOS-7:0 (local to host CentOS-7) UUID : 3520aab9:bf556c43:c26a1a76:c634588a Events : 17 Number Major Minor RaidDevice State 0 8 17 0 active sync /dev/sdb1 1 8 33 1 active sync /dev/sdc1 2 8 49 - spare /dev/sdd1
6. 創建一個大小為4G的RAID5設備,chunk大小為256k,格式化為ext4文件系統,要求開機可自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能;
# 首先新建4個2G的分區sda[5-8],將每個分區看作是一個獨立的磁盤,一個作為空閑盤 # RAID種每個磁盤大小可以不相同(但為了不浪費空間,建議磁盤大小相同) [root@CentOS-7 ~]# fdisk -l /dev/sda Disk /dev/sda: 128.8 GB, 128849018880 bytes, 251658240 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x0004aa3c Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 25591807 12590080 8e Linux LVM /dev/sda3 25591808 251658239 113033216 5 Extended /dev/sda5 25593856 29788159 2097152 83 Linux /dev/sda6 29790208 33984511 2097152 83 Linux /dev/sda7 33986560 38180863 2097152 83 Linux /dev/sda8 38182912 42377215 2097152 83 Linux [root@CentOS-7 ~]# mdadm -C /dev/md1 -n 3 -l 5 -a yes -c 256 -x 1 /dev/sda{5,6,7,8} mdadm: cannot open /dev/sda5: No such file or directory [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-2 [root@CentOS-7 ~]# partx -a /dev/sda partx: /dev/sda: error adding partitions 1-3 partx: /dev/sda: error adding partitions 5-8 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 8 3 1 sda3 8 5 2097152 sda5 8 6 2097152 sda6 8 7 2097152 sda7 8 8 2097152 sda8 8 16 10485760 sdb 8 17 10484736 sdb1 8 32 10485760 sdc 8 33 10484736 sdc1 8 48 10485760 sdd 8 49 10484736 sdd1 11 0 4228096 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 9 0 10476544 md0 [root@CentOS-7 ~]# mdadm -C /dev/md1 -n 3 -l 5 -a yes -c 256 -x 1 /dev/sda{5,6,7,8} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@CentOS-7 ~]# mdadm -D /dev/md1 /dev/md1: Version : 1.2 Creation Time : Sun Aug 21 17:56:32 2016 Raid Level : raid5 Array Size : 4190208 (4.00 GiB 4.29 GB) Used Dev Size : 2095104 (2046.34 MiB 2145.39 MB) Raid Devices : 3 Total Devices : 4 Persistence : Superblock is persistent Update Time : Sun Aug 21 17:56:43 2016 State : clean, degraded, recovering Active Devices : 2 Working Devices : 4 Failed Devices : 0 Spare Devices : 2 Layout : left-symmetric Chunk Size : 256K Rebuild Status : 18% complete Name : CentOS-7:1 (local to host CentOS-7) UUID : 0f18450d:cc3b25ac:d7034547:b1f0821c Events : 3 Number Major Minor RaidDevice State 0 8 5 0 active sync /dev/sda5 1 8 6 1 active sync /dev/sda6 4 8 7 2 spare rebuilding /dev/sda7 3 8 8 - spare /dev/sda8 # 格式化為ext4文件系統,要求開機可自動掛載至/backup目錄,而且不更新訪問時間戳,且支持acl功能; [root@CentOS-7 ~]# mke2fs -t ext4 /dev/md1 [root@CentOS-7 ~]# mkdir -p /backup [root@CentOS-7 ~]# blkid /dev/md1 /dev/md1: UUID="9de8fb0c-2211-4669-8a3b-a342c182e252" TYPE="ext4" [root@CentOS-7 ~]# echo "UUID="9de8fb0c-2211-4669-8a3b-a342c182e252" /backup ext4 defaults,noatime,acl 0 0" >> /etc/fstab [root@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# df -h | grep backup /dev/md1 3.9G 16M 3.7G 1% /backup
7. 寫一個腳本
a. 接收一個以上文件路徑作為參數;
b. 顯示每個文件擁有的行數;
c. 總結說明本次共為幾個文件統計了行數;
[root@rhel-6-2 tmp]# cat 1.sh #!/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@rhel-6-2 tmp]# sh 1.sh usage: sh 1.sh <FILE...> [root@rhel-6-2 tmp]# sh 1.sh /etc/fstab /etc/hosts 15 /etc/fstab 3 /etc/hosts Total number of files: 2
8. 寫一個腳本
a. 傳遞兩個以上字符串當作用戶名;
b. 創建這些用戶,且密碼同用戶名;
c. 總結說明共創建了幾個用戶;
[root@rhel-6-2 tmp]# cat 2.sh #!/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@rhel-6-2 tmp]# sh 2.sh usage: sh 2.sh <arg1> <arg2>... Pls enter at least two strings. [root@rhel-6-2 tmp]# sh 2.sh user1 user2 The number of users created: 2 User lists: user1 user2
9. 寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和。
[root@rhel-6-2 tmp]# cat 3.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: $Sum" [root@rhel-6-2 tmp]# sh 3.sh The sum of UID: 10230
10. 寫一個腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions、/etc/fstab文件中以#開頭的行數之和以及總的空白行數。
[root@rhel-6-2 init.d]# cat 3.sh #!/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 [root@rhel-6-2 init.d]# sh 3.sh Pls enter only one argument. [root@rhel-6-2 init.d]# sh 3.sh /etc/rc.d/rc.sysinit The number of #: 44 The number of blank lines: 96 [root@rhel-6-2 init.d]# sh 3.sh /etc/rc.d/init.d/functions The number of #: 40 The number of blank lines: 72 [root@rhel-6-2 init.d]# sh 3.sh /etc/fstab The number of #: 7 The number of blank lines: 1
11. 寫一個腳本,顯示當前系統上所有默認shell為bash的用戶的用戶名,UID以及此類所有用戶的UID之和。
[root@rhel-6-2 init.d]# cat 4.sh #!/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" [root@rhel-6-2 init.d]# sh 4.sh UserName:UID lists root:0 user1:500 user2:501 visitor1:502 ... ... visitor20:521 The sum of UID: 11231
12. 寫一個腳本,顯示系統上所有擁有附加組的用戶的用戶名,并說明共有多少個此類用戶。
[root@rhel-6-2 tmp]# cat 5.sh #!/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}" [root@rhel-6-2 tmp]# sh 5.sh User list: adm postfix user1 user2 The number of user: 4
13. 創建一個由至少兩個物理卷組成的大小為20G的卷組;要求PE大小為8M;在卷組中創建一個大小為5G的邏輯卷mylv1,格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl;
# 查看現有磁盤,使用sdb,sdc創建20G卷組并完成實驗 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-c] Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes # 首先創建分區并修改分區類型未lvm [root@CentOS-7 ~]# 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0x8a4a0c3b. Command (m for help): n Partition type: p primary (0 primary, 0 extended, 4 free) e extended Select (default p): p Partition number (1-4, default 1): First sector (2048-20971519, default 2048): Using default value 2048 Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): Using default value 20971519 Partition 1 of type Linux and of size 10 GiB is set Command (m for help): t Selected partition 1 Hex code (type L to list all codes): 8e Changed type of partition 'Linux' to 'Linux LVM' Command (m for help): w The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. #同樣操作sdc [root@CentOS-7 ~]# fdisk /dev/sdc [root@CentOS-7 ~]# partx -a /dev/sdb partx: /dev/sdb: error adding partition 1 [root@CentOS-7 ~]# partx -a /dev/sdc partx: /dev/sdc: error adding partition 1 [root@CentOS-7 ~]# cat /proc/partitions major minor #blocks name 8 16 10485760 sdb 8 17 10484736 sdb1 8 32 10485760 sdc 8 33 10484736 sdc1 8 0 125829120 sda 8 1 204800 sda1 8 2 12590080 sda2 11 0 4228096 sr0 253 0 10485760 dm-0 253 1 2097152 dm-1 # 查看分區狀態 [root@CentOS-7 ~]# fdisk -l /dev/sd[b-c] Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0x8a4a0c3b Device Boot Start End Blocks Id System /dev/sdb1 2048 20971519 10484736 8e Linux LVM Disk /dev/sdc: 10.7 GB, 10737418240 bytes, 20971520 sectors Units = sectors of 1 * 512 = 512 bytes Sector size (logical/physical): 512 bytes / 512 bytes I/O size (minimum/optimal): 512 bytes / 512 bytes Disk label type: dos Disk identifier: 0xeef74501 Device Boot Start End Blocks Id System /dev/sdc1 2048 20971519 10484736 8e Linux LVM # 創建物理卷 [root@CentOS-7 ~]# pvcreate /dev/sdb1 Physical volume "/dev/sdb1" successfully created [root@CentOS-7 ~]# pvcreate /dev/sdc1 Physical volume "/dev/sdc1" successfully created # 查看物理卷狀態 [root@CentOS-7 ~]# pvs [root@CentOS-7 ~]# pvdisplay # 創建卷組,要求PE大小為8M [root@CentOS-7 ~]# vgcreate myvg -s 8M /dev/sdb1 /dev/sdc1 Volume group "myvg" successfully created # 查看卷組狀態 [root@CentOS-7 ~]# vgs myvg VG #PV #LV #SN Attr VSize VFree myvg 2 0 0 wz--n- 19.98g 19.98g [root@CentOS-7 ~]# vgdisplay myvg --- 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 19.98 GiB PE Size 8.00 MiB Total PE 2558 Alloc PE / Size 0 / 0 Free PE / Size 2558 / 19.98 GiB VG UUID 8jRp4h-2YgZ-34EB-d7Ld-LwW7-cd1f-nHxmjl # 在卷組myvg中創建一個大小為5G的邏輯卷mylv1; [root@CentOS-7 ~]# lvcreate -L 5G -n mylv1 myvg Logical volume "mylv1" created. [root@CentOS-7 ~]# ls /dev/mapper/ centos_centos--7-root centos_centos--7-swap control myvg-mylv1 # 查看lv狀態 [root@CentOS-7 ~]# lvs LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert root centos_centos-7 -wi-ao---- 10.00g swap centos_centos-7 -wi-ao---- 2.00g mylv1 myvg -wi-a----- 5.00g [root@CentOS-7 ~]# lvs /dev/myvg/mylv1 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert mylv1 myvg -wi-a----- 5.00g # 查看lv詳細狀態 lvdisplay [path/of/lv] [root@CentOS-7 ~]# lvdisplay /dev/myvg/mylv1 --- Logical volume --- LV Path /dev/myvg/mylv1 LV Name mylv1 VG Name myvg LV UUID XA0VWt-DbCi-VfXX-30Eu-06IQ-GPG3-sTpkwE LV Write Access read/write LV Creation host, time CentOS-7, 2016-08-21 15:00:58 +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 8192 Block device 253:2 # 將新建lv格式化為ext4文件系統,開機自動掛載至/users目錄,支持acl; [root@CentOS-7 ~]# mke2fs -t ext4 -L mylv1 /dev/myvg/mylv1 # 創建掛載點并配置開機自動掛載; # 首先查看分區信息 [root@CentOS-7 ~]# blkid /dev/myvg/mylv1 /dev/myvg/mylv1: LABEL="mylv1" UUID="e9508003-f147-408b-a93c-11c2e6820380" TYPE="ext4" [root@CentOS-7 ~]# mkdir -p /users # /etc/fstab中UUID=XXX也可替換為blkid輸出中的/dev/myvg/mylv1或LABEL="mylv1" [root@CentOS-7 ~]# echo "UUID="e9508003-f147-408b-a93c-11c2e6820380" /users ext4 defaults,acl 0 0" >> /etc/fstab [root@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# df -h Filesystem Size Used Avail Use% Mounted on /dev/mapper/centos_centos--7-root 10G 4.7G 5.4G 47% / devtmpfs 474M 0 474M 0% /dev tmpfs 489M 84K 489M 1% /dev/shm tmpfs 489M 7.1M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 127M 71M 65% /boot tmpfs 98M 20K 98M 1% /run/user/42 tmpfs 98M 0 98M 0% /run/user/0 /dev/mapper/myvg-mylv1 4.8G 20M 4.6G 1% /users
14. 新建用戶magedu;其家目錄為/users/magedu,而后切換至此用戶,復制多個文件至家目錄;
[magedu@CentOS-7 ~]$ cp /etc/hosts /etc/fstab ~ [magedu@CentOS-7 ~]$ ll /users/magedu/ 總用量 8 -rw-r--r--. 1 magedu magedu 528 8月 21 15:31 fstab -rw-r--r--. 1 magedu magedu 180 8月 21 15:31 hosts
15. 擴展mylv1至9G,確保擴展完成后所有原數據完全可用;
[root@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 4.8G 21M 4.6G 1% /users [root@CentOS-7 ~]# lvextend -L 9G /dev/myvg/mylv1 Size of logical volume myvg/mylv1 changed from 5.00 GiB (640 extents) to 9.00 GiB (1152 extents). Logical volume mylv1 successfully resized. [root@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 4.8G 21M 4.6G 1% /users # 擴展文件系統到指定大小 [root@CentOS-7 ~]# resize2fs /dev/myvg/mylv1 resize2fs 1.42.9 (28-Dec-2013) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old_desc_blocks = 1, new_desc_blocks = 2 The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@CentOS-7 ~]# df -h | grep /users /dev/mapper/myvg-mylv1 8.8G 23M 8.3G 1% /users # 檢查文件是否丟失 [root@CentOS-7 ~]# ll /users/magedu/ total 8 -rw-r--r--. 1 magedu magedu 528 Aug 21 15:32 fstab -rw-r--r--. 1 magedu magedu 180 Aug 21 15:32 hosts [root@CentOS-7 ~]# cat /users/magedu/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7
16. 縮減mylv1至7G,確??s減完成后原有數據完全可用;
# 卸載文件系統 [root@CentOS-7 ~]# umount /dev/myvg/mylv1 # 強制進行文件系統檢測、修復 [root@CentOS-7 ~]# e2fsck -f /dev/myvg/mylv1 # 縮減文件系統到指定大小 [root@CentOS-7 ~]# resize2fs /dev/myvg/mylv1 7G # 縮減邏輯卷 [root@CentOS-7 ~]# lvreduce -L 7G /dev/myvg/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 myvg/mylv1 changed from 9.00 GiB (1152 extents) to 7.00 GiB (896 extents). Logical volume mylv1 successfully resized. # 掛載文件系統 [root@CentOS-7 ~]# mount -a [root@CentOS-7 ~]# !df df -h | grep /users /dev/mapper/myvg-mylv1 6.8G 23M 6.5G 1% /users # 查看該文件系統的文件是否損壞 [root@CentOS-7 ~]# ll /users/magedu/ total 8 -rw-r--r--. 1 magedu magedu 528 Aug 21 15:32 fstab -rw-r--r--. 1 magedu magedu 180 Aug 21 15:32 hosts [root@CentOS-7 ~]# cat /etc/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7
17. 對mylv1創建快照,并通過備份數據;要求保留原有的屬主和屬組等信息;
[root@CentOS-7 ~]# lvcreate -s -L 10M -n mylv1-snap -p r /dev/myvg/mylv1 Rounding up size to full physical extent 16.00 MiB Logical volume "mylv1-snap" created. [root@CentOS-7 ~]# mount /dev/myvg/mylv1-snap /mnt/ mount: /dev/mapper/myvg-mylv1--snap is write-protected, mounting read-only [root@CentOS-7 ~]# tree /mnt/ /mnt/ |-- lost+found `-- magedu |-- fstab `-- hosts 2 directories, 2 files [root@CentOS-7 ~]# mkdir -p /tmp/mylv1/ [root@CentOS-7 ~]# cp -a /mnt/* /tmp/mylv1/ [root@CentOS-7 ~]# tree /tmp/mylv1/ /tmp/mylv1/ |-- lost+found `-- magedu |-- fstab `-- hosts 2 directories, 2 files [root@CentOS-7 ~]# cat /tmp/mylv1/magedu/hosts 127.0.0.1 localhost localhost.localdomain localhost4 localhost4.localdomain4 ::1 localhost localhost.localdomain localhost6 localhost6.localdomain6 192.10.10.5 CentOS-7 # 備份完成無誤后,卸載并刪除快照 [root@CentOS-7 ~]# umount /mnt/ [root@CentOS-7 ~]# lvremove /dev/myvg/mylv1-snap Do you really want to remove active logical volume mylv1-snap? [y/n]: y Logical volume "mylv1-snap" successfully removed
原創文章,作者:N21_未來人,如若轉載,請注明出處:http://www.www58058.com/38525
寫的很好,排版也很棒,加油