您的位置:首页 > 运维架构 > Linux

20151117 Linux下磁盘管理(centos 6.5)1.添加硬盘 2.硬盘分区 3.格式化硬盘 4.挂载硬盘 5.设置开机自动挂载】

2015-11-17 17:14 981 查看
1. 查看磁盘或者目录的容量
df 查看磁盘各分区使用情况
不加参数以k为单位
df -i inode数,df -h 以G或者T或者M
df -m 以M单位显示

测试:
[root@niejicai-linux ~]# df -h 查看系统磁盘的使用情况
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 2.7G 15G 16% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 283M 54M 215M 20% /boot

[root@niejicai-linux ~]# df 不加参数以k为单位
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/sda2 19175716 2758780 15436200 16% /
tmpfs 124360 0 124360 0% /dev/shm 内存
/dev/sda1 289293 54311 219622 20% /boot
[root@niejicai-linux ~]# df -m 以M单位显示
Filesystem 1M-blocks Used Available Use% Mounted on
/dev/sda2 18727 2695 15075 16% /
tmpfs 122 0 122 0% /dev/shm
/dev/sda1 283 54 215 20% /boot

[root@niejicai-linux ~]# free 查看Swap
total used free shared buffers cached
Mem: 248724 166984 81740 196 29684 89520
-/+ buffers/cache: 47780 200944
Swap: 1048572 56 1048516

[root@niejicai-linux ~]# df -i inode数,每个文件到有自己的inodes,等inodes用完了,磁盘也用不了,需要去删除数据,或者去用软连接。
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda2 1226400 116658 1109742 10% /
tmpfs 31090 1 31089 1% /dev/shm
/dev/sda1 76912 44 76868 1% /boot

[root@niejicai-linux ~]# df -T 查看文件系统所用的格式。
Filesystem Type 1K-blocks Used Available Use% Mounted on
/dev/sda2 ext4 19175716 2758788 15436192 16% /
tmpfs tmpfs 124360 0 124360 0% /dev/shm
/dev/sda1 ext4 289293 54311 219622 20% /boot

2.du 查看目录或者文件的容量
不加参数 以k为单位显示
-s 列出总和
-a 列出所有文件和目录
-k 以k为单位
-m 以M为单位
-h 最合适的单位 最常用组合 -sh
扩展 ls -lh 看到的大小和du相比

测试:

[root@niejicai-linux ~]# du -sh /etc/
这 个用了最多!
40M /etc/
[root@niejicai-linux ~]# du -sh /tmp/
768K /tmp/
[root@niejicai-linux ~]# du -sm /etc/
40 /etc/
[root@niejicai-linux ~]# du -sk /etc/
40628 /etc/
[root@niejicai-linux ~]# du -sb /etc/
37625438 /etc/
扩展 ls -lh 看到的大小和du相比

[root@niejicai-linux ~]# ls -lhd /etc/
drwxr-xr-x. 103 root root 12K Nov 17 10:56 /etc/

[root@niejicai-linux ~]# touch 10.txt
[root@niejicai-linux ~]# echo "000000" > 10.txt
[root@niejicai-linux ~]# ls -lh 10.txt
-rw-r--r-- 1 root root 7 Nov 17 11:15 10.txt 7k
[root@niejicai-linux ~]# du -sh 10.txt
4.0K 10.txt
4k

3. 磁盘的分区和格式化
(第一步,添加硬盘;第二步分区;第三步格式化,第四挂载)
fdisk -l 查看磁盘和分区,大于2T用GPT
fdisk 设备名 m 列出常用命令,常用的有‘p’, ‘n’, ‘d’, ‘w’, ‘q’.
mke2fs, mkfs.ext2, mkfs.ext3, mkfs.ext4
-t 文件系统格式 ext3 ext4
-b 指定每个数据块大小,目前支持1024, 2048 及4096
-L 分区标签
格式化时默认blocks (5.00%) reserved for the super user //给超级用户预留的一部分空间,用来作为特殊用途
-m 可以更改这个比例,如 mke2fs -m 0.1 /dev/sdb1 //意思是,只预留出0.1%

测试:

3.1增加硬盘:
在虚拟机上面添加了一块10G硬盘

[root@niejicai-linux ~]# fdisk -l
查看磁盘和分区,大于2T用GPT
Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000afeab
Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2481 19614720 83 Linux
/dev/sda3 2481 2611 1048576 82 Linux swap / Solaris
Disk /dev/sdb: 10.7 GB, 10737418240 bytes 刚刚新添加进来的硬盘还没有分区!
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x00000000

3.2分区:

[root@niejicai-linux ~]# fdisk /dev/sdb
Device contains neither a valid DOS partition table, nor Sun, SGI or OSF disklabel
Building a new DOS disklabel with disk identifier 0xa927e91e.
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)

WARNING: DOS-compatible mode is deprecated. It's strongly recommended to
switch off the mode (command 'c') and change display units to
sectors (command 'u').

