2019/01/29

LVM 建置指令



每次要建lvm都忘記指令,來記錄一下

```shell

# 先建置分割區, 並且 type 設為8e
# 其實我不太了解如果都是用整顆硬碟的空間, 那麼要不要做這個步驟的差別是什麼(效能?對齊?其他?)
# fdisk <device Point>
$ fdisk /dev/xvdb

# 建立 Physical Volume
# pvcreate <device Partition>
$ pvcreate /dev/xvdb1

# 建立 Volume Group
# vgcreate <vg Name> <device Partition>
$ vgcreate VG1 /dev/xvdb1

# 建立 Logical Volume
# lvcreate -l 100%FREE -n <lv Name> <vg Name>
$ lvcreate -l 100%FREE -n DATA VG1

# 格式化
# mkfs.ext4 -L <label> /dev/<vg Name>/<lv Name>
$ mkfs.ext4 -L DATA /dev/VG1/DATA

# 開機自動掛載
echo "LABEL=DATA /data ext4 defaults 0 0" >> /etc/fstab
```

重灌作業系統後 使用 mdadm 重組你的 raid



前些時間因為升級 ubuntu 版本失敗導致必需重灌

然後就出現以下問題了

當初建好的 RAID5 消失了, 新的 os 不認識它所以顯示成三顆硬碟

這該怎麼辦呢?

其實不用擔心, 由於我們當時是用 mdadm 建置的, mdadm 有方法可以再把它們組起來

```shell
$ mdadm --assemble /dev/md0 /dev/sda1 /dev/sdb1 /dev/sdc1
```

平時資料還是要多多備份啊

2018/12/19

增大 Ubuntu 18.04 上 root lvm volume



昨天剛灌的機器今天磁碟就滿了, 怎麼可能?

一看, 原來當初給主磁碟 20G 空間 Ubuntu 18.04 只先配置 LV 4G 的空間, 難怪空間爆炸了

所以其實 PV 上空間還是夠的, 這時候就是把 LV 重新設定大小就好了

如果是用 lvextend 則會報錯:

```bash
$ sudo lvextend -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
/etc/lvm/archive/.lvm_template_2310_1202205345: write error failed: No space left on device
```

正確步驟:

```bash
# 1. 用 lvresize
$ sudo lvresize -A n -L -l +100%FREE /dev/ubuntu-vg/ubuntu-lv
Size of logical volume ubuntu-vg/ubuntu-lv changed from 4.00 GiB (1024 extents) to <19.00 GiB (4863 extents)
WARNING: This metadata update is NOT backed up.
Logical volume ubuntu-vg/ubuntu-lv successfully resized.


# 2. resize ext4
$ sudo resize2fs /dev/ubuntu-vg/ubuntu-lv
resize2fs 1.44.1 (24-Mar-2018)
Filesystem at /dev/ubuntu-vg/ubuntu-lv is mounted on /; on-line resizing required
old_desc_blocks = 2, new_desc_blocks = 3
The filesystem on /dev/ubuntu-vg/ubuntu-lv is now 4979712 (4k) blocks long.

# 3. df 看一下
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/ubuntu--vg-ubuntu--lv 19540624 4050844 14634724 22% /
```

2018/12/17

增大 EBS 磁碟容量後 LVM 要做的事情



前情提要:
1. 磁碟採用 NVME 介面(t3, m5, c5, r5)
2. 原先建置 LVM 時是用整顆磁碟, 所以裝置下只會有 /dev/nvme1n1 不會有 /dev/nvme1n1p1


增大前先看一下 lvm 配置

```sh
$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/nvme1n1
VG Name VG1
PV Size 50.00 GiB / not usable 4.00 MiB
Allocatable yes (but full)
PE Size 4.00 MiB
Total PE 12799
Free PE 0
Allocated PE 12799

$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12799
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

$ sudo vgdisplay
--- Volume group ---
VG Name VG1
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 50.00 GiB
PE Size 4.00 MiB
Total PE 12799
Alloc PE / Size 12799 / 50.00 GiB
Free PE / Size 0 / 0
```

在 ebs 頁面把該磁碟加大到100G後 直接跑 resize2fs 發現沒有做用

研究了一下發現需要先做 pvresize,做完後 pv 的確變成 100G 了

