1. 程式人生 > >1.9-磁盤分區過程

1.9-磁盤分區過程

uid 創建文件 page uuid reat phy label times urn

第1章 磁盤分區

1.1 磁盤分區表:

mbr 引導系統啟動 主引導記錄 0磁頭 0磁道 1扇區的前446字節

技術分享圖片

一個磁盤分區占分區表的16字節

技術分享圖片

主分區(primary): 一般一定要有 存放數據

擴展分區(extend): 最多一個 無法直接使用

邏輯分區(logical):存放數據

主分區+擴展分區 一共 最多四個

1.2 硬盤分區的設備名:

硬盤: SAS/SATA/SCSI /dev/sd?

第一塊SAS /dev/sda

第三塊SATA /dev/sdc

分區: 主分區和擴展分區 1-4

邏輯分區

5開始

1塊sas硬盤的第一個主分區 /dev/sda1
2塊sata硬盤的第2個主分區 /dev/sdb2
3塊sata硬盤的第1個邏輯分區 /dev/sdc5

1.3 磁盤格式化

1. fdisk 硬盤容量小於2T,只支持mbr分區表(應為mbr分區表最大支持2T)

2. proted 用於給大小超過2T的磁盤創建文件系統 基於gpt分區表

1.3.1 fdisk: 硬盤容量小於2T

實例1-1 創建一個40M主分區 40m邏輯分區

1.參數講解

fdisk -cu /dev/sdb

-u fdisk創建默認按柱面創建,, -u,按扇區創建

n 創建分區

d 刪除分區

p print 顯示分區縣信息

q 退出不保存

w 保存並退出

擴展分區 剩多少給多少 默認開始 默認結束

2.fdisk 創建分區

創建一個100M分區(使用全部磁盤空間),並掛載到/mnt

添加兩塊硬盤

[root@oldboy-gao ~]# fdisk -l |grep 'sd[a-c]:' 查看系統中所有磁盤及分區

Disk /dev/sda: 10.7 GB, 10737418240 bytes

Disk /dev/sdb: 106 MB, 106954752 bytes

Disk /dev/sdc: 106 MB, 106954752 bytes

#1.創建分區

[root@oldboy-gao ~]#fdisk /dev/sdb 給/dev/sdb分區
Command (m for help): p 顯示分區

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 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 identifier: 0x067df53e

Device Boot Start End Blocks Id System

Command (m for help): n 創建分區
Command action
e extended
p primary partition (1-4)
p
Partition number (1-4): 1 分區號
First sector (2048-208895, default 2048): 開始扇區
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-208895, default 208895): 結束扇區
Using default value 208895

Command (m for help): p

Disk /dev/sdb: 106 MB, 106954752 bytes
64 heads, 32 sectors/track, 102 cylinders, total 208896 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 identifier: 0x067df53e

Device Boot Start End Blocks Id System
/dev/sdb1 2048 208895 103424 83 Linux

Command (m for help): w 保存退出
The partition table has been altered!

Calling ioctl() to re-read partition table.
Syncing disks.
#2.通知系統/dev/sdb分區表變化了
partprobe /dev/sdb
#3.格式化 創建文件系統
[root@oldboyedu43-lnb ~]# mkfs.ext4 /dev/sdb1 創建ext4格式文件系統
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
25896 inodes, 103424 blocks
5171 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=67371008
13 block groups
8192 blocks per group, 8192 fragments per group
1992 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729

Writing inode tables: done
Creating journal (4096 blocks): done
Writing superblocks and filesystem accounting information: done

This filesystem will be automatically checked every 35 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

#4.關閉自動磁盤檢查功能

技術分享圖片

[root@oldboyedu43-lnb ~]# 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

-c 關閉每掛載N次的時候進行磁盤檢查
-i 關閉每隔一段時間的磁盤檢查

#5.掛載到/mnt
[root@oldboyedu43-lnb ~]# df -h 先查看一下是否有人掛載到/mnt上
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 3.4G 5.0G 41% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 40M 141M 22% /boot
[root@oldboyedu43-lnb ~]# mount /dev/sdb1 /mnt/ 臨時掛載
[root@oldboyedu43-lnb ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 8.8G 3.4G 5.0G 41% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 40M 141M 22% /boot
/dev/sdb1 94M 1.6M 88M 2% /mnt
[root@oldboyedu43-lnb ~]# touch /mnt/oldboy.txt
[root@oldboyedu43-lnb ~]# ls -l /mnt/
total 12
drwx------ 2 root root 12288 Jan 9 10:25 lost+found
-rw-r--r-- 1 root root 0 Jan 9 10:32 oldboy.txt

1.3.2 掛載永久生效

/dev/sdb1 永久掛載到/mnt

1) /etc/rc.local