Command (m for help): m
设备名 m 列出常用命令
Command action
a toggle a bootable flag
b edit bsd disklabel
c toggle the dos compatibility flag
d delete a partition
l list known partition types
m print this menu
n add a new partition
o create a new empty DOS partition table
p print the partition table
q quit without saving changes
s create a new empty Sun disklabel
t change a partition's system id
u change display/entry units
v verify the partition table
w write table to disk and exit
x extra functionality (experts only)

一个磁盘的主分区最多分4个,e+p<=4

Command (m for help): p
查看分区
Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x7db1aebb

Device Boot Start End Blocks Id System

Command (m for help): n
创建分区
e extended
p primary partition (1-4)
p 创建主分区
Partition number (1-4): 1 从第一块开始
First cylinder (1-1305, default 1): 1 (从一块柱面可以,也是大小从1开始)
Last cylinder, +cylinders or +size{K,M,G} (1-1305, default 1305): +1G
(增加1G)

Command (m for help): n
Command action
e extended
p primary partition (1-4)
e 创建扩展分区
Partition number (1-4): 2 这里要从第2块开始,因为第一块已经有人用了
First cylinder (133-1305, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-1305, default 1305): +5G 分到5G

Command (m for help): p 查看分区

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa927e91e

Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux 主分区
/dev/sdb2 133 786 5253255 5 Extended 扩展分区

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
l
First cylinder (133-786, default 133):
Using default value 133
Last cylinder, +cylinders or +size{K,M,G} (133-786, default 786): +1G

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa927e91e

Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 786 5253255 5 Extended 扩展分区包含逻辑分区
(扩展分区不用来存放数据,而是用来存放逻辑分区)
/dev/sdb5 133 264 1060258+ 83 Linux 逻辑分区(逻辑分区从sdb5开始)

Command (m for help): n
Command action
l logical (5 or over)
p primary partition (1-4)
p
Partition number (1-4): 3 这里从第三块开始,因为前面已经使用了2块。
First cylinder (787-1305, default 787):
Using default value 787
Last cylinder, +cylinders or +size{K,M,G} (787-1305, default 1305): +1G

Command (m for help): p

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa927e91e

Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux 主分区
/dev/sdb2 133 786 5253255 5 Extended 扩展分区
/dev/sdb3 787 918 1060290 83 Linux 主分区
/dev/sdb5 133 264 1060258+ 83 Linux 逻辑分区 e+P<=4
扩展分区+主分区<=4

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

Calling ioctl() to re-read partition table.
Syncing disks.

[root@niejicai-linux ~]# fdisk -l

Disk /dev/sda: 21.5 GB, 21474836480 bytes
255 heads, 63 sectors/track, 2610 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0x000afeab

Device Boot Start End Blocks Id System
/dev/sda1 * 1 39 307200 83 Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2 39 2481 19614720 83 Linux
/dev/sda3 2481 2611 1048576 82 Linux swap / Sola ris

Disk /dev/sdb: 10.7 GB, 10737418240 bytes
255 heads, 63 sectors/track, 1305 cylinders
Units = cylinders of 16065 * 512 = 8225280 bytes
Sector size (logical/physical): 512 bytes / 512 bytes
I/O size (minimum/optimal): 512 bytes / 512 bytes
Disk identifier: 0xa927e91e

Device Boot Start End Blocks Id System
/dev/sdb1 1 132 1060258+ 83 Linux
/dev/sdb2 133 786 5253255 5 Extended
/dev/sdb3 787 918 1060290 83 Linux
/dev/sdb5 133 264 1060258+ 83 Linux
[root@niejicai-linux ~]#

3.3格式化:

[root@niejicai-linux ~]# cat /etc/filesystems 文件系统格式
ext4
ext3
ext2
nodev proc
nodev devpts
iso9660
vfat
hfs
hfsplus

[root@niejicai-linux ~]# mke2fs -t ext4 /dev/sdb1
格式化第一块分区,ext4格式
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
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376
Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: don
This filesystem will be automatically checked every 20 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.
[root@niejicai-linux ~]#

[root@niejicai-linux ~]# mke2fs -t ext4 -b 1024 /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
66560 inodes, 1060256 blocks
53012 blocks (5.00%) reserved for the super user
First data block=1
Maximum filesystem blocks=68419584
130 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001
Writing inode tables: done
Creating journal (32768 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.
[root@niejicai-linux ~]#

[root@niejicai-linux ~]# mke2fs -t ext4 -b 1024 -m 0.1 /dev/sdb1 -L niejicai 可以更改标签
mke2fs 1.41.12 (17-May-2010)
Filesystem label=niejicai
OS type: Linux
Block size=1024 (log=0)
Fragment size=1024 (log=0)
Stride=0 blocks, Stripe width=0 blocks
66560 inodes, 1060256 blocks
1060 blocks (0.10%) reserved for the super user
First data block=1
Maximum filesystem blocks=68419584
130 block groups
8192 blocks per group, 8192 fragments per group
512 inodes per group
Superblock backups stored on blocks:
8193, 24577, 40961, 57345, 73729, 204801, 221185, 401409, 663553,
1024001

Writing inode tables: done
Creating journal (32768 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.
[root@niejicai-linux ~]#

3.4 挂载:

[root@niejicai-linux ~]# mount 查看系统所用硬盘的挂载,以及文件系统格式
/dev/sda2 on / type ext4 (rw)
proc on /proc type proc (rw)
sysfs on /sys type sysfs (rw)
devpts on /dev/pts type devpts (rw,gid=5,mode=620)
tmpfs on /dev/shm type tmpfs (rw)
/dev/sda1 on /boot type ext4 (rw)
none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)
vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)

