fdisk:对于一块硬盘来讲,最多只能管理15个分区 fdisk -l [-u] [device...] fdisk device 查看内核是否已经识别新的分区, 如果不识别,通知内核重新读取硬盘分区表; kpartx -a /dev/DEVICE Centos 5:使用partprobe |
创建文件系统: 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系列文件系统专用管理工具 mkswap:创建交换分区 前提:调整其分区的ID为82 其它常用工具: blkid:块设备属性值信息查看 e2label:管理ext系列文件系统的LABEL tune2fs:重新设定ext系列文件系统可调用参数的值 dumpe2fs: 既能查看超级块信息,又能查看整个文件的组织结构信息;又称查看布局信息 文件系统检测: Note:FS_TYPE一定要与分区上已有的文件系统类型相同 e2fsck: ext系列文件专用的检测的工具 |
挂载方法:mount DEVICE MOUNT_POINT 常用选项: 注意:查看内核追踪到的已挂载的所有设备:cat /proc/mounts -o options:(挂载文件系统的选项) 注意:上述选项可多个同时使用,彼此使用逗号分隔 卸载命令: 查看正在访问指定文件系统的进程: 终止所有正在访问的文件系统的进程: 挂载交换分区: 内存空间使用状态: 文件系统空间占用等信息查看工具: 查看某目录总体占用空间状态: du: |
mdadm:模式化的工具 命令的语法格式:mdadm [mode] <raiddevice> [options] <component-devices> 支持的RAID级别:LINEAR,RAID0,RAID1,RAID4,RAID5,RAID6,RAID10; 模式: <raiddevice>: /dev/md# -C:创建模式 -D:显示raid的详细信息: 管理模式: 观察md的状态: 停止md设备 watch命令: wach -n# ‘COMMAND‘ |
LVM: Logical Volume Manager,Version 2 逻辑卷管理器
dm: device mapper(设备映射),将一个或多个底层块设备组织成一个逻辑设备的模块 dev/mapper/VG_NAME(NAME=LV_NAME) pv管理工具 vg管理工具: vgcreate [-s](指定PE大小)#[bBsSkKmMgGtTpPeE](单位) VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] vgremove 删除卷组 lv管理工具: lvcreate -L #[mMgGtT] -n NAME VolumeGroup lvremove /dev/VG_NAME 删除逻辑卷 扩展逻辑卷: 缩减逻辑卷 快照:snapshot |
1、创建一个10G分区,并格式为ext4文件系统;
[[email protected] ~]# 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 #保存并推出 Calling ioctl() to re-read partition table. |
(1) 要求其block大小为2048, 预留空间百分比为2, 卷标为MYDATA, 默认挂载属性包含acl;
(2) 挂载至/data/mydata目录,要求挂载时禁止程序自动运行,且不更新文件的访问时间戳;
[[email protected] ~]# mke2fs -t ext4 -b 2048 -m 2 -L ‘MYDATA‘ /dev/sdc1 [[email protected] ~]# mkdir /data/mydata [[email protected] ~]# mount -o noauto,noatime,acl /dev/sdc1 /data/mydata/ [[email protected] ~]# mount |
2、创建一个大小为1G的swap分区,并创建好文件系统,并启用之;
[[email protected] ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to Command (m for help): n#新建分区 Command action e extended p#主分区 Partition number (1-4): 2 #主分区号 First cylinder (1307-10443, default 1307): 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 #显示已有分区 Disk /dev/sdc: 85.9 GB, 85899345920 bytes Device Boot Start End Blocks Id System Command (m for help): w #写入磁盘并退出 The partition table has been altered! Calling ioctl() to re-read partition table. [[email protected] ~]# mkswap /dev/sdc2 #创建交换分区 Setting up swapspace version 1, size = 1060284 KiB no label, UUID=880e15e9-f1f5-4682-861c-b217266ede96 |
3、写一个脚本
(1)、获取并列出当前系统上的所有磁盘设备;
(2)、显示每个磁盘设备上每个分区相关的空间使用信息;
[[email protected] 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 [[email protected] bin]# cat diskfile.sh #!/bin/bash disks=$(fdisk -l | grep -o ‘^Disk /dev/[sh]d[a-z]‘) |
4、总结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;有容错能力,每组镜像最多只能坏一块 |
5、创建一个大小为10G的RAID1,要求有一个空闲盘,而且CHUNK大小为128k;
[[email protected] ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to Command (m for help): n Command (m for help): n Command (m for help): n Command (m for help): n Command (m for help): p Disk /dev/sdc: 85.9 GB, 85899345920 bytes Device Boot Start End Blocks Id System Command (m for help): t Command (m for help): t Command (m for help): t Command (m for help): w Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. mdadm: array /dev/md0 started. [[email protected] ~]# mke2fs -t ext4 /dev/md0 正在写入inode表: 完成 This filesystem will be automatically checked every 32 mounts or [[email protected] ~]# mkdir /raid1 [[email protected] ~]# mount /dev/md0 /raid1 # [[email protected] ~]# mdadm -D /dev/md0 Update Time : Sat Aug 20 04:07:40 2016 Name : bogon:0 (local to host bogon) Number Major Minor RaidDevice State 2 8 39 - spare /dev/sdc7 [[email protected] ~]# cat /proc/mdstat |
6、创建一个大小为4G的RAID5设备,chunk大小为256k,格式化ext4文件系统,要求可开机自动挂载至/backup目录,而且不更新访问时间戳,且支持acl功能;
[[email protected] ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to Command (m for help): n Command (m for help): n Command (m for help): n Command (m for help): t Command (m for help): t Command (m for help): t Command (m for help): pp^H Disk /dev/sdc: 85.9 GB, 85899345920 bytes Device Boot Start End Blocks Id System Command (m for help): w Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. 正在写入inode表: 完成 This filesystem will be automatically checked every 31 mounts or 180 days, whichever comes first. Use tune2fs -c or -i to override. [[email protected] ~]# mdadm -D /dev/md1 Update Time : Sat Aug 20 04:48:29 2016 Layout : left-symmetric Name : bogon:1 (local to host bogon) Number Major Minor RaidDevice State [[email protected] ~]# mkdir /backup [[email protected] ~]# mount -o noatime,acl /dev/md1 /backup [[email protected] ~]# blkid /dev/md1 # |
7、写一个脚本
(1) 接受一个以上文件路径作为参数;
(2) 显示每个文件拥有的行数;
(3) 总结说明本次共为几个文件统计了其行数;
[[email protected] bin]# cat file1.sh #!/bin/bash if [ $# -eq 0 ];then echo "please input file path!" #exit 1 fi for i in $*;do echo "$i lines is:$(wc -l $i | cut -d‘ ‘ -f1)" done echo "------------" echo "files total number:$#" [[email protected] bin]# file1.sh /etc/fstab /etc/fstab lines is:18 ------------ files total number:1 |
8、写一个脚本
(1) 传递两个以上字符串当作用户名;
(2) 创建这些用户;且密码同用户名;
(3) 总结说明共创建了几个用户;
[[email protected] 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 !" [[email protected] bin]# useradd.sh oio po jkn 更改用户 oio 的密码 。 passwd: 所有的身份验证令牌已经成功更新。 更改用户 po 的密码 。 passwd: 所有的身份验证令牌已经成功更新。 更改用户 jkn 的密码 。 passwd: 所有的身份验证令牌已经成功更新。 Total Add 3 users ! |
9、写一个脚本,新建20个用户,visitor1-visitor20;计算他们的ID之和;
[[email protected] bin]# cat users.sh #!/bin/bash for i in {1..20};do useradd visitor$i uid=`grep "\<visitor$i\>" /etc/passwd | cut -d: -f3` sumid=$[ $sumid + $uid ] done echo "ID sum:$sumid" [[email protected] bin]# users.sh useradd: user ‘visitor1‘ already exists |
10、写一脚本,分别统计/etc/rc.d/rc.sysinit、/etc/rc.d/init.d/functions和/etc/fstab文件中以#号开头的行数之和,以及总的空白行数;
[[email protected] bin]# cat line.sh #!/bin/bash for i in {/etc/rc.d/rc.sysinit,/etc/rc.d/init.d/functions,/etc/fstab};do line1=$(grep "^#" $i | wc -l) line2=$(grep "^[[:space:]]" $i | wc -l) echo "$i # start line total: $line1" echo "$i space line total: $line2" done [[email protected] bin]# bash line.sh /etc/rc.d/rc.sysinit # start line total: 44 |
11、写一个脚本,显示当前系统上所有默认shell为bash的用户的用户名、UID以及此类所有用户的UID之和;
[[email protected] 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." [[email protected] bin]# useruid.sh |
12、写一个脚本,显示当前系统上所有,拥有附加组的用户的用户名;并说明共有多少个此类用户;
[[email protected] 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" [[email protected] bin]# bash userlist.sh bin daemon adm haldaemon postfix total users: 6 [[email protected] bin]# |
13、创建一个由至少两个物理卷组成的大小为20G的卷组;要求,PE大小为8M;而在卷组中创建一个大小为5G的逻辑卷mylv1,格式化为ext4文件系统,开机自动挂载至/users目录,支持acl;
[[email protected] ~]# fdisk /dev/sdc
WARNING: DOS-compatible mode is deprecated. It‘s strongly recommended to Command (m for help): n Command (m for help): n Command (m for help): t Command (m for help): t Command (m for help): w Calling ioctl() to re-read partition table. WARNING: Re-reading the partition table failed with error 16: 设备或资源忙. [[email protected] ~]# pvcreate /dev/sdc{11,12} # 将分区创建为物理卷 Physical volume "/dev/sdc11" successfully created Physical volume "/dev/sdc12" successfully created [[email protected] ~]# pvs # 简要查看创建的物理卷 pvdisplay 可查看详细PV信息 PV VG Fmt Attr PSize PFree /dev/sda2 VolGroup lvm2 a--u 19.51g 0 [[email protected] ~]# vgdisplay #查看卷组vg的详细信息 --- Volume group --- Logical volume "mylv1" created. [[email protected] ~]# mke2fs -t ext4 /dev/myvg/mylv1 # 将mylv1格式化 mke2fs 1.41.12 (17-May-2010) 正在写入inode表: 完成 This filesystem will be automatically checked every 25 mounts or |
14、新建用户magedu;其家目录为/users/magedu,而后su切换至此用户,复制多个文件至家目录;
[[email protected] ~]# useradd -d /users/magedu magedu [[email protected] magedu]# su - magedu [[[email protected] ~]# cp -r /etc/sysconfig/ /users/magedu/ |
15、扩展mylv1至9G,确保扩展完成后原有数据完全可用;
[[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# 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. [[email protected] ~]# df -lh #查看硬盘信息,/dev/mapper/myvg-mylv1还是5G Filesystem Size Used Avail Use% Mounted on [[email protected] ~]# 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 [[email protected] ~]# 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 [[email protected] ~]# 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. [[email protected] ~]# 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 [[email protected] ~]# 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. [[email protected] ~]# df -lh #查看硬盘信息,是否更新 [[email protected] ~]# df -lh #查看硬盘信息,是否更新 |
16、缩减mylv1至7G,确保缩减完成后原有数据完全可用;
[[email protected] ~]# umount /dev/myvg/mylv1 #卸载分区 [[email protected] ~]# resize2fs /dev/myvg/mylv1 7G #缩减逻辑卷大小 resize2fs 1.41.12 (17-May-2010) 请先运行 ‘e2fsck -f /dev/myvg/mylv1‘. [[email protected] ~]# e2fsck -f /dev/myvg/mylv1 [[email protected] ~]# e2fsck -f /dev/myvg/mylv1 |
17、对mylv1创建快照,并通过备份数据;要求保留原有的属主属组等信息;
[[email protected] /]# cp -r /etc/opt/ /users/ [[email protected] /]# 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. [[email protected] /]# mkdir /snapsh [[email protected] ~]# mount /dev/myvg/snapLv /snapsh #将快照文件挂载至/snapsh mount: block device /dev/mapper/myvg-snapLv is write-protected, mounting read-only [[email protected] ~]# cd /snapsh/ [[email protected] snapsh]# ls lost+found magedu opt [[email protected] ~]# cat /snapsh/magedu/sysconfig/grub boot=/dev/sda forcelba=0 [[email protected] ~]# vi /users/magedu/sysconfig/grub # 编辑源卷的grub文件 [[email protected] ~]# cat /users/magedu/sysconfig/grub # 查看修改后的grub文件 boot=/dev/sda [[email protected] snapsh]# pwd # 再查看/snapsh下快照里面的grub /snapsh [[email protected] snapsh]# cat /snapsh/magedu/sysconfig/grub # 文件内容没有变化 boot=/dev/sda forcelba=0 |