磁盘管理之磁盘分区
磁盘分区表
磁盘分区表位置:0磁头0磁道1扇区(前446字节表示mbr主引导记录,中单64字节表示分区表,最后2字节55AA标识表示结束标记)
主分区(primary):一般一定要有,用来存放数据(最多四个主分区)
扩展分区(extend):只能有一个扩展分区,无法直接使用
逻辑分区(logical):在扩展分区下新建,用来存放数据
磁盘分区的设备名
磁盘: SAS/SATA/SCSI表示/dev/sd(a-z)如第一块硬盘表示/dev/sda,第三块/dev/sdc
分区:主分区与扩展分区表示:1-4,逻辑分区从5开始
磁盘的分区过程(fdisk)
fdisk帮助信息
n add a new partition 创建分区
p print the partition table 显示分区信息
q quit without saving changes 退出不保存
w write table to disk and exit 退出并保存
fdisk分区过程
[root@web02 ~]# fdisk -cu /dev/sdb
#创建主分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
P
Partition number (1-4): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +300M
#创建扩展分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
e
Partition number (1-4): 2
First sector (616448-2097151, default 616448):
Using default value 616448
Last sector, +sectors or +size{K,M,G} (616448-2097151, default 2097151):
Using default value 2097151
#创建逻辑分区
Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First sector (618496-2097151, default 618496):
Using default value 618496
Last sector, +sectors or +size{K,M,G} (618496-2097151, default 2097151): +500M
#保存并退出
Command (m for help): W
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
分区格式化与挂载分区
[root@web02 ~]# fdisk -cu /dev/sdb
#分区
Command (m for help): n
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1
First sector (2048-2097151, default 2048):
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151):
Using default value 2097151
Command (m for help): w
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
#通知系统磁盘的分区信息变化
[root@web02 ~]# partprobe /dev/sdb1
#将磁盘格式化成ext4格式
[root@web02 ~]# mkfs.ext4 /dev/sdb1
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=4096 (log=2)
Fragment size=4096 (log=2)
Stride=0 blocks, Stripe width=0 blocks
65536 inodes, 261888 blocks
13094 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=268435456
8 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 29 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
#关闭磁盘分区的自动检查
[root@web02 ~]# tune2fs -c 0 -i 0 /dev/sdb1
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds
#挂载分区到/mnt
[root@web02 ~]# mount /dev/sdb1 /mnt
[root@web02 ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.6G 1.4G 5.8G 20% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 40M 141M 22% /boot
/dev/sdb1 991M 1.3M 939M 1% /mnt
开机自动挂载分区
方法一:修改/etc/rc.local的配置文件
vim /etc/rc.local 添加mount /dev/sdb1 /mnt
方法二:修改/etc/fstab配置文件
UUID=d3a44a4d-2224-41af-9616-47e0c6d195e1 / ext4 defaults 1 1
UUID=22f7e2c4-3e68-4e19-9390-59b4ccc5c680 /boot ext4 defaults 1 2
UUID=3cfe6ee0-8e29-4332-bbde-b573b6860fd2 swap swap defaults 0 0
tmpfs /dev/shm tmpfs defaults 0 0
devpts /dev/pts devpts gid=5,mode=620 0 0
sysfs /sys sysfs defaults 0 0
proc /proc proc defaults 0 0
/dev/sdb1 /mnt ext4 defaults 0 0
表示挂载磁盘 挂载到哪 文件格式 关闭磁盘自动检查
parted方式进行分区并挂载
[root@web02 /]# parted /dev/sdb
GNU Parted 2.1
Using /dev/sdb
Welcome to GNU Parted! Type 'help' to view a list of commands.
(parted) p #显示分区信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: loop
Number Start End Size File system Flags
1 0.00B 2147MB 2147MB ext4
(parted) mklabel gpt #创建gpt分区格式
Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will
be lost. Do you want to continue?
Yes/No? yes
(parted) p #显示分区
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
(parted) mkpart primary 0 300 #创建主分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) p #显示分区信息
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB primary
(parted) mkpart primary 300 600 #创建第二个主分区
Warning: The resulting partition is not properly aligned for best performance.
Ignore/Cancel? I
(parted) p
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdb: 2147MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 300MB 300MB primary
2 300MB 600MB 300MB primary
(parted) q
Information: You may need to update /etc/fstab.
[root@web02 /]# partprobe /dev/sdb1 #向系统发送分区信息已发生变化
[root@web02 /]# mkfs.ext4 /dev/sdb1 #格式化分区
mke2fs 1.41.12 (17-May-2010)
Filesystem label=
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
73440 inodes, 292952 blocks
14647 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67633152
36 block groups
8192 blocks per group, 8192 fragments per group
2040 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 39 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@web02 /]# tune2fs -c 0 -i 0 /dev/sdb1 #关闭磁盘自动检查
tune2fs 1.41.12 (17-May-2010)
Setting maximal mount count to -1
Setting interval between checks to 0 seconds
[root@web02 /]# mount /dev/sdb1 /data # 挂载分区
[root@web02 /]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 7.6G 1.4G 5.8G 20% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 40M 141M 22% /boot
/dev/sdb1 270M 2.1M 253M 1% /data
[root@web02 /]#
企业案例:java环境内存不够导致swap
#创建一个文件块
[root@web02 /]# dd if=/dev/zero of=/tmp/100m bs=100M count=100
^C36+1 records in
36+0 records out
3774873600 bytes (3.8 GB) copied, 94.3259 s, 40.0 MB/s
[root@web02 /]# file /tmp/100m
/tmp/100m: data
#变成swap
[root@web02 /]# mkswap /tmp/100m
mkswap: /tmp/100m: warning: don't erase bootbits sectors
on whole disk. Use -f to force.
Setting up swapspace version 1, size = 3686396 KiB
no label, UUID=80e73a04-f211-4acc-9802-e81c74e4d6d6
[root@web02 /]# file /tmp/100m
/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 921599 pages
#使swap生效
[root@web02 /]# swapon /tmp/100m
[root@web02 /]# free -h
total used free shared buffers cached
Mem: 1.8G 1.7G 168M 244K 3.2M 1.5G
-/+ buffers/cache: 178M 1.6G
Swap: 5.5G 0B 5.5G
#查看swap组成
[root@web02 /]# swapon -s
Filename Type Size Used Priority
/dev/sda3 partition 2097148 0 -1
/tmp/100m file 3686396 0 -2
#使swap永久生效
方法一:在/etc/rc.local中
swapon /tmp/100m
方法二:修改配置文件/etc/fstab
/tmp/100m swap swap defaults 0 0