一、设备查看及使用
1、设备状态
1)发现系统中的设备
fdisk -l
cat /proc/partitions
![这里写图片描述]()
2)系统发现的,但是没有投入使用,随时可以使用的设备
blkid
![这里写图片描述]()
3)发现正在使用的设备
df #默认大小的单位是k
df -h #使用最合适的单位,此时1G=2^n
df -H #此时1G=10^n
![这里写图片描述]()
2、设备名称的读取
/dev/xd* 1. x=s时, /dev/sd* 2. x=v时, /dev/vd* 3. x=h时, /dev/hd* *=a~... /dev/sda1 /dev/vdrom /dev/sr[0-..] /dev/mapper/*
3、设备的使用
设备必须通过目录来对设备中的内容进行读取,所以设备在使用时需要进行挂载,挂载的具体方式及易出现的问题会在划分分区部分中详细写出。
二、磁盘分区管理
(一)主分区
1. 分区常用参数
fdisk /dev/vdb #给/dev/vdb分区 -m -d -n -p -q -t -w
2.划分分区
1)划分分区
[root@localhost ~]# fdisk /dev/vdb Command (m for help): n #n代表新建一个分区 Partition type: 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): #开始 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: 0x000018b5 Device Boot Start End Blocks Id System /dev/vdb1 2048 206847 102400 83 Linux Command (m for help): wq #保存退出,此时创建分区成功 The partition table has been altered!
2)格式化文件系统 此时虽然分区已经被划分好,但是从blkid命令可以看出刚才划分的分区并不可用。所以此时需要格式化文件系统。 文件系统是操作系统用于明确磁盘或分区上的文件的方法和数据结构,即在磁盘上组织文件的方法。文件系统由三部分组成:与文件管理有关的软件、被管理文件以及实施文件管理所需要的数据结构。 常用的文件系统类型有如下几种: - ext2:早期的linux中常用的文件系统 - ext3:ext2的升级版,带日志功能 - RAMFS:内存文件系统,速度很快 - NFS:网络文件系统,由SUN发明,主要用于远程文件共享 - VFAT:WINDOWS 95/98使用 - FAT:WINDOWS XP使用
[root@localhost ~] major minor 253 0 10485760 vda 253 1 10484142 vda1 253 16 10485760 vdb 253 17 102400 vdb1 [root@localhost ~] /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" [root@localhost ~] meta-data=/dev/vdb1 isize=256 agcount=4, agsize=6400 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=25600, 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 [root@localhost ~] /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="27e7ce82-f8d5-481c-ac43-7e081e9c8ed9" TYPE="xfs"
3)挂载
挂载及卸载流程 1. blkid 2. mount 设备 挂载点 3. umount 设备|挂载点 eg: [root@localhost ~] [root@localhost ~] Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3158600 7315300 31% / devtmpfs 493408 0 493408 0% /dev tmpfs 508996 80 508916 1% /dev/shm tmpfs 508996 13460 495536 3% /run tmpfs 508996 0 508996 0% /sys/fs/cgroup /dev/vdb1 98988 5280 93708 6% /mnt [root@localhost ~] mount -o ro 设备 挂载点 可执行 mount -o remount,rw 挂载点 eg: [root@localhost ~] [root@localhost ~] Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3158600 7315300 31% / devtmpfs 493408 0 493408 0% /dev tmpfs 508996 80 508916 1% /dev/shm tmpfs 508996 13460 495536 3% /run tmpfs 508996 0 508996 0% /sys/fs/cgroup /dev/vdb1 98988 160 98828 1% /mnt [root@localhost ~] [root@localhost mnt] touch: cannot touch ‘file’: Read-only file system [root@localhost mnt] [root@localhost mnt] [root@localhost mnt] file 设备的永久挂载 [root@localhost ~] /dev/vdb1 /mnt xfs defaults 0 0 设备名 挂载点 文件系统类型 挂载参数 是否备份 是否检测设备 [root@localhost ~] [root@localhost ~] Filesystem 1K-blocks Used Available Use% Mounted on /dev/vda1 10473900 3158688 7315212 31% / devtmpfs 493408 0 493408 0% /dev tmpfs 508996 80 508916 1% /dev/shm tmpfs 508996 13460 495536 3% /run tmpfs 508996 0 508996 0% /sys/fs/cgroup /dev/vdb1 98988 5280 93708 6% /mnt
易出现错误的地方: 当卸载设备时出现 target is busy,表示该设备正在被系统的某个程序使用 此时发现使用进程的方式是: lsof + 设备名 或者 fuser -vm 设备名 进程终止的方式为: kill -9 pid 或者 fuser -kvm 设备名
[root@localhost 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@localhost mnt] COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 1238 root cwd DIR 253,17 17 128 /mnt lsof 1933 root cwd DIR 253,17 17 128 /mnt lsof 1934 root cwd DIR 253,17 17 128 /mnt [root@localhost mnt] USER PID ACCESS COMMAND /dev/vdb1: root kernel mount /mnt root 1238 ..c.. bash Connection to 172.25.254.170 closed.
4)删除分区 先卸载掉正在使用的设备,然后再通过fdisk–>d 删除分区
(二)swap分区
swap分区是交换分区,在系统的物理内存不够用的时候,把物理内存的一部分空间释放出来,以供当前运行的程序使用。这些被释放的空间被临时保存到Swap分区中,等到那些程序要运行的时候,在从Swap分区中恢复保存的数据到内存中。所以swap分区系统自己使用并不需要挂载。
1.swap分区的查看
swap分区用命令swapon -s来查看
2.swap分区的建立
1)swap分区建立 先要划分一个新的分区,划分分区的方法和主分区的方法相同,不过多赘述。但是在分区建立了之后,把分区的id修改为swap分区。例子如下:
[root@localhost ~]# fdisk /dev/vdb Last sector, +sectors or +size (2048-20971519, default 20971519): +100M Partition 1 of type Linux and of size 100 MiB is set Command (m for help): t #修改新建分区的id Selected partition 1 Hex code (type L to list all codes): l #列出所有对应的id号 Hex code (type L to list all codes): 82 #swap分区对应的id号为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: 0x000018b5 Device Boot Start End Blocks Id System /dev/vdb1 2048 206847 102400 82 Linux swap / Solaris Command (m for help): wq The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
2)swap分区的格式化 swap分区的格式化一定要注意不能在使用中格式化。
[root@localhost ~] /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="27e7ce82-f8d5-481c-ac43-7e081e9c8ed9" TYPE="xfs" [root@localhost ~] mkswap: /dev/vdb1: warning: wiping old xfs signature. Setting up swapspace version 1, size = 102396 KiB no label, UUID=53cd92d6-4e25-4f14-b6c0-8bce82fecf77 [root@localhost ~] /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="53cd92d6-4e25-4f14-b6c0-8bce82fecf77" TYPE="swap"
3)激活swap设备
[root@localhost ~] [root@localhost ~] [root@localhost ~] Filename Type Size Used Priority /dev/vdb1 partition 102396 0 -1 要是想永久修改,和主分区一样,编写/etc/fstab文件,让此设备开机永久激活,但是挂载点和文件系统类型都必须写swap。
4)用文件来代替分区作为swap分区 mbr分区的方式最多只能有四个分区,当分区不足时,可以用文件来代替分区。具体操作方式如下:
[root@localhost ~] 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB) copied, 29.0294 s, 36.1 MB/s [root@localhost ~] Setting up swapspace version 1, size = 1023996 KiB no label, UUID=f5291743-a9cc-4ef1-ad83-c9403c2413e7 [root@localhost ~] swapon: /swapfile: insecure permissions 0644, 0600 suggested. [root@localhost ~] Filename Type Size Used Priority /dev/vdb1 partition 102396 0 -1 /swapfile file 1023996 0 -2
5)swap分区的删除 swap分区删除的正确顺序: - 删除fstab文件里面添加的配置 - swapoff /dev/vdb1 - 用fdisk 删除设备
(三)磁盘配额
- 磁盘配额是为使用用户分配额度
- 分区配额是针对设备的
1.激活设备配额参数
[root@localhost /] [root@localhost /] [root@localhost /] Disk quotas for user student (uid 1000): Filesystem blocks soft hard inodes soft hard /dev/vdb1 0 0 0 0 0 2
blocks表示可存在的文件大小,soft为软额度,hard为最大额度,inodes为可存在文件个数。
测试: 把文件个数修改为2
[student@localhost ~]$ cd /westos/ [student@localhost westos]$ ls [student@localhost westos]$ touch file [student@localhost westos]$ touch file{1..3} touch: cannot touch ‘file2’: Disk quota exceeded touch: cannot touch ‘file3’: Disk quota exceeded
2.开机自动激活
开机自动激活也是修改/etc/fstab文件,只不过挂载参数修改为defaults,usrquota,修改完成后保存即可。
(三)分区加密
加密原理:磁盘分区的加密主要是其中的文件的加密,因为在文件上面会用一层文件系统来对文件进行管理。如果说加密在文件系统上的话,破解加密算法之后,文件仍然读取的是明文,并不安全。所以加密系统应该直接对文件来进行加密,而不是对文件系统进行加密,对文件直接进行加密,那么即使加密算法被破解,别人读取出来的仍然是加密过的密文,文件并不会泄露。 使用的是LUKS加密系统
1. 创建分区
创建分区的方法参考主分区或者swap分区的创建。
2. 加密
[root@localhost ~] WARNING! ======== This will overwrite data on /dev/vdb1 irrevocably. Are you sure? (Type uppercase yes): YES Enter passphrase: Verify passphrase: [root@localhost ~] Enter passphrase for /dev/vdb1: [root@localhost ~] meta-data=/dev/mapper/westos isize=256 agcount=4, agsize=12672 blks = sectsz=512 attr=2, projid32bit=1 = crc=0 data = bsize=4096 blocks=50688, 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 [root@localhost ~] [root@localhost ~] [root@localhost ~] file1 file2 file3 [root@localhost ~] [root@localhost ~] [root@localhost ~] [root@localhost ~] 若是想查看加密分区的文件,只能再次打开加密,输入密码,进行挂载查看。
3.加密设备开机自动挂载
(1) 编写/etc/fstab文件 /dev/mapper/westos /mnt xfs defaults 0 0
该文件的参数介绍都在上文 (2) 编写/etc/crypttab westos /dev/vdb1 /root/passfile
编写这个文件是因为每次对加密分区进行操作的时候,需要打开加密,输入密码,该文件会把加密分区和设置的密码文件联系起来。 (3)编写密码文件/root/passfile 这个文件里面直接写加密分区的时候设置的密码即可。但要记得给这个文件权限,600即可。 chmod 600 /root/passfile
(4)设备文件与密码关联 cryptsetup luksAddKey /dev/vdb1 /root/passfile
4.加密清除
加密清除应注意操作顺序 - 先卸载/dev/mapper/westos - 关掉加密cryptsetup close westos - 对分区进行格式化mkfs.xfs /dev/vdb1 (-f) - 删除/etc/fstab文件添加的所有配置 - 删除/etc/crypttab文件的修改 - 删除/root/passfile密码文件
四、mbr分区和gpt分区的转换
需要转换的原因 传统的mbr最大支持2TB的单分区,最多只能设置4个主分区,并不能满足企业的大容量需求,而GPT支持2TB以上的单分区,理论上能设置128个主分区。所以有时候需要两个的互相转换
[root@localhost ~]# fdisk -l Disk label type: dos [root@localhost ~]# parted /dev/vdb 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? aix amiga bsd dvh gpt loop mac msdos pc98 sun 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? yes (parted) q Information: You may need to update /etc/fstab. [root@localhost ~]# fdisk -l Disk label type: gpt