磁盘管理

前言:

下面将总结磁盘管理相关的知识,主要包括磁盘的分区,格式化,挂载,我觉得学习linux的磁盘管理应该和windos结合起来,要明白两种操作系统对磁盘管理的相同点与不同的地方,这样横向总结效果更好。

Windos与Linux对磁盘管理的对比:

1.Windos下的磁盘管理:

1)分区;

说明:我们的电脑磁盘出厂经过初始化后,要想使用,就要对其进行分区,由于受MBR分区表的限制,最多分成4个主分区,如果想要更多的分区,那么可以分成3个主分区和1个扩展分区,其中的扩展分区进而分成多个逻辑分区。

2)格式化;

说明:为分区分配文件系统类型,例如设置为FAT16、或FAT32又或者NTFS。

3)分配盘符;

说明:例如D盘,也可以想象成把其中的一个分区挂载到我的电脑/D盘目录下面。

4)直接对磁盘文件进行读写操作。

2.linux下的磁盘管理:

1)与windos相同,要使用该磁盘首先为磁盘分区;

2)与windos相同,要对磁盘进行格式化,为分区分配文件系统类型,不同的是linux的文件系统通常为ext2、ext3、ext4;

3)在Linux下面无法看到分区,无法像windos有图形化界面直接为磁盘分配盘符,而是给每个分区起了个名字,以文件形式存在,例如sda1,其中sd表示sata接口的磁盘,a表示第一块磁盘,在这使用磁盘方面,windos比linux操作更方便;

4)将磁盘的某一个分区挂载到特定目录,例如把sda2 挂载到/home目录;

5)与windos一样,直接对文件的读写操作。

Linux下的磁盘操作:

1.分区:

分区命令:fdisk + 设备名称

说明:

因为系统管理硬盘,一定要有root权限才能进行分区

fdisk 设备名称

查看硬盘的详细信息:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo fdisk -l /dev/sdb  
  2. Disk /dev/sdb: 21.5 GB, 21474836480 bytes  
  3. 255 heads, 63 sectors/track(扇区), 2610 cylinders(柱面)  
  4. Units = cylinders of 16065 * 512 = 8225280 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes  
若是已经分区了就会列出分区表:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo fdisk -l /dev/sda  
  2. Disk /dev/sda: 32.2 GB, 32212254720 bytes  
  3. 255 heads, 63 sectors/track, 3916 cylinders  
  4. Units = cylinders of 16065 * 512 = 8225280 bytes  
  5. Sector size (logical/physical): 512 bytes / 512 bytes  
  6. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  7. Disk identifier: 0x0007d77e  
  8.    Device Boot      Start         End      Blocks   Id  System  
  9. /dev/sda1               1         262     2097152   82  Linux swap / Solaris  
  10. Partition 1 does not end on cylinder boundary.  
  11. /dev/sda2   *         262        3917    29359104   83  Linux  
开始分区:要记住常用的选项,m,d,p,n,w

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo fdisk /dev/sdb  
  2. Command (m for help): m  
  3. Command action  
  4.    a   toggle a bootable flag  
  5.    b   edit bsd disklabel  
  6.    c   toggle the dos compatibility flag  
  7.    d   delete a partition        删除分区  
  8.    l   list known partition types  
  9.    m   print this menu           打印这个帮助信息  
  10.    n   add a new partition       添加一个新的分区  
  11.    o   create a new empty DOS partition table  
  12.    p   print the partition table   打印当前的分区表  
  13.    q   quit without saving changes  退出但是不保存你的改动  
  14.    s   create a new empty Sun disklabel  
  15.    t   change a partition's system id  
  16.    u   change display/entry units  
  17.    v   verify the partition table  
  18.    w   write table to disk and exit   写分区表到磁盘并且退出  
  19.    x   extra functionality (experts only)  
  20. Command (m for help): n      //添加一个新的分区  
  21. Command action  
  22.    e   extended                  //扩张分区  
  23.    p   primary partition (1-4)   //主分区  
  24. p  
  25. Partition number (1-4): 1        //分区标号  
  26. First cylinder (1-2610, default 1): 1       //分区的大小,默认是以柱面大小进行分配  
  27. Last cylinder, +cylinders or +size{K,M,G} (1-2610, default 2610): 1305  
  28.   
  29. Command (m for help): p   //打印当前的分区表    
  30. Command (m for help): w   //写分区表到磁盘并且退出  
将sdb磁盘的第二个主分划分为扩展分区,并且分配大小变为逻辑分区:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo fdisk /dev/sdb  
  2.   
  3. WARNING: DOS-compatible mode is deprecated. It's strongly recommended to  
  4.          switch off the mode (command 'c') and change display units to  
  5.          sectors (command 'u').  
  6.   
  7. Command (m for help): n  
  8. Command action  
  9.    e   extended  
  10.    p   primary partition (1-4)  
  11. e  
  12. Partition number (1-4): 2  
  13. First cylinder (1306-2610, default 1306):   
  14. Using default value 1306  
  15. Last cylinder, +cylinders or +size{K,M,G} (1306-2610, default 2610):   
  16. Using default value 2610  
  17. Command (m for help): w  
  18. Disk /dev/sdb: 21.5 GB, 21474836480 bytes  
  19. 255 heads, 63 sectors/track, 2610 cylinders  
  20. Units = cylinders of 16065 * 512 = 8225280 bytes  
  21. Sector size (logical/physical): 512 bytes / 512 bytes  
  22. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  23. Disk identifier: 0xeb3b51ab  
  24.    Device Boot      Start         End      Blocks   Id  System  
  25. /dev/sdb1               1        1305    10482381   83  Linux  
