第1章 如何查看內存使用情況
[root@zs ~]# free -h
total used free shared buffers cached
Mem: 980M 914M 66M 232K 61M 680M
-/+ buffers/cache: 172M 808M
真正使用的 內存 剩餘
Swap: 767M 0B 767M
Linux把你使用過的命令或文件 替你緩存(buffer cache)起來,提高下次使用速度
寫buffer
讀cache
(不要手動清空buffer和cache)會自動覆蓋
第2章 磁盤分區
Linux啓動流程
1. 開機自檢 (內存cpu硬件是否有問題【硬件檢查】)
2. MBR引導
3. GRUB菜單
4. 加載內核
5. 運行init進程 (Linux系統裏面的第一個進程)
6. 讀取運行級別(/etc/inittab配置文件)
7. 執行/etc/rc.d/rc.sysinit腳本(系統的初始化腳本【設置主機名 配置IP地址】)
8. 執行/etc/rc.d腳本 (3級別)(根據運行級別 啓動對應服務【開機自啓動】)
9. 啓動mingetty進程
10. 進入登陸界面
磁盤的引導扇區 0磁頭 0磁道 1扇區
MBR引導 0頭0道1扇區446字節
MBR(Master Boot Record) 主引導記錄 引導系統啓動
DPT(Disk Partition Table) 磁盤分區表 記錄着磁盤分區從哪裏開始到哪裏結束
2.1 主分區:(primary) 每個分區佔用16字節的分區表
2.2 擴展分區:(extended)無法直接使用
2.3 邏輯分區(logical):存放數據
2.4 磁盤分區表:
主分區 primary 一般一定要有 用於存放數據
擴展分區 extend 最多1個 無法直接使用
邏輯分區 logical 存放數據
2.5 磁盤分區的命名規則:
2.5.1 磁盤分區
[root@zs ~]# fdisk -l |grep sd[a-c]
Disk /dev/sda: 21.5 GB, 21474836480 bytes
/dev/sda1 * 1 26 204800 83 Linux
/dev/sda2 26 124 786432 82 Linux swap / Solaris
/dev/sda3 124 2611 19979264 83 Linux
Disk /dev/sdb: 213 MB, 213909504 bytes
Disk /dev/sdc: 20 MB, 20971520 bytes
fdisk命令用於觀察硬盤實體使用情況,也可對硬盤分區。它採用傳統的問答式界面,而非類似DOS fdisk互動式操作界面,因此在使用上較爲不便,但功能卻絲毫不打折扣。
2.6 fdisk:
fdisk: 創建分區
-u 磁盤分區的時候以扇區爲單位默認是按照柱面
-c 關閉DOS兼容模式
fdisk –cu /dev/sdb
fdisk 內部命令
m 顯示幫助
p 顯示所有分區信息
d 刪除分區
w 保存並退出
q 退出
創建一個10MB主分區
fdisk -cu /dev/sdb
Command (m for help): n
Command action #分區類型
e extended #擴展分區
p primary partition (1-4) #主分區
p
Partition number (1-4): #分區號碼
First sector (2048-417791, default 2048):
從哪裏開始(回車 使用默認)
Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791): +10M
#大小
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0x8dde1fa5
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
【練習】
第3章 創建10MB的主分區 創建一個使用所有容量的擴展分區 創建兩個50MB邏輯分區
[root@zs ~]# fdisk -cu /dev/sdb (創建分區)
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xb42cefa5.
Changes will remain in memory only, until you decide to write them.
After that, of course, the previous content won't be recoverable.
Warning: invalid flag 0x0000 of partition table 4 will be corrected by w(rite)
(警告)
Command (m for help): n n創建新的分區
Command action
e extended 擴展分區
p primary partition (1-4) 主分區
p 創建一個主分區
Partition number (1-4): 1 分區號碼
First sector (2048-417791, default 2048): 從哪裏開始(回車默認)
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-417791, default 417791): +10M 大小
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0xb42cefa5
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
Command (m for help): n n創建新的分區
Command action
e extended 擴展分區
p primary partition (1-4) 主分區
e 創建一個擴展分區
Partition number (1-4):
Value out of range.
Partition number (1-4): 2 擴展分區號碼
First sector (22528-417791, default 22528):
Using default value 22528
Last sector, +sectors or +size{K,M,G} (22528-417791, default 417791): 回車默認使用所有容量
Using default value 417791
Command (m for help): n n創建新的分區
Command action
l logical (5 or over) 邏輯分區
p primary partition (1-4) 主分區
l 創建邏輯分區
First sector (24576-417791, default 24576):
Using default value 24576
Last sector, +sectors or +size{K,M,G} (24576-417791, default 417791): +50M 邏輯分區大小
Command (m for help): n n創建新的分區
Command action
l logical (5 or over)
p primary partition (1-4)
l 創建邏輯分區
First sector (129024-417791, default 129024):
Using default value 129024
Last sector, +sectors or +size{K,M,G} (129024-417791, default 417791): +50M 邏輯分區大小
Command (m for help): w 保存並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@zs ~]# fdisk -cu /dev/sdb
Command (m for help): p 查看
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders, total 417792 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: 0xb42cefa5
Device Boot Start End Blocks Id System
/dev/sdb1 2048 22527 10240 83 Linux
/dev/sdb2 22528 417791 197632 5 Extended
/dev/sdb5 24576 126975 51200 83 Linux
/dev/sdb6 129024 231423 51200 83 Linux
第4章 增加硬盤200MB,硬盤創建一個分區掛載到/data目錄
4.1 第1個里程碑-創建分區
fdisk -cu /dev/sdb
Command (m for help): p 查看
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8dde1fa5
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 cylinder (1-204, default 1):
Using default value 1
Last cylinder, +cylinders or +size{K,M,G} (1-204, default 204): 回車默認
Using default value 204
Command (m for help): p 查看
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8dde1fa5
Device Boot Start End Blocks Id System
/dev/sdb1 1 204 208880 83 Linux
Command (m for help): w 保存並退出
The partition table has been altered!
Calling ioctl() to re-read partition table.
Syncing disks.
[root@oldboyedu50-lnb ~]# fdisk -l /dev/sdb 查看
Disk /dev/sdb: 213 MB, 213909504 bytes
64 heads, 32 sectors/track, 204 cylinders
Units = cylinders of 2048 * 512 = 1048576 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x8dde1fa5
Device Boot Start End Blocks Id System
/dev/sdb1 1 204 208880 83 Linux
4.2 第2個里程碑-通知系統sdb磁盤分區表變化
partprobe /dev/sdb
4.3 第3個里程碑-創建文件系統(格式化)
make filesystem
mkfs
#對每個房間裝修(磁盤分區)
mkfs.ext4 /dev/sdb1
4.4 第4個里程碑-關閉磁盤自動檢查
This filesystem will be automatically checked every 38 mounts or
這個磁盤分區會被自動檢查(沒掛載38次或每隔180天)
180 days, whichever comes first. Use tune2fs -c or -i to override.
自己創建的磁盤分區關閉磁盤檢查.
[root@oldboyedu50-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
1. -c 0 關閉 每掛載多少次進行一次磁盤檢查
2. -i 0 關閉 每隔180天進行一次磁盤檢查
4.5 第5個里程碑-掛載
[root@oldboyedu50-lnb ~]# mkdir -p /data 創建目錄/data
[root@oldboyedu50-lnb ~]# mount /dev/sdb1 /data/ 掛載/dev/sdb1 到/data
[root@oldboyedu50-lnb ~]# df –h 查看掛載情況
Filesystem Size Used Avail Use% Mounted on
/dev/sda3 19G 1.5G 17G 9% /
tmpfs 931M 0 931M 0% /dev/shm
/dev/sda1 190M 40M 141M 22% /boot
/dev/sdb1 194M 1.8M 182M 1% /data
4.6 第6個里程碑-永久掛載
4.6.1 #方法1
vim /etc/rc.local
寫入 /bin/mount /dev/sdb1 /data
4.6.2 #方法2
vim /etc/fstab 開機自動掛載
UUID=cf634253-6c41-4771-87b7-e86afb9284a7 / ext4 defaults 1 1
UUID=8519938a-dccb-4eb5-bbbc-4fd22f9f99fe /boot ext4 defaults 1 2
UUID=f70549a5-ec74-4cd1-99ba-49eb175e712d 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/cdrom /mnt iso9660 defaults 0 0
/dev/sdb1 /data ext4 defaults 0 0
3. 磁盤使用小結:
1.分區
2.格式化
3.掛載
第一個里程碑-創建分區並通知系統磁盤的分區表變化
第二個里程碑-創建文件系統-關閉分區的自動檢查功能
第三個里程碑-掛載及永久掛載
第5章 故障案例: java程序佔用大量內存,開始使用swap,swap不足
增加swap
5.1 創建一個文件成爲swap
[root@oldboyedu50-lnb ~]# free -h
total used free shared buffers cached
Mem: 1.8G 158M 1.7G 240K 11M 51M
-/+ buffers/cache: 95M 1.7G
Swap: 767M 0B 767M
5.1.1 創建一個100M的文件
[root@oldboyedu50-lnb ~]# dd if=/dev/zero of=/tmp/100m bs=1M count=100
(input file) (output file ) (block size)
從哪裏獲取數據 輸出到哪裏 每次複製多少文件 複製多少次
/dev/zero 不斷輸出 零
/dev/null 黑洞
[root@oldboyedu50-lnb ~]# file /tmp/100m
/tmp/100m: data
5.1.2 創建swap 讓這個文件成爲swap(格式化)
[root@oldboyedu50-lnb ~]# 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 = 102396 KiB
no label, UUID=531f48e2-be07-4f24-86e5-dfe9ed549b16
[root@oldboyedu50-lnb ~]# file /tmp/100m
/tmp/100m: Linux/i386 swap file (new style) 1 (4K pages) size 25599 pages
5.1.3 激活swap分區
swapon /tmp/100m
[root@oldboyedu50-lnb ~]# swapon /tmp/100m
[root@oldboyedu50-lnb ~]# free -h
total used free shared buffers cached
Mem: 1.8G 264M 1.6G 240K 11M 153M
-/+ buffers/cache: 98M 1.7G
Swap: 867M 0B 867M
swapon -s 顯示swap的組成情況(磁盤分區 和文件)
#fdisk -l
5.1.4 永久增加
Vim
/etc/rc.local
/etc/fstab
#/tmp/100m swap swap defaults 0 0
4. 增加swap小結:
1.創建一個文件
2.文件--->swap
3.激活 和永久生效
第6章 fdisk vs parted
fdisk 支持2TB以內的磁盤 只支持MBR磁盤分區表 |
parted 支持2TB以上的磁盤 MBR(主分區最多4個) GPT(無限 接近100) 主分區 擴展區 邏輯分區 |
6.1 parted
顯示分區信息 | ||
mktable | mklabel gpt msdos(mbr) | 創建磁盤分區表 |
mkpart | 創建分區 | |
rm | 刪除分區 | |
q | 退出不保存 |
6.1.1 #創建分區表
parted /dev/sdc
(parted) mktable gpt 創建磁盤分區表 gpt
(parted) p 查看
Model: VMware, VMware Virtual S (scsi)
Disk /dev/sdc: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
6.2 #創建分區
(parted) mkpart primary 0 10 創建分區primary 大小爲10MB
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: 214MB
Sector size (logical/physical): 512B/512B
Partition Table: gpt
Number Start End Size File system Name Flags
1 17.4kB 10.0MB 9983kB primary
6.3 非交互式創建分區
parted /dev/sdc p
parted /dev/sdc mktable gpt
parted /dev/sdc mkpart primary 0 10 ignore
parted /dev/sdc mkpart primary 10 20
parted /dev/sdc p
總結: |
0. 磁盤分區表 主分區 擴展分區 邏輯分區 |
1. 磁盤分區命名規則 |
2. 磁盤分區過程 |
3. 分區格式化掛載 |
增加swap |
4. 如何查看內存使用情況 |