管理磁盘和文件系统
温故而知新
IDE是并行设备,并行设备都是以hd开头
SCSI是串行设备,串行设备都是以sd开头(U盘也是SCSI设备)
虚拟硬盘以vd开头
一般来说,SWAP需设为物理内存的2倍,磁盘划分参考老男孩博文
主分区个数+扩展分区个数不能大于4,即逻辑分区必须从5开始。
主分区、逻辑分区可以格式化,扩展分区不可格式化
在扩展分区中创建逻辑分区
删除逻辑分区时要按一定顺序。若直接删除扩展分区,则逻辑分区就会被删除
lvextend用于动态扩展逻辑卷,可实现在线扩展(即在挂载时,也可扩展),
但是缩小时必须先卸载。
管理磁盘:
硬盘分区之fdisk
[root@localhost~]# fdisk �Cl #检测并确认硬盘设备
Disk /dev/sda:21.4 GB, 21474836480 bytes
255 heads, 63sectors/track, 2610 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 38 305203+ 83 Linux
/dev/sda2 39 2349 18563107+ 83 Linux
/dev/sda3 2350 2610 2096482+ 82 Linux swap / Solaris
Disk /dev/sdb:21.4 GB, 21474836480 bytes
255 heads, 63sectors/track, 2610 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Disk /dev/sdbdoesn't contain a valid partition table
Command (m forhelp): m #显示各种操作指令的帮助信息
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition #删除指定硬盘分区(d--delete)
l list known partition types #列出已知分区类型。如:8e代表LVM, 82--swap,83--ext3
m print this menu
n add a new partition #新建分区(n--new)
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 functio
磁盘配额管理
1:以支持磁盘配额功能挂载文件系统:。
可以带使用选项的mount命令重新挂载指定的分区,以便增加对用户、组配额功能的支持。对于支持配额功能的文件系统,将在mount信息中显示“usrquota,grpquota”。
[root@localhostCentOS]# mount -o usrquota,grpquota /dev/sdb1 /mnt/
[root@localhostCentOS]# mount |grep "quota" #验证上一步操作是否成功
/dev/sdb1 on/mnt type ext3 (rw,usrquota,grpquota)
[root@localhostCentOS]#chmod 1777 /mnt/ #为后续测试需要,允许用户写入数据
[root@localhostCentOS]#ll -d /mnt/ #验证上一步操作是否成功
[root@localhostCentOS]# vi /etc/fstab #添加如下行,设置开机自启动
/dev/sdb1 /mnt ext3 default,usrquota,grpquota 0 0
2:检测磁盘配额并生成磁盘配额文件
[root@localhostCentOS]# quotacheck -auvcg #
quotacheck:Scanning /dev/sdb1 [/mnt] quotacheck: Cannot stat old user quota file: No suchfile or directory
quotacheck:Cannot stat old group quota file: No such file or directory
quotacheck:Cannot stat old user quota file: No such file or directory
quotacheck:Cannot stat old group quota file: No such file or directory
done
quotacheck:Checked 3 directories and 2 files
quotacheck: Oldfile not found.
quotacheck: Oldfile not found.
注:选项“-a”表示扫描所有的分区,“-u”和“-g”分别表示检测用户和组配额,“-c”表示创建新的配额文件,-v表示显示命令执行过程中的细节信息。若未使用-a选项时,必须指定一个分区(设备文件或挂载点目录)作为命令参数。
由于/dev/sdb1分区中并未使用较早版本的配额文件,因此出现“Oldfile not found.”之类的提示信息是正常的。新建的配额文件包括“aquota.group”、“aquota.user”,分别用于保存用户、组的配额设置。配额文件保存在该文件系统的根目录下,默认权限为600。
[root@localhostCentOS]# ll /mnt/aquota.*
-rw------- 1root root 6144 May 8 23:26/mnt/aquota.group
-rw------- 1root root 6144 May 8 23:26/mnt/aquota.user
3:编辑用户和组账号的配额设置
[root@localhostCentOS]# edquota -u mingming #结合-u和-g可分别编辑用户和组
Disk quotas for user mingming (uid 500):
Filesystem blocks soft hard inodes files soft hard
/dev/sdb1 0 80000 100000 0 0 4 0
4:启动文件系统的磁盘配额功能
启动和关闭文件系统磁盘配额功能分别使用quotaon、quotaoff命令进行,需要指定设备文件名或文件系统的挂载点目录作为命令参数。
quotaon、quotaoff命令使用的选项和quotacheck的选项类似。
系统在每次开机后会自动检测是否有支持磁盘配额的文件系统,如果找到,则启动该文件系统的磁盘配额功能(相关操作在系统初始化脚本“/etc/rc.d/rc.sysinit”文件中来完成)。因此,下次开机后无需再手动执行quotaon命令。
[root@localhostCentOS]# quotaon -uvg /mnt/
/dev/sdb1[/mnt]: group quotas turned on
/dev/sdb1[/mnt]: user quotas turned on
5:验证磁盘配额功能
本案例以linux系统用户mingming为例。使用受磁盘配额限制的用户账号登陆linux系统,并切换到应用了配额的文件系统中,进行复制文件等写入操作,测试所设置的磁盘配额项是否生效。
使用dd命令作为调试命令,可以快速看到效果。
[mingming@localhostmnt]$ dd if=/dev/zero of=myfile bs=1M count=40
40+0 records in #在软限制范围内时成功写入数据
40+0 records out
41943040 bytes(42 MB) copied, 0.167078 seconds, 251 MB/s
[mingming@localhostmnt]$ dd if=/dev/zero of=myfile bs=1M count=90
sdb1: warning,user block quota exceeded. #超出软限制后发出警告信息
sdb1: writefailed, user block limit reached. #未超出硬限制前仍能写入数据
dd: writing`myfile': Disk quota exceeded
90+0 records in
90+0 records out
98070528 bytes(90 MB) copied, 0.613105 seconds, 160 MB/s
[mingming@localhostmnt]$ dd if=/dev/zero of=myfile bs=1M count=120
sdb1: warning,user block quota exceeded. #超出硬限制后发出警告信息
sdb1: writefailed, user block limit reached.
dd: writing`myfile': Disk quota exceeded
94+0 records in
93+0 records out #超出硬限制的数据将被中断
98070528 bytes(98 MB) copied, 0.613105 seconds, 160 MB/s
6:查看用户或分区的配额使用情况
可使用quota和repquota在查看文件系统中用户或组的配额使用情况。
quota命令可结合“-u”、“-g”选项分别查看指定用户和组的配额使用情况;
而requota命令主要针对指定的文件系统输出配额使用情况报告,结合-a选项可以查看所有可用分区的配额使用情况。
[root@localhost~]# quota -u mingming
Disk quotas foruser mingming (uid 500):
Filesystem blocks quota limit grace files quota limit grace
/dev/sdb1 100000* 80000 100000 4 0 0
[root@localhost~]# repquota /mnt/
*** Report foruser quotas on device /dev/sdb1
Block gracetime: 7days; Inode grace time: 7days
Block limits File limits
User used soft hard grace used soft hard grace
----------------------------------------------------------------------
root -- 140748 0 0 2 0 0
mingming +- 100000 80000 100000 6days 4 0 0
磁盘配额演示完毕!!!
[root@localhost ~]# df -hT
Filesystem Type Size Used Avail Use% Mounted on
/dev/sda2 ext3 18G 2.2G 15G 14% /
/dev/sda1 ext3 289M 16M 258M 6% /boot
tmpfs tmpfs 506M 0 506M 0% /dev/shm
/dev/hda iso9660 3.8G 3.8G 0 100% /media/CentOS_5.2_Final
/dev/sdb1 ext3 4.6G 236M 4.2G 6% /mnt
Lvm逻辑卷管理
1:创建pv物理卷(物理卷不可扩展)
[root@localhost ~]# pvcreate /dev/sdb1 /dev/sdb2 /dev/sdb6
Physical volume "/dev/sdb1"successfully created
Physical volume "/dev/sdb2"successfully created
Physical volume "/dev/sdb6"successfully created
[root@localhost~]# pvscan # pvscan用于显示系统中所有的物理卷
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
PV /dev/sdb1 lvm2 [4.67 GB]
PV /dev/sdb2 lvm2 [4.67 GB]
PV /dev/sdb6 lvm2 [8.79 GB]
Total: 3 [18.12 GB] / in use: 0 [0 ] / in no VG: 3 [18.12 GB]
[root@localhost~]# pvdisplay /dev/sdb1 #查看物理卷的详细信息
"/dev/sdb1" is a new physicalvolume of "4.67 GB"
--- NEW Physical volume ---
PV Name /dev/sdb1
VG Name
PV Size 4.67 GB
Allocatable NO
PE Size (KByte) 0
Total PE 0
Free PE 0
Allocated PE 0
PV UUID nhCKai-ZsCN-3p9k-axz2-5U73-Wiwg-UIyoP1
[root@localhost~]# pvremove /dev/sdb6 #将物理卷还原成普通分区或磁盘
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
Labels on physical volume"/dev/sdb6" successfully wiped
[root@localhost~]# pvscan #pvscan用于显示系统中所有的物理卷,发现上一步操作成功
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
PV /dev/sdb1 lvm2 [4.67 GB]
PV /dev/sdb2 lvm2 [4.67 GB]
Total: 2 [9.33 GB] / in use: 0 [0 ] / in no VG: 2 [9.33 GB]
2:创建VG物理卷组(物理卷组可扩展)
vgcreate命令用于将一个或多个物理卷创建为一个卷组,不同卷组用空格隔开
格式:vgcreate 卷组名卷组1卷组2
[root@localhost~]# vgcreate vg1 /dev/sdb1 /dev/sdb2
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
Volume group "vg1" successfullycreated
vgscan命令扫描当前系统中建立的LVM卷组,并显示相关信息
[root@localhost~]# vgscan
Reading all physical volumes. This may take a while...
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
Found volume group "vg1" usingmetadata type lvm2
vgdisplay命令用于显示系统中各卷组的详细信息,需要使用指定的卷组名作为命令参数,缺省时将显示所有卷组的信息。
[root@localhost~]# vgdisplay
--- Volume group ---
VG Name vg1
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 9.33 GB
PE Size 4.00 MB
Total PE 2388
AllocPE / Size 0 / 0
Free PE / Size 2388 / 9.33 GB
VG UUID 5gEh8v-Glbj-Odw0-nhHR-N9mA-mtrn-WfYN8c
[root@localhost~]# pvcreate /dev/sdb5 #创建物理卷组
Physical volume "/dev/sdb5"successfully created
vgextend命令用于扩展卷组磁盘空间,可增加一个或多个物理卷,不同物理卷以空格隔开
格式:vgextend 卷组物理卷1
[root@localhost~]# vgextend vg1 /dev/sdb6
/dev/cdrom-hda: open failed: Read-only filesystem
Attempt to close device '/dev/cdrom-hda'which is not open.
Volume group "vg1" successfullyextended
vgdisplay命令用于显示系统中各卷组的详细信息,验证上一步的操作是否成功
[root@localhost ~]# vgdisplay vg1
--- Volume group ---
VG Name vg1
System ID
Format lvm2
Metadata Areas 3
Metadata Sequence No 2
VG Access read/write
VG Status resizable
MAX LV 0
Cur LV 0
Open LV 0
Max PV 0
Cur PV 3
Act PV 3
VG Size 11.20 GB
PE Size 4.00 MB
Total PE 2866
AllocPE / Size 0 / 0
Free PE / Size 2866 / 11.20 GB
VG UUID 5gEh8v-Glbj-Odw0-nhHR-N9mA-mtrn-WfYN8c
3:创建LV逻辑卷(LVM可扩展,格式化之后才能用)
lvcreate用于从指定卷组分割空间,以创建LV逻辑卷,需指定逻辑卷大小,名称及所在卷组名作为参数。
格式:lvcreate �CL逻辑卷大小�Cn 逻辑卷名卷组名
[root@localhost~]# lvcreate -L 10G -n dan vg1
Logical volume "dan" created
[root@localhost~]# ll /dev/vg1/dan
lrwxrwxrwx 1root root 19 May 9 00:09 /dev/vg1/dan-> /dev/mapper/vg1-dan
lvdisplay命令用于显示逻辑卷的详细信息,需要指定逻辑卷文件位置作为参数。
若使用卷组名为参数,将显示该卷组中创建的所有逻辑卷信息
[root@localhost~]# lvdisplay /dev/vg1/dan
--- Logical volume ---
LV Name /dev/vg1/dan
VG Name vg1
LV UUID tWW2Vp-4QEh-QY79-3GAp-uY9x-zi0y-pJs6JB
LV Write Access read/write
LV Status available
# open 0
LVSize 10.00 GB
Current LE 2560
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
lvextend用于动态扩展逻辑卷的空间,若当前逻辑卷空间不足时,可以从所在卷组中分割额外的空间进行扩展。只需指定需增加的容量大小及逻辑卷文件位置即可。
前提条件是,该卷组中还有尚未分配的磁盘空间,否则需要先扩展卷组容量。
另外,调整逻辑卷容量后,需要执行“resize2fs /dev/卷组名/逻辑卷名”命令以便Linux系统重新识别文件系统大小
[root@localhost ~]# lvextend -L +10M /dev/vg1/dan
Rounding up size to full physical extent12.00 MB
Extending logical volume dan to 10.01 GB
Logical volume dan successfully resized
[root@localhost~]# lvdisplay /dev/vg1/dan
--- Logical volume ---
LV Name /dev/vg1/dan
VG Name vg1
LV UUID tWW2Vp-4QEh-QY79-3GAp-uY9x-zi0y-pJs6JB
LV Write Access read/write
LV Status available
# open 0
LVSize 10.01 GB
Current LE 2563
Segments 3
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 253:0
[root@localhost~]# resize2fs /dev/vg1/dan
[root@localhost~]# lvscan
ACTIVE '/dev/vg1/dan' [10.01 GB] inherit
虽然/dev/sdb1, /dev/sdb2, /dev/sdb6刚刚已被我制作成逻辑卷,但是他们在系统的id标识还没有改变,这个变更分区类型是需要手动操作的,LVM卷id标识是8e
Command (m forhelp): t
Partition number(1-6): 1
Hex code (type Lto list codes): 8e
Changed systemtype of partition 1 to 8e (Linux LVM)
Command (m forhelp): t
Partition number(1-6): 2
Hex code (type Lto list codes): 8e
Changed systemtype of partition 2 to 8e (Linux LVM)
Command (m forhelp): t
Partition number(1-6): 6
Hex code (type Lto list codes): 8e
Changed systemtype of partition 6 to 8e (Linux LVM)
Command (m forhelp): w #保存刚刚的操作并退出
The partitiontable 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 stilluses the old table.
The new tablewill be used at the next reboot.
Syncing disks.
验证刚刚的操作
[root@localhost~]# fdisk -l
Disk /dev/sda:21.4 GB, 21474836480 bytes
255 heads, 63sectors/track, 2610 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sda1 * 1 38 305203+ 83 Linux
/dev/sda2 39 2349 18563107+ 83 Linux
/dev/sda3 2350 2610 2096482+ 82 Linux swap / Solaris
Disk /dev/sdb:21.4 GB, 21474836480 bytes
255 heads, 63sectors/track, 2610 cylinders
Units =cylinders of 16065 * 512 = 8225280 bytes
Device Boot Start End Blocks Id System
/dev/sdb1 1 609 4891761 8e Linux LVM
/dev/sdb2 610 1218 4891792+ 8e Linux LVM
/dev/sdb3 1219 2610 11181240 5 Extended
/dev/sdb5 1219 1462 1959898+ 82 Linux swap / Solaris
/dev/sdb6 1463 2610 9221278+ 8e Linux LVM
[root@localhost~]# fdisk -l|grep "LVM"
/dev/sdb1 1 609 4891761 8e Linux LVM
/dev/sdb2 610 1218 4891792+ 8e Linux LVM
/dev/sdb6 1463 2610 9221278+ 8e Linux LVM
格式化LVM卷,格式化之后才能用。
[root@localhost~]# mkfs.ext3 /dev/vg1/dan #等价于mkfs -t ext3 /dev/vg1/dan
[root@localhost~]# mkdir /mail
[root@localhost~]# mount /dev/vg1/dan /mail/
[root@localhost~]# df -hT /mail/
Filesystem Type Size Used Avail Use% Mounted on
/dev/mapper/vg1-dan
ext3 9.9G 151M 9.3G 2% /mail
[root@localhost ~]# partprobe /dev/sdb
[root@localhost ~]# resize2fs /dev/vg1/dan
本文出自 “勇敢的心” 博客,谢绝转载!