磁盤分區

第1章 知識回顧:

1.磁盤組成

2.RAID級別

3.如何讓系統更安全

[root@oldboyedu50-lnb ~]# md5sum oldboy.txt

890b185727556f1be31d7fe5ee5ce4dc  oldboy.txt

[root@oldboyedu50-lnb ~]# sha512sum oldboy.txt

c9a326ffb217c4dc7f72ccf02aba9abf9ec94ca40aa47d848f57741e313a7df52b80f8cca9130acc5930815a1728d93bd781b29d4598eb5cbcaf55ef6e2a7d98  oldboy.txt

4.如何防止系統中×××

5磁盤接口 磁盤組成

6如何進行計算

實例1-1              如何查看內存使用情況

[root@oldboyedu50-lnb ~]# free -h

             total       used       free     shared    buffers     cached

Mem:          1.8G       1.6G       195M       236K       106M       1.3G

-/+ buffers/cache:       189M       1.6G   

Swap:         767M         0B       767M

實例1-2              磁盤分區

1.2 Linux啓動流程:

1. 開機自檢BIOS

2. MBR引導

3. GRUB菜單

4. 加載內核

5. 運行init進程 1個進程

6. 讀取運行級別 /etc/inittab

7. /etc/rc.sysinit 系統初始化

8. /etc/rc3.d  根據運行級別 啓動對應服務(開機自啓動)

9. mingetty    登錄頁面

1.3 磁盤分區

1.磁盤的引導扇區  0磁頭 0磁道 1扇區

MBR引導         001扇區前446字節

MBR Master Boot Record    主引導記錄 引導系統啓動

DPT  (Disk   Partition Table) 磁盤分區表 記錄着磁盤分區從哪裏開始到哪裏結束

1.      主分區(primary) 每個分區佔用16個字節的分區表

一般一定要有

存放數據

2.      擴展分區(extended) 無法直接使用的

最多1

無法直接使用

3.      邏輯分區(logical

存放數據

1.4 磁盤分區

[root@oldboyedu50-lnb ~]# fdisk -l |grep sd[a-c]:

Disk /dev/sda: 21.5 GB, 21474836480 bytes

Disk /dev/sdc: 213 MB, 213909504 bytes

Disk /dev/sdb: 213 MB, 213909504 bytes

fdisk  創建分區

-u    磁盤分區的時候以扇區爲單位默認是按照柱面

-c     關閉dos兼容模式

fdisk -cu  /dev/sdb

fdisk內部命令   

顯示幫助

n  new 創建分區

顯示所有分區信息

刪除分區

保存並退出

退出不保存

實例1-3              創建一個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

實例1-4              創建10MB的主分區 創建一個使用所有容量的擴展分區  創建兩個50MB邏輯分區

[root@oldboyedu50-lnb ~]# 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: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

Command (m for help): n

Command action

   e   extended

   p   primary partition (1-4)

e                               #創建擴展分區

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): 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: 0x8dde1fa5

   Device Boot      Start         End      Blocks   Id  System

/dev/sdb1            2048       22527       10240   83  Linux

/dev/sdb2           22528      417791      197632    5  Extended

Command (m for help): 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  #大小50MB

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: 0x8dde1fa5

   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

Command (m for help): 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  #大小50MB

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: 0x8dde1fa5

   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

實例1-5              增加硬盤200MB,硬盤創建一個分區掛載到/data目錄

1個里程碑-創建分區

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

2個里程碑-通知系統sdb磁盤分區表變化

partprobe /dev/sdb

3個里程碑-創建文件系統(格式化)

            make filesystem

            mkfs

            #對每個房間裝修(磁盤分區)

mkfs.ext4  /dev/sdb1

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

-c  0  關閉 每掛載多少次進行一次磁盤檢查

-i  0  關閉 每隔180天進行一次磁盤檢查

5個里程碑-掛載

[root@oldboyedu50-lnb ~]# mkdir -p /data

[root@oldboyedu50-lnb ~]# mount  /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

6個里程碑-永久掛載

#方法1

/etc/rc.local

#方法2

/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 

實例1-6              故障案例: java程序佔用大量內存,開始使用swapswap不足

創建一個文件成爲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

1.創建一個100M的文件

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

                               input                  output          block

                               file                   file            size

[root@oldboyedu50-lnb ~]# file /tmp/100m

/tmp/100m: data

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

 

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

 

4.永久增加

/etc/rc.local

/etc/fstab

#/tmp/100m               swap                    swap    defaults        0 0

print    顯示分區信息

parted /dev/sdc

mktable mklabel  創建磁盤分區表 

                 gpt

                 msdos(mbr)

mkpart           創建分區

rm               刪除分區

q                退出不保存

 

實例1-7              企業創建分區

1.創建分區表

(parted) mktable  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

 

2.創建分區

(parted) mkpart  primary  0  10

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

非交互式創建分區

 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


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章