美文网首页
note_9.4_LVM以及dd命令

note_9.4_LVM以及dd命令

作者: 人間失格_430b | 来源:发表于2019-02-20 20:58 被阅读0次

LVM2:

LVM: Logical Volume Manager, Version: 2

dm: device mapper,将一个或多个底层块设备组织成一个逻辑设备的模块;

/dev/dm-#

    /dev/mapper/VG_NAME-LV_NAME
        /dev/mapper/vol0-root
    /dev/VG_NAME/LV_NAME
        /dev/vol0/root

  • PV(Physical Volume)管理工具:
    pvs:简要pv信息显示
    pvdisplay:显示pv的详细信息
    pvcreate /dev/DEVICE: 创建pv
    pvmove:移动pv上的pe至其他pv上
    pvremove:移除LVM物理卷

  • VG(Volume Group)管理工具:
    vgs
    vgdisplay
    vgcreate [-s #[kKmMgGtTpPeE]] VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
    vgextend VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...]
    vgreduce VolumeGroupName PhysicalDevicePath [PhysicalDevicePath...] 先做pvmove
    vgremove

  • LV(Logical Volume)管理工具:
    lvs
    lvdisplay
    lvcreate -L #[mMgGtT] -n NAME VolumeGroup
    lvremove /dev/VG_NAME/LV_NAME

    • 扩展逻辑卷:

      1. 扩展lv
        lvextend -L [+]#[mMgGtT] /dev/VG_NAME/LV_NAME
      2. 扩展后resize
        resize2fs /dev/VG_NAME/LV_NAME
    • 缩减逻辑卷:

      1. 先卸载文件系统
        umount /dev/VG_NAME/LV_NAME
      2. 文件系统强制检测
        e2fsck -f /dev/VG_NAME/LV_NAME
      3. resize
        resize2fs /dev/VG_NAME/LV_NAME #[mMgGtT]
      4. 缩减lv
        lvreduce -L [-]#[mMgGtT] /dev/VG_NAME/LV_NAME
      5. 重新挂载
        mount
  • 快照:snapshot
    lvcreate -L #[mMgGtT] -p r -s -n snapshot_lv_name original_lv_name


练习

1:创建一个至少有两个PV组成的大小为20G的名为testvg的VG;要求PE大小为16MB, 而后在卷组中创建大小为5G的逻辑卷testlv;挂载至/users目录;
创建2个10G的LVM分区

[root@localhost ~]# 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-41943039, default 2048): 
Using default value 2048
Last sector, +sectors or +size{K,M,G} (2048-41943039, default 41943039): +10G
Partition 1 of type Linux and of size 10 GiB is set

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 (20973568-41943039, default 20973568): 
Using default value 20973568
Last sector, +sectors or +size{K,M,G} (20973568-41943039, default 41943039): 
Using default value 41943039
Partition 2 of type Linux and of size 10 GiB is set

Command (m for help): t
Partition number (1,2, default 2): 
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

Command (m for help): t
Partition number (1,2, default 2): 1
Hex code (type L to list all codes): 8e
Changed type of partition 'Linux' to 'Linux LVM'

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.

创建pv

[root@localhost ~]# pvcreate /dev/sdb1
WARNING: ext4 signature detected on /dev/sdb1 at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/sdb1.
WARNING: dos signature detected on /dev/sdb1 at offset 510. Wipe it? [y/n]: y
  Wiping dos signature on /dev/sdb1.
  Physical volume "/dev/sdb1" successfully created.
[root@localhost ~]# pvcreate /dev/sdb2
  Physical volume "/dev/sdb2" successfully created.
[root@localhost ~]# pvs
  PV         VG     Fmt  Attr PSize    PFree  
  /dev/sda2  centos lvm2 a--  <119.00g   4.00m
  /dev/sdb1         lvm2 ---    10.00g  10.00g
  /dev/sdb2         lvm2 ---   <10.00g <10.00g

创建vg

[root@localhost ~]# vgcreate -s 16m testvg /dev/sdb{1,2}
  Volume group "testvg" successfully created
[root@localhost ~]# vgdisplay testvg
  --- Volume group ---
  VG Name               testvg
  System ID             
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  1
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                0
  Open LV               0
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               <19.97 GiB
  PE Size               16.00 MiB
  Total PE              1278
  Alloc PE / Size       0 / 0   
  Free  PE / Size       1278 / <19.97 GiB
  VG UUID               zvIceJ-oYey-6hOx-qeU7-fsBA-okcR-ZjJfjv

创建lv

