Linux系统的磁盘管理及分区应用

要求:

    第一部分:磁盘分区

        1)为虚拟机添加一块80GB、SCSI接口的硬盘

        2)划分2个10GB的EXT4主分区,剩余的作为扩展分区

        3)划分一个2G的swap交换分区

        4)划分一个10G的FAT32的VFAT逻辑分区

    第二部分:新磁盘分区的应用

        a)将系统中的普通用户king的宿主目录迁移到所添加的新硬盘的其中一个主分区上

            并能实现开机自动挂载,不影响开机时用户king的正常登陆(修改fstab配置文件)

        b)实现新添加交换分区的开机自动挂载(修改fstab配置文件)

        c)实现先添加另一个主分区和FAT32逻辑分区的按需挂载(即触发挂载,autofs等服务实现)

环境:

    - VMware WorkStation 9

    - Read Hat Enterprise Linux 6.5

实现步骤:

    (1)添加新硬盘到新系统中:

        - 按要求将新硬盘添加如虚拟机中

        - 开启或重启系统 

        [root@localhost ~]# ls /dev/sd*

        /dev/sda  /dev/sda1  /dev/sda2  /dev/sda3  /dev/sda4  /dev/sda5  /dev/sdb

        [root@localhost ~]# 

        当成功添加新硬盘到系统中后,会在/dev目录下出现新的设备文件,类似如上述结果中红色标注部                        分,新的硬盘在系统中显示的设备名为sdb

     (2)按要求对新磁盘进行分区以及创建对应的文件系统

        - 查看新硬盘信息:

        [root@localhost dev]# fdisk -l /dev/sdb


        Disk /dev/sdb: 85.9 GB, 85899345920 bytes

        255 heads, 63 sectors/track, 10443 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


        [root@localhost dev]# 

        注:此结果显示的是未执行任何处理的新硬盘


        - 执行分区操作

        [root@localhost ~]# 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 0x507a5b54.

        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                                //查看指令帮助

        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)


        - 创建主分区(2个):

        Command (m for help): n

        Command action

           e   extended                                //输入e指定分区类型为扩展分区

           p   primary partition (1-4)            //输入p指定分区类型为主分区

        p                                                    //输入p

        Partition number (1-4): 1                //指定分区编号

        First cylinder (1-10443, default 1):   //指定起始柱面,通常不做指定

        Using default value 1

        Last cylinder, +cylinders or +size{K,M,G} (1-10443, default 10443): +10G 

                                                          //指定分区大小,也可指定结束柱面,由系统自行计算(10G)

        - 创建扩展分区:  

        Command (m for help): n

        Command action

           e   extended

           p   primary partition (1-4)

        e

        Partition number (1-4): 3

        First cylinder (2613-10443, default 2613): 

        Using default value 2613

        Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): 

        Using default value 10443

        

        -创建逻辑分区:(逻辑分区是在扩展分区的基础上创建的)

        (注:swap交换分区以及FAT32分区都是在逻辑分区上建立的,所以在建立逻辑分区时要建立与其            大小一致的逻辑分区)

        Command (m for help): n

        Command action

           l   logical (5 or over)                    //输入l建立逻辑分区

           p   primary partition (1-4)

        l                                                    //建立逻辑分区

        First cylinder (2613-10443, default 2613): 

        Using default value 2613

        Last cylinder, +cylinders or +size{K,M,G} (2613-10443, default 10443): +2G

        //此逻辑分区用于建立swap交换分区,按要求交换分区大小为2G,建立FAT32分区时将大小按要                  //求建立即可

        

        - 参看到目前为止的分区设置

        Command (m for help): p


        Disk /dev/sdb: 85.9 GB, 85899345920 bytes

        255 heads, 63 sectors/track, 10443 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: 0xdc6ad4da


           Device Boot      Start         End      Blocks   Id  System

        /dev/sdb1               1        1306    10490413+  83  Linux

        /dev/sdb2            1307        2612    10490445   83  Linux

        /dev/sdb3            2613       10443    62902507+   5  Extended

        /dev/sdb5            2613        2874     2104483+  83  Linux

        /dev/sdb6            2875        4180    10490413+  83  Linux


        - 指定分区类型

        Command (m for help): t                    //输入t指定分区类型

        Partition number (1-6): 1                    //输入分区号

        Hex code (type L to list codes): 83       //输入分区类型编号,83为linux文件系统


        Command (m for help): t                    

        Partition number (1-6): 2                    

        Hex code (type L to list codes): 83


        Command (m for help): t

        Partition number (1-6): 5

        Hex code (type L to list codes): 82        //82为linux的swap分区文件系统类型

        Changed system type of partition 5 to 82 (Linux swap / Solaris)


        Command (m for help): t

        Partition number (1-6): 6

        Hex code (type L to list codes): b           //b为windows 95中的FAT32文件系统类型

        Changed system type of partition 6 to b (W95 FAT32)


        - 参看并确认分区设置

        Command (m for help): p


        Disk /dev/sdb: 85.9 GB, 85899345920 bytes

        255 heads, 63 sectors/track, 10443 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: 0xd18c03c5


           Device Boot      Start         End      Blocks   Id  System

        /dev/sdb1               1        1306    10490413+  83  Linux

        /dev/sdb2            1307        2612    10490445   83  Linux

        /dev/sdb3            2613       10443    62902507+   5  Extended

        /dev/sdb5            2613        2874     2104483+  82  Linux swap / Solaris

        /dev/sdb6            2875        4180    10490413+   b  W95 FAT32


        - 保存分区设置并退出

        Command (m for help): w

        The partition table has been altered!


        Calling ioctl() to re-read partition table.


        WARNING: If you have created or modified any DOS 6.x

        partitions, please see the fdisk manual page for additional

        information.

        Syncing disks.

        [root@localhost ~]# 

        

        - 创建文件系统类型

        [root@localhost ~]# mkfs            //双击tab键可参看系统可创建的文件系统类型的命令

        mkfs          mkfs.ext2     mkfs.ext4     mkfs.msdos    

        mkfs.cramfs   mkfs.ext3     mkfs.ext4dev  mkfs.vfat 

        

        - 为主分区创建文件系统类型(2个主分区)

        [root@localhost ~]# mkfs.ext4 /dev/sdb1   //为第一个主分区sdb1创建文件系统类型为ext4

        mke2fs 1.41.12 (17-May-2010)

        文件系统标签=

        操作系统:Linux

        块大小=4096 (log=2)

        分块大小=4096 (log=2)

        Stride=0 blocks, Stripe width=0 blocks

        655776 inodes, 2622603 blocks

        131130 blocks (5.00%) reserved for the super user

        第一个数据块=0

        Maximum filesystem blocks=2688548864

        81 block groups

        32768 blocks per group, 32768 fragments per group

        8096 inodes per group

        Superblock backups stored on blocks: 

        32768, 98304, 163840, 229376, 294912, 819200, 884736, 1605632


        正在写入inode表: 完成                            

        Creating journal (32768 blocks): 完成

        Writing superblocks and filesystem accounting information: 完成


        This filesystem will be automatically checked every 32 mounts or

        180 days, whichever comes first.  Use tune2fs -c or -i to override.

        [root@localhost ~]# 


        - 为交换分区创建文件系统类型 

        [root@localhost ~]# mkswap /dev/sdb5           

        //为第一个逻辑分区(即swap交换分区)创建文件系统类型

        Setting up swapspace version 1, size = 2104476 KiB

        no label, UUID=af0c56b4-61c2-49b3-b817-08dc03cd58d5

        //在RHEL6中创建完swap交换分区的文件系统类型后,会为交换分区设备添加一个UUID,在之前                        //的版本中是没有的

        [root@localhost ~]# 


        - 为FAT32分区创建文件系统类型

        [root@localhost ~]# mkfs.vfat -F 32 /dev/sdb5

        //为第二个逻辑分区(即FAT32分区)创建文件系统类型

        mkfs.vfat 3.0.9 (31 Jan 2010)

        [root@localhost ~]# 


        - 查看对应分区的信息

        [root@localhost ~]# blkid /dev/sdb1        //第一个主分区

        /dev/sdb1: UUID="e3ecf581-9d8b-4411-9c74-bbf5ba8e41d9" TYPE="ext4" 

        [root@localhost ~]# blkid /dev/sdb1        //第二个主分区

        /dev/sdb1: UUID="e3ecf581-9d8b-4411-9c74-bbf5ba8e41d9" TYPE="ext4" 


        [root@localhost ~]# blkid /dev/sdb5        //swap交换分区

        /dev/sdb5: UUID="af0c56b4-61c2-49b3-b817-08dc03cd58d5" TYPE="swap" 

        [root@localhost ~]# blkid /dev/sdb6        //FAT32分区

        /dev/sdb6: UUID="48F1-5927" TYPE="vfat" 

        [root@localhost ~]#

 

    (3)应用新的磁盘分区

        - 将用户king的宿主目录/home/king迁移到新磁盘的主分区之一中,并设置目录                

        为/data/sdb1/home/king


        创建目录:/data/sdb1

        [root@localhost ~]# ls /data/sdb1

        ls: 无法访问/data/sdb1: 没有那个文件或目录

        [root@localhost ~]# mkdir -p /data/sdb1

        [root@localhost ~]# ls -a /data/sdb1

        .  ..

        [root@localhost ~]# 


        将/dev/sdb1临时挂载到/data/sdb1目录下 

        [root@localhost ~]# mount /dev/sdb1 /data/sdb1

        [root@localhost ~]# df -h

        Filesystem      Size  Used Avail Use% Mounted on

        /dev/sda2       9.7G  2.6G  6.6G  29% /

        tmpfs           504M   76K  504M   1% /dev/shm

        /dev/sda1        97M   28M   65M  30% /boot

        /dev/sda5       8.1G  147M  7.6G   2% /data

        /dev/sr0        3.1G  3.1G     0 100% /media/RHEL_6.5 i386 Disc 1

        /dev/sdb1       9.9G  151M  9.3G   2% /data/sdb1   //成功挂载

        [root@localhost ~]# 

        

        - 将用户king的宿主目录拷贝到/data/sdb1/home,并保持目录属性不变

        [root@localhost ~]# cp -rp /home /data/sdb1/home

        [root@localhost ~]# cd /data/sdb1/home/

        [root@localhost home]# ls -l

        总用量 4

        drwx------. 4 king king 4096 8月   2 09:16 king

        [root@localhost home]# pwd

        /data/sdb1/home

        [root@localhost home]# 


        - 修改用户king的宿主目录,此用户目录迁移过程必须确保用户为登陆

        [root@localhost ~]# vim /etc/passwd

        [root@localhost ~]# tail -n 5 /etc/passwd

        gdm:x:42:42::/var/lib/gdm:/sbin/nologin

        sshd:x:74:74:Privilege-separated SSH:/var/empty/sshd:/sbin/nologin

        tcpdump:x:72:72::/:/sbin/nologin

        oprofile:x:16:16:Special user account to be used by OProfile:/home/oprofile:/sbin/nologin

        king:x:500:500::/data/sdb1/home/king:/bin/bash

        //更改结果如红色标注所示

        [root@localhost ~]# su - king

        [king@localhost ~]$ pwd

        /data/sdb1/home/king

        [king@localhost ~]$ 


        - 按要求实现用户king的宿主目录所在主分区的开机自动挂载以及,新建swap交换分区的开机自动            挂载,通过修改/etc/fstab配置文件实现。

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

        [root@localhost ~]# tail -n 5 /etc/fstab 

        proc                    /proc                   proc    defaults        0 0

        UUID=e3ecf581-9d8b-4411-9c74-bbf5ba8e41d9 /data/sdb1  ext4  defaults  0 0

        UUID=af0c56b4-61c2-49b3-b817-08dc03cd58d5 swap  swap  defaults  0 0

        //添加如上红色标注部分信息,其中UUID必须于启动挂载设备相对应,否则会无法启动系统

        //UUID可是使用 blkid [ 设备名 ] 命令进行查看

        [root@localhost ~]# 

        

        - 按要求将另一个主分区及FAT32分区实现触发挂载

        创建触发挂载点目录:

        [root@localhost ~]# mkdir -p /mnt


        修改触发挂载配置文件:

        [root@localhost ~]# vim /etc/auto.master 

        [root@localhost ~]# grep -vE "^#|^$" /etc/auto.master 

        /misc /etc/auto.misc

        /mnt /etc/auto.mnt       

        //触发挂载点目录:/data/mnt ;触发挂载设备配置文件为/etc/auto.mnt

        /net -hosts

        +auto.master

        [root@localhost ~]#

        [root@localhost ~]# vim /etc/auto.mnt

        [root@localhost ~]# cat /etc/auto.mnt

        sdb2 -fstype=ext4 :/dev/sdb2

        FAT32 -fstype=vfat :/dev/sdb6

        [root@localhost ~]# service autofs restart

        停止 automount:                                                 [确定]

        正在启动 automount:                                           [确定]

        [root@localhost ~]# cd /mnt

        [root@localhost mnt]# ls     

        [root@localhost mnt]# cd sdb2

        [root@localhost sdb2]# ls

        lost+found

        [root@localhost sdb2]# cd /mnt/FAT32

        [root@localhost FAT32]# ls

        [root@localhost FAT32]# cd ..

        [root@localhost mnt]# ls 

        FAT32  sdb2                                                    //验证触发挂载成功

        [root@localhost mnt]# 

        可以重启进行实践测试:

        


      再此测试中登陆king用户会提示找不到/data/sdb1/home/king这个从新迁移的宿主目录但是却可以用king用户正常目录身份登陆,并且cd ~命令是可正常执行的,所以仅从达到目的的角度来说本实验算是成功了,可并不完美,有待进一步的修改。也希望有高手能帮忙解决,或给个解释,十分感谢


   

Linux系统的磁盘管理及分区应用,古老的榕树,5-wow.com

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。