zoukankan      html  css  js  c++  java
  • 磁盘分区管理基础题讲解

    基础技能演练

    1. 要求创建一个 ext4 的分区,大小为 1G, ID号指示为 linux。格式化后,挂载到/media/test,并在/medita/test/目录内创建一个文件 mount.txt 。然后将其设为开机自动挂载,重启检查实验结果。


    [root@server01 ~]# fdisk -cul

    Disk /dev/vda: 21.5 GB, 21474836480 bytes
    16 heads, 63 sectors/track, 41610 cylinders, total 41943040 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: 0x000a7672

    Device Boot Start End Blocks Id System
    /dev/vda1 * 2048 1026047 512000 83 Linux
    /dev/vda2 1026048 41943039 20458496 8e Linux LVM

    Disk /dev/vdb: 5368 MB, 5368709120 bytes
    16 heads, 63 sectors/track, 10402 cylinders, total 10485760 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: 0x1e4d4021

    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 2099199 1048576 83 Linux
    /dev/vdb2 2099200 4196351 1048576 83 Linux

    这里有两个硬盘,vda, vdb,观察sectors,vda已经全部分完区,vdb总共10485760 sectors,现分区用到4196351 sectors , vdb还有剩余的空间(注意,留意每个分区是否是连续,即上一分区的End扇区和下一分区的Start扇区是否连续)。

    如果系统中所有硬盘都没有剩余的空间,那就需要再添加一块虚拟硬盘。

    过程参考《09_linux磁盘分区管理KVM环境演示.txt》

    -----------------------------------------
    提高技能实践

    2.要求创建新的一个分区,大小为 512M ,ID号指示为 linux swap 。
    1)将该512M的分区设置成交换分区,并启用,用 free 查看实验结果。
    2)再使用dd命令创建一个512M的空文件,设置成交换文件,并启用。


    [root@server01 ~]# fdisk -cu /dev/vdb

    Command (m for help): p
    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 2099199 1048576 83 Linux
    /dev/vdb2 2099200 4196351 1048576 83 Linux
    /dev/vdb3 4196352 6293503 1048576 83 Linux

    观察到现在已经有了3个主分区,如果考虑到将来需要创建>4个分区,就必须第4个分区为扩展分区。
    Command (m for help): n
    Command action
    e extended
    p primary partition (1-4)
    e //选择创建扩展分区
    Selected partition 4
    First sector (6293504-10485759, default 6293504):
    Using default value 6293504
    Last sector, +sectors or +size{K,M,G} (6293504-10485759, default 10485759):
    Using default value 10485759

    Command (m for help): n //在扩展分区上再创建主分区
    First sector (6295552-10485759, default 6295552):
    Using default value 6295552
    Last sector, +sectors or +size{K,M,G} (6295552-10485759, default 10485759): +512M

    Command (m for help): p

    Disk /dev/vdb: 5368 MB, 5368709120 bytes
    16 heads, 63 sectors/track, 10402 cylinders, total 10485760 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: 0x1e4d4021

    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 2099199 1048576 83 Linux
    /dev/vdb2 2099200 4196351 1048576 83 Linux
    /dev/vdb3 4196352 6293503 1048576 83 Linux
    /dev/vdb4 6293504 10485759 2096128 5 Extended
    /dev/vdb5 6295552 7344127 524288 83 Linux

    Command (m for help): t // 修改分区的ID和System
    Partition number (1-5): 5
    Hex code (type L to list codes): L
    0 Empty 24 NEC DOS 81 Minix / old Lin bf Solaris
    1 FAT12 39 Plan 9 82 Linux swap / So c1 DRDOS/sec (FAT-

    .........


    Hex code (type L to list codes): 82
    Changed system type of partition 5 to 82 (Linux swap / Solaris)

    Command (m for help): p

    Disk /dev/vdb: 5368 MB, 5368709120 bytes
    16 heads, 63 sectors/track, 10402 cylinders, total 10485760 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: 0x1e4d4021

    Device Boot Start End Blocks Id System
    /dev/vdb1 2048 2099199 1048576 83 Linux
    /dev/vdb2 2099200 4196351 1048576 83 Linux
    /dev/vdb3 4196352 6293503 1048576 83 Linux
    /dev/vdb4 6293504 10485759 2096128 5 Extended
    /dev/vdb5 6295552 7344127 524288 82 Linux swap / Solaris

    Command (m for help): w


    [root@server01 ~]# partx -a /dev/vdb

    [root@server01 ~]# cat /proc/partitions |grep vdb
    252 16 5242880 vdb
    252 17 1048576 vdb1
    252 18 1048576 vdb2
    252 19 1048576 vdb3
    252 20 1024 vdb4
    252 21 524288 vdb5


    把分区格式化为swap的文件系统:
    [root@server01 ~]# mkswap /dev/vdb5
    Setting up swapspace version 1, size = 524284 KiB
    no label, UUID=eff6eaaf-17d2-437a-b063-8167bcd69e5f

    启用新增的swap分区前观察当前的Swap大小为2015M.
    [root@server01 ~]# free -m
    total used free shared buffers cached
    Mem: 996 112 884 0 13 42
    -/+ buffers/cache: 55 940
    Swap: 2015 0 2015

    启用/dev/vdb5作为swap分区:
    [root@server01 ~]# swapon /dev/vdb5


    观察启用新的swap分区很Swap的大小变成了2527M:
    [root@server01 ~]# free -m
    total used free shared buffers cached
    Mem: 996 112 883 0 14 42
    -/+ buffers/cache: 55 940
    Swap: 2527 0 2527

    如果要关闭新的swap分区:
    [root@server01 ~]# swapoff /dev/vdb5
    [root@server01 ~]# free -m
    total used free shared buffers cached
    Mem: 996 112 883 0 14 42
    -/+ buffers/cache: 55 940
    Swap: 2015 0 2015


    如果要开机自动加载新的swap分区,写入到/etc/fstab:

    [root@server01 ~]# blkid /dev/vdb5
    /dev/vdb5: UUID="eff6eaaf-17d2-437a-b063-8167bcd69e5f" TYPE="swap"


    [root@server01 ~]# vim /etc/fstab

    UUID=eff6eaaf-17d2-437a-b063-8167bcd69e5f swap swap defaults 0 0


    2)再使用dd命令创建一个512M的空文件,设置成交换文件,并启用。

    [root@server01 ~]# dd if=/dev/zero of=/swap.file bs=1M count=512
    记录了512+0 的读入
    记录了512+0 的写出
    536870912字节(537 MB)已复制,3.87168 秒,139 MB/秒
    [root@server01 ~]# ll -h /swap.file
    -rw-r--r-- 1 root root 512M 7月 31 15:26 /swap.file


    [root@server01 ~]# mkswap -f /swap.file
    Setting up swapspace version 1, size = 524284 KiB
    no label, UUID=7703d441-ee47-4f24-b262-b09652206076

    [root@server01 ~]# free -m
    total used free shared buffers cached
    Mem: 996 641 354 0 14 555
    -/+ buffers/cache: 72 924
    Swap: 2015 0 2015

    [root@server01 ~]# swapon /swap.file


    [root@server01 ~]# free -m
    total used free shared buffers cached
    Mem: 996 641 354 0 14 555
    -/+ buffers/cache: 72 924
    Swap: 2527 0 2527

    如果要开机自动加载swap.file的文件呢?


    3. 如果/home目录并不是一个单独的分区,请在现有的系统中新建一个 2G 的分区,将/home 目录移动到该分区。移动完成后,不影响整个系统和/home 目录的文件结构和用户使用。

    查看分区使用情况:
    [root@server01 ~]# df -hT
    Filesystem Type Size Used Avail Use% Mounted on
    /dev/mapper/vg_mini-lv_root ext4 18G 1.7G 15G 11% /
    tmpfs tmpfs 499M 0 499M 0% /dev/shm
    /dev/vda1 ext4 485M 33M 427M 8% /boot
    /dev/sr0 iso9660 3.6G 3.6G 0 100% /rhel65
    结果显示没有建立单独的/home分区。

    首先移动/home分区的数据到临时存放目录:
    [root@server01 ~]# ls /home/
    zhangsan

    [root@server01 ~]# mkdir /home_tmp
    [root@server01 ~]# mv /home/* /home_tmp/
    [root@server01 ~]# ls /home/
    [root@server01 ~]# ls /home_tmp/
    zhangsan


    然后新建一个2G的标准分区,过程略。

    把该分区挂载到/home目录
    [root@server01 ~]# mount /dev/vdb6 /home

    再将移走的数据移回到/home目录
    [root@server01 ~]# mv /home_tmp/* /home/

    在fstab中添加以下配置使开机自动挂载生效。
    UUID=41de6c25-9797-4fba-a94e-bfb7b4a8b635 /home ext4 defaults 1 1


    查看/home分区当前的使用情况:
    [root@server01 ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    ......

    /dev/vdb6 2G 35M 1.9G 1% /home

  • 相关阅读:
    web--ajax--json
    4.26
    4.25
    4.23
    4.22
    4.20
    4.19
    4.18
    4月问题总结章
    4.17
  • 原文地址:https://www.cnblogs.com/yczlove/p/7326749.html
Copyright © 2011-2022 走看看