1.本地系统设备的识别
fdisk -l ##真实存在的设备
cat /proc/partition ##系统识别的设备
blkid ##系统可使用的设备
df ##系统正在挂载的设备
2.设备的挂载和卸载
1.设备名称
/dev/xdx ##/dev/hd0 /dev/hd1 /dev/sda /dev/sdb /dev/sda1 /dev/sdb1
/dev/sr0 ##光驱
/dev/mapper/* ##虚拟设备
硬件 | 设备文件名 |
---|---|
IDE硬盘 | /dev/hd[a-d] |
SCSI/SATA/USB硬盘 | /dev/sd[a-p] |
U盘 | /dev/sd[a-p](与SATA相同) |
软驱 | /dev/fd[0-1] |
打印机25针 | /dev/lp[0-2] ;USB:/dev/usb/lp[0-15] |
鼠标USB | /dev/usb/mouse[0-15] ;PS2:/dev/psaux |
当前CD ROM/DVD ROM | /dev/cdrom |
当前鼠标 | /dev/mouse |
磁带机IDE | /dev/ht0 ;SCSI:/dev/st0 |
2.设备的挂载
mount 设备 挂载点
mount /dev/sdb1 /mnt ##挂载/dev/sdb1到/mnt
umount /dev/sdb1|/mnt ##卸载
mount -o ro /dev/sdb1 /mnt ##只读挂载
mount ##查看挂载信息
mount -o remount,rw /dev/sdb1|/mnt ##重新读写挂载
3.解决设备正忙情况
[root@tomcat-mh ~]# umount /mnt/
umount: /mnt: target is busy.
(In some cases useful info about processes that use
the device is found by lsof(8) or fuser(1))
[root@tomcat-mh ~]#
解决方法1
fuser -kvm /mnt/
umount /mnt/
解决方法2
lsof /dev/sdb1
kill -9 进程ID
umount /mnt/
3.磁盘分区
1.硬盘0磁盘1扇区的512个字节中记录的信息如下
512= 446 + 64 + 2
^ ^ ^
mbr(主引导记录) mpt(主分区表) 55aa(硬盘的有效性标识)
注意:
硬盘分区表
64字节
1个分区占用16个字节
1块硬盘上最多可以划分4个主分区
2.分区步骤
1.fdisk -l
2.fdisk /dev/vdb
[root@120 ~]# fdisk /dev/vdb
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): m
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
g create a new empty GPT partition table
G create an IRIX (SGI) partition table
l list known partition types
m print this menu
n add a new partition
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 functionality (experts only)
Command (m for help):
建立主分区
[root@170 ~]# fdisk /dev/vdb
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): ##ID默认
First sector (2048-20971519, default 2048): ##分区起始
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +100M
##分区结束或分区指定大小
Partition 1 of type Linux and of size 100 MiB is set
Command (m for help): p
Disk /dev/vdb: 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: 0xd3d1072e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
Command (m for help): wq
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 (616448-20971519, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-20971519, default 20971519): ##大小默认剩余全部
Using default value 20971519
Partition 4 of type Extended and of size 9.7 GiB is set
Command (m for help): p ##显示分区信息
Disk /dev/vdb: 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: 0xd3d1072e
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@170 ~]# fdisk /dev/vdb
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 (618496-20971519, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-20971519, default 20971519): +500M
Partition 5 of type Linux and of size 500 MiB is set
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (1644544-20971519, default 1644544):
Using default value 1644544
Last sector, +sectors or +size{K,M,G} (1644544-20971519, default 20971519): +500M
Partition 6 of type Linux and of size 500 MiB is set
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@170 ~]# fdisk -l
Disk /dev/vda: 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: 0x00013f3e
Device Boot Start End Blocks Id System
/dev/vda1 * 2048 20970332 10484142+ 83 Linux
Disk /dev/vdb: 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: 0x1211e740
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
/dev/vdb5 618496 1642495 512000 83 Linux
/dev/vdb6 1644544 2668543 512000 83 Linux
[root@170 ~]#
4.给设备安装文件系统
文件系统 | 支持系统版本 | 最大支持的文件系统 | 最大支持的文件 |
---|---|---|---|
ext3 | rhel5及以前的版本 | 2TB | 16GB |
ext4 | rhel6 | 1EB | 16TB |
xfs | rhel7 | 18EB | 9EB |
XFS 能以接近裸设备I/O的性能存储数据。在单个文件系统的测试中,其吞吐量最高可达7GB每秒,对单个文件的读写操作,其吞吐量可达4GB每秒。
mkfs.xfs /dev/vdb1 ##格式化,也就是给硬盘装文件系统
mount /dev/vdb1 /mnt/
设定系统启动硬盘自动挂载:
vim /etc/fstab
设备 挂载点 文件系统 挂载参数 是否备份 是否检测
/dev/vdb1 /mnt xfs defaults 0 0
mount -a ##查看文件书写是否有错误
这样这个新建立的文件系统在开机启动或者重启时就不需要手动挂载了,文件系统会自动挂载。
5.swap分区管理
1)swap分区建立
##划分分区并设定分区标签为82
Command (m for help): n
All primary partitions are in use
Adding logical partition 6
First sector (825344-20971519, default 825344):
Using default value 825344
Last sector, +sectors or +size{K,M,G} (825344-20971519, default 20971519):
Using default value 20971519
Partition 6 of type Linux and of size 9.6 GiB is set
Command (m for help): t
Partition number (1-6, default 6): 6
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
例子:
[root@170 ~]# fdisk /dev/vdb
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 7
First sector (2670592-20971519, default 2670592):
Using default value 2670592
Last sector, +sectors or +size{K,M,G} (2670592-20971519, default 20971519): +1G
Partition 7 of type Linux and of size 1 GiB is set
Command (m for help): n
All primary partitions are in use
Adding logical partition 8
First sector (4769792-20971519, default 4769792):
Using default value 4769792
Last sector, +sectors or +size{K,M,G} (4769792-20971519, default 20971519): +1G
Partition 8 of type Linux and of size 1 GiB is set
Command (m for help): t
Partition number (1-8, default 8): 7
Hex code (type L to list all codes): 82
Changed type of partition 'Linux' to 'Linux swap / Solaris'
Command (m for help): t
Partition number (1-8, default 8): 8
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/vdb: 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: 0x1211e740
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
/dev/vdb5 618496 1642495 512000 83 Linux
/dev/vdb6 1644544 2668543 512000 83 Linux
/dev/vdb7 2670592 4767743 1048576 82 Linux swap / Solaris
/dev/vdb8 4769792 6866943 1048576 82 Linux swap / Solaris
Command (m for help): wq
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@170 ~]# partprobe
[root@170 ~]# mkswap /dev/vdb7
mkswap: /dev/vdb7: warning: wiping old swap signature.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=113ba5b4-293b-4374-8405-c88b86fb6cec
[root@170 ~]# mkswap /dev/vdb8
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=b2e2517d-1574-4582-8d74-1aed3a582d50
[root@170 ~]# swapon -a /dev/vdb7
[root@170 ~]# swapon -a /dev/vdb8
[root@170 ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb7 partition 1048572 0 -1
/dev/vdb8 partition 1048572 0 -2
[root@170 ~]#
mkswap /dev/vdb6 ##格式化swap分区
swapon -a /dev/vdb6 ##设定swap分区
swapon -s ##查看swap分区
永久设定swap分区
vim /etc/fstab
/dev/vdb7 swap swap defaults 0 0
mount -a
2)swap分区删除
vim /etc/fstab ##清空需要删除的swap分区信息
swapoff /dev/vdb6 ##取消swap分区vdb6
swapon -s ##查看swap分区
fdisk /dev/vdb ##删除vdb分区
partprobe ##更新分区
例子:
[root@170 ~]# vim /etc/fstab
[root@170 ~]# cat /etc/fstab
#
# /etc/fstab
# Created by anaconda on Wed May 7 01:22:57 2014
#
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
#
UUID=9bf6b9f7-92ad-441b-848e-0257cbb883d1 / xfs defaults 1 1
[root@170 ~]# swapon -s
Filename Type Size Used Priority
/dev/vdb7 partition 1048572 0 -1
/dev/vdb8 partition 1048572 0 -2
[root@170 ~]# swapoff /dev/vdb7
[root@170 ~]# swapoff /dev/vdb8
[root@170 ~]# swapon -s
[root@170 ~]# fdisk /dev/vdb
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): d
Partition number (1-8, default 8): 8
Partition 8 is deleted
Command (m for help): d
Partition number (1-7, default 7): 7
Partition 7 is deleted
Command (m for help): p
Disk /dev/vdb: 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: 0x1211e740
Device Boot Start End Blocks Id System
/dev/vdb1 2048 206847 102400 83 Linux
/dev/vdb2 206848 411647 102400 83 Linux
/dev/vdb3 411648 616447 102400 83 Linux
/dev/vdb4 616448 20971519 10177536 5 Extended
/dev/vdb5 618496 1642495 512000 83 Linux
/dev/vdb6 1644544 2668543 512000 83 Linux
Command (m for help): wq
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@170 ~]# partprobe
[root@170 ~]#
6.配额
首先建立一个共享目录
[root@170 /]# mkdir /pub
把挂载信息写入/etc/fstab 文件中,设置永久挂载。
vim /etc/fstab
/dev/vdb7 /pub xfs defaults,usrquota 0 0
配额是针对分区的
mount -o usrquota /dev/vdb7 /pub ##挂载vd5时开启用户配额
chmod 777 /pub ##设置共享目录权限
edquota -u xiaoma /dev/vdb7 ##给xiaoma用户在vdb7硬盘开启配额文件,文件里面的默认单位是kb。
Disk quotas for user xiaoma (uid 1001):
Filesystem blocks soft hard inodes soft hard
/dev/vdb7 20480 0 20480 1 0 0
限制配额分区 | 总文件大小 | 额度软限 | 额度硬限 | 文件个数 | 文件个数软限 | 文件个数硬限 |
---|---|---|---|---|---|---|
Filesystem | blocks | soft | hard | inodes | soft | hard |
分析:所谓总文件大小就是这个用户在这个硬盘建立的所有文件实际大小,限制额度软线就是用户创建的文件大于这个设定值时会发出警告信息,额度硬线就是用户所能创建的文件最大大小。文件个数同理。
例子:
dd if=/dev/zero of=/pub/redhatfile bs=1M count=18
du -sh /pub/redhatfile
dd if=/dev/zero of=/pub/redhatfile bs=1M count=25
du -sh /pub/redhatfile
分析:用户写入硬盘的文件的总大小最多就是额度硬线的值。
7.磁盘加密
对硬盘进行加密,如果硬盘已经有文件系统会被覆盖和清除所有内容。
cryptsetup luksFormat /dev/vdb8 ##加密
cryptsetup open /dev/vdb8 xiaoma ##打开加密服务
mkfs.xfs /dev/mapper/xiaoma ##格式化文件系统
mount /dev/mapper/xiaoma /mnt/ ##挂载设备
touch /mnt/file{1..4} ##创建文件
umount /mnt/ ##卸载设备
cryptsetup close xiaoma ##关闭加密服务
分析:打开加密服务就会在/dev/mapper/提供一个可以挂载的接口,挂载后才可以正常的读写操作。使用后,卸载、关闭加密服务则会关闭这个接口。
有需求时
上面我们已经在打开、挂载后建立了四个文件。重新打开加密服务,挂载在其他位置,查看建立的文件。
cryptsetup open /dev/vdb8 xiaoma ##打开
mount /dev/mapper/xiaoma /mnt/ ##挂载设备
加密磁盘开机自动挂载
vim /etc/fstab
/dev/mapper/xiaoma /mnt xfs defaults 0 0
vim /etc/crypttab
xiaoma /dev/vdb8 /root/xiaomakey
vim /root/xiaomakey
westos123
cryptsetup luksAddkey /dev/vdb8 /root/xiaomakey