08-磁盘操作

来源:互联网 发布:2017淘宝剁手排名 编辑:程序博客网 时间:2024/06/05 15:56

八、磁盘操作

磁盘分区

1、磁盘分区

1.1磁盘分区参数

fdisk /dev/vdb
m —help帮助
d —删除分区
n —添加分区
l —列出支持分区类型
t —指定分区类型
q —quit退出
w —保存退出
partx -x /dev/vdb —刷新分区表
partprobe /dev/vdb —刷新分区表
ls /dev/vdc* —查看分区文件
mkfs.ext4 /dev/vdb1 —格式化分区
mount /dev/vdb1 mnt —临时挂载

1.2磁盘用就挂载
1.2.1使用盘符挂载

vim /etc/fstab —永久挂载
/dev/vdb1 /mnt ext4 defaults 0 0
物理设备 挂载点 文件类型 挂载选项 (是1否0支持备份)(是否支持磁盘检测,0-根分区检测,2-非根分区检测,0-不支持)
mount -a —磁盘即刻生效

1.2.2使用UUID挂载

blkid /dev/vdb1 —查看UUID
vim /etc/fstab
UUID=”22978908-94cf-40bd-93f3-53e3741a6592” /mnt ext4 defaults 0 0

1.2.3使用label标签

e2label /dev/sdb1 mai —写标签(ext4文件类型)
xfs_admin -L mai /dev/sdb1 —(xfs文件类型)
vim /etc/fstab —编辑
LABEL=mai /mnt ext4 defaults 0 0

1.2.4自动挂载

rpm -q autofs
yum -y install autofs —安装自动挂载软件
rpm -ql autofs —查看所属文件
vim /etc/auto.master —主配置文件
/misc /etc/auto.misc –timeout=60 (在60秒内不再使用挂载内容,将
自动卸载)
挂载目录 指定自动挂载选择的扩展配置文件 挂载超时
vim /etc/auto.misc —扩展配置文件
#boot -fstype=ext4 :/dev/sbd1 —本地磁盘挂载
#linux -ro,soft,intr url:/dir —远程磁盘挂载

eg:
1.主配置文件修改
vim /etc/auto.master
/mai /etc/auto.mai –timeout=5
2.子配置文件修改
cp /etc/auto.misc /etc/auto.mai
vim /et/auto.mai
/dir fstype=ext4 :/dev/sdb1
3.启动autofs
servicer autofs restart —rhel6
systemctl restart sutofs.service —rhel7
4.测试
cd /mai/dir —触发挂载点
df -h —查看是否被挂载

1.2.5卸载

1.删除配置文件里面的配置
2.临时卸载
umount /dev/sdb1
umount /mnt

1.3详细磁盘分区实例
1.3.1磁盘分区

[root@#mai Desktop]# 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): 1
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1G
Partition 1 of type Linux and of size 1 GiB is set

Command (m for help): p

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: 0x5b910042

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux

Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): 83
Changed type of partition ‘Linux’ to ‘Linux’

Command (m for help): p

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: 0x5b910042

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux

