美文网首页
DAY22磁盘管理关于格式化和挂载

DAY22磁盘管理关于格式化和挂载

作者: 余仔丶 | 来源:发表于2019-03-31 20:03 被阅读0次

    [root@oldboyedu ~]# parted /dev/sdb #交互式开始分区
    GNU Parted 3.1
    Using /dev/sdb
    Welcome to GNU Parted! Type 'help' to view a list of commands.
    (parted) help
    (parted) mklabel gpt #修改分区格式为gpt
    Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
    continue?
    Yes/No? Yes
    (parted) p
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 1074MB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt #修改结果

    (parted) mkpart primary 0 480 #创建第一个主分区480M。
    Warning: The resulting partition is not properly aligned for best performance.
    Ignore/Cancel? Ignore #忽略提示。

    (parted) mkpart primary 481 580 #创建第二个主分区100M。
    Warning: You requested a partition from 481MB to 580MB (sectors 939453..1132812).
    The closest location we can manage is 481MB to 481MB (sectors 940031..940031).
    Is this still acceptable to you?
    Yes/No? Y
    Warning: The resulting partition is not properly aligned for best performance.
    Ignore/Cancel? I #忽略提示。

    (parted) mkpart logic 581 600 #创建一个20M逻辑分区。
    (parted) p
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 1074MB
    Sector size (logical/physical): 512B/512B
    Partition Table: gpt
    Disk Flags:

    Number Start End Size File system Name Flags
    1 17.4kB 480MB 480MB primary
    3 481MB 481MB 512B primary
    2 481MB 580MB 98.6MB primary
    4 581MB 600MB 18.9MB logic

    (parted) rm 4 #删除
    (parted) p

    分区被parted搞了。

    修改分区格式:
    (parted) mklabel msdos
    Warning: The existing disk label on /dev/sdb will be destroyed and all data on this disk will be lost. Do you want to
    continue?
    Yes/No? Y
    (parted) p
    Model: VMware, VMware Virtual S (scsi)
    Disk /dev/sdb: 1074MB
    Sector size (logical/physical): 512B/512B
    Partition Table: msdos
    Disk Flags:

    创建分区:
    [root@oldboyedu ~]# fdisk /dev/sdb
    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.

    Command (m for help): n
    Partition type:
    p primary (0 primary, 0 extended, 4 free)
    e extended
    Select (default p): p
    Partition number (1-4, default 1):
    First sector (2048-2097151, default 2048):
    Using default value 2048
    Last sector, +sectors or +size{K,M,G} (2048-2097151, default 2097151): +100M
    Partition 1 of type Linux and of size 100 MiB is set

    Command (m for help): p

    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x000006b6

    Device Boot Start End Blocks Id System
    /dev/sdb1 2048 206847 102400 83 Linux

    Command (m for help): n
    Partition type:
    p primary (1 primary, 0 extended, 3 free)
    e extended
    Select (default p): p
    Partition number (2-4, default 2):
    First sector (206848-2097151, default 206848):
    Using default value 206848
    Last sector, +sectors or +size{K,M,G} (206848-2097151, default 2097151): +100M
    Partition 2 of type Linux and of size 100 MiB is set

    Command (m for help): p

    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x000006b6

    Device Boot Start End Blocks Id System
    /dev/sdb1 2048 206847 102400 83 Linux
    /dev/sdb2 206848 411647 102400 83 Linux

    Command (m for help): w
    The partition table has been altered!

    Calling ioctl() to re-read partition table.
    Syncing disks.
    [root@oldboyedu ~]# ls -l /dev/sdb*
    brw-rw---- 1 root disk 8, 16 Mar 29 11:31 /dev/sdb
    brw-rw---- 1 root disk 8, 17 Mar 29 11:31 /dev/sdb1
    brw-rw---- 1 root disk 8, 18 Mar 29 11:31 /dev/sdb2

    [root@oldboyedu ~]# mkfs -t ext4 /dev/sdb1 #格式化ext4文件系统 mkfs.ext4
    其实关键就是生成一定数量的Inode和Block。

    mke2fs 1.42.9 (28-Dec-2013)
    Filesystem label=
    OS type: Linux
    Block size=1024 (log=0)
    Fragment size=1024 (log=0)
    Stride=0 blocks, Stripe width=0 blocks
    25688 inodes, 102400 blocks
    5120 blocks (5.00%) reserved for the super user
    First data block=1
    Maximum filesystem blocks=33685504
    13 block groups
    8192 blocks per group, 8192 fragments per group
    1976 inodes per group
    Superblock backups stored on blocks:
    8193, 24577, 40961, 57345, 73729

    Allocating group tables: done
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done

    挂载: 开门、开窗住人使用

    [root@oldboyedu ~]# mount /dev/sdb1 /mnt #临时挂载到/mnt
    [root@oldboyedu ~]# df -h #查看挂载结果
    Filesystem Size Used Avail Use% Mounted on
    /dev/sda3 19G 1.9G 18G 10% /
    devtmpfs 980M 5.0M 975M 1% /dev
    tmpfs 991M 0 991M 0% /dev/shm
    tmpfs 991M 18M 973M 2% /run
    tmpfs 991M 0 991M 0% /sys/fs/cgroup
    /dev/sda1 253M 136M 118M 54% /boot
    tmpfs 199M 0 199M 0% /run/user/0
    tmpfs 199M 0 199M 0% /run/user/1000
    /dev/sdb1 93M 1.6M 85M 2% /mnt #<====已挂载。

    [root@oldboyedu ~]# cat /proc/mounts #查看挂载结果
    rootfs / rootfs rw 0 0
    sysfs /sys sysfs rw,nosuid,nodev,noexec,relatime 0 0
    proc /proc proc rw,nosuid,nodev,noexec,relatime 0 0
    configfs /sys/kernel/config configfs rw,relatime 0 0
    /dev/sda3 / xfs rw,relatime,attr2,inode64,noquota 0 0
    systemd-1 /proc/sys/fs/binfmt_misc autofs rw,relatime,fd=32,pgrp=1,timeout=0,minproto=5,maxproto=5,direct,pipe_ino=19277 0 0
    hugetlbfs /dev/hugepages hugetlbfs rw,relatime 0 0
    debugfs /sys/kernel/debug debugfs rw,relatime 0 0
    mqueue /dev/mqueue mqueue rw,relatime 0 0
    /dev/sda1 /boot xfs rw,relatime,attr2,inode64,noquota 0 0
    tmpfs /run/user/0 tmpfs rw,nosuid,nodev,relatime,size=202812k,mode=700 0 0
    tmpfs /run/user/1000 tmpfs rw,nosuid,nodev,relatime,size=202812k,mode=700,uid=1000,gid=1000 0 0
    /dev/sdb1 /mnt ext4 rw,relatime,data=ordered 0 0 #<==已挂载。
    [root@oldboyedu ~]# touch /mnt/oldboy #<==测试。
    [root@oldboyedu ~]# ls /mnt
    lost+found oldboy

    [root@oldboyedu /mnt]# umount /mnt
    umount: /mnt: target is busy.
    (In some cases useful info about processes that use
    the device is found by lsof(8) or fuser(1))
    [root@oldboyedu /mnt]# pwd
    /mnt
    You have new mail in /var/spool/mail/root
    [root@oldboyedu /mnt]# umount -lf /mnt #<==强制卸载
    [root@oldboyedu /mnt]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/sda3 19G 1.9G 18G 10% /
    devtmpfs 980M 5.0M 975M 1% /dev
    tmpfs 991M 0 991M 0% /dev/shm
    tmpfs 991M 18M 973M 2% /run
    tmpfs 991M 0 991M 0% /sys/fs/cgroup
    /dev/sda1 253M 136M 118M 54% /boot
    tmpfs 199M 0 199M 0% /run/user/0
    tmpfs 199M 0 199M 0% /run/user/1000

    [root@oldboyedu ~]# mount /dev/sdb2 /opt #没有格式化没法挂载
    mount: /dev/sdb2 is write-protected, mounting read-only
    mount: unknown filesystem type '(null)'

    [root@oldboyedu ~]# mkfs.xfs /dev/sdb2 #格式化xfs文件系统
    meta-data=/dev/sdb2 isize=512 agcount=4, agsize=6400 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=0, sparse=0
    data = bsize=4096 blocks=25600, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal log bsize=4096 blocks=855, version=2
    = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0

    [root@oldboyedu ~]# mount -t xfs /dev/sdb2 /opt
    You have new mail in /var/spool/mail/root
    [root@oldboyedu ~]# df -h|grep opt
    /dev/sdb2 97M 5.3M 92M 6% /opt
    [root@oldboyedu ~]# touch /opt/oldgirl
    [root@oldboyedu ~]# ls /opt
    oldgirl

    如何开机自动挂载?
    /etc/fstab 开机自动挂载
    几列? 6列

    [root@oldboyedu ~]# cat /etc/fstab
    UUID=3a3a295f-88f8-456d-94dc-1a3eeb517c02 / xfs defaults 0 0
    UUID=fd2e0ca7-32be-425f-86a2-85c02b9ec5ea /boot xfs defaults 0 0
    UUID=79a3924b-739e-48dc-ab0c-0444b9ac6591 swap swap defaults 0 0
    设备 挂载点 文件系统类型 默认挂载选项 是否备份 是否开机磁盘检查
    /dev/sdb2 /opt xfs defaults 0 0

    mount -t xfs /dev/sdb2 /opt

    [root@oldboyedu ~]# blkid
    /dev/sdb1: UUID="a45b004f-2322-4dc4-a281-2a212dd2ab79" TYPE="ext4"
    /dev/sdb2: UUID="7daf8142-b641-4b60-a287-4e08dfa18611" TYPE="xfs"
    /dev/sda1: UUID="fd2e0ca7-32be-425f-86a2-85c02b9ec5ea" TYPE="xfs"
    /dev/sda2: UUID="79a3924b-739e-48dc-ab0c-0444b9ac6591" TYPE="swap"
    /dev/sda3: UUID="3a3a295f-88f8-456d-94dc-1a3eeb517c02" TYPE="xfs"

    [root@oldboyedu ~]# tail -1 /etc/fstab
    /dev/sdb2 /opt xfs defaults 0 0

    mount -a 可以将fstab里的信息挂载。

    放入/etc/rc.local 实现自动挂载 但不推荐。
    /sbin/mount -t xfs /dev/sdb2 /opt

    查看:
    [root@oldboyedu ~]# df -h
    Filesystem Size Used Avail Use% Mounted on
    /dev/sda3 19G 1.9G 18G 10% /
    devtmpfs 980M 0 980M 0% /dev
    tmpfs 991M 0 991M 0% /dev/shm
    tmpfs 991M 9.6M 981M 1% /run
    tmpfs 991M 0 991M 0% /sys/fs/cgroup
    /dev/sdb2 97M 5.3M 92M 6% /opt
    /dev/sda1 253M 136M 118M 54% /boot
    tmpfs 199M 0 199M 0% /run/user/0
    tmpfs 199M 0 199M 0% /run/user/1000

    [root@oldboyedu ~]# df -i
    Filesystem Inodes IUsed IFree IUse% Mounted on
    /dev/sda3 9960960 74572 9886388 1% /
    devtmpfs 250820 390 250430 1% /dev
    tmpfs 253514 1 253513 1% /dev/shm
    tmpfs 253514 724 252790 1% /run
    tmpfs 253514 16 253498 1% /sys/fs/cgroup
    /dev/sdb2 51200 4 51196 1% /opt
    /dev/sda1 131072 332 130740 1% /boot
    tmpfs 253514 1 253513 1% /run/user/0
    tmpfs 253514 1 253513 1% /run/user/1000

    [root@oldboyedu ~]# blkid
    /dev/sdb1: UUID="a45b004f-2322-4dc4-a281-2a212dd2ab79" TYPE="ext4"
    /dev/sdb2: UUID="7daf8142-b641-4b60-a287-4e08dfa18611" TYPE="xfs"
    /dev/sda1: UUID="fd2e0ca7-32be-425f-86a2-85c02b9ec5ea" TYPE="xfs"
    /dev/sda2: UUID="79a3924b-739e-48dc-ab0c-0444b9ac6591" TYPE="swap"
    /dev/sda3: UUID="3a3a295f-88f8-456d-94dc-1a3eeb517c02" TYPE="xfs"

    查看文件系统内部细节:
    [root@oldboyedu ~]# dumpe2fs /dev/sdb1 #查看ext文件系统内部细节
    [root@oldboyedu ~]# dumpe2fs /dev/sdb1|egrep -i "size"
    dumpe2fs 1.42.9 (28-Dec-2013)
    Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super huge_file uninit_bg dir_nlink extra_isize
    Block size: 1024
    Fragment size: 1024
    Group descriptor size: 64
    Flex block group size: 16
    Inode size: 128
    Journal size: 4096k

    [root@oldboyedu ~]# xfs_
    xfs_admin xfs_db xfs_fsr xfs_io xfs_metadump xfs_quota
    xfs_bmap xfs_estimate xfs_growfs xfs_logprint xfs_mkfile xfs_repair
    xfs_copy xfs_freeze xfs_info xfs_mdrestore xfs_ncheck xfs_rtcp
    [root@oldboyedu ~]# xfs_

    [root@oldboyedu ~]# xfs_info /dev/sda1
    meta-data=/dev/sda1 isize=512 agcount=4, agsize=16384 blks
    = sectsz=512 attr=2, projid32bit=1
    = crc=1 finobt=0 spinodes=0
    data = bsize=4096 blocks=65536, imaxpct=25
    = sunit=0 swidth=0 blks
    naming =version 2 bsize=4096 ascii-ci=0 ftype=1
    log =internal bsize=4096 blocks=855, version=2
    = sectsz=512 sunit=0 blks, lazy-count=1
    realtime =none extsz=4096 blocks=0, rtextents=0

    [root@oldboyedu ~]# fsck
    fsck fsck.btrfs fsck.cramfs fsck.ext2 fsck.ext3 fsck.ext4 fsck.minix fsck.xfs
    [root@oldboyedu ~]# fsck

    fsck 磁盘检查和修复
    1、正常的磁盘不能操作。
    2、卸载挂载点在操作

    [root@oldboyedu ~]# fsck -a /dev/sdb1
    fsck from util-linux 2.23.2
    /dev/sdb1: clean, 12/25688 files, 8896/102400 blocks
    [root@oldboyedu ~]# mount /dev/sdb1 /mnt
    [root@oldboyedu ~]# fsck -a /dev/sdb1
    fsck from util-linux 2.23.2
    /dev/sdb1 is mounted.
    e2fsck: Cannot continue, aborting.

    [root@oldboyedu ~]# e2fsck /dev/sdb1
    e2fsck 1.42.9 (28-Dec-2013)
    /dev/sdb1: clean, 12/25688 files, 8896/102400 blocks
    [root@oldboyedu ~]# fsck -a /dev/sdb1
    fsck from util-linux 2.23.2
    /dev/sdb1: clean, 12/25688 files, 8896/102400 blocks

    [root@oldboyedu ~]# xfs_repair /dev/sdb2
    xfs_repair: /dev/sdb2 contains a mounted filesystem
    xfs_repair: /dev/sdb2 contains a mounted and writable filesystem

    fatal error -- couldn't initialize XFS library
    [root@oldboyedu ~]# umount /opt
    [root@oldboyedu ~]# xfs_repair /dev/sdb2
    Phase 1 - find and verify superblock...
    Phase 2 - using internal log
    - zero log...
    - scan filesystem freespace and inode maps...
    - found root inode chunk
    Phase 3 - for each AG...
    - scan and clear agi unlinked lists...
    - process known inodes and perform inode discovery...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3
    - process newly discovered inodes...
    Phase 4 - check for duplicate blocks...
    - setting up duplicate extent list...
    - check for inodes claiming duplicate blocks...
    - agno = 0
    - agno = 1
    - agno = 2
    - agno = 3
    Phase 5 - rebuild AG headers and trees...
    - reset superblock...
    Phase 6 - check inode connectivity...
    - resetting contents of realtime bitmap and summary inodes
    - traversing filesystem ...
    - traversal finished ...
    - moving disconnected inodes to lost+found ...
    Phase 7 - verify and correct link counts...
    done
    ==============================指定块大小和inode大小格式===========================================
    [root@oldboyedu ~]# mkfs -t ext4 -b 4096 -I 512 /dev/sdb3
    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
    38400 inodes, 38400 blocks
    1920 blocks (5.00%) reserved for the super user
    First data block=0
    Maximum filesystem blocks=39845888
    2 block groups
    32768 blocks per group, 32768 fragments per group
    19200 inodes per group
    Superblock backups stored on blocks:
    32768

    Allocating group tables: done
    Writing inode tables: done
    Creating journal (4096 blocks): done
    Writing superblocks and filesystem accounting information: done

    [root@oldboyedu ~]# dumpe2fs /dev/sdb3|egrep -i "size"
    dumpe2fs 1.42.9 (28-Dec-2013)
    Filesystem features: has_journal ext_attr resize_inode dir_index filetype extent 64bit flex_bg sparse_super large_file huge_file uninit_bg dir_nlink extra_isize
    Block size: 4096
    Fragment size: 4096
    Group descriptor size: 64
    Flex block group size: 16
    Inode size: 512
    Required extra isize: 28
    Desired extra isize: 28
    Journal size: 16M

    文件删除原理
    no space left on device.

    swap作用,内存不够时候,用来充当内存,一般内存1.5倍。大于8G给8G
    将来JAVA服务,内存泄漏。。。
    swap就会占用。。。操作系统性能下降

    [root@oldboyedu ~]# free -m
    total used free shared buff/cache available
    Mem: 1980 143 1689 9 146 1671
    Swap: 767 0 767

    增加swap分区 100M

    [root@oldboyedu ~]# fdisk /dev/sdb
    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.

    Command (m for help): n
    Partition type:
    p primary (3 primary, 0 extended, 1 free)
    e extended
    Select (default e): p
    Selected partition 4
    First sector (718848-2097151, default 718848):
    Using default value 718848
    Last sector, +sectors or +size{K,M,G} (718848-2097151, default 2097151): +150M
    Partition 4 of type Linux and of size 150 MiB is set

    Command (m for help): p

    Disk /dev/sdb: 1073 MB, 1073741824 bytes, 2097152 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: 0x000006b6

    Device Boot Start End Blocks Id System
    /dev/sdb1 2048 206847 102400 83 Linux
    /dev/sdb2 206848 411647 102400 83 Linux
    /dev/sdb3 411648 718847 153600 83 Linux
    /dev/sdb4 718848 1026047 153600 83 Linux

    Command (m for help): w
    The partition table has been altered!

    Calling ioctl() to re-read partition table.

    WARNING: Re-reading the partition table failed with error 16: Device or resource busy.
    The kernel still uses the old table. The new table will be used at
    the next reboot or after you run partprobe(8) or kpartx(8)
    Syncing disks.
    [root@oldboyedu ~]# partprobe /dev/sdb
    [root@oldboyedu ~]# mkswap /dev/sdb4
    Setting up swapspace version 1, size = 153596 KiB
    no label, UUID=2d1ac352-4cc0-4727-8318-5526a3c309c2
    [root@oldboyedu ~]# swapon -a /dev/sdb4
    You have new mail in /var/spool/mail/root
    [root@oldboyedu ~]# free -m
    total used free shared buff/cache available
    Mem: 1980 144 1673 19 162 1658
    Swap: 917 0 917

    相关文章

      网友评论

          本文标题:DAY22磁盘管理关于格式化和挂载

          本文链接:https://www.haomeiwen.com/subject/sfgubqtx.html