[root@localhost ~]# lvcreate -L 5g -n testlv testvg
WARNING: linux_raid_member signature detected on /dev/testvg/testlv at offset 4096. Wipe it? [y/n]: y
  Wiping linux_raid_member signature on /dev/testvg/testlv.
WARNING: ext4 signature detected on /dev/testvg/testlv at offset 1080. Wipe it? [y/n]: y
  Wiping ext4 signature on /dev/testvg/testlv.
  Logical volume "testlv" created.
[root@localhost ~]# lvdisplay testvg
  --- Logical volume ---
  LV Path                /dev/testvg/testlv
  LV Name                testlv
  VG Name                testvg
  LV UUID                U3tnW9-e60D-uSn1-Pjfd-nJOd-orvr-JAGANJ
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2019-02-20 07:17:11 -0500
  LV Status              available
  # open                 0
  LV Size                5.00 GiB
  Current LE             320
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     8192
  Block device           253:3

挂载至/users

[root@localhost ~]# mkdir /users
[root@localhost ~]# mke2fs -t ext4 /dev/testvg/testlv 
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
327680 inodes, 1310720 blocks
65536 blocks (5.00%) reserved for the super user
First data block=0
Maximum filesystem blocks=1342177280
40 block groups
32768 blocks per group, 32768 fragments per group
8192 inodes per group
Superblock backups stored on blocks: 
    32768, 98304, 163840, 229376, 294912, 819200, 884736

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

[root@localhost ~]# mount /dev/testvg/testlv /users
[root@localhost ~]# ls users
ls: cannot access users: No such file or directory
[root@localhost ~]# ls /users
lost+found

2: 新建用户archlinux,要求其家目录为/users/archlinux,而后su切换至archlinux用户,复制/etc/pam.d目录至自己的家目录;

[root@localhost ~]# useradd -d /users/archlinux archlinux
[root@localhost ~]# su archlinux
[archlinux@localhost root]$ cp -R /etc/pam.d ~/
[archlinux@localhost root]$ ls ~
pam.d
[archlinux@localhost root]$ tree ~
/users/archlinux
└── pam.d
    ├── chfn
    ├── chsh
    ├── config-util
    ├── crond
    ├── fingerprint-auth -> fingerprint-auth-ac
    ├── fingerprint-auth-ac
    ├── login
    ├── other
    ├── passwd
    ├── password-auth -> password-auth-ac
    ├── password-auth-ac
    ├── polkit-1
    ├── postlogin -> postlogin-ac
    ├── postlogin-ac
    ├── remote
    ├── runuser
    ├── runuser-l
    ├── smartcard-auth -> smartcard-auth-ac
    ├── smartcard-auth-ac
    ├── smtp -> /etc/alternatives/mta-pam
    ├── smtp.postfix
    ├── sshd
    ├── su
    ├── sudo
    ├── sudo-i
    ├── su-l
    ├── system-auth -> system-auth-ac
    ├── system-auth-ac
    ├── systemd-user
    ├── vlock
    └── vmtoolsd

1 directory, 31 files

3:扩展testlv至7G,要求archlinux用户的文件不能丢失;

[root@localhost ~]# lvreduce -L -2G /dev/testvg/testlv 
  WARNING: Reducing active logical volume to 5.00 GiB.
  THIS MAY DESTROY YOUR DATA (filesystem etc.)
Do you really want to reduce testvg/testlv? [y/n]: y
  Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 5.00 GiB (320 extents).
  Logical volume testvg/testlv successfully resized.
[root@localhost ~]# mount /dev/testvg/testlv /users
[root@localhost ~]# lvextend -r -L +2G /dev/testvg/testlv 
  Size of logical volume testvg/testlv changed from 5.00 GiB (320 extents) to 7.00 GiB (448 extents).
  Logical volume testvg/testlv successfully resized.
resize2fs 1.42.9 (28-Dec-2013)
Filesystem at /dev/mapper/testvg-testlv is mounted on /users; on-line resizing required
old_desc_blocks = 1, new_desc_blocks = 1
The filesystem on /dev/mapper/testvg-testlv is now 1835008 blocks long.

4:收缩testlv至3G,要求archlinux用户的文件不能丢失;