Command (m for help): w
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
[root@#mai Desktop]# partx -a /dev/sdb1
[root@#mai Desktop]# fdisk -l |grep sd
[root@#mai Desktop]# mkfs.ext4 /dev/sdb1
[root@#mai Desktop]# mount /dev/sdb1 /mnt

1.3.2扩容虚拟分区

[root@#mai Desktop]# 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 (1 primary, 0 extended, 3 free)
e extended
Select (default p): p
Partition number (2-4, default 2): 2
First sector (2099200-20971519, default 2099200):
Using default value 2099200
Last sector, +sectors or +size{K,M,G} (2099200-20971519, default 20971519): +1G
Partition 2 of type Linux and of size 1 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 2
Hex code (type L to list all codes): 82
Changed type of partition ‘Linux’ to ‘Linux swap / Solaris’

Command (m for help): p

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: 0x5b910042

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 82 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@#mai Desktop]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-2
[root@#mai Desktop]# mkswap /dev/sdb2
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=f78f1247-b1b6-4561-9d96-380364096389
[root@#mai Desktop]# swapon /dev/sdb2
[root@#mai Desktop]#vim /etc/fstab
/dev/vdc2 swap swap defaults 0 0
[root@#mai Desktop]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
/dev/sdb2 partition 1048572 0 -2

2、逻辑卷

2.1卷简介

pv —物理卷
vg —卷组
lv —逻辑卷

2.2创建逻辑卷
2.2.1准备分区

[root@#mai Desktop]# 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
All primary partitions are in use
Adding logical partition 5
First sector (6295552-20971519, default 6295552):
Using default value 6295552
Last sector, +sectors or +size{K,M,G} (6295552-20971519, default 20971519): +1G
Partition 5 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (8394752-20971519, default 8394752):
Using default value 8394752
Last sector, +sectors or +size{K,M,G} (8394752-20971519, default 20971519): +1G
Partition 6 of type Linux and of size 1 GiB is set

Command (m for help): n
All primary partitions are in use
Adding logical partition 7
First sector (10493952-20971519, default 10493952):
Using default value 10493952
Last sector, +sectors or +size{K,M,G} (10493952-20971519, default 20971519): +1G
Partition 7 of type Linux and of size 1 GiB is set

Command (m for help): p

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: 0x5b910042

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 82 Linux swap / Solaris
/dev/sdb3 4196352 6293503 1048576 83 Linux
/dev/sdb4 6293504 20971519 7339008 5 Extended
/dev/sdb5 6295552 8392703 1048576 83 Linux
/dev/sdb6 8394752 10491903 1048576 83 Linux
/dev/sdb7 10493952 12591103 1048576 83 Linux

Command (m for help): t
Partition number (1-7, default 7): 7
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux’ to ‘Linux LVM’

Command (m for help): t
Partition number (1-7, default 7): 6
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux’ to ‘Linux LVM’

Command (m for help): t
Partition number (1-7, default 7): 5
Hex code (type L to list all codes): 8e
Changed type of partition ‘Linux’ to ‘Linux LVM’

Command (m for help): p

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: 0x5b910042

Device Boot Start End Blocks Id System
/dev/sdb1 2048 2099199 1048576 83 Linux
/dev/sdb2 2099200 4196351 1048576 82 Linux swap / Solaris
/dev/sdb3 4196352 6293503 1048576 83 Linux
/dev/sdb4 6293504 20971519 7339008 5 Extended
/dev/sdb5 6295552 8392703 1048576 8e Linux LVM
/dev/sdb6 8394752 10491903 1048576 8e Linux LVM
/dev/sdb7 10493952 12591103 1048576 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: 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.

2.2.2添加逻辑卷

[root@#mai Desktop]# partx -a /dev/sdb
partx: /dev/sdb: error adding partitions 1-7
[root@#mai Desktop]# pvcreate /dev/sdb[5-7]
Physical volume “/dev/sdb5” successfully created
Physical volume “/dev/sdb6” successfully created
Physical volume “/dev/sdb7” successfully created
[root@#mai Desktop]# pvs
PV VG Fmt Attr PSize PFree
/dev/sdb5 lvm2 — 1.00g 1.00g
/dev/sdb6 lvm2 — 1.00g 1.00g
/dev/sdb7 lvm2 — 1.00g 1.00g

2.2.3创建卷组

[root@#mai Desktop]# vgcreate vgmai /dev/sdb[5-7]
Volume group “vgmai” successfully created
[root@#mai Desktop]# vgs
VG #PV #LV #SN Attr VSize VFree
vgmai 3 0 0 wz–n- 2.99g 2.99g

2.2.4创建逻辑卷

[root@#mai Desktop]# lvcreate -L 1200M vgmai -n lvmai
Logical volume “lvmai” created.
[root@#mai Desktop]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvmai vgmai -wi-a—– 1.17g

2.2.5使用逻辑卷

[root@#mai Desktop]# mkfs.ext4 /dev/vgmai/lvmai
[root@#mai Desktop]# mount /dev/vgmai/lvmai /mnt
[root@#mai Desktop]# df -h
/dev/mapper/vgmai-lvmai 1.2G 3.6M 1.1G 1% /mnt

2.2.6强大的拷贝工具

dd —拷贝工具
if=infile —输入文件(源)
of=outfile —输出文件(目)
bs=block —块大小
count —次数
eg:
dd if=/dev/vdb of=/mnt/file1 bs=1M conut=100 —拷贝100M文件

3.空间扩缩
3.1在线扩容

[root@#mai Desktop]# pvcreate /dev/sdb8
Physical volume “/dev/sdb8” successfully created
[root@#mai Desktop]# vgextend vgmai /dev/sdb8
Volume group “vgmai” successfully extended
[root@#mai Desktop]# lvextend /dev/vgmai/lvmai -L +1G
Size of logical volume vgmai/lvmai changed from 1.17 GiB (300 extents) to 2.17 GiB (556 extents).
Logical volume lvmai successfully resized.
[root@#mai Desktop]# resize2fs /dev/vgmai/lvmai
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/vgmai/lvmai is mounted on /mnt; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/vgmai/lvmai is now 569344 blocks long.
[root@#mai Desktop]# df -h
/dev/mapper/vgmai-lvmai 2.2G 3.6M 2.0G 1% /mnt

3.2离线减容

[root@#mai Desktop]# e2fsck /dev/vgmai/lvmai
e2fsck 1.42.9 (28-Dec-2013)
/dev/vgmai/lvmai: clean, 11/138240 files, 17780/569344 blocks
[root@#mai Desktop]# resize2fs /dev/vgmai/lvmai 800M
resize2fs 1.42.9 (28-Dec-2013)
Please run ‘e2fsck -f /dev/vgmai/lvmai’ first.
[root@#mai Desktop]# lvreduce -L 800M /dev/vgmai/lvmai
WARNING: Reducing active logical volume to 800.00 MiB
THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce lvmai? [y/n]: y
Size of logical volume vgmai/lvmai changed from 2.17 GiB (556 extents) to 800.00 MiB (200 extents).
Logical volume lvmai successfully resized.
[root@#mai Desktop]# lvs
LV VG Attr LSize Pool Origin Data% Meta% Move Log Cpy%Sync Convert
lvmai vgmai -wi-a—– 800.00m

4、磁盘配额

4.1磁盘配额步骤
4.1.1指定磁盘挂在目录

[root@#mai ~]# mkdir /dir
[root@#mai ~]# mkfs.ext4 /dev/sdb3
[root@#mai ~]# mount /dev/sdb3 /dir

4.1.2让磁盘支持磁盘配额

[root@#mai ~]# mount -o remount,usrquota,grpquota /dir
[root@#mai ~]# mount
/dev/sdb3 on /dir type ext4 (rw,relatime,seclabel,quota,usrquota,grpquota,data=ordered)

4.1.3生成磁盘配额数据库

[root@#mai ~]# setenforce 0
[root@#mai ~]# quotacheck -ugcvm /dir
[root@#mai ~]# ls /dir
aquota.group aquota.user lost+found

quotacheck
-u —userquota
-g —grpquota
-c —create
-v —详情
-m —no-remount

4.1.4设置配额

[root@#mai ~]# edquota -u mai
Disk quotas for user mai (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/sdb3 0 9 8 0 4 2

/dev/sdb3 —磁盘
blocks —当前磁盘容量使用情况
soft —容量软限制
hard —容量硬限制
inodes —当前磁盘文件使用
soft —文件个数的软件限制
hard —文件个数的硬限制

[root@#mai dir]# edquota -g maiadmin —组磁盘配额

4.1.5激活磁盘配置

[root@#mai ~]# quotaon /dir

4.1.6查看报告

[root@#mai ~]# repquota -v /dir/
* Report for user quotas on device /dev/sdb3
Block grace time: 7days; Inode grace time: 7days
Block limits File limits

User used soft hard grace used soft hard grace

root – 20 0 0 2 0 0
mai – 0 9 8 0 4 2

Statistics:
Total blocks: 7
Data blocks: 1
Entries: 2
Used average: 2.000000

4.1.7测试

[root@#mai dir]# chmod 777 /dir
[root@#mai dir]# su mai
[mai@#mai dir]$ dd if=/dev/zero of=file2 bs=1M count=6
dd: error writing ‘file2’: Disk quota exceeded
1+0 records in
0+0 records out
8192 bytes (8.2 kB) copied, 0.0449205 s, 182 kB/s
[mai@#mai dir]$

0 0
原创粉丝点击