删除逻辑分区:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. Command (m for help): d  
  2. Partition number (1-5): 2  
  3. Command (m for help): p  
  4. Disk /dev/sdb: 21.5 GB, 21474836480 bytes  
  5. 255 heads, 63 sectors/track, 2610 cylinders  
  6. Units = cylinders of 16065 * 512 = 8225280 bytes  
  7. Sector size (logical/physical): 512 bytes / 512 bytes  
  8. I/O size (minimum/optimal): 512 bytes / 512 bytes  
  9. Disk identifier: 0xf622ac89  
  10.    Device Boot      Start         End      Blocks   Id  System  
  11. /dev/sdb1               1        1305    10482381   83  Linux  

2.格式化:

命令:mkfs -t fstype device
说明:

-t fstype  fstype是要创建的文件系统的类型,如ext2、ext3、ext4等,不同版本的Linux具有不同的默认文件系统;
device 为要在其上面创建文件系统的设备的名称。

将sdb1格式化为ext4格式:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo mkfs -t ext4 /dev/sdb1  
  2. mke2fs 1.41.12 (17-May-2010)  
  3. 文件系统标签=  
  4. 操作系统:Linux  
  5. 块大小=4096 (log=2)  
  6. 分块大小=4096 (log=2)  
  7. Stride=0 blocks, Stripe width=0 blocks  
  8. 655360 inodes, 2620595 blocks  
  9. 131029 blocks (5.00%) reserved for the super user  
  10. 第一个数据块=0  
  11. Maximum filesystem blocks=2684354560  
  12. 80 block groups  
  13. 32768 blocks per group, 32768 fragments per group  
  14. 8192 inodes per group  
  15. Superblock backups stored on blocks:   
  16.     32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632  
  17. 正在写入inode表: 完成                              
  18. Creating journal (32768 blocks): 完成  
  19. Writing superblocks and filesystem accounting information: 完成  
  20. This filesystem will be automatically checked every 20 mounts or  
  21. 180 days, whichever comes first.  Use tune2fs -c or -i to override.  

3.挂载:

命令:mout -t 文件系统 挂载设备 挂载点

说明:

挂载点通常要挂载/mnt目录下
通常在/mnt目录下创建文件夹,作为挂载点:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ cd /mnt/  
  2. [xianzan@centos mnt]$ sudo mkdir sdb1  
考虑到这个挂载点是用来存放数据,将sdb1文件夹命名为data:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos mnt]$ sudo mv sdb1 data  
挂载,将需要使用的磁盘分区挂载到挂载点:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos mnt]$ sudo mount -t ext4 /dev/sdb1 /mnt/data/  
  2. [xianzan@centos mnt]$ ls  
  3. cdrom  data  hgfs  
用mount命令查看挂载点:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos mnt]$ mount  
  2. /dev/sda2 on / type ext4 (rw)  
  3. proc on /proc type proc (rw)  
  4. sysfs on /sys type sysfs (rw)  
  5. devpts on /dev/pts type devpts (rw,gid=5,mode=620)  
  6. tmpfs on /dev/shm type tmpfs (rw,rootcontext="system_u:object_r:tmpfs_t:s0")  
  7. none on /proc/sys/fs/binfmt_misc type binfmt_misc (rw)  
  8. .host:/ on /mnt/hgfs type vmhgfs (rw,ttl=1)  
  9. vmware-vmblock on /var/run/vmblock-fuse type fuse.vmware-vmblock (rw,nosuid,nodev,default_permissions,allow_other)  
  10. /dev/sr0 on /media/CentOS_6.7_Final type iso9660 (ro,nosuid,nodev,uhelper=udisks,uid=500,gid=500,iocharset=utf8,mode=0400,dmode=0500)  
  11. /dev/sdb1 on /mnt/data type ext4 (rw)  

对挂载点的分区进行操作:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos mnt]$ cd data/  
  2. [xianzan@centos data]$ ls  
  3. lost+found  
存在lost+found目录,你可能丢失的数据存放在该目录下,一般很少用到它:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos data]$ sudo rm -rf *  
在data挂载点存放sql.db文件:
[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos data]$ sudo touch sql.db  
  2. [xianzan@centos data]$ ls  
  3. sql.db  
卸载挂载点用umount 挂载点:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos ~]$ sudo umount /mnt/data/  
  2. [xianzan@centos ~]$ ls /mnt/data/  
  3. 你找不到sql.db文件,只有重新挂载才能找到sql.db文件  
在为挂载前的挂载点创建文件:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos data]$ sudo touch hello.txt  
  2. [xianzan@centos data]$ ls  
  3. hello.txt  
重新挂载:

[plain]  view plain  copy
  在CODE上查看代码片 派生到我的代码片
  1. [xianzan@centos data]$ sudo mount /dev/sdb1 /mnt/data/  
  2. [xianzan@centos data]$ ls  
  3. sql.db  

问题:

只存在挂载点创建的sql.db文件,为挂载是在挂载点创建的hello.txt没有找到,那文件在哪里了?

可以这样理解:

1)在挂载点挂载之前创建的文件夹或者文件是属于根的;

2)在挂载点挂载之后创造的文件或者文件夹是属于挂载的磁盘的;

3)就像在Windos下,没有插U盘时,在文件夹下创建文件或者文件夹,和插U盘后在U盘创建文件或者文件夹。









  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值