Amazonダッシュボタンで、好きな処理を実行する(iptables,syslog活用編)
Wifi接続可能なIoTデバイスとしては破格値の、Amazon Dash Button。(500円也)
「これを、ただのIoTボタンとして使ってみたい。」
ふつふつと込み上げる思いを形にすべく、Webで下調べを始めたところ、
nodeやruby用のライブラリを使ってハックされている方がいるようでした。
[参照]
Amazon Dash Buttonの仕組みとハック - Qiita
Amazon Dash Buttonを(正しくない方向で)使ってみた - Qiita
TCP/IP - DHCPとは
動作原理を見る限り、Linux標準機能で実装できそうな為とりあえずやってみます。
[環境]
IoTボタン:
Amazon Dash Button(Wilkinson)
フックサーバ:
HW) Raspberry Pi 2 Model B
OS) Raspbian GNU/Linux 8 (jessie)
MW) rsyslog 8.4.2
[動作概要]
Amazonダッシュボタンは押下する事でWifiに接続し、DHCPディスカバーリクエストを投げます。
そこで同じLAN内に、このブロードキャストパケットを受け取るサーバを設置し
特定MACアドレスからのパケット受信をトリガとして好きな処理を実行、
というのが大まか流れです。
ダッシュボタン押下(DHCPディスカバー)
-> フックサーバ/iptables -> syslog -> fifo <-> shell script
[前準備]
1.Amazonダッシュボタンを、Wifiに繋げる。
(下記URLを参考に設定を進める。商品選択前にキャンセルするのがポイント)
Amazon Dash ButtonをただのIoTボタンとして使う - Qiita
2.ダッシュボタンのMACアドレスを確認する
# tcpdump -i wlan0 | grep BOOTP/DHCP ※ここでダッシュボタンを押下。 tcpdump: verbose output suppressed, use -v or -vv for full protocol decode listening on wlan0, link-type EN10MB (Ethernet), capture size 262144 bytes 19:06:55.729394 IP 0.0.0.0.bootpc > 255.255.255.255.bootps: BOOTP/DHCP, Request from ac:63:be:98:xx:xx (oui Unknown), length 261 ^C172 packets captured 180 packets received by filter 0 packets dropped by kernel ※例では、wlan0がダッシュボタンと同一のNWにつながるインタフェース ※「ac:63:be:98:xx:xx」が本機器のMacアドレス
[設定]
・iptablesの設定
# iptables -I INPUT -i wlan0 -p udp --dport 67 -m mac --mac-source ac:63:be:98:xx:xx -j LOG --log-level=debug ※iptablesのsyslog facilityはkern固定なので、priorityを変更し、kern.debugとしてsyslogへ渡す。
# iptables -nvL Chain INPUT (policy ACCEPT 966 packets, 123K bytes) pkts bytes target prot opt in out source destination 1 289 LOG udp -- wlan0 * 0.0.0.0/0 0.0.0.0/0 udp dpt:67 MAC AC:63:BE:98:xx:xx LOG flags 0 level 7 ~以下略~ ※反映確認
# vi /etc/rc.local ※再起動すると消えるので、とりあえずrc.localに書いておく。 ※exit 0の前に上記iptables設定を追記。インタフェース名は環境の応じ適宜変更。
・rsyslog設定
# cp -p /etc/rsyslog.conf{,.orig} # vi /etc/rsyslog.conf # diff /etc/rsyslog.conf{.orig,} 65c65 < kern.* -/var/log/kern.log --- > kern.*;kern.!=debug -/var/log/kern.log 90c90 < news.none;mail.none -/var/log/debug --- > news.none;mail.none;kern.none -/var/log/debug 121a122 > kern.=debug |/var/log/dash.fifo ※rsyslog.confへkern.debugの設定を追加。 および「kern.debug」をkern.*/*.debugの処理から除外 ※kern.debug宛のログが来たら、名前付きパイプ/var/log/dash.fifoに渡す
・名前付きパイプの作成
# mkfifo -m 600 /var/log/dash.fifo
・名前付きパイプ監視スクリプトの作成
# mkdir -p /root/dash_button # vi /root/dash_button/check_fifo.sh --- #!/bin/sh PID='check_fifo.pid' trap 'echo check_fifo process was terminated.;rm -f /var/run/$PID' INT TERM echo $$ > /var/run/$PID while read log; do sh -c '/root/dash_button/dash_button.sh > /dev/null 2>&1 &' done < /var/log/dash.fifo --- # chmod u+x /root/sh/check_fifo.sh # /bin/sh -c '/root/dash_button/check_fifo.sh &' # vi /etc/rc.local --- /bin/sh -c '/root/dash_button/check_fifo.sh &' --- ※exit 0の前に追記
・任意の処理をさせるスクリプトの作成
# vi /root/dash_button/dash_button.sh --- #!/bin/sh touch /tmp/hoge ---
ここに好きな処理を書きます。
私はボタンを押すことで、職場のIRCへ出勤/退勤挨拶を書き込むスクリプトにしてます。
あと監視プロセス自体は、monitでお手軽プロセス監視中。
以上
Arch Linuxインストールメモ (archlinux-2013.09.01)
ArchLinux 2013.09.01 インストールメモです。
今回は64bit版を使用しました。
・使用メディア
ISO: archlinux-2013.02.01-dual.iso
md5:f87e89a502fb2d1f30ca0f9a927c9a91
・メディアからのブート
上記イメージよりブート。
今回は64bit版を選択する。
ブート完了後、インストール用のライブOSが起動したところ。
(rootで自動ログインした状態)
事前準備として、まずは日本語キーボードを使えるようにする。
# loadkeys jp106
・インストール準備
[ Network ]
手動設定を行う。
(DHCP環境なら特に設定不要)
iproute2コマンドでの設定例 # ip addr show # ip link show # ethデバイス名を確認 # ip link set eth0 up # ip addr add 192.168.100.100/24 dev eth0 # ip route add default via 192.168.100.254 dev eth0 # # vi /etc/resolv.conf # cat /etc/resolv.conf ### Google Public DNS nameserver 8.8.8.8 nameserver 8.8.4.4 # ping google.com PING google.com (173.194.38.78) 56(84) bytes of data. 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=1 ttl=46 time=17.2 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=2 ttl=46 time=21.9 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=3 ttl=46 time=14.0 ms ^C --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 14.006/17.750/21.997/3.285 ms
ついでに、リモートから作業できるようにsshdを起動する。
# passwd # rootユーザのパスワードを設定 # systemctl start sshd.service
[ Disk Partition ]
ディスクパーティションの設定。
本環境では、/dev/sdaをインストール先HDDとして使用する。
Disk Size: 16GB
device | mountpoint | size |
---|---|---|
/dev/sda1 | /boot | 100MB |
/dev/sda2 | swap | 512MB |
/dev/sda3 | / | 残り全て |
# fdisk /dev/sda # fdisk -l Disk /dev/sda: 17.2 GB, 17179869184 bytes, 33554432 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: 0xa8a49eff Device Boot Start End Blocks Id System /dev/sda1 * 2048 206847 102400 83 Linux /dev/sda2 206848 1255423 524288 82 Linux swap / Solaris /dev/sda3 1255424 33554431 16149504 83 Linux
※bootパーティションへbootフラグの付与を忘れない事
※fdiskの使い方は このあたりを参照の事。
[ Format ]
/(root),/bootをext4でフォーマットする。
# mkfs.ext4 /dev/sda1 # mkfs.ext4 /dev/sda3 #
[ Mount ]
先ほど作成した/dev/sdaの各パーティションを、ライブOSの/mnt配下にマウント。
# mount /dev/sda3 /mnt # rootパーティションのマウント # mkdir -p /mnt/boot # /boot,/var用マウントポイント作成 # mount /dev/sda1 /mnt/boot # /bootパーティション # df -hT Filesystem Type Size Used Avail Use% Mounted on ~ 中略 ~ /dev/sda3 ext4 16G 39M 15G 1% /mnt /dev/sda1 ext4 93M 1.6M 85M 2% /mnt/boot
・システムのインストール
[ mirrorlist ]
近くて早いミラー先を使用できるように、rankmirrorコマンドにてリストを更新する。
# cp -p /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.orig # rankmirrors -n 3 /etc/pacman.d/mirrorlist.orig > /etc/pacman.d/mirrorlist #けっこう時間がかかる # cat /etc/pacman.d/mirrorlist #上位3件がリストされている。 Server = http://ftp.tsukuba.wide.ad.jp/Linux/archlinux/$repo/os/$arch Server = http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/$repo/os/$arch Server = http://archlinux.cs.nctu.edu.tw/$repo/os/$arch
[ System Install ]
pacstrapスクリプトにて必要なパッケージグループを指定し、インストールを行う。
まずは基本的なパッケージグループのみ選択。
- base
- base-devel
# pacstrap /mnt base base-devel ==> Creating install root at /mnt ==> Installing packages to /mnt :: Synchronizing package databases... core 104.5 KiB 177K/s 00:01 [######################################################################] 100% extra 1449.0 KiB 594K/s 00:02 [######################################################################] 100% community 2024.7 KiB 421K/s 00:05 [######################################################################] 100% ~ 中略 ~ (126/128) installing patch [######################################################################] 100% Optional dependencies for patch ed: for patch -e functionality (127/128) installing pkg-config [######################################################################] 100% (128/128) installing sudo [######################################################################] 100% pacstrap /mnt base base-devel 42.14s user 22.91s system 13% cpu 7:56.05 total #
[ Bootloader ]
Bootloaderは以下が、使用可能。
- grub-bios
- grub-efi-x86_64
- syslinux
# pacstrap /mnt grub-bios #
[ その他パッケージ ]
個人的に最小限必要そうなものを適宜選択。
# pacstrap /mnt net-tools netctl dnsutils sysstat mlocate openssh syslog-ng # systemctl enable sshd # systemctl enable syslog-ng
・システム設定
[ fstab ]
# genfstab -p /mnt >> /mnt/etc/fstab #
[ chroot ]
以降は、新規インストールしたシステムにchrootして作業する。
# arch-chroot /mnt sh-4.2#
[ Hostname ]
適宜設定。
# echo "arch01.localdomain" >> /etc/hostname #
[ Timezone ]
日本時間に設定
# ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime #
[ Locale ]
# echo "LANG=ja_JP.UTF-8" > /etc/locale.conf # # cp -p /etc/locale.gen /etc/locale.gen.orig # vi /etc/locale.gen # diff -wb /etc/locale.gen.orig /etc/locale.gen 281c281 < #ja_JP.UTF-8 UTF-8 --- > ja_JP.UTF-8 UTF-8 # # locale-gen Generating locales... ja_JP.UTF-8... done Generation complete.
[ Keyboard ]
# echo "KEYMAP=jp106" > /etc/vconsole.conf #
[ ram image ]
# mkinitcpio -p linux #
[ grub install ]
# grub-install --target=i386-pc --boot-directory=/boot --recheck --debug /dev/sda # grub-mkconfig -o /boot/grub/grub.cfg # cp /usr/share/locale/en@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo # cd /boot/grub/locale # gzip en.mo #
[ root passwort]
rootユーザーのパスワードを設定。
# passwd Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
・swapの設定
# fdisk -l # mkswap /dev/sda2 # echo "/dev/sda2 swap swap defaults 0 0" >> /etc/fstab #
・再起動
chroot環境より抜けインストール用ディレクトリをunmount後、システム再起動。
# exit # umount /mnt/{boot,} # reboot #
・初期設定
[network(DHCP)]
# ip link show # ethデバイス名確認 (私の環境では「eno16777736」) # cp -p /etc/netctl/examples/ethernet-dhcp /etc/netctl/dhcp # vi /etc/netctl/dhcp # Interface名eth0をデバイス名に変更 # netctl enable dhcp # netctl start dhcp # ip addr show #ipアドレスが配布されたことを確認
[network(固定IP)]
# ip link show # ethデバイス名確認 (私の環境では「eno16777736」) # cp -p /etc/netctl/example/ethernet-static /etc/netctl/static # vi /etc/netctl/static # Interface名eth0および各種値を変更 # diff -wb /etc/netctl/examples/ethernet-static /etc/netctl/static 2c2 < Interface=eth0 --- > Interface=eno16777736 5c5 < Address=('192.168.1.23/24' '192.168.1.87/24') --- > Address=('192.168.6.233/24') 7,8c7,8 < Gateway='192.168.1.1' < DNS=('192.168.1.1') --- > Gateway='192.168.6.2' > DNS=('192.168.6.2') # netctl enable static # netctl start static # ip addr show #ipアドレスが付与されたことを確認 # #動作確認 # ping google.com PING google.com (173.194.38.78) 56(84) bytes of data. 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=1 ttl=46 time=17.2 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=2 ttl=46 time=21.9 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=3 ttl=46 time=14.0 ms ^C --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 14.006/17.750/21.997/3.285 ms
[ rc.local ]
ちょっとした処理で、systemdをいじるのが手間なので、rc.local相当を作成しておく
# vi /etc/rc.local # cat /etc/rc.local --- #!/bin/bash touch /tmp/hoge --- # chmod +x /etc/rc.local # # vi /usr/lib/systemd/system/rc-local.service # cat /usr/lib/systemd/system/rc-local.service [Unit] Description=/etc/rc.local Compatibility Wants=network.target [Service] Type=oneshot ExecStart=/etc/rc.local TimeoutSec=0 #StandardInput=tty RemainAfterExit=yes [Install] WantedBy=multi-user.target # ln -s /usr/lib/systemd/system/rc-local.service /etc/systemd/system/multi-user.target.wants/ # systemctl --system daemon-reload # systemctl enable rc-local.service
・Yaourtのインストール
AURを使用する場合。公式パッケージ(pacman)で事足りる場合は特に不要。
# cd /usr/local/src # pacman -Sy diffutils gettext yajl curl wget # # wget https://aur.archlinux.org/packages/pa/package-query/package-query.tar.gz # tar xvzf package-query.tar.gz # cd package-query # makepkg --asroot ; echo $? # pacman -U package-query-1.1-2-i686.pkg.tar.xz # cd ../ # # wget https://aur.archlinux.org/packages/ya/yaourt/yaourt.tar.gz # tar xvzf yaourt.tar.gz # cd yaourt # makepkg --asroot ; echo $? # pacman -U yaourt-1.2.2-1-any.pkg.tar.xz # yaourt --stats
・Xfce4のインストール
# pacman -S xorg-server xorg-server-utils xorg-xinit xf86-video-vesa xfce4 otf-ipafontfirefox # startxfce4
今回ビデオドライバについては汎用のvesaを使用しているが、使用するビデオカードに合わせ以下が使用可
# pacman -Ss xf86-video extra/xf86-video-apm 1.2.5-3 X.org Alliance ProMotion video driver extra/xf86-video-ark 0.7.5-3 (xorg-drivers xorg) X.org ark video driver extra/xf86-video-ast 0.97.0-3 (xorg-drivers xorg) X.org ASPEED AST Graphics video driver extra/xf86-video-ati 1:7.2.0-1 (xorg-drivers xorg) X.org ati video driver extra/xf86-video-chips 1.2.5-3 X.org Chips and Technologies video driver extra/xf86-video-cirrus 1.5.2-2 (xorg-drivers xorg) X.org Cirrus Logic video driver extra/xf86-video-dummy 0.3.6-3 (xorg-drivers xorg) X.org dummy video driver extra/xf86-video-fbdev 0.4.3-3 (xorg-drivers xorg) X.org framebuffer video driver extra/xf86-video-glint 1.2.8-3 (xorg-drivers xorg) X.org GLINT/Permedia video driver extra/xf86-video-i128 1.3.6-3 (xorg-drivers xorg) X.org Number 9 I128 video driver extra/xf86-video-i740 1.3.4-3 X.org Intel i740 video driver extra/xf86-video-intel 2.21.15-1 (xorg-drivers xorg) X.org Intel i810/i830/i915/945G/G965+ video drivers extra/xf86-video-mach64 6.9.4-2 (xorg-drivers xorg) X.org mach64 video driver extra/xf86-video-mga 1.6.2-3 (xorg-drivers xorg) X.org mga video driver extra/xf86-video-modesetting 0.8.0-1 (xorg-drivers xorg) X.org generic modesetting video driver extra/xf86-video-neomagic 1.2.8-1 (xorg-drivers xorg) X.org neomagic video driver extra/xf86-video-nouveau 1.0.9-1 (xorg-drivers xorg) Open Source 2D acceleration driver for nVidia cards extra/xf86-video-nv 2.1.20-3 (xorg-drivers xorg) X.org nv video driver extra/xf86-video-openchrome 0.3.3-2 (xorg-drivers xorg) X.Org Openchrome drivers extra/xf86-video-r128 6.9.1-4 (xorg-drivers xorg) X.org ati Rage128 video driver extra/xf86-video-rendition 4.2.5-3 X.org Rendition video driver extra/xf86-video-s3 0.6.5-3 X.org S3 video driver extra/xf86-video-s3virge 1.10.6-3 X.org S3 Virge video driver extra/xf86-video-savage 2.3.6-4 (xorg-drivers xorg) X.org savage video driver extra/xf86-video-siliconmotion 1.7.7-3 (xorg-drivers xorg) X.org siliconmotion video driver extra/xf86-video-sis 0.10.7-4 (xorg-drivers xorg) X.org SiS video driver extra/xf86-video-sisimedia 0.9.1-7 X.org SiS 671 video driver extra/xf86-video-sisusb 0.9.6-3 X.org SiS USB video driver extra/xf86-video-tdfx 1.4.5-3 (xorg-drivers xorg) X.org tdfx video driver extra/xf86-video-trident 1.3.6-4 (xorg-drivers xorg) X.org Trident video driver extra/xf86-video-tseng 1.2.5-3 X.org tseng video driver extra/xf86-video-v4l 0.2.0-12 (xorg-drivers xorg) X.org v4l video driver extra/xf86-video-vesa 2.3.2-3 (xorg-drivers xorg) [インストール済み] X.org vesa video driver extra/xf86-video-vmware 13.0.1-1 (xorg-drivers xorg) X.org vmware video driver extra/xf86-video-voodoo 1.2.5-3 (xorg-drivers xorg) X.org 3dfx Voodoo1/Voodoo2 2D video driver
以上
・参照
https://wiki.archlinux.org/index.php/Installation_Guide
https://wiki.archlinux.org/index.php/GRUB2
https://wiki.archlinux.org/index.php/Systemd
https://wiki.archlinux.org/index.php/Yaourt
キーワードに反応しシェルコマンドを実行するだけのTiarra用モジュール(Auto::RunCMD)
職場の業務連絡にはIRCを使用していますが、特定のキーワード(例えば名前やシステムの障害通知)が流れた際に、シェルスクリプトをキックして自動処理させるためのIRC Proxy Tiarra用モジュールを用意しました。
今回は面白法人カヤックさんのAuto::IMモジュールをひな形に、ごにょっとさせていただきました。ありがとうございます。
RunCMD.pm
tiarraを格納したディレクトリのmodule/Auto以下に保存します。
# ----------------------------------------------------------------------------- # $Id$ # ----------------------------------------------------------------------------- package Auto::RunCMD; use strict; use warnings; use base qw(Module); use Module::Use qw(Auto::AliasDB Auto::Utils); use Auto::AliasDB; use Auto::Utils; sub new { my ($class) = shift; my $this = $class->SUPER::new(@_); $this->config_reload(undef); return $this; } sub config_reload { my ($this, $old_config) = @_; my $regex = join '|', ( (map { "(?:$_)" } $this->config->regex_keyword('all')), (map { "(?i:\Q$_\E)" } map { split /,/ } $this->config->keyword('all')), ); eval { $this->{regex} = qr/$regex/; }; if ($@) { $this->_runloop->notify_error($@); } return $this; } sub message_arrived { my ($this,$msg,$sender) = @_; my @result = ($msg); # サーバーからのメッセージか? if ($sender->isa('IrcIO::Server')) { # PRIVMSGか? if ($msg->command eq 'PRIVMSG') { my $text = $msg->param(1); my $full_ch_name = $msg->param(0); if ($text =~ $this->{regex} && Mask::match_deep_chan( [Mask::array_or_all_chan($this->config->mask('all'))], $msg->prefix,$full_ch_name)) { my $run_command = system $this->config->command; } } } return @result; } 1; =pod info: 特定のキーワードが呼ばれると、指定のシェルコマンドを実行する。 default: off # 反応する人のマスクを指定します。 # 省略すると全員に反応します。 mask: * *!*@* # 反応するキーワードを正規表現で指定します。 # 複数指定したい時は複数行指定してください。 -regex-keyword: (?i:fugahoge) # 反応するキーワードを指定します。 # 複数指定したい時は,(コンマ)で区切るか、複数行指定してください。 keyword: hoge keyword: 日本語,テスト # 実行するシェルコマンドを指定します。 # コマンドは、ワンライナーで記述し、()で綴じてください。 # キーワードにマッチする度に何度でも実行されるので、困る場合は # スクリプトをキックするようにして、そちら側で制御してください。 command: (echo hogehoge >> /tmp/hoge.txt) =cut
tiarra.conf
下記を追記します。
+ Auto::RunCMD { # 特定のキーワードが呼ばれると、指定のシェルコマンドを実行する # 反応する人のマスクを指定します。 # 省略すると全員に反応します。 mask: * *!*@* # 反応するキーワードを正規表現で指定します。 # 複数指定したい時は複数行指定してください。 -regex-keyword: (?i:fugahoge) # 反応するキーワードを指定します。 # 複数指定したい時は,(コンマ)で区切るか、複数行指定してください。 keyword: hoge keyword: 日本語,テスト # 実行するシェルコマンドを指定します。 # コマンドは、ワンライナーで記述し、()で綴じてください。 # キーワードにマッチする度に何度でも実行されるので、困る場合は # スクリプトをキックするようにして、そちら側で制御してください。 command: (echo hogehoge >> /tmp/hoge.txt) }
以上
cgroupで、お手軽CPU使用率制限
先日無償公開されたSoftetherをAmazon EC2のmicroインスタンスへ入れてみた。
しかしCPU性能がしょぼいのか、暗号化処理に結構負荷がかかるのか、CPUを使い果たしてしまう事態に。
さらには仮想環境ということでstealが多発し、輪をかけて使い物にならない。
※これはEC2/microインスタンスの特性らしい。
http://qiita.com/items/8248ec4654cb809504c9
http://memo-off.blogspot.jp/2011/08/ec2-t1microcpu-steal.html
そこで今回はcroup/cgroupsを用いて当該プロセスのCPU使用率を制限、さらにはstealも発生しないようにする。
※croupは、Redhat系ならRHEL6/CentOS6から利用可能。
まずは、何もしない時のSoftetherプロセス(vpnserver)
# top top - 15:12:18 up 98 days, 11:08, 1 user, load average: 0.47, 0.21, 0.28 Tasks: 71 total, 1 running, 70 sleeping, 0 stopped, 0 zombie Cpu(s): 4.3%us, 8.4%sy, 0.0%ni, 0.0%id, 0.0%wa, 0.0%hi, 0.0%si, 87.3%st Mem: 608732k total, 590452k used, 18280k free, 115108k buffers Swap: 0k total, 0k used, 0k free, 269748k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 25549 root 0 -20 651m 87m 2452 S 99.9 14.6 0:24.00 vpnserver 25632 root 20 0 15052 1160 896 R 0.3 0.2 0:00.36 top 1 root 20 0 19400 1320 1016 S 0.0 0.2 0:01.14 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:02.63 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0 5 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u:0 6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 ~以下略~
stealも発生しまくり。
[root@mail local]# vmstat 1 100000 procs -----------memory---------- ---swap-- -----io---- --system-- -----cpu----- r b swpd free buff cache si so bi bo in cs us sy id wa st 1 0 0 37176 110844 252856 0 0 0 33 1 3 0 0 99 1 0 3 0 0 37504 110844 252860 0 0 0 0 76 505 3 10 0 0 88 1 0 0 37528 110844 252876 0 0 0 0 97 1355 5 7 0 0 87 1 0 0 37528 110844 252892 0 0 0 0 114 865 8 10 0 0 82 1 0 0 37528 110852 252896 0 0 0 12 62 633 3 7 0 0 90 2 0 0 37528 110852 252912 0 0 0 0 69 559 4 9 0 0 87 2 0 0 37528 110852 252920 0 0 0 0 77 516 4 9 0 0 87 2 0 0 37528 110852 252932 0 0 0 0 73 472 3 10 0 0 87 2 0 0 37528 110852 252940 0 0 0 0 65 427 5 10 0 0 85 2 0 0 37544 110860 252944 0 0 0 16 55 403 3 11 0 0 86 1 0 0 37592 110860 252948 0 0 0 0 60 391 3 8 0 0 89 1 0 0 37648 110860 252956 0 0 0 192 66 366 2 10 0 0 88 ~以下略~
そこで、cgroupの導入、設定
# yum install libcgroup # vi /etc/cgconfig.conf ※下記を追記 --- group vpnserver { cpu { cpu.cfs_quota_us = 250000; cpu.cfs_period_us = 1000000; } cpuacct { } } --- ※1秒あたり0.25秒間、単一のCPUを使用するように指定 (この環境が1コアCPUなので、アクセス頻度を1/4に抑えることで、結果25%までの使用率とする)
# chkconfig cgconfig on # /etc/init.d/cgconfig start
cgroup経由でSofteterを起動
# cgexec -g cpu:vpnserver /usr/local/vpnserver/vpnserver start
適用後のcpu使用率
# top top - 15:26:20 up 98 days, 11:22, 1 user, load average: 0.64, 0.76, 0.68 Tasks: 71 total, 1 running, 70 sleeping, 0 stopped, 0 zombie Cpu(s): 8.4%us, 16.4%sy, 0.0%ni, 74.9%id, 0.0%wa, 0.0%hi, 0.3%si, 0.0%st Mem: 608732k total, 541124k used, 67608k free, 111492k buffers Swap: 0k total, 0k used, 0k free, 257740k cached PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 26003 root 0 -20 651m 58m 2456 S 25.3 9.9 0:09.09 vpnserver 1 root 20 0 19400 1320 1016 S 0.0 0.2 0:01.14 init 2 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kthreadd 3 root 20 0 0 0 0 S 0.0 0.0 0:02.63 ksoftirqd/0 4 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/0:0 5 root 20 0 0 0 0 S 0.0 0.0 0:00.00 kworker/u:0 6 root RT 0 0 0 0 S 0.0 0.0 0:00.00 migration/0 ~以下略~
さらには、stealも発生しなくなり快適になった。
以上
■参照
・Product Documentation > Red Hat Enterprise Linux > Resource Management Guide > 3.2. cpu
https://access.redhat.com/site/documentation/ja-JP/Red_Hat_Enterprise_Linux/6/html/Resource_Management_Guide/sec-cpu.html
Linux機にWiMAX機器をUSB接続して使う
Linux機へUSB接続して使えるWimaxモデムについて調べてみると、技術的にかなり深い所まで調査されている方もいらっしゃるようだけれども、結局使えるのかどうかはわからなかった。(認証周りが鬼門のようだ)
ただ情報収集を進めたところ、最近のwifiルータータイプの一部機器ではUSB接続すると、cdc_etherもしくはrndisデバイスとして振る舞うらしい事がわかった。
それならLinux機でもすんなり使えるだろうと期待して、以下の機種を入手し実験してみる。
入手機器:
・Mobile Cube(IMW-C910W) [ネットワークコンサルティング社製]
※USB接続する # dmesg ~ 中略 ~ [82973.472357] usb 1-1.3: new high-speed USB device number 7 using dwc_otg [82973.574186] usb 1-1.3: New USB device found, idVendor=19f2, idProduct=1700 [82973.574220] usb 1-1.3: New USB device strings: Mfr=1, Product=2, SerialNumber=0 [82973.574263] usb 1-1.3: Product: RNDIS/Ethernet Gadget [82973.574282] usb 1-1.3: Manufacturer: Linux 2.6.23-uc1/vincent7-udc [82973.651904] usbcore: registered new interface driver cdc_ether [82973.665039] rndis_host 1-1.3:2.0: usb0: register 'rndis_host' at usb-bcm2708_usb-1.3, RNDIS device, 26:f3:64:18:**:** [82973.667437] usbcore: registered new interface driver rndis_host # # ifconfig usb0 up # dhcpcd usb0 # # ifconfig usb0 usb0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST> mtu 1400 inet 192.168.1.10 netmask 255.255.255.0 broadcast 192.168.1.255 inet6 fe80::24f3:64ff:fe18:3e5b prefixlen 64 scopeid 0x20<link> ether 26:f3:64:18:**:** txqueuelen 1000 (Ethernet) RX packets 41723 bytes 37717112 (35.9 MiB) RX errors 0 dropped 0 overruns 0 frame 0 TX packets 29681 bytes 6144531 (5.8 MiB) TX errors 0 dropped 0 overruns 0 carrier 0 collisions 0 # # ip route show default via 192.168.1.1 dev usb0 metric 205 192.168.1.0/24 dev usb0 proto kernel scope link src 192.168.1.10 metric 205 #
すんなりとDHCPでアドレスを取得し、インターネットに接続できた。
モデム接続とは違うけれども、無線以外の方法で接続出来たので満足。
この機種は、USB接続時に無線LANをOFFに出来るのもいいね。
以上
Unboundでお手軽キャッシュDNS ~家庭内LAN編~
家庭内LAN環境に追加でキャッシュDNSを置く必要があったので、その時のメモ。
・環境
OS: Arch Linux ARM (for Raspberry Pi)
# pacman -Sy unbound # vi /etc/unbound/unbound.conf # cat /etc/unbound/unbound.conf server: username: "unbound" directory: "/etc/unbound" use-syslog: yes interface: 0.0.0.0 interface: ::0 access-control: 192.168.0.0/16 allow forward-zone: name: "." forward-addr: 192.168.1.1 # # systemctl start unbound.service # systemctl enable unbound.service #
デフォルトでは、ループバックインタフェースのみのListenおよびアクセス許可が効いているので、
interface,access-controlで別途指定してやる。
また、上位に別のキャッシュDNS(ブロードバンドルーター等)がある場合は、forward-zoneセクション内で指定する。
以上
Arch Linuxインストールメモ (archlinux-2013.02.01)
ArchLinux 2013.02.01 インストールメモです。 → Arch Linux 2013.09.01版を追加しました。
今回は32bit版を使用しました。
・使用メディア
ISO: archlinux-2013.02.01-dual.iso
・メディアからのブート
上記イメージよりブート。
今回は32bit版を選択する。
ブート完了後、インストール用のライブOSが起動したところ。
(rootで自動ログインした状態)
事前準備として、まずは日本語キーボードを使えるようにする。
# loadkeys jp106
・インストール準備
[ Network ]
手動設定を行う。
(DHCP環境なら特に設定不要)
iproute2コマンドでの設定例 # ip addr show # ip link show # ethデバイス名を確認 # ip link set eth0 up # ip addr add 192.168.100.100/24 dev eth0 # ip route add default via 192.168.100.254 dev eth0 # # vi /etc/resolv.conf # cat /etc/resolv.conf ### Google Public DNS nameserver 8.8.8.8 nameserver 8.8.4.4 # ping google.com PING google.com (173.194.38.78) 56(84) bytes of data. 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=1 ttl=46 time=17.2 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=2 ttl=46 time=21.9 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=3 ttl=46 time=14.0 ms ^C --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 14.006/17.750/21.997/3.285 ms
ついでに、リモートから作業できるようにsshdを起動する。
# passwd # rootユーザのパスワードを設定 # systemctl start sshd.service
[ Disk Partition ]
ディスクパーティションの設定。
本環境では、/dev/sdaをインストール先HDDとして使用する。
Disk Size: 8GB
device | mountpoint | size |
---|---|---|
/dev/sda1 | /boot | 100MB |
/dev/sda2 | swap | 512MB |
/dev/sda3 | / | 残り全て |
# fdisk /dev/sda # fdisk -l Disk /dev/sda: 8589 MB, 8589934592 bytes, 16777216 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 identifier: 0xdf6c562f Device Boot Start End Blocks Id System /dev/sda1 * 2048 206847 102400 83 Linux /dev/sda2 206848 1255423 524288 83 Linux /dev/sda3 1255424 16777215 7760896 83 Linux
※bootパーティションへbootフラグの付与を忘れない事
※fdiskの使い方は このあたりを参照の事。
[ Format ]
/(root),/bootをext4でフォーマットする。
# mkfs.ext4 /dev/sda1 # mkfs.ext4 /dev/sda3 #
[ Mount ]
先ほど作成した/dev/sdaの各パーティションを、ライブOSの/mnt配下にマウント。
# mount /dev/sda3 /mnt # rootパーティションのマウント # mkdir -p /mnt/boot # /boot,/var用マウントポイント作成 # mount /dev/sda1 /mnt/boot # /bootパーティション # df -hT Filesystem Type Size Used Avail Use% Mounted on ~ 中略 ~ /dev/sda3 ext4 7.3G 145M 6.8G 3% /mnt /dev/sda1 ext4 97M 5.6M 87M 7% /mnt/boot
・システムのインストール
[ mirrorlist ]
一番近いミラー先を使用できるように、リストの先頭に
Score値0.1にて、日本のレポジトリを追記。
# cp -p /etc/pacman.d/mirrorlist /etc/pacman.d/mirrorlist.org # vi /etc/pacman.d/mirrorlist # diff -wb /etc/pacman.d/mirrorlist.org /etc/pacman.d/mirrorlist 6a7,8 > ## Score: 0.1, Japan > Server = http://ftp.jaist.ac.jp/pub/Linux/ArchLinux/$repo/os/$arch
[ System Install ]
pacstrapスクリプトにて必要なパッケージグループを指定し、インストールを行う。
今回はGUI不要の為、基本的なパッケージグループのみ選択。
- base
- base-devel
# pacstrap /mnt base base-devel ==> Creating install root at /mnt ==> Installing packages to /mnt warning: database file for 'core' does not exist warning: database file for 'extra' does not exist warning: database file for 'community' does not exist :: Synchronizing package databases... core 106.0 KiB 1185K/s 00:00 [#########################################] 100% extra 1419.3 KiB 3.12M/s 00:00 [#########################################] 100% community 1921.0 KiB 3.28M/s 00:01 [#########################################] 100% ~ 中略 ~ (127/129) installing make [#########################################] 100% (128/129) installing patch [#########################################] 100% Optional dependencies for patch ed: for patch -e functionality (129/129) installing pkg-config [#########################################] 100% pacstrap /mnt base base-devel 98.35s user 14.78s system 51% cpu 3:41.15 total #
[ Bootloader ]
Bootloaderは以下が、使用可能。
- grub-bios
- grub-efi-x86_64
- syslinux
# arch-chroot /mnt pacman -S grub-bios #
・システム設定
[ fstab ]
# genfstab -p /mnt >> /mnt/etc/fstab #
[ chroot ]
以降は、新規インストールしたシステムにchrootして作業する。
# arch-chroot /mnt sh-4.2#
[ Hostname ]
適宜設定。
# echo "arch01.localdomain" >> /etc/hostname #
[ Timezone ]
BIOSの時間をローカルタイムにする場合は、adjtimeファイルを作成する
# ln -s /usr/share/zoneinfo/Asia/Tokyo /etc/localtime # # vi /etc/adjtime # cat /etc/adjtime 0.0 0.0 0.0 0 LOCAL
[ Locale ]
# vi /etc/locale.conf # cat /etc/locale.conf LANG="ja_JP.UTF-8" # # cp -p /etc/locale.gen /etc/locale.gen.org # vi /etc/locale.gen # diff -wb /etc/locale.gen.org /etc/locale.gen 281c281 < #ja_JP.UTF-8 UTF-8 --- > ja_JP.UTF-8 UTF-8 # # locale-gen Generating locales... ja_JP.UTF-8... done Generation complete.
[ Keyboard ]
# vi /etc/vconsole.conf # cat /etc/vconsole.conf KEYMAP=jp106
[ ram image ]
# mkinitcpio -p linux #
[ grub install ]
# grub-install --target=i386-pc --boot-directory=/boot --recheck --debug /dev/sda # grub-mkconfig -o /boot/grub/grub.cfg # cp /usr/share/locale/en@quot/LC_MESSAGES/grub.mo /boot/grub/locale/en.mo # cd /boot/grub/locale # gzip en.mo #
[ root passwort]
rootユーザーのパスワードを設定。
# passwd Enter new UNIX password: Retype new UNIX password: passwd: password updated successfully
・swapの設定
# fdisk -l # mkswap /dev/sda2 # echo "/dev/sda2 swap swap defaults 0 0" >> /etc/fstab #
・再起動
chroot環境より抜けインストール用ディレクトリをunmount後、システム再起動。
# exit # umount /mnt/{boot,} # reboot #
・初期設定
必要最低限のパッケージを追加する。
[network(DHCP)]
# ip link show # ethデバイス名確認 (私の環境では「enp2s1」) # systemctl start dhcpcd@enp2s1 # systemctl enable dhcpcd@enp2s1 #次回より自動起動
[network(固定IP)]
# ip link show # ethデバイス名確認 (私の環境では「enp2s1」) # vi /etc/conf.d/network # cat /etc/conf.d/network interface=enp2s1 address=192.168.100.100 netmask=24 broadcast=192.168.100.255 gateway=192.168.100.254 # # vi /usr/lib/systemd/system/network.service # cat vi /usr/lib/systemd/system/network.service [Unit] Description=Network Connectivity Wants=network.target Before=network.target [Service] Type=oneshot RemainAfterExit=yes EnvironmentFile=/etc/conf.d/network ExecStart=/sbin/ip link set dev ${interface} up ExecStart=/sbin/ip addr add ${address}/${netmask} broadcast ${broadcast} dev ${interface} ExecStart=/sbin/ip route add default via ${gateway} [Install] WantedBy=multi-user.target # # ln -s /usr/lib/systemd/system/network.service /etc/systemd/system/multi-user.target.wants/ # systemctl start network.service # systemctl enable network.service # 次回より自動起動 # # #DNS設定 # vi /etc/resolv.conf # cat /etc/resolv.conf nameserver 8.8.8.8 nameserver 8.8.4.4 # #動作確認 # ping google.com PING google.com (173.194.38.78) 56(84) bytes of data. 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=1 ttl=46 time=17.2 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=2 ttl=46 time=21.9 ms 64 bytes from nrt19s17-in-f14.1e100.net (173.194.38.78): icmp_req=3 ttl=46 time=14.0 ms ^C --- google.com ping statistics --- 3 packets transmitted, 3 received, 0% packet loss, time 2003ms rtt min/avg/max/mdev = 14.006/17.750/21.997/3.285 ms
[ syslog-ng,openssh ]
syslogとsshd。ついでにインストール済みのcornも自動起動設定
# pacman -Sy openssh syslog-ng # systemctl enable syslog-ng.service # systemctl start syslog-ng.service # # systemctl enable sshd.service # systemctl starty sshd.service # # systemctl enable cronie.service # systemctl start cronie.service #
[ net-tools,dnsutils ]
ifconfig,netstat,digなどのネットワーク系コマンドツール
# pacman -Sy net-tools dnsutils #
[ sysstat,mlocate ]
sarなどモニタリングツールおよびlocateコマンド
# pacman -Sy sysstat mlocate #
[ rc.local ]
ちょっとした処理で、systemdをいじるのが手間なので、rc.local相当を作成しておく
# vi /etc/rc.local # chmod +x /etc/rc.local # cat /etc/rc.local --- #!/bin/bash touch /tmp/hoge # test --- # # vi /usr/lib/systemd/system/rc-local.service # cat /usr/lib/systemd/system/rc-local.service [Unit] Description=/etc/rc.local Compatibility Wants=network.target [Service] Type=oneshot ExecStart=/etc/rc.local TimeoutSec=0 #StandardInput=tty RemainAfterExit=yes [Install] WantedBy=multi-user.target # ln -s /usr/lib/systemd/system/rc-local.service /etc/systemd/system/multi-user.target.wants/ # systemctl --system daemon-reload # systemctl enable rc-local.service
・Yaourtのインストール
AURを使用する場合。公式パッケージ(pacman)で事足りる場合は特に不要。
# cd /usr/local/src # pacman -Sy diffutils gettext yajl curl wget # # wget https://aur.archlinux.org/packages/pa/package-query/package-query.tar.gz # tar xvzf package-query.tar.gz # cd package-query # makepkg --asroot ; echo $? # pacman -U package-query-1.1-2-i686.pkg.tar.xz # cd ../ # # wget https://aur.archlinux.org/packages/ya/yaourt/yaourt.tar.gz # tar xvzf yaourt.tar.gz # cd yaourt # makepkg --asroot ; echo $? # pacman -U yaourt-1.2.2-1-any.pkg.tar.xz # yaourt --stats
以上
・参照
https://wiki.archlinux.org/index.php/Installation_Guide
https://wiki.archlinux.org/index.php/GRUB2
https://wiki.archlinux.org/index.php/Systemd
https://wiki.archlinux.org/index.php/Yaourt