fdisk:對於一塊硬盤來說,最多隻能管理15個分區node fdisk -l [-u] [device...] |
建立文件系統: mkfs命令: (1) # mkfs.FS_TYPE(分區類型) /dev/DEVICE ext4 xfs btrfs vfat (2) # mkfs -t FS_TYPE /dev/DEVICE ext4 xfs btrfs vfat -L 'LABEL':設定卷標 blkid /dev/DEVICE 顯示分區屬性 mke2fs:ext系列文件系統專用管理工具 -t {ext2|ext3|ext4} -b {1024|2048|4096}(設置塊大小) -L 'LABEL' -j:至關於 -t ext3 mkfs.ext3=mkfs -t ext3 = mke2fs -j = mke2fs -t ext3 -i #:爲數據空間中每多少個字節建立一個inode;此大小不該該小於block的大小; -N #:爲數據空間建立多少個inode; -m #:爲管理人員預留的空間佔據的百分比; -O FEATURE[,...]:啓用指定特性 -O ^FEATURE:關閉指定特性 mkswap:建立交換分區 mkswap [OPTIONS] device -L 'LABEL' 前提:調整其分區的ID爲82 其它經常使用工具: blkid:塊設備屬性值信息查看 blkid [OPTION]... [DEVICE] -U UUID:根據指定的UUID來查找對應的設備 -L LABEL:根據指定的LABEL來查找對應的設備 e2label:管理ext系列文件系統的LABEL # e2label DEVICE [LABEL] tune2fs:從新設定ext系列文件系統可調用參數的值 -l:查看指定文件系統超級塊信息:super block -L 'LABEL':修改卷標 -m #:修改預留給管理員的空間百分比 -j:將ext2升級爲ext3 -O:文件系統屬性啓用或禁用 -o:調整文件系統的默認掛載選項 -U UUID: 修改UUID號: dumpe2fs: 既能查看超級塊信息,又能查看整個文件的組織結構信息;又稱查看佈局信息 -h:查看超級塊信息 文件系統檢測: fsck:File System CheCk fsck.FS_TYPE fsck -t FS_TYPE -a:自動修復錯誤 -r:交互式修復錯誤 Note:FS_TYPE必定要與分區上已有的文件系統類型相同 e2fsck: ext系列文件專用的檢測的工具 -y:自動回答爲yes; -f:強制修復; |
掛載方法:mount DEVICE MOUNT_POINT du [OPTION]... DIR -h: human-readable 以人類可讀的 -s:summary 查看文件的整體大小之和 |
mdadm:模式化的工具 命令的語法格式:mdadm [mode] <raiddevice> [options] <component-devices> 支持的RAID級別:LINEAR,RAID0,RAID1,RAID4,RAID5,RAID6,RAID10; 模式: 建立:-C 裝配:-A 監控:-F 管理:-f,-r,-a <raiddevice>: /dev/md# <component-device>:任意塊設備 -C:建立模式 -n #:使用#個塊設備來建立此RAID;; -l #:指明要建立的RAID的級別; -a {yes|no}:自動建立目標RAID設備的設備文件; -c CHUNK_SIZE:指明塊大小; -x #:指明空閒盤的個數: -D:顯示raid的詳細信息: mdadm -D /dev/md# 管理模式: -f:標記指定磁盤爲損壞; -a:添加磁盤 -r:移除磁盤 觀察md的狀態: cat /por/mdstat 中止md設備 mdadm -S /dev/md# watch命令: -n #:刷新間隔,單位是秒: wach -n# 'COMMAND' |
LVM: Logical Volume Manager,Version 2 邏輯卷管理器 dm: device mapper(設備映射),將一個或多個底層塊設備組織成一個邏輯設備的模塊 /dev/dm-# dev/mapper/VG_NAME(NAME=LV_NAME) /dev/mapper/vol0-root /dev/VG_NAME/LV_NAME /dev/vol0/root pv管理工具 pvs:簡要pv信息顯示 pvdisplay:顯示pv的詳細信息 pvcreate /dev/DEVICE:建立pv pvremove 刪除pv vg管理工具: vgs:簡要vg信息顯示 vgdisplay:顯示vg的詳細信息 vgcreate [-s](指定PE大小)#[bBsSkKmMgGtTpPeE](單位) VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 添加VG vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 移除VG 先作pvmove vgremove 刪除卷組 lv管理工具: lvs:簡要lv信息顯示 lvdisplay:顯示lv的詳細信息 lvcreate -L #[mMgGtT] -n NAME VolumeGroup lvremove /dev/VG_NAME 刪除邏輯卷 擴展邏輯卷: lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME resize2fs /dev/VG_NAME/LV_NAME 修改邏輯邊界 縮減邏輯卷 # umount /dev/VG_NAME/LV_NAME # e2fsck -f /dev/VG_NAME/LV_NAME 強制檢測修復文件系統 # resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]修改邏輯邊界 # lverduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME 修改物理邊界 # mount /dev/VG_NAME/LV_NAME /NAME 快照:snapshot lvcreate -L #[mMgGtT] -p r(只讀) -s(指明這是快照) -n(指明快照卷名字) snapshot_lv_name original_lv_name(源卷名字) |
一、建立一個10G分區,並格式爲ext4文件系統;
shell
[root@localhost ~]# fdisk /dev/sdc Command (m for help): n #新建分區 Command action e extended p primary partition (1-4) p #主分區 Partition number (1-4): 1 #主分區號 First cylinder (1-10443, default 1): Using default value 1 Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G #建立空間大小 Command (m for help): w #保存並推出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. |
(1) 要求其block大小爲2048, 預留空間百分比爲2, 卷標爲MYDATA, 默認掛載屬性包含acl;
bash
(2) 掛載至/data/mydata目錄,要求掛載時禁止程序自動運行,且不更新文件的訪問時間戳;
app
[root@localhost ~]# mke2fs -t ext4 -b 2048 -m 2 -L 'MYDATA' /dev/sdc1異步 [root@localhost ~]# mkdir /data/mydataasync [root@localhost ~]# mount -o noauto,noatime,acl /dev/sdc1 /data/mydata/ide [root@localhost ~]# mount |
二、建立一個大小爲1G的swap分區,並建立好文件系統,並啓用之;
[root@bogon ~]# fdisk /dev/sdc 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 extendedp primary partition (1-4) p#主分區 Partition number (1-4): 2 #主分區號 First cylinder (1307-10443, default 1307):Using default value 1307 Last cylinder, +cylinders or +size{K,M,G} (1307-10443, default 10443): +1G #建立空間大小 Command (m for help): t #調整磁盤類型 Partition number (1-4): 2 #主分區號 Hex code (type L to list codes): 82 #swap 類型 Changed system type of partition 2 to 82 (Linux swap / Solaris) Command (m for help): p #顯示已有分區 255 heads, 63 sectors/track, 10443 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: 0x0dd03239 Device Boot Start End Blocks Id System /dev/sdc1 1 1306 10490413+ 83 Linux /dev/sdc2 1307 1438 1060290 82 Linux swap / Solaris Command (m for help): w #寫入磁盤並退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks. [root@bogon ~]# mkswap /dev/sdc2 #建立交換分區 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=880e15e9-f1f5-4682-861c-b217266ede96You have new mail in /var/spool/mail/root [root@bogon ~]# swapon /dev/sdc2 [root@bogon ~]# free -m total used free shared buffers cached Mem: 474 325 149 0 48 152 -/+ buffers/cache: 124 350 Swap: 1995 0 1995 [root@bogon ~]# swapoff /dev/sdc2 [root@bogon ~]# free -m total used free shared buffers cached Mem: 474 324 150 0 48 152 -/+ buffers/cache: 123 351 Swap: 959 0 959 |
三、寫一個腳本
(1)、獲取並列出當前系統上的全部磁盤設備;
(2)、顯示每一個磁盤設備上每一個分區相關的空間使用信息;
[root@bogon bin]# diskfile.sh All of the disk: Disk /dev/sda Disk /dev/sdb Disk /dev/sdc Each partition on disk devices related information use of space: Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 5.7G 12G 33% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 477M 33M 419M 8% /boot /dev/md127 9.7G 23M 9.2G 1% /mydata /dev/sdc1 9.8G 13M 9.6G 1% /data/mydata [root@bogon bin]# cat diskfile.sh #!/bin/bash disks=$(fdisk -l | grep -o '^Disk /dev/[sh]d[a-z]') echo -e "All of the disk:\n$disks" disks_partition=$(fdisk -l | grep -o '/dev/[sh]d[a-z][1-9]' | df -h) echo -e "Each partition on disk devices related information use of space:\n$disks_partition" |
四、總結RAID的各個級別及其組合方式和性能的不一樣;
RAID-0:讀寫性能提高;可用空間:N*min(S1,S2,....);無容錯能力;最少2塊磁盤。 RAID-1:讀性能提高、寫性能略有降低;可用空間1*min(s1,s2...);有冗餘能力;最少2塊磁盤。 RAID-4:有獨立的硬盤作校驗盤, RAID-5:讀寫性能提高;可用空間:(n-1)*min(s1,s2,...);有容錯能力,每組鏡像最多隻能壞一塊磁盤;最少3塊磁盤。 RAID-6:讀寫性能提高;可用空間(N-2)*min(s1,s2,...);有容錯能力,每組鏡像最多隻能壞l兩塊磁盤;最少4塊磁盤。 RAID-10:讀寫性能提高;可用空間:N*min(s1,s2,...)/2;有容錯能力,每組鏡像最多隻能壞一塊 最少4塊磁盤。 |
五、建立一個大小爲10G的RAID1,要求有一個空閒盤,並且CHUNK大小爲128k;
[root@bogon ~]# fdisk /dev/sdc 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) e Partition number (1-4): 3 First cylinder (1439-10443, default 1439): Using default value 1439 Last cylinder, +cylinders or +size{K,M,G} (1439-10443, default 10443): Using default value 10443 Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (1439-10443, default 1439): Using default value 1439 Last cylinder, +cylinders or +size{K,M,G} (1439-10443, default 10443): +10G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (2745-10443, default 2745): Using default value 2745 Last cylinder, +cylinders or +size{K,M,G} (2745-10443, default 10443): +10G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (4051-10443, default 4051): Using default value 4051 Last cylinder, +cylinders or +size{K,M,G} (4051-10443, default 10443): +10G Command (m for help): p Disk /dev/sdc: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x0dd03239 Device Boot Start End Blocks Id System /dev/sdc1 1 1306 10490413+ 83 Linux /dev/sdc2 1307 1438 1060290 82 Linux swap / Solaris /dev/sdc3 1439 10443 72332662+ 5 Extended /dev/sdc5 1439 2744 10490413+ 83 Linux /dev/sdc6 2745 4050 10490413+ 83 Linux /dev/sdc7 4051 5356 10490413+ 83 Linux Command (m for help): t Partition number (1-7): 5 Hex code (type L to list codes): fd Changed system type of partition 5 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-7): 6 Hex code (type L to list codes): fd Changed system type of partition 6 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-7): 7 Hex code (type L to list codes): fd Changed system type of partition 7 to fd (Linux raid autodetect) 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: 設備或資源忙. 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@bogon ~]# partx -a /dev/sdc [root@bogon ~]# partx -a /dev/sdc [root@bogon ~]# mdadm -C /dev/md0 -a yes -n 2 -x 1 -l 1 -c 128 /dev/sdc{5,6,7} 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@bogon ~]# mke2fs -t ext4 /dev/md0 [root@bogon ~]# mkdir /raid1 [root@bogon ~]# mount /dev/md0 /raid1 # [root@bogon ~]# mdadm -D /dev/md0 Personalities : [raid6] [raid5] [raid4] [raid1] md0 : active raid1 sdc7[2](S) sdc6[1] sdc5[0] 10482176 blocks super 1.2 [2/2] [UU] md127 : active raid5 sdb7[4] sdb8[3] sdb6[6](S) sdb5[5] 10401792 blocks super 1.2 level 5, 512k chunk, algorithm 2 [3/3] [UUU] |
六、建立一個大小爲4G的RAID5設備,chunk大小爲256k,格式化ext4文件系統,要求可開機自動掛載至/backup目錄,並且不更新訪問時間戳,且支持acl功能;
[root@bogon ~]# fdisk /dev/sdc 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 l logical (5 or over) p primary partition (1-4) l First cylinder (5357-10443, default 5357): Using default value 5357 Last cylinder, +cylinders or +size{K,M,G} (5357-10443, default 10443): +2G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (5619-10443, default 5619): Using default value 5619 Last cylinder, +cylinders or +size{K,M,G} (5619-10443, default 10443): +2G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (5881-10443, default 5881): +2 Value out of range. First cylinder (5881-10443, default 5881): Using default value 5881 Last cylinder, +cylinders or +size{K,M,G} (5881-10443, default 10443): +2G Command (m for help): t Partition number (1-10): 8 Hex code (type L to list codes): fd Changed system type of partition 8 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-10): 9 Hex code (type L to list codes): fd Changed system type of partition 9 to fd (Linux raid autodetect) Command (m for help): t Partition number (1-10): 10 Hex code (type L to list codes): fd Changed system type of partition 10 to fd (Linux raid autodetect) Command (m for help): pp^H Disk /dev/sdc: 85.9 GB, 85899345920 bytes 255 heads, 63 sectors/track, 10443 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: 0x0dd03239 Device Boot Start End Blocks Id System /dev/sdc1 1 1306 10490413+ 83 Linux /dev/sdc2 1307 1438 1060290 82 Linux swap / Solaris /dev/sdc3 1439 10443 72332662+ 5 Extended /dev/sdc5 1439 2744 10490413+ fd Linux raid autodetect /dev/sdc6 2745 4050 10490413+ fd Linux raid autodetect /dev/sdc7 4051 5356 10490413+ fd Linux raid autodetect /dev/sdc8 5357 5618 2104483+ fd Linux raid autodetect /dev/sdc9 5619 5880 2104483+ fd Linux raid autodetect /dev/sdc10 5881 6142 2104483+ fd Linux raid autodetect 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: 設備或資源忙. 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@bogon ~]# partx -a /dev/sdc [root@bogon ~]# partx -a /dev/sdc [root@bogon ~]# mdadm -C /dev/md1 -a yes -n 3 -l 5 -c 256 /dev/sdc{8,9,10} mdadm: Defaulting to version 1.2 metadata mdadm: array /dev/md1 started. [root@bogon ~]# mke2fs -t ext4 /dev/md1 mke2fs 1.41.12 (17-May-2010) 文件系統標籤= 操做系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=64 blocks, Stripe width=128 blocks 262944 inodes, 1051136 blocks 52556 blocks (5.00%) reserved for the super user 第一個數據塊=0 Maximum filesystem blocks=1077936128 33 block groups 32768 blocks per group, 32768 fragments per group 7968 inodes per group Superblock backups stored on blocks: 32768, 98304, 163840, 229376, 294912, 819200, 884736 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@bogon ~]# mdadm -D /dev/md1 [root@bogon ~]# mkdir /backup [root@bogon ~]# mount -o noatime,acl /dev/md1 /backup [root@bogon ~]# blkid /dev/md1 # |
七、寫一個腳本
(1) 接受一個以上文件路徑做爲參數;
(2) 顯示每一個文件擁有的行數;
(3) 總結說明本次共爲幾個文件統計了其行數;
#!/bin/bash # for i in $*;do if [ -z $i ]; then echo "Usage:Enter a file path." exit 2 fi if [ ! -e $i ]; then echo "No such file." exit 3 fi if [ -f $i ]; then echo "$i lines are:$(wc -l $i | cut -d' ' -f1)" else echo "Other type." fi done echo "------" echo "files total number:$#" |
八、寫一個腳本
(1) 傳遞兩個以上字符串看成用戶名;
(2) 建立這些用戶;且密碼同用戶名;
(3) 總結說明共建立了幾個用戶;
[root@bogon bin]# cat useradd.sh #!/bin/bash if [ $# -lt 2 ];then echo "please at lease two char!" exit 1 fi for i in $* do useradd $i echo $i | passwd --stdin $i done echo "Total Add $# users !" [root@bogon bin]# useradd.sh oio po jkn 更改用戶 oio 的密碼 。 passwd: 全部的身份驗證令牌已經成功更新。 更改用戶 po 的密碼 。 passwd: 全部的身份驗證令牌已經成功更新。 更改用戶 jkn 的密碼 。 passwd: 全部的身份驗證令牌已經成功更新。 Total Add 3 users ! |
九、寫一個腳本,新建20個用戶,visitor1-visitor20;計算他們的ID之和;
[root@localhost bin]# cat users.sh [root@localhost bin]# users.sh useradd: user 'visitor1' already existsuseradd: user 'visitor2' already exists useradd: user 'visitor3' already exists useradd: user 'visitor4' already exists useradd: user 'visitor5' already exists useradd: user 'visitor6' already exists useradd: user 'visitor7' already exists useradd: user 'visitor8' already exists useradd: user 'visitor9' already exists useradd: user 'visitor10' already exists useradd: user 'visitor11' already exists useradd: user 'visitor12' already exists useradd: user 'visitor13' already exists useradd: user 'visitor14' already exists useradd: user 'visitor15' already exists useradd: user 'visitor16' already exists useradd: user 'visitor17' already exists useradd: user 'visitor18' already exists useradd: user 'visitor19' already exists useradd: user 'visitor20' already exists ID sum:10190 |
十、寫一腳本,分別統計/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#號開頭的行數之和,以及總的空白行數;
[root@localhost bin]# cat line.sh [root@localhost bin]# bash line.sh /etc/rc.d/rc.sysinit # start line total: 44/etc/rc.d/rc.sysinit space line total: 384 /etc/rc.d/init.d/functions # start line total: 40 /etc/rc.d/init.d/functions space line total: 623 /etc/fstab # start line total: 7 /etc/fstab space line total: 0 |
十一、寫一個腳本,顯示當前系統上全部默認shell爲bash的用戶的用戶名、UID以及此類全部用戶的UID之和;
[root@localhost bin]# cat useruid.sh #!/bin/bash echo "User And Uid:" grep "/bin/bash" /etc/passwd | cut -d: -f1,3 for i in $(grep "/bin/bash" /etc/passwd | cut -d: -f3);do let sumuid+=$i done echo "Uid sum is $sumuid." [root@localhost bin]# useruid.sh |
十二、寫一個腳本,顯示當前系統上全部,擁有附加組的用戶的用戶名;並說明共有多少個此類用戶;
[root@localhost bin]# cat userlist.sh #!/bin/bash declare i=1 userlist=`grep "[^:]$" /etc/group | cut -d: -f4` for username in `cut -d : -f1 /etc/passwd`;do echo "$userlist" | grep -o "$username" &> /dev/null if [ $? == 0 ];then let sum+=i fi echo "$userlist" | grep -o "$username" | sort -u done echo "total users: $sum" [root@localhost bin]# bash userlist.sh bin daemon adm haldaemon postfix total users: 6 [root@localhost bin]# |
1三、建立一個由至少兩個物理卷組成的大小爲20G的卷組;要求,PE大小爲8M;而在卷組中建立一個大小爲5G的邏輯卷mylv1,格式化爲ext4文件系統,開機自動掛載至/users目錄,支持acl;
[root@localhost ~]# fdisk /dev/sdc 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 l logical (5 or over) p primary partition (1-4) l First cylinder (6143-10443, default 6143): Using default value 6143 Last cylinder, +cylinders or +size{K,M,G} (6143-10443, default 10443): +10G Command (m for help): n Command action l logical (5 or over) p primary partition (1-4) l First cylinder (7449-10443, default 7449): Using default value 7449 Last cylinder, +cylinders or +size{K,M,G} (7449-10443, default 10443): +10G Command (m for help): t Partition number (1-12): 11 Hex code (type L to list codes): 8e Changed system type of partition 11 to 8e (Linux LVM) Command (m for help): t Partition number (1-12): 12 Hex code (type L to list codes): 8e Changed system type of partition 12 to 8e (Linux LVM) 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: 設備或資源忙. 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@localhost ~]# partx -a /dev/sdc # 通知內核從新讀取硬盤分區表 [root@localhost ~]# partx -a /dev/sdc # 通知內核從新讀取硬盤分區表 [root@localhost ~]# pvcreate /dev/sdc{11,12} # 將分區建立爲物理卷 Physical volume "/dev/sdc11" successfully created Physical volume "/dev/sdc12" successfully created[root@localhost ~]# pvs # 簡要查看建立的物理卷 pvdisplay 可查看詳細PV信息 PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 19.51g 0/dev/sdc11 lvm2 ---- 10.00g 10.00g /dev/sdc12 lvm2 ---- 10.00g 10.00g [root@localhost ~]# vgcreate -s 8M myvg /dev/sdc{11,12} Volume group "myvg" successfully created [root@localhost ~]# vgdisplay #查看卷組vg的詳細信息 --- 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 GeGT7X-iqbB-KtJx-8nbJ-djV8-Dd38-rLPbAy --- Volume group --- VG Name VolGroup System ID Format lvm2 Metadata Areas 1 Metadata Sequence No 3 VG Access read/write VG Status resizable MAX LV 0 Cur LV 2 Open LV 2 Max PV 0 Cur PV 1 Act PV 1 VG Size 19.51 GiB PE Size 4.00 MiB Total PE 4994 Alloc PE / Size 4994 / 19.51 GiB Free PE / Size 0 / 0 VG UUID eP7T48-AO71-yMjl-kRhP-zd9I-onLM-8vFhH1 [root@localhost ~]# lvcreate -L 5G -n mylv1 myvg # 在卷組中建立大小爲5G的邏輯卷mylv1 Logical volume "mylv1" created. [root@localhost ~]# mke2fs -t ext4 /dev/myvg/mylv1 # 將mylv1格式化 mke2fs 1.41.12 (17-May-2010)文件系統標籤= 操做系統:Linux 塊大小=4096 (log=2) 分塊大小=4096 (log=2) Stride=0 blocks, Stripe width=0 blocks 327680 inodes, 1310720 blocks 65536 blocks (5.00%) reserved for the super user 第一個數據塊=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 正在寫入inode表: 完成 Creating journal (32768 blocks): 完成 Writing superblocks and filesystem accounting information: 完成 This filesystem will be automatically checked every 25 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [root@localhost ~]# mkdir /users # 建立/users目錄 [root@localhost ~]# mount -o acl /dev/myvg/mylv1 /users # 掛載至/users [root@localhost ~]# vi /etc/fstab # 設爲開機自動掛載至/users /dev/myvg/mylv1 /users ext4 defaults,acl 0 0 [root@localhost ~]# mount | grep '^/dev/mapper/myvg-mylv1' #查看掛載狀況 /dev/mapper/myvg-mylv1 on /users type ext4 (rw,acl) # 說明掛載成功 [root@localhost ~]# df -lh # Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 820M 17G 5% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 477M 33M 419M 8% /boot /dev/md127 9.7G 23M 9.2G 1% /mydata /dev/sdc1 9.8G 13M 9.6G 1% /data/mydata /dev/md0 9.8G 23M 9.2G 1% /raid1 /dev/md1 3.9G 1.7G 2.1G 45% /backup /dev/mapper/myvg-mylv1 4.8G 10M 4.6G 1% /users |
1四、新建用戶magedu;其家目錄爲/users/magedu,然後su切換至此用戶,複製多個文件至家目錄;
[root@localhost ~]# useradd -d /users/magedu magedu [root@localhost magedu]# su - magedu [[root@localhost ~]# cp -r /etc/sysconfig/ /users/magedu/ |
1五、擴展mylv1至9G,確保擴展完成後原有數據徹底可用;
[root@localhost ~]# pvs #查看pv信息,看物理卷是否能擴展 PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 19.51g 0 /dev/sdc11 myvg lvm2 a--u 10.00g 5.00g /dev/sdc12 myvg lvm2 a--u 10.00g 10.00g [root@localhost ~]# vgs #查看vg信息,看卷組是否能擴展 VG #PV #LV #SN Attr VSize VFree VolGroup 1 2 0 wz--n- 19.51g 0 myvg 2 1 0 wz--n- 20.00g 15.00g [root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 #擴展mylv1至9G 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@localhost ~]# df -lh #查看硬盤信息,/dev/mapper/myvg-mylv1仍是5G Filesystem Size Used Avail Use% Mounted on [root@localhost ~]# pvs #查看pv信息,看物理卷是否能擴展 PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 19.51g 0 /dev/sdc11 myvg lvm2 a--u 10.00g 5.00g /dev/sdc12 myvg lvm2 a--u 10.00g 10.00g [root@localhost ~]# vgs #查看vg信息,看卷組是否能擴展 VG #PV #LV #SN Attr VSize VFree VolGroup 1 2 0 wz--n- 19.51g 0 myvg 2 1 0 wz--n- 20.00g 15.00g [root@localhost ~]# lvextend -L 9G /dev/myvg/mylv1 #擴展mylv1至9G 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@localhost ~]# df -lh #查看硬盤信息,/dev/mapper/myvg-mylv1仍是5G Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 821M 17G 5% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 477M 33M 419M 8% /boot /dev/md127 9.7G 23M 9.2G 1% /mydata /dev/sdc1 9.8G 13M 9.6G 1% /data/mydata /dev/md0 9.8G 23M 9.2G 1% /raid1 /dev/md1 3.9G 1.7G 2.1G 45% /backup /dev/mapper/myvg-mylv1 4.8G 11M 4.6G 1% /users [root@localhost ~]# resize2fs -p /dev/myvg/mylv1 #經過resize2fs工具來修復文件系統,把新添加扒 文件系統更新,至關於卸載後從新掛載 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@localhost ~]# df -lh #查看硬盤信息,是否更新 Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 821M 17G 5% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 477M 33M 419M 8% /boot /dev/md127 9.7G 23M 9.2G 1% /mydata /dev/sdc1 9.8G 13M 9.6G 1% /data/mydata /dev/md0 9.8G 23M 9.2G 1% /raid1 /dev/md1 3.9G 1.7G 2.0G 46% /backup /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /users [root@localhost ~]# ls /user/magedu/sysconfig/ # 查看magedu用戶文件是否丟失 [root@localhost ~]# resize2fs -p /dev/myvg/mylv1 #經過resize2fs工具來修改邏輯邊界 resize2fs 1.41.12 (17-May-2010) Filesystem at /dev/myvg/mylv1 is mounted on /users; on-line resizing required old desc_blocks = 1, new_desc_blocks = 1 Performing an on-line resize of /dev/myvg/mylv1 to 2359296 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 2359296 blocks long. [root@localhost ~]# df -lh #查看硬盤信息,是否更新 Filesystem Size Used Avail Use% Mounted on /dev/mapper/VolGroup-lv_root 19G 821M 17G 5% / tmpfs 238M 0 238M 0% /dev/shm /dev/sda1 477M 33M 419M 8% /boot /dev/md127 9.7G 23M 9.2G 1% /mydata /dev/sdc1 9.8G 13M 9.6G 1% /data/mydata /dev/md0 9.8G 23M 9.2G 1% /raid1 /dev/md1 3.9G 1.7G 2.0G 46% /backup /dev/mapper/myvg-mylv1 8.8G 12M 8.3G 1% /users [root@localhost ~]# ls /user/magedu/sysconfig/ # 查看magedu用戶文件是否丟失 |
1六、縮減mylv1至7G,確保縮減完成後原有數據徹底可用;
[root@localhost ~]# umount /dev/myvg/mylv1 #卸載分區 [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G #縮減邏輯卷大小 resize2fs 1.41.12 (17-May-2010) 請先運行 'e2fsck -f /dev/myvg/mylv1'. [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.41.12 (17-May-2010) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄鏈接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/myvg/mylv1: 15/589824 files (0.0% non-contiguous), 72675/2359296 blocks [root@localhost ~]# resize2fs /dev/myvg/mylv1 7G #縮減邏輯卷大小 resize2fs 1.41.12 (17-May-2010) Resizing the filesystem on /dev/myvg/mylv1 to 1835008 (4k) blocks. The filesystem on /dev/myvg/mylv1 is now 1835008 blocks long. [root@localhost ~]# e2fsck -f /dev/myvg/mylv1 e2fsck 1.41.12 (17-May-2010) 第一步: 檢查inode,塊,和大小 第二步: 檢查目錄結構 第3步: 檢查目錄鏈接性 Pass 4: Checking reference counts 第5步: 檢查簇概要信息 /dev/myvg/mylv1: 15/458752 files (0.0% non-contiguous), 64451/1835008 blocks [root@localhost ~]# lvreduce -L 7G /dev/myvg/mylv1 #縮減邏輯卷lv物理邊界大小至3G WARNING: Reducing active logical volume to 7.00 GiB. THIS MAY DESTROY YOUR DATA (filesystem etc.) Do you really want to reduce myvg/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@localhost ~]# lvs #查看邏輯卷 LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert lv_root VolGroup -wi-ao---- 18.57g lv_swap VolGroup -wi-ao---- 960.00m mylv1 myvg -wi-a----- 7.00g [root@localhost ~]# mount /dev/myvg/mylv1 /users/ [root@localhost ~]# ls /users lost+found magedu [root@localhost ~]# ls /users/magedu/sysconfig/ |
1七、對mylv1建立快照,並經過備份數據;要求保留原有的屬主屬組等信息;
[root@localhost /]# cp -r /etc/opt/ /users/ [root@localhost /]# lvcreate -s -L 30M -n snapLv -p r /dev/myvg/mylv1 #建立快照-s,-L指定快照.小爲30M,-n指定快照名爲snapLv,-p r只讀權限。 Rounding up size to full physical extent 32.00 MiB Logical volume "snapLv" created. [root@localhost /]# mkdir /snapsh [root@localhost ~]# mount /dev/myvg/snapLv /snapsh #將快照文件掛載至/snapsh mount: block device /dev/mapper/myvg-snapLv is write-protected, mounting read-only [root@localhost ~]# cd /snapsh/ [root@localhost snapsh]# ls lost+found magedu opt [root@localhost ~]# cat /snapsh/magedu/sysconfig/grub boot=/dev/sda forcelba=0 [root@localhost ~]# vi /users/magedu/sysconfig/grub # 編輯源卷的grub文件 [root@localhost ~]# cat /users/magedu/sysconfig/grub # 查看修改後的grub文件 boot=/dev/sdaforcelba=0 welcome new line! #新增 [root@localhost ~]# cd /snapsh/ [root@localhost snapsh]# pwd # 再查看/snapsh下快照裏面的grub /snapsh [root@localhost snapsh]# cat /snapsh/magedu/sysconfig/grub # 文件內容沒有變化 boot=/dev/sda forcelba=0[root@localhost snapsh]# cp -r /etc/init.d/ /users/ #複製某文件至/users [root@localhost snapsh]# cd /users/[root@localhost users]# ll # /users源卷仍能夠新增文件等操做總用量 32-rw-r--r--. 1 root root 1050 8月 20 21:20 fstabdrwxr-xr-x. 2 root root 4096 8月 20 21:34 init.ddrwx------. 2 root root 16384 8月 20 18:55 lost+founddrwx------. 3 magedu magedu 4096 8月 20 21:06 magedudrwxr-xr-x. 2 root root 4096 8月 20 21:10 opt |