zoukankan      html  css  js  c++  java
  • Linux硬盘分区挂载及swap分区扩容

    本文以纯代码形式书写,请各位同仁在阅读时抓住带颜色字体的位置。这才是重中之重!!

     (小编也不知道使用代码称呼呢还是用指令称呼 O(∩_∩)O哈哈~)

    [root@localhost ~]fdisk -l             查看磁盘信息

    Disk /dev/sda: 32.2 GB, 32212254720 bytes, 62914560 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 label type: dos
    Disk identifier: 0x000b4990


       Device Boot      Start         End      Blocks   Id  System
    /dev/sda1   *        2048     2099199     1048576   83  Linux
    /dev/sda2         2099200    62914559    30407680   8e  Linux LVM

    Disk /dev/sdb: 10.7 GB, 10737418240 bytes, 20971520 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 label type: dos
    Disk identifier: 0x24dd6e32

       Device Boot      Start         End      Blocks   Id  System
    /dev/sdb1            2048    10487807     5242880   83  Linux
    /dev/sdb2        10487808    18876415     4194304   83  Linux

    Disk /dev/sdc: 8589 MB, 8589934592 bytes, 16777216 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

    这是我刚分区的一快硬盘8G 。 简单讲述一下硬盘命名规则: sda 是指第一块一盘。 sdb是指第二块一盘。sdc则是第三块一盘。以此类推。。。

    拓展知识: 假设硬盘分区名称是: sdb1 ,是指第二块硬盘分区序号为1。 sdb3则是分区序号为3 。  并不能直接判断是第三块分区。

    现在需要对这块硬盘进行分区:

    [root@localhost ~]fdisk /dev/sdc
    Welcome to fdisk (util-linux 2.23.2).

    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.

    Device does not contain a recognized partition table
    Building a new DOS disklabel with disk identifier 0xbb8b6c44.

    Command (m for help): n         按 “n” 进入分区模式 。   (非专业术语)       

                            
    Partition type:
       p   primary (0 primary, 0 extended, 4 free)     按 "p"  创建主分区

      e   extended                    按 "e"  创建逻辑分区

    Select (default p): p
    Partition number (1-4, default 1): 1      系统要求您先输入主分区的编号,主分区的编号范围是1~4。 默认输出1
    First sector (2048-16777215, default 2048):   此处按回车键即可
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-16777215, default 16777215): +2G      输入分区的容量 (一般以G作为单位)
    Partition 1 of type Linux and of size 2 GiB is set

    Command (m for help): w          在这里可以按 p  查看刚才分区的信息 。 按 w  为保存分区 。 
    The partition table has been altered!
    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@localhost ~] fdisk -l
       Device Boot      Start         End      Blocks   Id  System
    /dev/sdc1            2048     4196351     2097152   83  Linux                    这是刚才分区的信息。
     
    Disk /dev/mapper/centos-root: 29.0 GB, 28982640640 bytes, 56606720 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

    上述步骤只是对硬盘进行分区,新分区信息系统无法自动化同步给Linux内核需要手动操作。

    [root@localhost ~]file /dev/sbc1         这个指令的意思呢 (个人理解):查看分区是否存在。
    /dev/sbc1: cannot open (No such file or directory)
    [root@localhost ~]partprobe             把新分区信息同步到Linux内核,(建议敲打两次指令,如果还是不行那就重新启动系统)
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    [root@localhost ~]partprobe
    Warning: Unable to open /dev/sr0 read-write (Read-only file system).  /dev/sr0 has been opened read-only.
    [root@localhost ~]

    新分区进行格式化。

    [root@localhost ~]mkfs             这个是格式化分区的指令之一(肯定还有其它的咯)    连续按两次Tab键就会出现下列文件系统格式。
    mkfs         mkfs.btrfs   mkfs.cramfs   mkfs.ext2    mkfs.ext3    mkfs.ext4    mkfs.minix   mkfs.xfs     这个是文件系统的格式。(自己想要什么样的格式,就选择什么样的格式)
    [root@localhost ~]mkfs.ext4 /dev/sdc1            mkfs使用的语法
    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=4096 (log=2)
    Fragment size=4096 (log=2)
    Stride=0 blocks, Stripe width=0 blocks
    131072 inodes, 524288 blocks
    26214 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=536870912
    16 block groups
    32768 blocks per group, 32768 fragments per group
    8192 inodes per group
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912

    Allocating group tables: done
    Writing inode tables: done
    Creating journal (16384 blocks): done
    Writing superblocks and filesystem accounting information:     此处按回车键。。。。
    done
    [root@localhost ~]
    终于到了挂载的时候了。。 。 (好心塞!用的还是午休的时间  ┭┮﹏┭┮)

    简单理解点呢就是:先建立一个目录文件夹(必须是空目录)。这个文件夹的作用是用来关联挂载。变相的来说也就是一个中转站。

    本文以 /.newFS  隐藏目录作为实验。(不一定要跟俺一样哈)

    [root@localhost ~]mount     /dev/sdc1 /.newFS/
    [root@localhost ~]                    就是这个样子。  可以尝试复制一些目录到这个 /.newFS
    [root@localhost ~]cp  -pr  /etc/*    /.newFS
    [root@localhost /.newFS]ls   -l    /.newFS
    total 1336
    -rw-r--r--.  1 root root     16 Jan 25 08:11 adjtime
    -rw-r--r--.  1 root root   1518 Jan 25 08:11 aliases
    -rw-r--r--.  1 root root  12288 Jan 25 08:11 aliases.db
    drwxr-xr-x.  2 root root   4096 Jan 25 08:11 alternatives
    -rw-------.  1 root root    541 Jan 25 08:11 anacrontab
    -rw-r--r--.  1 root root     55 Jan 25 08:11 asound.conf
    drwxr-x---.  3 root root   4096 Jan 25 08:11 audisp
    drwxr-x---.  3 root root   4096 Jan 25 08:11 audit
    drwxr-xr-x.  2 root root   4096 Jan 25 08:11 bash_completion.d
    -rw-r--r--.  1 root root   2853 Jan 25 08:11 bashrc
    drwxr-xr-x.  2 root root   4096 Jan 25 08:11 binfmt.d
    -rw-r--r--.  1 root root     38 Jan 25 08:11 centos-release
    -rw-r--r--.  1 root root     51 Jan 25 08:11 centos-release-upstream
    drwxr-xr-x.  2 root root   4096 Jan 25 08:11 chkconfig.d                         省略其它文件..........

    为了能够让新的交换分区设备在重启后依然生效,需要按照下面的格式将相关信息写入到配置文件中,并记得保存。

    [root@localhost ~]vi      /etc/fstab                编辑配置文件
    #
    # /etc/fstab
    # Created by anaconda on Tue Jan 16 05:39:55 2018
    #
    # 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
    #
    /dev/mapper/centos-root /                       xfs     defaults        0 0
    UUID=96ff1fb8-970f-4804-af36-140cc753ca5d /boot                   xfs     defaults        0 0
    /dev/mapper/centos-swap swap                    swap    defaults        0 0
    /dev/sdb2 swap swap default 0 0
    /dev/sdc1   /.newFS    ext4    defaults  0  0                      /dev/sdc1(设备)   /.newFS(挂载目录)   ext4(文件系统类型)     defaults  0  0 (默认值为0)
    编辑完后,记得要保存。

    OK , 硬盘分区及挂载到此结束。


    对swap分区进行扩容。

    先对硬盘进行分区(主分区)及同步到Linux内核。(如何分区请看上面的操作。)

    swap分区专用的格式化命令mkswap,对新建的主分区进行格式化操作。

    [root@localhost ~]free -m              查看swap分区的信息
                  total        used        free      shared  buff/cache   available
    Mem:           1839         106        1499           8         233        1558
    Swap:          6143           0        6143
    [root@localhost ~]
    [root@localhost ~]mkswap /dev/sdc2                   格式化新分区
    Setting up swapspace version 1, size = 3145724 KiB
    no label, UUID=fee6a4e0-44ae-4818-a49e-be10a5c0ae1a
    [root@localhost ~]swapon /dev/sdc2                 准备好的swap分区挂在到系统中
    [root@localhost ~]free -m               
                  total        used        free      shared  buff/cache   available
    Mem:           1839         109        1497           8         233        1556
    Swap:          9215           0        9215                       这个位置6143变成了9215。

    [root@localhost ~]vi   /etc/fstab
    /dev/sdc2  swap  swap  defaults 0 0          swap分区保存          
    OK,swap分区扩大到此结束。

    注:转载时请标明出处及作者。如果这篇让您看起来头疼的文章对您有所帮助,请给个赞。 Thank you!

    刚入们的超级小白,学习过程当中记录一些异常信息及解决方法。 欢迎大家一起交流。
  • 相关阅读:
    Picture Control点击事件
    在C/C++中获取可执行文件的图标和信息
    C++获取系统图标方法
    C++ Vector 使用总结
    C++中vector和list的区别
    STL STD::list使用说明
    演示My97 DatePicker过程中的错误
    HTML5的语法变化
    利用 ACE 来实现 UDP 通讯
    VS2010中“工具>选项中的VC++目录编辑功能已被否决”解决方法
  • 原文地址:https://www.cnblogs.com/JiangDameiniu/p/8352951.html
Copyright © 2011-2022 走看看