```sh
$ sudo pvresize /dev/nvme1n1
Physical volume "/dev/nvme1n1" changed
1 physical volume(s) resized / 0 physical volume(s) not resized

$ sudo pvdisplay
--- Physical volume ---
PV Name /dev/nvme1n1
VG Name VG1
PV Size 100.00 GiB / not usable 3.00 MiB
Allocatable yes
PE Size 4.00 MiB
Total PE 25599
Free PE 12800
Allocated PE 12799
```

看一下 lv,還是 50G 沒變

```sh
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 50.00 GiB
Current LE 12799
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0
```

lv 也要 resize一下,這邊把它稱大到所有的可用空間 +100%FREE

```sh
$ sudo lvresize -l +100%FREE /dev/mapper/VG1-DATA
Size of logical volume VG1/DATA changed from 50.00 GiB (12799 extents) to 100.00 GiB (25599 extents).
Logical volume DATA successfully resized.
```

lv resize 完後看一下,恩 變大了

```sh
$ sudo lvdisplay
--- Logical volume ---
LV Path /dev/VG1/DATA
LV Name DATA
VG Name VG1
LV Write Access read/write
LV Creation host, time ip-172-31-37-10, 2018-10-08 09:05:34 +0000
LV Status available
# open 1
LV Size 100.00 GiB
Current LE 25599
Segments 1
Allocation inherit
Read ahead sectors auto
- currently set to 256
Block device 252:0

$ sudo vgdisplay
--- Volume group ---
VG Name VG1
System ID
Format lvm2
Metadata Areas 1
Metadata Sequence No 4
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 100.00 GiB
PE Size 4.00 MiB
Total PE 25599
Alloc PE / Size 25599 / 100.00 GiB
Free PE / Size 0 / 0
```

不過用 df 看 還是沒變大

```sh
$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VG1-DATA 51470972 48766280 67076 100% /data
```

跑 resize2fs 後就正常了

```sh
$ sudo resize2fs /dev/mapper/VG1-DATA
resize2fs 1.42.13 (17-May-2015)
Filesystem at /dev/mapper/VG1-DATA is mounted on /data; on-line resizing required
old_desc_blocks = 4, new_desc_blocks = 7
The filesystem on /dev/mapper/VG1-DATA is now 26213376 (4k) blocks long.

$ df
Filesystem 1K-blocks Used Available Use% Mounted on
/dev/mapper/VG1-DATA 103077152 48774260 49568340 50% /data
```

2018/10/14

臺北市政府違反勞動法令公布專區


[臺北市政府違反勞動法令公布專區](https://bola.gov.taipei/News_Content.aspx?n=7C26DE8810A680AC&sms=EBEC4546C77557A3&s=94390C8160186CB5)

[臺北市政府勞動局-公開資料線上查詢系統(違反勞動基準法)](http://web2.bola.taipei/bolasearch/chhtml/page/20)

2018/10/13

在 Raspberry Pi 上看 CPU 溫度



手上剛好有兩台樹莓派, 一台有接風扇 一台沒接

心血來潮想看看這樣溫度可以差多少

指令如下

```shell
/opt/vc/bin/vcgencmd measure_temp
```

有風扇的

```shell
/opt/vc/bin/vcgencmd measure_temp
temp=37.6'C
```

無風扇的

```shell
/opt/vc/bin/vcgencmd measure_temp
temp=48.3'C
```

差了將近有10度阿...看來還是把風扇接上好了(雖然現在已進入秋天惹)


其實 vcgencmd 還有許多功能, 例如看 cpu頻率, gpu記憶體, 工作電壓, ...等等

可以透過以下指令查看
```shell
/opt/vc/bin/vcgencmd commands
```

2018/10/11

macOS 中解決 SSH 連線 key exchange method 與 cipher 問題



之前在公司連一台電腦時候出現了

```
Unable to negotiate with 3.3.2.3 port 014: \
no matching key exchange method found. Their offer: diffie-hellman-group1-sha1
```

原因是這種 key exchange method 的安全性比較低, 所以 macOS 預設沒有開啟

只要在 ssh_config 加上就好了

```
$ head ~/.ssh/config
KexAlgorithms +diffie-hellman-group1-sha1
```

加完後出現了不一樣的訊息

```
Unable to negotiate with 3.3.2.3 port 014: \
no matching cipher found. \
Their offer: aes128-cbc,3des-cbc,blowfish-cbc,aes192-cbc,aes256-cbc
```

看來是 cipher 也少了,aes256-cbc 比較安全些, 那就加上它吧

```
$ head ~/.ssh/config
KexAlgorithms +diffie-hellman-group1-sha1
Ciphers +aes256-cbc
```

這樣就可以成功 ssh 進去了