磁盤管理之磁盤分區
阿新 • • 發佈:2019-01-30
java swa bits 1.4 pre backups 無法 幫助信息 checked
磁盤管理之磁盤分區
磁盤分區表
磁盤分區表位置: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
磁盤管理之磁盤分區