在Linux系统中,每一个硬件设备都映射到一个系统的文件,那么在Linux系统中如何去分区?
1、fdisk:查看系统分区详细信息
fdisk -l
fdisk 对磁盘进行分区
[root@localhost ~]# fdisk /dev/sda //👀对sda磁盘进行分区
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
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
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
Command action
e extended //👀扩展分区
p primary partition (1-4) //👀主分区
p //👀主分区最多为4个,扩展分区最多为1个,逻辑分区不限
Selected partition 4
First cylinder (1593-2610, default 1593):
Using default value 1593
Last cylinder, +cylinders or +size{K,M,G} (1593-2610, default 2610): +1M
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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@localhost ~]# partx -a /dev/sda //👀对硬盘进行更新
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
[root@localhost ~]# partx -a /dev/sda
BLKPG: Device or resource busy
error adding partition 1
BLKPG: Device or resource busy
error adding partition 2
BLKPG: Device or resource busy
error adding partition 3
BLKPG: Device or resource busy
error adding partition 4
[root@localhost ~]# fdisk -l
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000373e3
Device Boot Start End Blocks Id System
/dev/sda1 * 1 26 204800 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 26 1332 10485760 83 Linux
/dev/sda3 1332 1593 2097152 82 Linux swap / Solaris
/dev/sda4 1593 1593 7036+ 83 Linux
[root@localhost ~]# mkfs.ext4 /dev/sda4 //👀进行格式化
mke2fs 1.41.12 (17-May-2010)
文件系统标签=
操作系统:Linux
块大小=1024 (log=0)
分块大小=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
1760 inodes, 7036 blocks
351 blocks (4.99%) reserved for the super user
第一个数据块=1
Maximum filesystem blocks=7340032
1 block group
8192 blocks per group, 8192 fragments per group
1760 inodes per group
正在写入inode表: 完成
Creating journal (1024 blocks): 完成
Writing superblocks and filesystem accounting information: 完成
This filesystem will be automatically checked every 27 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@localhost ~]# mount /dev/sda4 /mnt/ //👀进行挂载使用
[root@localhost ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.8G 2.4G 6.9G 26% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 31M 150M 17% /boot
/dev/sr0 3.7G 3.7G 0 100% /media
/dev/sda4 5.7M 41K 5.3M 1% /mnt
2、文件系统管理工具命令
创建文件系统工具
mkfs:mkfs.ext2,mkfs.ext3,mkfs.ext4,mkfs.xfs,mkfs.vfatf’s
检测及修复文件系统的工具
fsck:fscf.ext2,fscf.ext2
查看其属性的工具
dumpe2fs,tune2fs
调整文件系统特性
tune2fs
** mke2fs:专门管理ext系统文件系统工具**
-t {ext2|ext3|ext4}指明要创建的文件系统类型
mkfs.ext2=mkfs -t ext2 =mke2fs -t ext2
-b {1024|2048|4096}:指定文件系统块大小
-L LABEL:指明卷标
-i #:指明inode百分比 每多少个字节创建一个inode
-N #:直接给定inode数量
-m # :设定预留空间 #代表百分比,默认占用5%的空间
e2label:卷标的查看与设定
查看:e2label device
设定:e2label device LABEL
dumpe2fs:
tune2fs :查看或者修改ext系统文件系统的属性
-l:查看超级快内容
修改文件系统属性
-j:ext2-----> ext3
-L LABEL:修改卷标
-m # :设定预留空间 #代表百分比,默认占用5%的空间
[root@localhost ~]# dumpe2fs /dev/sda4
dumpe2fs 1.41.12 (17-May-2010)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: a78a04d1-5d29-4e48-b330-8e867f0d711d
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 1760
Block count: 7036
Reserved block count: 351
Free blocks: 5746
Free inodes: 1749
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 27
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 1760
Inode blocks per group: 220
Flex block group size: 16
Filesystem created: Sat Mar 28 21:13:14 2020
Last mount time: Sat Mar 28 21:14:05 2020
Last write time: Sat Mar 28 21:14:05 2020
Mount count: 1
Maximum mount count: 27
Last checked: Sat Mar 28 21:13:14 2020
Check interval: 15552000 (6 months)
Next check after: Thu Sep 24 21:13:14 2020
Lifetime writes: 1427 kB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: c634f82f-c51d-4d7d-a18e-0c478da8e341
Journal backup: inode blocks
Journal features: (none)
日志大小: 1024k
Journal length: 1024
Journal sequence: 0x00000001
Journal start: 0
Group 0: (Blocks 1-7035) [ITABLE_ZEROED]
校验和 0x02c5,1749个未使用的inode
主 superblock at 1, Group descriptors at 2-2
保留的GDT块位于 3-29
Block bitmap at 30 (+29), Inode bitmap at 46 (+45)
Inode表位于 62-281 (+61)
5746 free blocks, 1749 free inodes, 2 directories, 1749个未使用的inodes
可用块数: 1290-7035
可用inode数: 12-1760
[root@localhost ~]# tune2fs -l /dev/sda4
tune2fs 1.41.12 (17-May-2010)
Filesystem volume name: <none>
Last mounted on: <not available>
Filesystem UUID: a78a04d1-5d29-4e48-b330-8e867f0d711d
Filesystem magic number: 0xEF53
Filesystem revision #: 1 (dynamic)
Filesystem features: has_journal ext_attr resize_inode dir_index filetype needs_recovery extent flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
Filesystem flags: signed_directory_hash
Default mount options: (none)
Filesystem state: clean
Errors behavior: Continue
Filesystem OS type: Linux
Inode count: 1760
Block count: 7036
Reserved block count: 351
Free blocks: 5746
Free inodes: 1749
First block: 1
Block size: 1024
Fragment size: 1024
Reserved GDT blocks: 27
Blocks per group: 8192
Fragments per group: 8192
Inodes per group: 1760
Inode blocks per group: 220
Flex block group size: 16
Filesystem created: Sat Mar 28 21:13:14 2020
Last mount time: Sat Mar 28 21:14:05 2020
Last write time: Sat Mar 28 21:14:05 2020
Mount count: 1
Maximum mount count: 27
Last checked: Sat Mar 28 21:13:14 2020
Check interval: 15552000 (6 months)
Next check after: Thu Sep 24 21:13:14 2020
Lifetime writes: 1427 kB
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)
First inode: 11
Inode size: 128
Journal inode: 8
Default directory hash: half_md4
Directory Hash Seed: c634f82f-c51d-4d7d-a18e-0c478da8e341
Journal backup: inode blocks
e2fsck :磁盘修复 , 卸载后
-a:自动修复
-b:设定surperblock位置
-B:设定块大小
-c:检查坏道
-C file:检查结果保存到file中
-f:强制检查
-F:检查前清空缓存
-v:显示详细报告
-y:交互式
fsck:可以检测多个系统
-s:按照顺序检测
-A:对/etc/fstab中列出的分区进行检查 (默认 mount -a )
-a:自动修复
-r:如果有错误,由使用者决定是否修复
blkid:获取文件系统类型 UUID
-L LABEL:根据卷标定位设备
-U UUID:根据UUID定位设备
3、交换分区
swap的调整对Linux服务器,特别是web服务器的性能至关重要。通过调整swap,有时可以越过系统的性能瓶颈,节省系统升级费用
free -m:
swapon -s
swapon -a
mkswap
[root@localhost ~]# free -m //👀查看当前交换分区
total used free shared buffers cached
Mem: 980 129 850 0 11 33
-/+ buffers/cache: 83 896
Swap: 2047 0 2047
[root@localhost ~]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
[root@localhost ~]# fdisk /dev/sda //👀创建一个分区
WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').
Command (m for help): n //👀分区分配不足删除一个
You must delete some partition and add an extended partition first
Command (m for help): d //👀删除分区4
Partition number (1-4): 4
Command (m for help): n //👀重新创建主分区
Command action
e extended
p primary partition (1-4)
p
Selected partition 4
First cylinder (1593-2610, default 1593):
Using default value 1593
Last cylinder, +cylinders or +size{K,M,G} (1593-2610, default 2610): +5M
Command (m for help): T //👀修改主分区的id
Partition number (1-4): 4
Hex code (type L to list codes): 82 //👀82为swap分区
Changed system type of partition 4 to 82 (Linux swap / Solaris)
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
WARNING: Re-reading the partition table failed with error 16: 设备或资源忙.
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@localhost ~]# partprobe //👀yum install parted
Warning: WARNING: the kernel failed to re-read the partition table on /dev/sda ( 设备或资源忙). As a result, it may not reflect all of your changes until after reboot.
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开 。
Warning: 无法以读写方式打开 /dev/sr0 (只读文件系统)。/dev/sr0 已按照只读方式打开 。
Error: 无效的分区表 - /dev/sr0 出现递归分区。
[root@localhost ~]# ls -l /dev/sda
brw-rw---- 1 root disk 8, 0 3月 29 09:37 /dev/sda
[root@localhost ~]# mkswap /dev/sda4 //👀初始化
Setting up swapspace version 1, size = 7032 KiB
no label, UUID=4644d685-56dc-4a5d-a889-876f00f52184
[root@localhost ~]# df -h //👀查看分区信息
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 9.8G 2.4G 6.9G 26% /
tmpfs 491M 0 491M 0% /dev/shm
/dev/sda1 190M 31M 150M 17% /boot
/dev/sr0 3.7G 3.7G 0 100% /media
[root@localhost ~]# vi /etc/fstab
[root@localhost ~]# vi /etc/fstab //👀将UUID写入
[root@localhost ~]# swapon -a //👀读取/etc/fstab
[root@localhost ~]# swapon -s //👀查看swap分区
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
/dev/sda4 partition 7032 0 -2
[root@localhost ~]# dd if=/dev/zero of=/mnt/swap.file bs=1M count=1024
//👀还有一种方法就是文件创建swap分区
记录了1024+0 的读入
记录了1024+0 的写出
1073741824字节(1.1 GB)已复制,26.4904 秒,40.5 MB/秒
[root@localhost ~]# cd /mnt/
[root@localhost mnt]# ls --lh
ls:无法识别的选项“--lh”
请尝试执行"ls --help"来获取更多信息。
[root@localhost mnt]# ls -lh
总用量 1.1G
-rw-r--r-- 1 root root 1.0G 3月 29 11:15 swap.file
[root@localhost mnt]# mkswap /mnt/swap.file
mkswap: /mnt/swap.file: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 1048572 KiB
no label, UUID=7e821de7-6fd4-4127-a9a0-4c6274a5e3b3
[root@localhost mnt]# vi /etc/fstab
[root@localhost mnt]# swapon -a
swapon: cannot find the device for UUID=7e821de7-6fd4-4127-a9a0-4c6274a5e3b3
[root@localhost mnt]# vi /etc/fstab //👀文件创建swap不允许使用UUID,可以使用文件路径
[root@localhost mnt]# swapon -a
[root@localhost mnt]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
/dev/sda4 partition 7032 0 -2
/mnt/swap.file file 1048572 0 -3
挂载点:用于作为另外一个文件系统的访问入口
1、必须事先存在
2、使用未被系统或者其他进程使用的目录
3、挂载点下的原文件会被隐藏(访问路径会被改变,若取消挂载路径又指向原路径
)
mount -t 文件系统 -o 选项 设备名 挂载点
#mount -t iso9660 -o loop /dev/sr0 /media
##############################################
默认选项:
defaults: rw,suid,dev,exec,auto,nouser,async,relatime
quota 磁盘配额
acl:支持facl功能
# mount -o acl decice dir
# tune2fs -o acl device
mount:
mount [-nrw] [-t vfstype] [-o options] device dir
-r:只读挂载
-w:读写挂载
-n:默认情况下 只要挂载或者卸载会同步更新/etc/mtab,-n禁止同步
-t:指明文件系统
-L LABEL:挂载的时候以卷标指明设备
-U UUID:挂载的时候以UUID指明设备
-o:指定挂载选项:
查看当前系统挂载那些设备
# mount
# cat /etc/mtab
# cat /proc/mounts
umount:
umount device/directory
注意:正在使用的挂载点是无法被卸载的
查看被那些进程所占用
# lsof mountpoint
# fuser -v MOUNT_POINT