linux磁盘设备分区--实践篇
2017-11-08 19:39
302 查看
linux磁盘设备分区–实践篇
一、设备查看及使用
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* #设备名称,d代表硬盘 1. x=s时, /dev/sd* #sata硬盘,或者iscsi网络存储 2. x=v时, /dev/vd* #虚拟硬盘,一般出现在虚拟机里面 3. x=h时, /dev/hd* #ide硬盘,一般出现在老式电脑中 *=a~... #eg:/dev/vda,为系统的第一块虚拟硬盘 /dev/sda1 #系统中第一个sata硬盘的第一个分区 /dev/vdrom /dev/sr[0-..] #系统中的光驱 /dev/mapper/* #系统中的虚拟设备
3、设备的使用
设备必须通过目录来对设备中的内容进行读取,所以设备在使用时需要进行挂载,挂载的具体方式及易出现的问题会在划分分区部分中详细写出。二、磁盘分区管理
(一)主分区
1. 分区常用参数
fdisk /dev/vdb #给/dev/vdb分区 -m #显示帮助 -d #删除分区 -n #添加分区 -p #现实分区表信息 -q #推出分区操作 -t #修改分区id -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 ~]# cat /proc/partitions #查看系统可以找到的分区 major minor #blocks name 253 0 10485760 vda 253 1 10484142 vda1 253 16 10485760 vdb 253 17 102400 vdb1 [root@localhost ~]# blkid #列出系统中可以使用的分区 /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" [root@localhost ~]# mkfs.xfs /dev/vdb1 #格式化新建的分区 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 ~]# blkid /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 ~]# mount /dev/vdb1 /mnt [root@localhost ~]# df 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 ~]# umount /mnt mount -o ro 设备 挂载点 #将设备挂载为只读模式,此时若是想写文件 可执行 mount -o remount,rw 挂载点 #对设备进行热参数更改,从只读模式变为读写模式 eg: [root@localhost ~]# mount -o ro /dev/vdb1 /mnt [root@localhost ~]# df 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 ~]# cd /mnt [root@localhost mnt]# touch file touch: cannot touch ‘file’: Read-only file system [root@localhost mnt]# mount -o remount, rw /mnt [root@localhost mnt]# touch file [root@localhost mnt]# ls file 设备的永久挂载 [root@localhost ~]# vim /etc/fstab /dev/vdb1 /mnt xfs defaults 0 0 设备名 挂载点 文件系统类型 挂载参数 是否备份 是否检测设备 [root@localhost ~]# mount -a #使fstab中未生效的策略生效 [root@localhost ~]# df 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 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]# lsof /dev/vdb1 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]# fuser -kvm /dev/vdb1 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{K,M,G} (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 ~]# blkid /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 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 ~]# blkid /dev/vda1: UUID="9bf6b9f7-92ad-441b-848e-0257cbb883d1" TYPE="xfs" /dev/vdb1: UUID="53cd92d6-4e25-4f14-b6c0-8bce82fecf77" TYPE="swap"
3)激活swap设备
[root@localhost ~]# swapon -s [root@localhost ~]# swapon -a /dev/vdb1 [root@localhost ~]# swapon -s Filename Type Size Used Priority /dev/vdb1 partition 102396 0 - db5c 1 要是想永久修改,和主分区一样,编写/etc/fstab文件,让此设备开机永久激活,但是挂载点和文件系统类型都必须写swap。
4)用文件来代替分区作为swap分区
mbr分区的方式最多只能有四个分区,当分区不足时,可以用文件来代替分区。具体操作方式如下:
[root@localhost ~]# dd if=/dev/zero of=/swapfile bs=1M count=1000 #dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换 #if=文件名,指定源文件 of=文件名,指定目的文件 #bs:同时设置读入/输出的块大小为多少字节 count:设置块的个数 1000+0 records in 1000+0 records out 1048576000 bytes (1.0 GB) copied, 29.0294 s, 36.1 MB/s [root@localhost ~]# mkswap /swapfile Setting up swapspace version 1, size = 1023996 KiB no label, UUID=f5291743-a9cc-4ef1-ad83-c9403c2413e7 [root@localhost ~]# swapon -a /swapfile swapon: /swapfile: insecure permissions 0644, 0600 suggested. [root@localhost ~]# swapon -s 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 /]# mount -o usrquota /dev/vdb1 /westos/ [root@localhost /]# chmod 777 /westos/ [root@localhost /]# edquota -u student /dev/vdb1 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} #可见student用户最多只能建立2个文件 touch: cannot touch ‘file2’: Disk quota exceeded touch: cannot touch ‘file3’: Disk quota exceeded #若修改的是可存在的文件大小的最大额度,可用dd命令来进行测试。
2.开机自动激活
开机自动激活也是修改/etc/fstab文件,只不过挂载参数修改为defaults,usrquota,修改完成后保存即可。(三)分区加密
加密原理:磁盘分区的加密主要是其中的文件的加密,因为在文件上面会用一层文件系统来对文件进行管理。如果说加密在文件系统上的话,破解加密算法之后,文件仍然读取的是明文,并不安全。所以加密系统应该直接对文件来进行加密,而不是对文件系统进行加密,对文件直接进行加密,那么即使加密算法被破解,别人读取出来的仍然是加密过的密文,文件并不会泄露。使用的是LUKS加密系统
1. 创建分区
创建分区的方法参考主分区或者swap分区的创建。2. 加密
[root@localhost ~]# cryptsetup luksFormat /dev/vdb1 #对指定分区进行加密 WARNING! ======== This will overwrite data on /dev/vdb1 irrevocably. Are you sure? (Type uppercase yes): YES #此处有一个bug^_^,必须输入大写的YES Enter passphrase: #输入密码 Verify passphrase: [root@localhost ~]# cryptsetup open /dev/vdb1 westos #只有打开加密,才能对该分区进行使用 Enter passphrase for /dev/vdb1: [root@localhost ~]# mkfs.xfs /dev/mapper/westos #格式化 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 ~]# mount /dev/mapper/westos /mnt [root@localhost ~]# touch /mnt/file{1..3} [root@localhost ~]# ls /mnt file1 file2 file3 [root@localhost ~]# umount /mnt [root@localhost ~]# cryptsetup close westos #关闭后,会发现/dev/mapper/westos消失 [root@localhost ~]# ls /mnt [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
相关文章推荐
- Linux下磁盘的分区,文件系统的格式化与raid设备
- Linux系列- 磁盘、设备、分区
- linux磁盘、分区、设备简单介绍
- 各种硬件设备在linux的文件名及磁盘组成分区
- linux下磁盘进行分区、文件系统创建、挂载和卸载
- 七、Linux 磁盘基本概念(磁盘、分区、MBR、GPT)
- Linux之磁盘分区
- linux 磁盘 分区、格式化、挂载
- linux中磁盘分区
- s3c2440基于linux的gpio led字符设备驱动实践
- Linux磁盘和文件系统管理(5)_创建交换分区
- Linux对超大容量磁盘进行分区
- Linux下磁盘两种分区方式
- Linux对磁盘的查看、分区、格式化、挂载及IO性能测试
- linux分区知识与大磁盘的分区
- 详解Linux 虚拟机根分区磁盘扩充空间记录
- linux磁盘的两种分区方法
- linux磁盘 分区 物理卷 卷组 逻辑卷 文件系统加载点操作案例
- Linux 交换分区,回环设备
- Linux新增磁盘、分区、格式化和自动挂载