[root@localhost ~]# umount /dev/testvg/testlv 
[root@localhost ~]# e2fsck -f /dev/testvg/testlv 
e2fsck 1.42.9 (28-Dec-2013)
Pass 1: Checking inodes, blocks, and sizes
Pass 2: Checking directory structure
Pass 3: Checking directory connectivity
Pass 4: Checking reference counts
Pass 5: Checking group summary information
/dev/testvg/testlv: 47/458752 files (0.0% non-contiguous), 67357/1835008 blocks
[root@localhost ~]# lvreduce -r -L -4G /dev/testvg/testlv
fsck from util-linux 2.23.2
/dev/mapper/testvg-testlv: clean, 47/458752 files, 67357/1835008 blocks
resize2fs 1.42.9 (28-Dec-2013)
Resizing the filesystem on /dev/mapper/testvg-testlv to 786432 (4k) blocks.
The filesystem on /dev/mapper/testvg-testlv is now 786432 blocks long.

  Size of logical volume testvg/testlv changed from 7.00 GiB (448 extents) to 3.00 GiB (192 extents).
  Logical volume testvg/testlv successfully resized.
[root@localhost ~]# mount /dev/testvg/testlv /users

5:对testlv创建快照,并尝试基于快照备份数据,验正快照的功能;

[root@localhost ~]# lvcreate -L 512M -p r -s -n testlv_bak /dev/testvg/testlv
  Logical volume "testlv_bak" created.
[root@localhost ~]# lvs /dev/testvg
  LV         VG     Attr       LSize   Pool Origin Data%  Meta%  Move Log Cpy%Sync Convert
  testlv     testvg owi-aos---   3.00g                                                    
  testlv_bak testvg sri-a-s--- 512.00m      testlv 0.01                                   
[root@localhost ~]# mount /dev/testvg/testlv_bak /mnt
mount: /dev/mapper/testvg-testlv_bak is write-protected, mounting read-only
[root@localhost ~]# ls /mnt
archlinux  lost+found
[root@localhost ~]# \cp -R /mnt/* /users/
[root@localhost ~]# umount /dev/testvg/testlv_bak 
[root@localhost ~]# lvremove /dev/testvg/testlv_bak 
Do you really want to remove active logical volume testvg/testlv_bak? [y/n]: y
  Logical volume "testlv_bak" successfully removed

文件系统挂载使用:

挂载光盘设备:

  • 光盘设备文件:

    • IDE: /dev/hdc
    • SATA: /dev/sr0
  • 符号链接文件:
    /dev/cdrom
    /dev/cdrw
    /dev/dvd
    /dev/dvdrw

mount -r /dev/cdrom /media/cdrom
umount /dev/cdrom


dd命令:

convert and copy a file

  • 用法:
    dd if=/PATH/FROM/SRC of=/PATH/TO/DEST

if=FILE
  read from FILE instead of stdin
of=FILE
  write to FILE instead of stdout

  bs=#:block size, 复制单元大小
  count=#:复制多少个bs

  • 磁盘拷贝:
    dd if=/dev/sda of=/dev/sdb

    • 备份MBR
      dd if=/dev/sda of=/tmp/mbr.bak bs=512 count=1

    • 破坏MBR中的bootloader:
      dd if=/dev/zero of=/dev/sda bs=256 count=1

  • 两个特殊设备:
    /dev/null: 数据黑洞
    /dev/zero:吐零机

相关文章

  • note_9.4_LVM以及dd命令

    LVM2: LVM: Logical Volume Manager, Version: 2 dm: device ...

  • dd命令使用

    dd命令 dd :convert and copy a file用法: dd if=/PATH/FROM/SRC ...

  • dd命令

    一、dd命令的解释dd命令主要是用来块拷贝一个指定大小的文件,并在拷贝的同时进行指定的转换。 注意:指定数字的地方...

  • 创建虚拟磁盘

    1.1 dd 命令简介(部分说明来自dd (Unix) wiki) dd命令用于转换和复制文件,不过它的复制不同于...

  • 加入邪教Archlinux

    1.引导 与U盘制作: linux系统下, dd命令:dd if=/dev/zero of=dd.dd win...

  • LINUX 下刻录iso到u盘

    打开终端,使用dd命令,格式如下:sudo dd if=xxx.iso of=/dev/sdb命令中xxx.iso...

  • dd 命令详解

    dd 命令用于复制文件并对原文件的内容进行转换和格式化处理。 语法 实例 生成一个100M的空镜像文件: 将本地的...

  • Linux dd命令

    dd命令可以轻易实现创建指定大小的文件,如dd if=/dev/zero of=test bs=1M count=...

  • dd命令妙用

    [toc] dd命令详解 参数含义if=文件名指定输入文件名,缺省为标准输入。< if=input file >o...

  • dd命令详解

    一、dd命令的解释 dd:用指定大小的块拷贝一个文件,并在拷贝的同时进行指定的转换。 注意:指定数字的地方若以下列...

网友评论

      本文标题:note_9.4_LVM以及dd命令

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