/bin/mount /dev/sdb1 /mnt

2)/etc/fstab 開機自動掛載配置文件

UUID=e9d0cb5d-fa9d-42e5-8eb7-1bc269194fd1 / ext4 defaults 1 1

UUID=7aebd13d-f378-4ac0-a6cd-3a018cf083ab /boot ext4 defaults 1 2

第一列 第二列 ext4 defaults 1 1

設備名字 掛載點 文件系統類型 掛載參數 是否dump備份 是否進行磁盤檢查(自己創建的禁止)

/dev/adb1 /mnt ext4 defaults 0 0

小結:創建一個100M分區,把它掛載到/mnt上面

1.創建分區並通知系統磁盤的分區表變化

2.創建文件系統--關閉分區的自動檢查功能

3.掛載及永久掛載

技術分享圖片

特殊目錄,相當於內存

1.3.3 parted 用於給大小超過2T的磁盤創建文件系統 基於gpt分區表

gpt 格式分區表 主分區無限(相當於) 支持更大的硬盤

mklabel,mktable LABEL-TYPE 創建磁盤分區表

mbr(msdos)

gpt

h 幫助信息

mkpart PART-TYPE [FS-TYPE] START END 創建分區

print 顯示分區信息

quit 退出不保存

rm NUMBER 刪除一個分區

1.3.4 特點

默認單位MB

創建分區實時生效

支持tab

[root@oldboy-gao ~]# parted /dev/sdc parted 給/dev/sdc創建分區

GNU Parted 2.1

Using /dev/sdc

Welcome to GNU Parted! Type 'help' to view a list of commands.

(parted)

(parted) p 顯示分區

Error: /dev/sdc: unrecognised disk label

(parted) # 沒有分區表

(parted) mklabel gpt 創建gpt分區表

(parted) p

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 107MB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Number Start End Size File system Name Flags

(parted) mkpart primary 0 20M 創建分區

Warning: The resulting partition is not properly aligned for best performance.

Ignore/Cancel? I

(parted) p

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 107MB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Number Start End Size File system Name Flags

1 17.4kB 20.0MB 20.0MB primary

(parted) rm 2 刪除

(parted) p

Model: VMware, VMware Virtual S (scsi)

Disk /dev/sdc: 107MB

Sector size (logical/physical): 512B/512B

Partition Table: gpt

Number Start End Size File system Name Flags

1 17.4kB 20.0MB 20.0MB primary


fdiskparted區別

fdisk只支持mbr mbr支持小於2T的硬盤

parted支持多種分區表 gpt支持大於2T的硬盤


1.1 故障:Java環境內存不夠用,大量占用swap

思想:臨時增加swap 創建一個文件,讓它作為swap

解決:

dd if=/dev/zero of=/tmp/100M bs=1M count=1

從那裏獲取數據 到哪裏 大小/個 個數

1.創建一個文件

[root@oldboy-gao ~]# dd if=/dev/zero of=/tmp/100M bs=1M count=100

[root@oldboy-gao ~]# ls /tmp/100M

/tmp/100M

[root@oldboy-gao ~]# file /tmp/100M

/tmp/100M: data

2. 讓這個文件成為swap

makeswap /tmp/100M

[root@oldboy-gao ~]# mkswap /tmp/100M 創建swap分區

mkswap: /tmp/100M: warning: don't erase bootbits sectors

on whole disk. Use -f to force.

Setting up swapspace version 1, size = 102396 KiB

no label, UUID=b570b13c-8356-4bec-95e5-588b1cf98783

[root@oldboy-gao ~]# file /tmp/100M

/tmp/100M: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages

查看swap大小

[root@oldboy-gao ~]# free -h

total used free shared buffers cached

Mem: 1.8G 325M 1.5G 244K 11M 164M

-/+ buffers/cache: 149M 1.7G

Swap: 767M 0B 767M

3. swap文件加入到系統swap(臨時)

[root@oldboy-gao ~]# swapon /tmp/100M

[root@oldboy-gao ~]# free -h

total used free shared buffers cached

Mem: 1.8G 325M 1.5G 244K 11M 164M

-/+ buffers/cache: 149M 1.7G

Swap: 867M 0B 867M

[root@oldboy-gao ~]# swapon -s 查看系統swap有幾部分組成

Filename Type Size Used Priority

/dev/sda2 partition 786428 0 -1

/tmp/100M file 102396 0 -2

永久生效(尤其雲服務器)

1. /etc/rc.local

/sbin/swapon /tmp/100M

2. /etc/fstab

技術分享圖片


1.9-磁盤分區過程