[Draft]在已存在的 Ubuntu lvm 開機碟建立 RAID1

  • 環境 : Ubuntu 20.04 採用 iso 開機預設 lvm 建立開機碟 sda(32G)
  • 目標 : 加入 sdb(32G) 設定 sda+sdb 成為 RAID1, 當 sda 損毀還能透過 sdb 開機使用, 並能加回更換後的 sda
  1. 確認現有 sda 內的相關資訊

    root@test-lvm-102:~# fdisk -l /dev/sda
    Disk /dev/sda: 32 GiB, 34359738368 bytes, 67108864 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 4F956088-02A8-402A-931B-440831A9F332
    
    Device       Start      End  Sectors Size Type
    /dev/sda1     2048     4095     2048   1M BIOS boot
    /dev/sda2     4096  4198399  4194304   2G Linux filesystem
    /dev/sda3  4198400 67106815 62908416  30G Linux filesystem
    
    root@test-lvm-102:~# df -h
    Filesystem                         Size  Used Avail Use% Mounted on
    udev                               941M     0  941M   0% /dev
    tmpfs                              198M  1.0M  197M   1% /run
    /dev/mapper/ubuntu--vg-ubuntu--lv   30G  4.5G   24G  16% /
    tmpfs                              986M     0  986M   0% /dev/shm
    tmpfs                              5.0M     0  5.0M   0% /run/lock
    tmpfs                              986M     0  986M   0% /sys/fs/cgroup
    /dev/loop0                          62M   62M     0 100% /snap/core20/1611
    /dev/sda2                          2.0G  108M  1.7G   6% /boot
    /dev/loop1                          68M   68M     0 100% /snap/lxd/22753
    /dev/loop2                          47M   47M     0 100% /snap/snapd/16292
    tmpfs                              198M     0  198M   0% /run/user/1000
    
    root@test-lvm-102:~# pvdisplay
      --- Physical volume ---
      PV Name               /dev/sda3
      VG Name               ubuntu-vg
      PV Size               <30.00 GiB / not usable 0
      Allocatable           yes (but full)
      PE Size               4.00 MiB
      Total PE              7679
      Free PE               0
      Allocated PE          7679
      PV UUID               1yb1vq-CMMu-7NLs-nif3-QFVU-SrAd-r81xKs
    
    root@test-lvm-102:~# vgdisplay
      --- Volume group ---
      VG Name               ubuntu-vg
      System ID
      Format                lvm2
      Metadata Areas        1
      Metadata Sequence No  2
      VG Access             read/write
      VG Status             resizable
      MAX LV                0
      Cur LV                1
      Open LV               1
      Max PV                0
      Cur PV                1
      Act PV                1
      VG Size               <30.00 GiB
      PE Size               4.00 MiB
      Total PE              7679
      Alloc PE / Size       7679 / <30.00 GiB
      Free  PE / Size       0 / 0
      VG UUID               1ZEt2r-TSxy-jyR8-TBh8-W4tf-azfb-iRn4cG
    
    root@test-lvm-102:~# lvdisplay
      --- Logical volume ---
      LV Path                /dev/ubuntu-vg/ubuntu-lv
      LV Name                ubuntu-lv
      VG Name                ubuntu-vg
      LV UUID                7t6pa0-Fx5y-31DZ-lE1B-vKWP-v6at-nNLp0V
      LV Write Access        read/write
      LV Creation host, time ubuntu-server, 2023-12-18 21:50:55 +0000
      LV Status              available
      # open                 1
      LV Size                <30.00 GiB
      Current LE             7679
      Segments               1
      Allocation             inherit
      Read ahead sectors     auto
      - currently set to     256
      Block device           253:0

  2. 安裝 mdadm 套件(Ubuntu 20.04預設已安裝)

    apt install initramfs-tools mdadm

    並載入 Linux Kernel

    modprobe md
    modprobe linear
    modprobe multipath
    modprobe raid0
    modprobe raid1
    modprobe raid5
    modprobe raid6
    modprobe raid10

    檢查 md 狀態

    root@test-lvm-102:~# cat /proc/mdstat
    Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
    unused devices: <none>

  3. 準備將 sdb 加入

    root@test-lvm-102:~# sfdisk -d /dev/sda | sfdisk /dev/sdb
    Checking that no-one is using this disk right now ... OK
    
    Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Script header accepted.
    >>> Created a new GPT disklabel (GUID: 4F956088-02A8-402A-931B-440831A9F332).
    /dev/sdb1: Created a new partition 1 of type 'BIOS boot' and of size 1 MiB.
    /dev/sdb2: Created a new partition 2 of type 'Linux filesystem' and of size 2 GiB.
    /dev/sdb3: Created a new partition 3 of type 'Linux filesystem' and of size 30 GiB.
    /dev/sdb4: Done.
    
    New situation:
    Disklabel type: gpt
    Disk identifier: 4F956088-02A8-402A-931B-440831A9F332
    
    Device       Start      End  Sectors Size Type
    /dev/sdb1     2048     4095     2048   1M BIOS boot
    /dev/sdb2     4096  4198399  4194304   2G Linux filesystem
    /dev/sdb3  4198400 67106815 62908416  30G Linux filesystem
    
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.
    
    root@test-lvm-102:~# fdisk -l /dev/sdb
    Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 4F956088-02A8-402A-931B-440831A9F332
    
    Device       Start      End  Sectors Size Type
    /dev/sdb1     2048     4095     2048   1M BIOS boot
    /dev/sdb2     4096  4198399  4194304   2G Linux filesystem
    /dev/sdb3  4198400 67106815 62908416  30G Linux filesystem

    將 /dev/sdb2 與 /dev/sdb3 的檔案系統類型設定為 Linux RAID

    root@test-lvm-102:~# fdisk /dev/sdb
    
    Welcome to fdisk (util-linux 2.34).
    Changes will remain in memory only, until you decide to write them.
    Be careful before using the write command.
    
    Command (m for help): t
    Partition number (1-3, default 3): 2
    Partition type (type L to list all types): 29
    
    Changed type of partition 'Linux RAID' to 'Linux RAID'.
    
    Command (m for help): p
    Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 4F956088-02A8-402A-931B-440831A9F332
    
    Device       Start      End  Sectors Size Type
    /dev/sdb1     2048     4095     2048   1M BIOS boot
    /dev/sdb2     4096  4198399  4194304   2G Linux RAID
    /dev/sdb3  4198400 67106815 62908416  30G Linux filesystem
    
    Command (m for help): t
    Partition number (1-3, default 3): 3
    Partition type (type L to list all types): 29
    
    Changed type of partition 'Linux filesystem' to 'Linux RAID'.
    
    Command (m for help): p
    Disk /dev/sdb: 32 GiB, 34359738368 bytes, 67108864 sectors
    Disk model: QEMU HARDDISK
    Units: sectors of 1 * 512 = 512 bytes
    Sector size (logical/physical): 512 bytes / 512 bytes
    I/O size (minimum/optimal): 512 bytes / 512 bytes
    Disklabel type: gpt
    Disk identifier: 4F956088-02A8-402A-931B-440831A9F332
    
    Device       Start      End  Sectors Size Type
    /dev/sdb1     2048     4095     2048   1M BIOS boot
    /dev/sdb2     4096  4198399  4194304   2G Linux RAID
    /dev/sdb3  4198400 67106815 62908416  30G Linux RAID
    
    Command (m for help): w
    The partition table has been altered.
    Calling ioctl() to re-read partition table.
    Syncing disks.

    確認 /dev/sdb2 與 /dev/sdb3 內沒有 RAID 資訊

    mdadm --zero-superblock /dev/sdb2
    mdadm --zero-superblock /dev/sdb3

  4. 建立 RAID1 : /dev/sdb2 → /dev/md0 , /dev/sdb3 → /dev/md1

    mdadm --create /dev/md0 --level=1 --raid-disks=2 --metadata=0.90 missing /dev/sdb2
    mdadm --create /dev/md1 --level=1 --raid-disks=2 missing /dev/sdb3
    
    root@test-lvm-102:~# mdadm --zero-superblock /dev/sdb2
    mdadm: Unrecognised md component device - /dev/sdb2
    root@test-lvm-102:~# mdadm --create /dev/md0 --level=1 --raid-disks=2 missing /dev/sdb2
    mdadm: Note: this array has metadata at the start and
        may not be suitable as a boot device.  If you plan to
        store '/boot' on this device please ensure that
        your boot-loader understands md/v1.x metadata, or use
        --metadata=0.90
    Continue creating array?
    Continue creating array? (y/n) y
    mdadm: Defaulting to version 1.2 metadata
    mdadm: array /dev/md0 started.

    查看 md 狀態

    root@test-lvm-102:~# cat /proc/mdstat
    Personalities : [linear] [multipath] [raid0] [raid1] [raid6] [raid5] [raid4] [raid10]
    md0 : active raid1 sdb2[1]
          2097088 blocks [2/1] [_U]
    
    md1 : active raid1 sdb3[1]
          31436800 blocks super 1.2 [2/1] [_U]
    
    unused devices: <none>

    格式化 /dev/md0 → ext4 , /dev/md1 → LVM

    root@test-lvm-102:~# mkfs.ext4 /dev/md0
    mke2fs 1.45.5 (07-Jan-2020)
    Discarding device blocks: done
    Creating filesystem with 524272 4k blocks and 131072 inodes
    Filesystem UUID: f24c70d1-2144-4bd5-9870-8f2c138c940a
    Superblock backups stored on blocks:
            32768, 98304, 163840, 229376, 294912
    
    Allocating group tables: done
    Writing inode tables: done
    Creating journal (8192 blocks): done
    Writing superblocks and filesystem accounting information: done
    
    pvcreate /dev/md1
    vgextend ubuntu-vg /dev/md1
    pvdisplay

  • tech/ubuntu-boot-lvm-raid1.txt
  • 上一次變更: 2023/12/19 07:09
  • jonathan