[root@niejicai-linux ~]# mount /dev/sdb1 /mnt/
[root@niejicai-linux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 2.7G 15G 16% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 283M 54M 215M 20% /boot
/dev/sdb1 987M 2.8M 984M 1% /mnt
继续格式化,以下是格式化(sdb5,逻辑分区)

[root@niejicai-linux ~]# mke2fs -t ext4 /dev/sdb5
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
66384 inodes, 265064 blocks
13253 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=272629760
9 block groups
32768 blocks per group, 32768 fragments per group
7376 inodes per group
Superblock backups stored on blocks:
32768, 98304, 163840, 229376

Writing inode tables: done
Creating journal (8192 blocks): done
Writing superblocks and filesystem accounting information: done
This filesystem will be automatically checked every 30 mounts or
180 days, whichever comes first. Use tune2fs -c or -i to override.

[root@niejicai-linux ~]# mount /dev/sdb5 /mnt/
[root@niejicai-linux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 2.7G 15G 16% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 283M 54M 215M 20% /boot
/dev/sdb1 988M 1.3M 935M 1% /mnt
/dev/sdb5 988M 1.3M 935M 1% /mnt
[root@niejicai-linux ~]#

[root@niejicai-linux ~]# umount /mnt/ 除去挂载
[root@niejicai-linux ~]# umount /mnt/ 除去挂载
[root@niejicai-linux ~]# mkdir /db1 /db5 建立两个目录,db1和db5,然后分别挂载进去
[root@niejicai-linux ~]# mount /dev/sdb1 /db1 把/dev/sdb1分区挂载到db1
[root@niejicai-linux ~]# mount /dev/sdb5 /db5 把/dev/sdb5分区挂载到db5
[root@niejicai-linux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 2.7G 15G 16% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 283M 54M 215M 20% /boot
/dev/sdb1 987M 2.8M 984M 1% /db1
/dev/sdb5 988M 1.3M 935M 1% /db5

[root@niejicai-linux ~]# cat /etc/fstab
# /etc/fstab
# Created by anaconda on Tue Nov 10 02:09:27 2015
# Accessible filesystems, by reference, are maintained under '/ dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) f or more info
#
UUID=ef85e327-ef53-49b5-b2bc-f7ffa0221180 / ext4 defaults 1 1
UUID=b7a93971-6ac6-4a27-9ae6-b8b4756ffab9 /boot ext4 defaults 1 2
UUID=4139feb2-6d52-4950-9b86-73c94002c044 swap swap defaults 0 0
tmpfs /dev/shm tmpfs default s 0 0
devpts /dev/pts devpts gid=5,m ode=620 0 0
sysfs /sys sysfs default s 0 0
proc /proc proc default s 0 0

3.5设置开机自动挂载

[root@niejicai-linux ~]# vim /etc/fstab

# /etc/fstab
# Created by anaconda on Tue Nov 10 02:09:27 2015
# Accessible filesystems, by reference, are maintained under '/dev/disk'
# See man pages fstab(5), findfs(8), mount(8) and/or blkid(8) for more info
UUID=ef85e327-ef53-49b5-b2bc-f7ffa0221180 / ext4 defaults 1 1
UUID=b7a93971-6ac6-4a27-9ae6-b8b4756ffab9 /boot ext4 defaults 1 2
UUID=4139feb2-6d52-4950-9b86-73c94002c044 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 /db1 ext4 defaults 0 0 添加
/dev/sdb5 /db5 ext4 defaults 0 0

3.6挂载完之后就可以写东西了

[root@niejicai-linux ~]# df -h
Filesystem Size Used Avail Use% Mounted on
/dev/sda2 19G 2.7G 15G 16% /
tmpfs 122M 0 122M 0% /dev/shm
/dev/sda1 283M 54M 215M 20% /boot
/dev/sdb1 987M 2.8M 984M 1% /db1
/dev/sdb5 988M 1.3M 935M 1% /db5
[root@niejicai-linux ~]# touch /db5/3333.txt
[root@niejicai-linux ~]# ls -l /db5/
total 16
-rw-r--r-- 1 root root 0 Nov 17 16:06 3333.txt
drwx------ 2 root root 16384 Nov 17 15:41 lost+found

[root@niejicai-linux ~]# cd /db5/
[root@niejicai-linux db5]# ls
3333.txt lost+found

还可以直接挂载iso

[root@niejicai-linux ~]# mount -o loop niejicai.iso /mnt/

强制卸载
[root@niejicai-linux ~]# umount -l /db5

补充:



内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: