コピペコードで快適生活

明日使えるソースを自分のために

ファイルディスクリプタの上限変更

確認

$ cat /proc/sys/fs/file-nr
576	0	1636820

file-nr内のパラメータはそれぞれ、

  • オープンされているファイル数
  • 空きファイル管理データの数
  • システム中のオープンファイル管理データの最大数

を示します。

一時的な設定

ログインプロセスとその子プロセスでのみ有効。

# 確認
$ ulimit -n
1024

# 変更
$ ulimit -n 2048

恒久的な設定

システム全体
# 確認
cat /proc/sys/fs/file-nr

$ sudo vi /etc/sysctl.conf
# fs.file-max = 1232457
# を追記する

# 設定を反映
sudo /sbin/sysctl -p

# 確認
cat /proc/sys/fs/file-nr
ユーザごと

nodoc

daemonプロセス

limits.confに設定した値は、ログインしないdameonプロセスには効かないので、
起動スクリプト(/etc/init.d/*)に

ulimit -n 123456

を追記して対応する。

当該プロセスのファイルディスクリプタ上限の確認は下記でわかる。

cat /proc/#{pid}/limits
Limit                     Soft Limit           Hard Limit           Units
# --- 略 ---
Max open files            500000               500000               files
# --- 略 ---

該当プロセスが開いているファイル数はこれで確認できる

ls /proc/#{pid}/fd | wc -l

削除済のファイルを掴んでいるのもわかる

ls -la /proc/#{pid}/fd
# 出てきたリストに (deleted) と表示されていれば、削除済ファイルを掴んでいる

参考にさせていただきました。
@IT:/procによるLinuxチューニング [後編](2/4)

http://mikio.github.io/article/2013/03/02_.html

デーモンごとにulimitで設定した値を確認する - foxtrot0304's diary

AmazonLinuxでタイムゾーン設定メモ

どうするんだっけとなったときのメモ。
Linux インスタンスの時刻の設定 - Amazon Elastic Compute Cloud

sudo vi /etc/sysconfig/clock # 下記編集する
# ----------
# ZONE="UTC" を消して↓とする
ZONE="Asia/Tokyo" 
# ----------

# シンボリックリンクはる
sudo ln -sf /usr/share/zoneinfo/Asia/Tokyo /etc/localtime

# リブートする
sudo reboot

pyenvとpyenv-virtualenvでPython環境を作る

Pythonインストール
# cloneする
git clone https://github.com/yyuu/pyenv.git ~/.pyenv

# 設定追加
vim  ~/.bash_profile
===
export PYENV_ROOT=$HOME/.pyenv
export PATH=$PYENV_ROOT/bin:$PATH
eval "$(pyenv init -)"
===
# 上記設定の再読み込み
exec $SHELL -l

# バージョン指定してインストール
pyenv install 2.7

# インストール済のバージョン確認
pyenv versions

# デフォルト設定
pyenv global 2.7
prenv global system # で戻る

# カレントディレクトリでの設定
pyenv local 2.7 
pyenv-virtualenvの設定(gemsetみたいなやつ)
git clone https://github.com/yyuu/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

vim ~/.bash_profile
===
eval "$(pyenv virtualenv-init -)"
===
# 上記設定の再読み込み
exec $SHELL -l

# virtualenv作る
pyenv virtualenv 2.7 2.7_ansible

# 確認(versionと同じように扱える)
pyenv versions
pyenv uninstall 2.7-ansible
pyenv global 2.7_ansible

# あとはお好きにpip install
pip install boto3
pip install botocore
pip install boto
pip install ansible

Vim:改行時のコメントアウト対応

vimコメントアウトつきのテキストを貼り付けたいとき、
コメント行の次行以降が全部コメントアウトされてインデントも崩れて「あぁぁぁぁぁ」ってならないように、
貼り付ける前に以下のおまじないを唱える。

:set formatoptions-=ro

redis-cliコマンドメモ

接続

# localhost:6379接続
redis-cli

# リモート接続
redis-cli -h #{ホスト名} -p #{ポート番号} -n #{DB番号}

# 状態取得
reids-cli info

コマンド

# キー一覧
keys *

# キーの値を取得
get #{key名}

# キーをセット
set #{key名} #{値}

# キーを削除
del #{key名}

# ttl取得 (-1の場合は値がないか、期限がない)
ttl #{key名}

# キーを全削除
flushall

vagrant環境を100GBまで増やした時のメモ

8GBしかディスク容量がないvagrant環境を100GBまで増やした時のメモ

ホストOSでの作業

# vagrantのディスクパスへ移動
cd /cygdrive/c/Users/kinosuke/VirtualBox VMs/golem_default_1514254535481_98090

# vdi形式にクローンする
VBoxManage clonehd "centos-vm-disk1.vmdk" "clone-disk1.vdi" --format vdi

# 100GBに増やす
VBoxManage modifyhd clone-disk1.vdi --resize 102400

容量確認
VBoxManage showhdinfo clone-disk1.vdi

# vidからvmdkに変換
VBoxManage clonehd "clone-disk1.vdi" "centos-vm-disk2.vmdk" --format vmdk

VirtualBoxGUIでディスクを差し替える。

ゲストOSでの作業(パーティションの追加)

ゲストOSの/dev/sdaが増えているので、これから新たなパーティションを作り論理ボリュームに割り当てていく。

# 確認
[root@golem ~]# fdisk -l
Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00051d1a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1275     9726976   8e  Linux LVM

Disk /dev/mapper/VolGroup-lv_root: 8908 MB, 8908701696 bytes
255 heads, 63 sectors/track, 1083 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


Disk /dev/mapper/VolGroup-lv_swap: 1048 MB, 1048576000 bytes
255 heads, 63 sectors/track, 127 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@golem ~]# fdisk /dev/sda

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): n
Command action
   e   extended
   p   primary partition (1-4)
p
Partition number (1-4): 3
First cylinder (1275-13054, default 1275):
Using default value 1275
Last cylinder, +cylinders or +size{K,M,G} (1275-13054, default 13054):
Using default value 13054

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00051d1a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1275     9726976   8e  Linux LVM
/dev/sda3            1275       13054    94616255   83  Linux

Command (m for help): t
Partition number (1-4): 3
Hex code (type L to list codes): 8e
Changed system type of partition 3 to 8e (Linux LVM)

Command (m for help): p

Disk /dev/sda: 107.4 GB, 107374182400 bytes
255 heads, 63 sectors/track, 13054 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: 0x00051d1a

   Device Boot      Start         End      Blocks   Id  System
/dev/sda1   *           1          64      512000   83  Linux
Partition 1 does not end on cylinder boundary.
/dev/sda2              64        1275     9726976   8e  Linux LVM
/dev/sda3            1275       13054    94616255   8e  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.

再起動して反映する

ゲストOSでの作業(ボリュームの作成)

# 物理ボリューム確認
[root@golem ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               9.28 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2374
  Free PE               0
  Allocated PE          2374
  PV UUID               hTIxaE-XcZT-GoSQ-sIy7-jD2T-vHZE-MvgwTM

# 物理ボリューム作成
[root@golem ~]# pvcreate /dev/sda3
  Physical volume "/dev/sda3" successfully created

# 物理ボリューム確認
[root@golem ~]# pvdisplay
  --- Physical volume ---
  PV Name               /dev/sda2
  VG Name               VolGroup
  PV Size               9.28 GiB / not usable 3.00 MiB
  Allocatable           yes (but full)
  PE Size               4.00 MiB
  Total PE              2374
  Free PE               0
  Allocated PE          2374
  PV UUID               hTIxaE-XcZT-GoSQ-sIy7-jD2T-vHZE-MvgwTM

  "/dev/sda3" is a new physical volume of "90.23 GiB"
  --- NEW Physical volume ---
  PV Name               /dev/sda3
  VG Name
  PV Size               90.23 GiB
  Allocatable           NO
  PE Size               0
  Total PE              0
  Free PE               0
  Allocated PE          0
  PV UUID               QD6Ybs-UbFs-ddl8-XUj1-TYHh-hFOk-xO8AYT

# ボリュームグループ確認
[root@golem ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        1
  Metadata Sequence No  3
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                1
  Act PV                1
  VG Size               9.27 GiB
  PE Size               4.00 MiB
  Total PE              2374
  Alloc PE / Size       2374 / 9.27 GiB
  Free  PE / Size       0 / 0
  VG UUID               vSK8ek-anHw-vzIU-hLSC-YyUi-JJCK-43dkaR

# 物理ボリュームを、ボリュームグループに追加する
[root@golem ~]# vgextend VolGroup /dev/sda3
  Volume group "VolGroup" successfully extended

# ボリュームグループ確認
[root@golem ~]# vgdisplay
  --- Volume group ---
  VG Name               VolGroup
  System ID
  Format                lvm2
  Metadata Areas        2
  Metadata Sequence No  4
  VG Access             read/write
  VG Status             resizable
  MAX LV                0
  Cur LV                2
  Open LV               2
  Max PV                0
  Cur PV                2
  Act PV                2
  VG Size               99.50 GiB
  PE Size               4.00 MiB
  Total PE              25473
  Alloc PE / Size       2374 / 9.27 GiB
  Free  PE / Size       23099 / 90.23 GiB
  VG UUID               vSK8ek-anHw-vzIU-hLSC-YyUi-JJCK-43dkaR

# 論理ボリューム確認
[root@golem ~]# lvdisplay
  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_root
  LV Name                lv_root
  VG Name                VolGroup
  LV UUID                BbyXpl-Lu8z-aDXc-hF5u-ib1j-ml0f-PRlYvR
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-11-02 23:49:55 +0900
  LV Status              available
  # open                 1
  LV Size                8.30 GiB
  Current LE             2124
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:0

  --- Logical volume ---
  LV Path                /dev/VolGroup/lv_swap
  LV Name                lv_swap
  VG Name                VolGroup
  LV UUID                yk1n3U-cHOX-jeC6-p8yJ-sMu5-YMLL-oOs2cH
  LV Write Access        read/write
  LV Creation host, time localhost.localdomain, 2015-11-02 23:49:56 +0900
  LV Status              available
  # open                 1
  LV Size                1000.00 MiB
  Current LE             250
  Segments               1
  Allocation             inherit
  Read ahead sectors     auto
  - currently set to     256
  Block device           253:1

# 論理ボリュームの容量を増やす
# Lオプションで指定する追加サイズは、vgdisplayで表示されたFree PE / SizeとPE Sizeをかけた値
[root@golem ~]# lvextend -L +92396 /dev/VolGroup/lv_root
  Size of logical volume VolGroup/lv_root changed from 8.30 GiB (2124 extents) to 98.53 GiB (25223 extents).
  Logical volume lv_root successfully resized

# ファイルシステムに反映
[root@golem ~]# resize2fs -f /dev/VolGroup/lv_root
resize2fs 1.41.12 (17-May-2010)
Filesystem at /dev/VolGroup/lv_root is mounted on /; on-line resizing required
old desc_blocks = 1, new_desc_blocks = 7
Performing an on-line resize of /dev/VolGroup/lv_root to 25828352 (4k) blocks.
The filesystem on /dev/VolGroup/lv_root is now 25828352 blocks long.

# 確認
[root@golem ~]# df -h
Filesystem            Size  Used Avail Use% Mounted on
/dev/mapper/VolGroup-lv_root
                       97G  7.6G   85G   9% /
tmpfs                 1.9G  4.0K  1.9G   1% /dev/shm
/dev/sda1             477M   57M  396M  13% /boot

参考にさせていただきました
https://qiita.com/koitaro/items/c8bf8eb1e67e78890a5c
http://te2u.hatenablog.jp/entry/2015/04/20/012914