linux管理储存设备和磁盘分区
管理系统存储
一、查看系统中的设备1、发现系统中的设备
fdisk -l 查看系统中真实存在的设备
cat /proc/partitions 查看系统中被系统识别的设备
2、blkid 系统发现的,并系统利用的,有id信息的设备
3、发现并且在挂载的设备
df -H
/dev/sda1 sata硬盘,或者iscsi网络存储(v表示虚拟硬盘,h表示ide硬盘,一般出现在老式电脑中)a1表示第一块硬盘中的第一个分区
/dev/cdrom 光驱
/dev/mapper/* 系统中的虚拟设备
三、设备的使用
设备必须要用目录来对设备中的内容进行读取
所以设备在使用时需要作挂在动作
1,挂载设备
blkid 识别可用设备
mount /dev/vdb1 /mnt 把系统中的第二块硬盘下的第一个分区挂载到mnt下
mount -o ro /dev/vdb1 /mnt/只读挂载第二块硬盘下的第一个分区到/mnt下
mount -o remount,rw /mnt 在设备被使用时梗概设备的挂载参数为rw
umount 设备(挂载点) umount /dev/vdb1(/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))
一般原因是因为当前路径在此挂载点,或者此目录下有进程
四、分区管理
1、mbr主引导记录446个字节
2、mtp主分区表64个字节
3、硬盘的有效性标示“55aa”占用2字节
4、一个主分区占用16个字节记录分区信息
5、一块硬盘上如果用mbr的分区方式最多可以存在4个主分区
6、主分区 主分区表记录主分区
7、拓展分区 主分区表记录拓展分区
8、逻辑分区 拓展分区记录逻辑分区
分区的划分
fdisk -l 查找当前可用于分区的设备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 修改分区id
u change display/entry units
v verify the partition table
w write table to disk and exit 将当前操作写入硬盘分区(如不w保存,则之前分区操作不被记录)
x extra functionality (experts only)
Command (m for help): n 新建分区
p primary (0 primary, 0 extended, 4 free) 分区类型 p主分区
e extended e 扩展分区
Select (default p): p 选择主分区
Partition number (1-4, default 1):1 选择主分区编号
First sector (2048-20971519, default 2048):设置起始扇区 默认2048开始
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519):设置终止扇区
Partition 1 of type Linux and of size 100 MiB is set 建立成功
Command (m for help): wq 保存推出,如果安q表示放弃更改退出
[[email protected] mnt]# partprobe 同步分区表
cat /proc/partitions 查看系统识别的分区信息
mkfs.xfs /dev/vdb2 格式化
mount /dev/vdb2 /mnt 临时挂载
vim /etc/fstab 永久挂载
/dev/vdb2 /mnt xfs defaults 0 0
mount -a 使/etc/fstab中记录的挂载策略生效
设定分区方式为gpt
GNU Parted 3.1
Using /dev/vdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) mklabel
New disk label type? gpt
Warning: The existing disk label on /dev/vdb will be destroyed and all data on this disk will be lost. Do you
want to continue?
Yes/No? y
(parted) quit
mkfs.xfs /dev/vdb 格式化挂载文件格式化后重新分区fdisk /dev/vdb看效果
swap分区
swapon -s 查看有没有syapon分区
[[email protected] ~]# fdisk /dev/vdb 新建分区,并修改分区ip为swapon分区id
Changes will remain in memory only, until you decide to write them.
Be careful before using the write command.
Device does not contain a recognized partition table
Building a new DOS disklabel with disk identifier 0x6eadbf6f.
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):
First sector (2048-20971519, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-20971519, default 20971519): +1000M
Partition 1 of type Linux and of size 1000 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: 0x6eadbf6f
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2050047 1024000 83 Linux
Command (m for help): t
Selected partition 1
Hex code (type L to list all codes): l
0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
1 FAT12 27 Hidden NTFS Win 82 Linux swap / So c1 DRDOS/sec (FAT-
2 XENIX root 39 Plan 9 83 Linux c4 DRDOS/sec (FAT-
3 XENIX usr 3c PartitionMagic 84 OS/2 hidden C: c6 DRDOS/sec (FAT-
4 FAT16 <32M 40 Venix 80286 85 Linux extended c7 Syrinx
5 Extended 41 PPC PReP Boot 86 NTFS volume set da Non-FS data
6 FAT16 42 SFS 87 NTFS volume set db CP/M / CTOS / .
7 HPFS/NTFS/exFAT 4d QNX4.x 88 Linux plaintext de Dell Utility
8 AIX 4e QNX4.x 2nd part 8e Linux LVM df BootIt
9 AIX bootable 4f QNX4.x 3rd part 93 Amoeba e1 DOS access
a OS/2 Boot Manag 50 OnTrack DM 94 Amoeba BBT e3 DOS R/O
b W95 FAT32 51 OnTrack DM6 Aux 9f BSD/OS e4 SpeedStor
c W95 FAT32 (LBA) 52 CP/M a0 IBM Thinkpad hi eb BeOS fs
e W95 FAT16 (LBA) 53 OnTrack DM6 Aux a5 FreeBSD ee GPT
f W95 Ext'd (LBA) 54 OnTrackDM6 a6 OpenBSD ef EFI (FAT-12/16/
10 OPUS 55 EZ-Drive a7 NeXTSTEP f0 Linux/PA-RISC b
11 Hidden FAT12 56 Golden Bow a8 Darwin UFS f1 SpeedStor
12 Compaq diagnost 5c Priam Edisk a9 NetBSD f4 SpeedStor
14 Hidden FAT16 <3 61 SpeedStor ab Darwin boot f2 DOS secondary
16 Hidden FAT16 63 GNU HURD or Sys af HFS / HFS+ fb VMware VMFS
17 Hidden HPFS/NTF 64 Novell Netware b7 BSDI fs fc VMware VMKCORE
18 AST SmartSleep 65 Novell Netware b8 BSDI swap fd Linux raid auto
1b Hidden W95 FAT3 70 DiskSecure Mult bb Boot Wizard hid fe LANstep
1c Hidden W95 FAT3 75 PC/IX be Solaris boot ff BBT
1e Hidden W95 FAT1 80 Old Minix
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: 0x6eadbf6f
Device Boot Start End Blocks Id System
/dev/vdb1 2048 2050047 1024000 82 Linux swap / Solaris
Command (m for help): wq
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[[email protected] ~]# mkswap /dev/vdb2 将/dev/vdb2分区设置为linux的交换去
Setting up swapspace version 1, size = 1023996 KiB
no label, UUID=28c3c77b-91f3-4098-9736-2d694a870c22
[[email protected] ~]# swapon -a /dev/vdb1 启动/dev/vdb2分区swap装置[[email protected] ~]# swapon -s 显示swap分区
Filename Type Size Used Priority
/dev/vdb2 partition 1023996 0 -1
[[email protected] ~]# vim /etc/fstab 永久修改
/dev/vdb2 swap swap defaults 0 0
删除swap分区
swapoff /dev/vdb2
删除配置文件里边的配置
分区配额
通过mount查看是否开启配额(noquota表示没有开启,usrquota表示开启配额)如果显示没有开启配额需要卸载掉重新挂载,并在挂载的时候开启配额
mount -o usrquota /dev/vdb2 /mnt/ 挂载并开启配额
Disk quotas for user student (uid 1000):
Filesystem blocks soft hard inodes soft hard
/dev/vdb2 0 0 20480 0 0 0
此修改为临时修改,如果永久设定需要修改 /etc/fstab文件
切换到student用户测试,在测试前先给/mnt目录权限给到777
[[email protected] ~]$ dd if=/dev/zero of=/mnt/studentfile bs=1M count=30
21+0 records in
20+0 records out
20971520 bytes (21 MB) copied, 0.00945847 s, 2.2 GB/s
磁盘加密
1磁盘加密fdisk /dev/vdb 新建一个磁盘
cryptsetup luksFormat /dev/vdb1 给这个磁盘设置磁盘加密,并设置密码
cryptsetup open /dev/vdb1 westos 将这个加密磁盘打开
mkfs.xfs /dev/mapper/westos 格式化磁盘
mount /dev/mapper/westos /mnt/ 将该磁盘挂载
cryptsetup close westos 关闭加密磁盘
[[email protected] ~]# cryptsetup luksFormat /dev/vdb1
WARNING!
========
This will overwrite data on /dev/vdb1 irrevocably.
Are you sure? (Type uppercase yes): YES
Enter passphrase:
Verify passphrase:
[[email protected] ~]# cryptsetup open /dev/vdb1 westos
Enter passphrase for /dev/vdb1:
[[email protected] ~]# ll /dev/mapper/westos
lrwxrwxrwx. 1 root root 7 Apr 16 22:44 /dev/mapper/westos -> ../dm-0
[[email protected] ~]# mkfs.xfs /dev/mapper/westos
meta-data=/dev/mapper/westos isize=256 agcount=4, agsize=31872 blks
= sectsz=512 attr=2, projid32bit=1
= crc=0
data = bsize=4096 blocks=127488, imaxpct=25
= sunit=0 swidth=0 blks
naming =version 2 bsize=4096 ascii-ci=0 ftype=0
log =internal log bsize=4096 blocks=853, version=2
= sectsz=512 sunit=0 blks, lazy-count=1
realtime =none extsz=4096 blocks=0, rtextents=0
[[email protected] ~]# mount /dev/mapper/westos /mnt/
[[email protected] ~]# df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/vda1 10473900 3703460 6770440 36% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 84 484848 1% /dev/shm
tmpfs 484932 12800 472132 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/westos 506540 25656 480884 6% /mnt
[[email protected] ~]# ls /mnt/
[[email protected] ~]# touch /mnt/file{1..4}
[[email protected] ~]# ls /mnt/
file1 file2 file3 file4
[[email protected] ~]# umount /mnt/
[[email protected] ~]# ls /mnt/
[[email protected] ~]# cryptsetup open /dev/vdb1 redhat
Enter passphrase for /dev/vdb1:
[[email protected] ~]# mount /dev/mapper/redhat /mnt/
[[email protected] ~]# ls /mnt/
file1 file2 file3 file4
[[email protected] ~]# umount /mnt/
[[email protected] ~]# cryptsetup close westos
Device westos is not active.
2、加密磁盘开机自动挂载
[[email protected] ~]# vim /root/diskpass 写入磁盘密码
[[email protected] ~]# chmod 600 /root/diskpass 将磁盘权限设置成只对超级用户可操作
[[email protected] ~]# vim /etc/crypttab 写入磁盘名称,磁盘设备,以及磁盘密码所在文件
[[email protected] ~]# vim /etc/fstab 修改配置文件
[[email protected] ~]# cryptsetup luksAddKey /dev/vdb1 /root/diskpass 告诉系统加密磁盘的密码指定
Enter any passphrase:
[[email protected] ~]# df
/dev/vda1 10473900 3704528 6769372 36% /
devtmpfs 469344 0 469344 0% /dev
tmpfs 484932 80 484852 1% /dev/shm
tmpfs 484932 12760 472172 3% /run
tmpfs 484932 0 484932 0% /sys/fs/cgroup
/dev/mapper/westos 506540 25656 480884 6% /mnt
3加密磁盘开机自动挂载的清除
[[email protected] ~]# vim /etc/fstab 删除掉配置文件里面的设定
[[email protected] ~]# umount /mnt/ 卸载挂载
[[email protected] ~]# vim /etc/crypttab 删除里面的内容指定
[[email protected] ~]# rm -fr /root/diskpass 删除密码文件
[[email protected] ~]# cryptsetup close westos 关闭加密磁盘
[[email protected] ~]# mkfs.xfs /dev/vdb1 格式化加密磁盘
mkfs.xfs: /dev/vdb1 appears to contain an existing filesystem (crypto_LUKS).
mkfs.xfs: Use the -f option to force overwrite.
[[email protected] ~]# mkfs.xfs /dev/vdb1 -f 加密此番格式化需要加-
磁盘阵列
watch -n 1 cat /proc/mdstat 监控命令
cat /proc/mdstat 查看raid设备状态
建立三个分区[[email protected] ~]# fdisk /dev/vdb,修改id为Linux raid autodetect模式
创建模式
选项-C
-l 级别
-n 运行设备个数
-a (yes/no)自动为其创建设备文件
-c 指定数据块大小
-x 指定空闲盘的个数,空闲盘能在工作盘损坏后自动顶替
2.管理raid磁盘
[[email protected] ~]# mdadm -C /dev/md0 -a yes -l 1 -n 2 -x 1 /dev/vdb{2,3,5} 建立/dev/md0设备
[[email protected] ~]# mdadm -D /dev/md0 查看指定raid设备信息
[[email protected] ~]# mkfs.xfs /dev/md0 格式化磁盘
[[email protected] ~]# mount /dev/md0 /mnt/ 挂载 df查看是否挂载
[[email protected] ~]# mdadm /dev/md0 -f /dev/vdb2 强制破坏vdb2分区
[[email protected] ~]# mdadm /dev/md0 -r /dev/vdb2 删除分区
[[email protected] ~]# mdadm /dev/md0 -a /dev/vdb2 添加新的分区
[[email protected] ~]# mdadm -S /dev/md0 停止阵列
卸载阵列
[[email protected] ~]# umount /mnt/ 卸载挂载
[[email protected] ~]# mdadm -S /dev/md0 停止阵列
[[email protected] ~]# fdisk /dev/vdb 删除分区
[[email protected] ~]# partprobe 重读分区表
[[email protected] ~]# reboot 重起虚拟机