磁盘配置
1、了解磁盘的物理结构
磁盘通常由磁头,盘面,主轴以及磁盘接口主要的四个部件组成,而盘片又分为磁面(Side)、磁道(Track)、柱面(Cylinder)与扇区(Sector)等4个结构构成;磁头通常由磁头、传动手臂、传动轴等构成;
磁头用于读取数据,盘片用于存储数据,磁盘接口用于连接主板以及阵列卡;
磁盘内部结构详细说明
-
磁头
用来读取和写入数据,盘片的数量等于磁头的数量,磁头通过径向移动来读取和写入数据;
-
磁道
用于存储数据,由多个同心圆构成,磁盘默认按照磁道寻找数据;
-
扇区
扇区是磁盘最小的存储单位,一个扇区默认为512字节;
-
柱面
不同盘片上的统一磁道构成的圆柱体为柱面;
2、磁盘的读取数据过程
3、磁盘的分区
磁盘有两种分区方法,通常对于磁盘容量大于2T的使用 parted进行分区,对于磁盘容量小于2T的,通常使用fdisk来进行分区;
fdisk分区
创建分区环境
在vmvare中添加一块硬盘容量为10G的硬盘,用于分区(三个主分区,两个逻辑分区);
创建完成之后使用fdisk -l 命令查看磁盘是否添加成功
[root@doctor-10 ~]# fdisk -l 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 /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b41ef Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 4605951 2097152 82 Linux swap / Solaris /dev/sda3 4605952 25577471 10485760 83 Linux
fdisk命令的介绍
说明:用于对磁盘进行分区;
格式:fdisk [option]
fdisk [-uc] [-b sectorsize] [-C cyls] [-H heads] [-S sects] device fdisk -l [-u] [device...] fdisk -s partition... fdisk -v fdisk -h
常用分区指令:
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) #常用指令如下: n add a new partition 添加一个新的分区 d delete a partition 删除分区 g create a new empty GPT partition table 创建一个新的GPT分区表(并且表为空) l list known partition types 列出已知的分区类型 m print this menu 打印帮助菜单 p print the partition table 打印分区信息表 t change a partition's system id 改变分区的系统id q quit without saving changes 不保存退出 w write table to disk and exit 将分区信息写入到磁盘后退出
创建测试环境以及掌握了fdisk命令的基本使用之后,下面进入分区
[root@doctor-10 ~]# 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. Device does not contain a recognized partition table Building a new DOS disklabel with disk identifier 0xc0a469d8. 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): n Partition type: #分区类型 p primary (0 primary, 0 extended, 4 free) #0个主分区,0个扩展分区 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): n Partition type: p primary (1 primary, 0 extended, 3 free) e extended Select (default p): p Partition number (2-4, default 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): Command (m for help): n Partition type: p primary (2 primary, 0 extended, 2 free) e extended Select (default p): p Partition number (3,4, default 3): First sector (4196352-20971519, default 4196352): Using default value 4196352 Last sector, +sectors or +size{K,M,G} (4196352-20971519, default 20971519): +1G Partition 3 of type Linux and of size 1 GiB is set 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 (6293504-20971519, default 6293504): Using default value 6293504 Last sector, +sectors or +size{K,M,G} (6293504-20971519, default 20971519): Using default value 20971519 Partition 4 of type Extended and of size 7 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: 0xc0a469d8 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux /dev/sdb2 2099200 4196351 1048576 83 Linux /dev/sdb3 4196352 6293503 1048576 83 Linux /dev/sdb4 6293504 20971519 7339008 5 Extended #创建逻辑分区 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): 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: 0xc0a469d8 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux /dev/sdb2 2099200 4196351 1048576 83 Linux /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 Command (m for help): w #保存分区信息然后退出 The partition table has been altered! Calling ioctl() to re-read partition table. Syncing disks.
分区完成之后,系统是不识别分区文件的,使用如下命令可以加载识别分区文件
partprobe /dev/sdb
挂载文件系统
#详解本章中的文件的挂载与卸载
查看挂载是否成功
[root@doctor-10 /mountpoint]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 10G 2.0G 8.1G 20% / devtmpfs 983M 0 983M 0% /dev tmpfs 993M 0 993M 0% /dev/shm tmpfs 993M 8.6M 984M 1% /run tmpfs 993M 0 993M 0% /sys/fs/cgroup /dev/sdb2 1014M 33M 982M 4% /mountpoint/sdb2 /dev/sdb1 1014M 33M 982M 4% /mountpoint/sdb1 /dev/sr0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 197M 97M 100M 50% /boot tmpfs 199M 0 199M 0% /run/user/0
parted分区
parted命令的说明
说明:
parted is a program to manipulate disk partitions. It supports multiple partition table formats, including MS-DOS and GPT. It is useful for creating space for new operating systems, reorganising disk usage, and copying data to new hard disks. This manual page documents parted briefly.
Complete documentation is distributed with the package in GNU Info format.
创建测试环境,添加一个2T 的硬盘,添加完成之后可以查看是否添加成功;
[root@doctor-10 ~]# fdisk -l Disk /dev/sda: 21.5 GB, 21474836480 bytes, 41943040 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: 0x000b41ef Device Boot Start End Blocks Id System /dev/sda1 * 2048 411647 204800 83 Linux /dev/sda2 411648 4605951 2097152 82 Linux swap / Solaris /dev/sda3 4605952 25577471 10485760 83 Linux 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: 0xc0a469d8 Device Boot Start End Blocks Id System /dev/sdb1 2048 2099199 1048576 83 Linux /dev/sdb2 2099200 4196351 1048576 83 Linux /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 Disk /dev/sdc: 2254.9 GB, 2254857830400 bytes, 4404019200 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
添加成功之后,使用parted进行分区,下面先对parted命令使用做以说明
[root@doctor-10 ~]# parted /dev/sdc GNU Parted 3.1 Using /dev/sdc Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted (parted) 常见指令详解: mklabel,mktable LABEL-TYPE create a new disklabel (partition table) 创建一个分区表 (默认为mbr) mklabel gpt print [devices|free|list,all|NUMBER] 显示分区信息 mkpart PART-TYPE [FS-TYPE] START END make a partition 创建一个分区 PART-TYPE分区类型,主分区还或者扩展分区 quit exit program 退出分区状态 rm NUMBER delete partition NUMBER 删除分区
分区实战:
[root@doctor-10 ~]# parted /dev/sdc GNU Parted 3.1 Using /dev/sdc Welcome to GNU Parted! Type 'help' to view a list of commands. (parted) help align-check TYPE N check partition N for TYPE(min|opt) alignment help [COMMAND] print general help, or help on COMMAND mklabel,mktable LABEL-TYPE create a new disklabel (partition table) mkpart PART-TYPE [FS-TYPE] START END make a partition name NUMBER NAME name partition NUMBER as NAME print [devices|free|list,all|NUMBER] display the partition table, available devices, free space, all found partitions, or a particular partition quit exit program rescue START END rescue a lost partition near START and END rm NUMBER delete partition NUMBER select DEVICE choose the device to edit disk_set FLAG STATE change the FLAG on selected device disk_toggle [FLAG] toggle the state of FLAG on selected device set NUMBER FLAG STATE change the FLAG on partition NUMBER toggle [NUMBER [FLAG]] toggle the state of FLAG on partition NUMBER unit UNIT set the default unit to UNIT version display the version number and copyright information of GNU Parted (parted) mklabel gpt #用于创建 gpt (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 2255GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags (parted) mkpart primary 0 2100G Warning: The resulting partition is not properly aligned for best performance. Ignore/Cancel? ignore (parted) print Model: VMware, VMware Virtual S (scsi) Disk /dev/sdc: 2255GB Sector size (logical/physical): 512B/512B Partition Table: gpt Disk Flags: Number Start End Size File system Name Flags 1 17.4kB 2100GB 2100GB primary (parted) quit #分区完成之后,不需要将分区信息写入到磁盘中
分区完成之后,系统是不识别分区文件的,使用如下命令可以加载识别分区文件
partprobe /dev/sdc
格式化文件系统
[root@doctor-10 ~]# mkfs.xfs /dev/sdc1 meta-data=/dev/sdc1 isize=512 agcount=4, agsize=128173827 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=512695308, imaxpct=5 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=250339, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
挂载文件系统
[root@doctor-10 /mountpoint]# mount /dev/sdc1 /mountpoint/sdc1/
查看是否挂载成功
[root@doctor-10 /mountpoint]# df -h Filesystem Size Used Avail Use% Mounted on /dev/sda3 10G 2.0G 8.1G 20% / devtmpfs 983M 0 983M 0% /dev tmpfs 993M 0 993M 0% /dev/shm tmpfs 993M 8.6M 984M 1% /run tmpfs 993M 0 993M 0% /sys/fs/cgroup /dev/sdb2 1014M 33M 982M 4% /mountpoint/sdb2 /dev/sdb1 1014M 33M 982M 4% /mountpoint/sdb1 /dev/sr0 4.3G 4.3G 0 100% /mnt/cdrom /dev/sda1 197M 97M 100M 50% /boot tmpfs 199M 0 199M 0% /run/user/0 /dev/sdc1 2.0T 33M 2.0T 1% /mountpoint/sdc1
4、磁盘的格式化
磁盘分区完成之后,使用命令 mkfs 对磁盘进行格式化;
mkfs
- 说明:用于创建Linux文件系统(也就是格式化磁盘);
- 格式:mkfs [options] [-t type] [fs-options] device [size]
- 常用参数:
- -t :指定创建文件系统的类型,centos7.0开始默认文件系统是xfs,centos6是ext4,centos5是ext3
- 等同于 mkfs.xfs mkfs.ext4 mkfs.ext3
#按两次TABLE
[root@doctor-10 ~]# mkfs
mkfs mkfs.btrfs mkfs.cramfs mkfs.ext2 mkfs.ext3 mkfs.ext4 mkfs.minix mkfs.xfs
- 对上述已经分区完成的磁盘进行格式化
[root@doctor-10 ~]# mkfs.xfs /dev/sdb1 meta-data=/dev/sdb1 isize=512 agcount=4, agsize=65536 blks = sectsz=512 attr=2, projid32bit=1 = crc=1 finobt=0, sparse=0 data = bsize=4096 blocks=262144, imaxpct=25 = sunit=0 swidth=0 blks naming =version 2 bsize=4096 ascii-ci=0 ftype=1 log =internal log bsize=4096 blocks=2560, version=2 = sectsz=512 sunit=0 blks, lazy-count=1 realtime =none extsz=4096 blocks=0, rtextents=0
5、磁盘的挂载与卸载
挂载的概念:挂载操作指的是当用户需要使用硬盘设备或分区数据时,需要先将其与一个已存在的目录文件做关联,而这个动作就叫“挂载”。(以上概念摘自 刘遄老师的《Linux就该这么学》)linux就该这么学
mount命令的详解
说明:挂载一个文件系统;
格式:
mount [-lhV] mount -a [-fFnrsvw] [-t vfstype] [-O optlist] mount [-fnrsvw] [-o option[,option]...] device|dir mount [-fnrsvw] [-t vfstype] [-o options] device dir
常用参数详解:
-
-V -V, --version
Output version 打印版本信息
[root@doctor-10 ~]# mount -V mount from util-linux 2.23.2 (libmount 2.23.0: selinux, debug, assert)
-
-h, --help
Print a help message 打印帮助信息
-
-r, --read-only
Mount the filesystem read-only. A synonym is -o ro.以只读的方式挂载文件系统,等同于使用参数 -o ro
-
-w, --rw, --read-write
Mount the filesystem read/write. This is the default. A synonym is -o rw.以读写的方式挂载文件系统,等同于使用参数 -o rw
-
-U, --uuid uuid Mount the partition that has the specified uuid. These two options require the file /proc/partitions (present since Linux 2.1.116) to exist. 挂载指定UUID的文件系
-
-o 指定挂载的方式,以只读(一切正常,但是无法创建文件)或者读写的方式进行挂载
ro Mount the filesystem read-only. rw Mount the filesystem read-write
umount命令详解
说明:卸载挂载文件系统,即卸载挂载点
格式:unmount 挂载点
卸载的时候必须退出挂载点目录,否在无法卸载挂载点
6、实现开机自动挂载的两种方式
在/etc/rc.d/rc.local中添加挂载命令
[root@doctor-10 ~]# vim /etc/rc.d/rc.local #!/bin/bash # THIS FILE IS ADDED FOR COMPATIBILITY PURPOSES # # It is highly advisable to create own systemd services or udev rules # to run scripts during boot instead of using this file. # # In contrast to previous versions due to parallel execution during boot # this script will NOT be run after all other services. # # Please note that you must run 'chmod +x /etc/rc.d/rc.local' to ensure # that this script will be executed during boot. mount /dev/sdb1 /mnt #将 sdb1挂载到 /mnt目录下
挂载磁盘的时候如果挂载点目录下面有数据,那么数据会进行隐藏,所以建议创建空目录进行挂载
添加上述信息之后,发现开机之后并没有挂载文件系统,一般对文件的执行权限进行查看,发现并没有执行权限,所以进行添加
[root@doctor-10 /mountpoint]# ll /etc/rc.d/rc.local -rw-r--r--. 1 root root 503 Mar 13 11:56 /etc/rc.d/rc.local [root@doctor-10 /mountpoint]# chmod 744 /etc/rc.d/rc.local [root@doctor-10 /mountpoint]# ll /etc/rc.d/rc.local -rwxr--r--. 1 root root 503 Mar 13 11:56 /etc/rc.d/rc.local
编辑文件 /etc/fstab
/etc/fstab文件内容如下,对文件内容做以说明;
UUID:格式化文件系统之后,会创建一个UUID,全局唯一,可通过blkid进行查看;
[root@doctor-10 /mountpoint]# blkid /dev/sda1: UUID="e77d8175-c155-4733-855e-05dd7af9a16d" TYPE="xfs" /dev/sda2: UUID="58ce83e4-5360-49a7-960f-cba82a3789c7" TYPE="swap" /dev/sda3: UUID="babed68a-5ab3-47d9-abab-a65de81feb65" TYPE="xfs" /dev/sdb1: UUID="306ea0ac-1975-4ca5-92bd-ac4819fdff78" TYPE="xfs" /dev/sr0: UUID="2017-09-06-10-51-00-00" LABEL="CentOS 7 x86_64" TYPE="iso9660" PTTYPE="dos" /dev/sdb2: UUID="415db166-84b8-4022-8473-51c876db95d8" TYPE="xfs"
第二列:文件系统的挂载点,将指定文件系统挂载到那个目录下面;
第三列:文件系统的类型;
第四列:默认信息,如下所述
defaults Use default options: rw, suid, dev, exec, auto, nouser, and async.
第五列:0 代表是否备份磁盘
第六列:0 是否检查磁盘
实战:将 /dev/sdb1,/dev/sdb2分别挂载到 /mountpoint/sdb1,/mountpoint/sdb2中
# /etc/fstab # Created by anaconda on Mon Dec 30 01:55:05 2019 # # 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=babed68a-5ab3-47d9-abab-a65de81feb65 / xfs defaults 0 0 UUID=e77d8175-c155-4733-855e-05dd7af9a16d /boot xfs defaults 0 0 UUID=58ce83e4-5360-49a7-960f-cba82a3789c7 swap swap defaults 0 0 /dev/cdrom /mnt/cdrom iso9660 defaults 0 0 #分别使用UUID和挂载文件系统名进行挂载 /dev/sdb1 /mountpoint/sdb1 xfs defaults 0 0 UUID=415db166-84b8-4022-8473-51c876db95d8 /mountpoint/sdb2 xfs defaults 0