2017年5月31日 星期三

[Linux] cross compile tmux for Synology DS216play via DSM 6.1 Tool Chains - STMicroelectronics Monaco Linux 3.10.102 @ Ubuntu 16.04 64bit

連假最後一天跑來把玩一下 cross compiler ,想說未來應該會越來越常用 NAS 做事,覺得裡頭沒有個 tmux/screen 很煩,然而,等了很久還是沒人為 DS216play 編譯 tmux ,一直很搞不懂,直到我翻了這張表出來:

https://www.synology.com/en-global/knowledgebase/DSM/tutorial/General/What_kind_of_CPU_does_my_NAS_have

System Model CPU Model Cores Threads FPU Package Arch RAM
DS216play STM STiH412 Dual Core 2 Yes Monaco DDR3 1 GB

好的,看起來有點冷門的 STM STiH412/Monaco 囧,不過找一些討論串才知道,由於要讓 DS216play 可以做一些多媒體影音的事,所以,就別抱怨了 XD

下載 Tool chains: https://sourceforge.net/projects/dsgpl/files/DSM%206.1%20Tool%20Chains/STMicroelectronics%20Monaco%20Linux%203.10.102/

$ wget https://sourceforge.net/projects/dsgpl/files/DSM%206.1%20Tool%20Chains/STMicroelectronics%20Monaco%20Linux%203.10.102/monaco-gcc493_glibc220_hard-GPL.txz/download -O monaco-gcc493_glibc220_hard-GPL.txz
$ tar -xvf monaco-gcc493_glibc220_hard-GPL.txz
$ tree -L 1 arm-unknown-linux-gnueabi/
arm-unknown-linux-gnueabi/
├── arm-unknown-linux-gnueabi
├── bin
├── build.log.bz2
├── include
├── lib
├── libexec
└── share


前置環境:

$ sudo apt install make python gcc
$ chmod 755 $HOME/arm-unknown-linux-gnueabi $HOME/arm-unknown-linux-gnueabi/include $HOME/arm-unknown-linux-gnueabi/lib $HOME/arm-unknown-linux-gnueabi/bin $HOME/arm-unknown-linux-gnueabi/share


編譯 ncurses:

$ wget ftp://invisible-island.net/ncurses/ncurses.tar.gz
$ tar -xvf ncurses.tar.gz
$ cd ncurses-5.9
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin CC=$HOME/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc CFLAGS="-I$HOME/arm-unknown-linux-gnueabi/include"  ./configure --host arm-unknown-linux-gnueabi --prefix=$HOME/arm-unknown-linux-gnueabi
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin make
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin make install


編譯 libevent:

$ wget https://github.com/libevent/libevent/releases/download/release-2.1.8-stable/libevent-2.1.8-stable.tar.gz
$ tar -xvf libevent-2.1.8-stable.tar.gz
$ cd libevent-2.1.8-stable
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin CC=$HOME/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc CFLAGS="-I$HOME/arm-unknown-linux-gnueabi/include"  ./configure --host arm-unknown-linux-gnueabi --prefix=$HOME/arm-unknown-linux-gnueabi
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin CC=$HOME/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc CFLAGS="-I$HOME/arm-unknown-linux-gnueabi/include"  make
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin make install


編譯 tmux:

$ wget https://github.com/tmux/tmux/releases/download/2.5/tmux-2.5.tar.gz
$ tar -xvf tmux-2.5.tar.gz
$ cd tmux-2.5
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin CC=$HOME/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc CFLAGS="-I$HOME/arm-unknown-linux-gnueabi/include -I$HOME/arm-unknown-linux-gnueabi/include/ncurses" LDFLAGS="-L$HOME/arm-unknown-linux-gnueabi/lib" ./configure --host arm-unknown-linux-gnueabi --prefix=$HOME/arm-unknown-linux-gnueabi
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin make
$ file tmux
tmux: ELF 32-bit LSB executable, ARM, EABI5 version 1 (SYSV), dynamically linked, interpreter /lib/ld-linux-armhf.so.3, for GNU/Linux 2.6.32, not stripped


or:

$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin CC=$HOME/arm-unknown-linux-gnueabi/bin/arm-unknown-linux-gnueabi-gcc CFLAGS="-I$HOME/arm-unknown-linux-gnueabi/include -I$HOME/arm-unknown-linux-gnueabi/include/ncurses" LDFLAGS="-L$HOME/arm-unknown-linux-gnueabi/lib" ./configure --host arm-unknown-linux-gnueabi --prefix=$HOME/arm-unknown-linux-gnueabi --enable-static
$ PATH=$PATH:$HOME/arm-unknown-linux-gnueabi/bin make
...
...
cmd-string.o: In function `cmd_string_split':
cmd-string.c:(.text+0x820): warning: Using 'getpwnam' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
cmd-string.c:(.text+0x760): warning: Using 'getpwuid' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$HOME/arm-unknown-linux-gnueabi/lib/libevent.a(evutil.o): In function `test_for_getaddrinfo_hacks':
evutil.c:(.text+0x1be8): warning: Using 'getaddrinfo' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$HOME/arm-unknown-linux-gnueabi/lib/libevent.a(evutil.o): In function `evutil_unparse_protoname':
evutil.c:(.text+0x1554): warning: Using 'getprotobynumber' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$HOME/arm-unknown-linux-gnueabi/lib/libevent.a(evutil.o): In function `evutil_parse_servname':
evutil.c:(.text+0x14a0): warning: Using 'getservbyname' in statically linked applications requires at runtime the shared libraries from the glibc version used for linking
$ file tmux
tmux: ELF 32-bit LSB executable, ARM, EABI5 version 1 (GNU/Linux), statically linked, for GNU/Linux 2.6.32, not stripped


收工

2017年5月28日 星期日

ASUS X556U 更換 SSD 筆記

mainboard

前幾天室友的 ACER 4810TZG 掛了,於是乎人生中第二台 ASUS 的筆電入手,找了許久不想買太舊也不想買太新,就挑了個接近兩萬且大螢幕的 ASUS X556U ,其特色是 CPU 是 i5-6198UD 吧,看起來不差勁 XD 可惜預設是 HDD 就真的悲劇了點,按了電源後到進入 Windows 10 桌面,大概要 3-5 分鐘吧,所以,讓我想把舊筆電的 120GB SSD 拿來頂用,果然效果奇佳,開機就不到 10 秒進入到 Windows 10 桌面。筆記一下做了哪些事:

首先,是下載 Windows 10 ISO 並製作成 USB 開機碟,找了良久,原來當年可以從微軟官網透過簡單的 JS Code 可下載各種版本的 Windows ISO 的部分已被拿掉,但在 macOS 環境上還是可以輕鬆下載 ISO - https://www.microsoft.com/zh-tw/software-download/windows10ISO, 而在 Windows 上會被引導下載 MediaCreationTool.exe 工具,就透過 MediaCreationTool.exe 引導而製作 USB 開機碟。

如此一來,就只剩下換掉硬碟的部分,先上網隨便找個 ASUS 筆電看一下,才知道換硬碟真麻煩,主因是螺絲有一個躲在墊子下方,而就算所有螺絲都弄下來,拆殼還是很麻煩 Orz 後來,某天晚上終於牙一咬動手了

step01 step02

先拆座墊!在拆記憶體旁的螺絲,就開始用一字慢慢橇開背殼,自己手工很差,所以都會有刮傷,反正自用啦 XD

step03 affected

如此一來,就看到硬碟所在之處,在拆兩個螺絲、拆掉兩邊排線後,再把旁邊的小板拿起來就可換硬碟了!

subboard disk-move

2017年5月22日 星期一

[macOS] m3u/m3u8 -> grep -> xargs -> wget -> ffmpeg: ts -> mp4

目的是把 m3u8 的資料備份,接著又覺得檔案一堆,不如就用 ffmepg 轉 mp4 ,久久用一次,又忘得差不多。

連續動作:

$ wget http://.../index.m3u8 -O index.m3u8
$ grep -v "#" index.m3u8 | xargs wget
$ vim index.m3u8
:%s/http[^\/]+//g
:%s/?.*//g
$ grep -v "#" index.m3u8 | tr -d '\r' | awk '{cmd="mv "$1"* "$1; print cmd}'
$ grep -v "#" index.m3u8 | tr -d '\r' | awk '{cmd="mv "$1"* "$1; system(cmd)}'
$ ffmpeg -i index.m3u8 -vcodec copy -acodec copy -bsf:a aac_adtstoasc out.mp4

2017年5月21日 星期日

[PHP] 使用 AWS SDK 進行 S3 檔案上傳

寫過又忘了,筆記一下,環境是 PHP 5.5.9:

<?php
require 'aws.phar'; // http://docs.aws.amazon.com/aws-sdk-php/v3/download/aws.phar //aws-3.27.5.phar
$s3 = Aws\S3\S3Client::factory(array(
'region' => 'us-west-2',
// http://docs.aws.amazon.com/aws-sdk-php/v3/api/index.html
'version' => '2006-03-01',
'credentials' => array(
'key' => 'key',
'secret' => 'secret',
),
        ));

try {
$result = $s3->putObject(array(
'Bucket' => 'my-bucket',
'Key' => 'my-object-key'
'SourceFile' => $_FILES['userfile']['tmp_name'],
        ));
} catch (Exception $e) {
             
echo $e->getMessage() . "\n";
}

print_r($result);
// $result['ObjectURL'];


Aws\Result Object
(
    [data:Aws\Result:private] => Array
        (
            [Expiration] =>
            [ETag] =>
            [ServerSideEncryption] =>
            [VersionId] =>
            [SSECustomerAlgorithm] =>
            [SSECustomerKeyMD5] =>
            [SSEKMSKeyId] =>
            [RequestCharged] =>
            [@metadata] => Array
                (
                    [statusCode] => 200
                    [effectiveUri] => https://s3-us-west-2.amazonaws.com/my-bucket/my-object-key
                    [headers] => Array
                        (
                            [x-amz-id-2] =>
                            [x-amz-request-id] =>
                            [date] =>
                            [etag] =>
                            [content-length] => 0
                            [server] => AmazonS3
                        )

                    [transferStats] => Array
                        (
                            [http] => Array
                                (
                                    [0] => Array
                                        (
                                        )

                                )

                        )

                )

            [ObjectURL] => https://s3-us-west-2.amazonaws.com/my-bucket/my-object-key
        )

)

[PHP] 使用 SimpleXML 處理 RSS 資料

以前寫過,又忘又沒筆記 Orz

連續動作:

$ch = curl_init();
curl_setopt($ch , CURLOPT_URL, 'https://YourSite/rss');
curl_setopt($ch , CURLOPT_RETURNTRANSFER , true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);

$result = curl_exec($ch);

$output = array();
$xml = @simplexml_load_string($result);

if (isset($xml->channel) && isset($xml->channel->item)) {
foreach($xml->channel->item as $item) {
$date = date_parse((string)$item->pubDate);
array_push($output, array(
'date' => date('Ymd', mktime(0, 0, 0, $date['month'], $date['day'], $date['year'])),
'title' => (string)$item->title,
'link' => (string)$item->link,
'content' => (string)$item->children('http://purl.org/rss/1.0/modules/content/')->encoded,
'thumbnail' => null,
));
}
}

2017年5月18日 星期四

[macOS] 透過 MacPorts 安裝 MySQL Client

每次裝完都很感到疑惑,為何當下沒 mysql 指令可以用,然後又跑去 linux server 把玩 :p 其實是自己沒做設定罷了:

$ sudo port install mysql56
$ mysql
-bash: mysql: command not found
$ sudo port select mysql mysql56
$ mysql --version
mysql  Ver 14.14 Distrib 5.6.34, for osx10.11 (x86_64) using  EditLine wrapper


收工

2017年5月10日 星期三

[Javascript] 添加密碼規則要求 - 使用 jQuery.validator

剛好碰到需求,且沒有太嚴重的部分,就先偷懶用前端擋:

<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js"></script>

<script>

$(document).ready(function() {
// 要有 a-z
$.validator.methods.passwd_rule1 = function( value, element ) {
return this.optional( element ) || /[a-z]+/.test( value );
}
// 要有 A-Z
$.validator.methods.passwd_rule2 = function( value, element ) {
return this.optional( element ) || /[A-Z]+/.test( value );
}
// 要有 0-9
$.validator.methods.passwd_rule3 = function( value, element ) {
return this.optional( element ) || /[0-9]+/.test( value );
}


$("form").validate({
rules: {
newPassword: {
required: true,
minlength: 8,
passwd_rule1: true,
passwd_rule2: true,
passwd_rule3: true,
},

checkNewPassword: {
equalTo: "#newPassword"
}
},

messages: {
newPassword: "密碼必須8位數以上,且需含大小寫英文跟數字",
checkNewPassword: "輸入錯誤,請確認密碼",
}
});
});
</script>


其他把玩:

在 Chrome console 引入 library 測試,而非修改 source code

var js = document.createElement('script');
js.src = "https://ajax.googleapis.com/ajax/libs/jquery/2.1.4/jquery.min.js";
document.getElementsByTagName('head')[0].appendChild(js);
var js = document.createElement('script');
js.src = "https://cdnjs.cloudflare.com/ajax/libs/jquery-validate/1.16.0/jquery.validate.min.js";
document.getElementsByTagName('head')[0].appendChild(js);


替沒有 id tag 的 input 添加 id:

$('[name=newPassword]').attr('id', 'newPassword');

2017年5月8日 星期一

[PHP] 把玩 Laravel @ macOS 10.11.6

最近使用 Vue.js 後,想說練一下怎樣跟 php 整合,就挑 Laravel 來試試了。這次全靠 MacPorts 跟 Composer 來把玩,碰到了一些問題筆記一下。

$ sudo port install wget tmux php71 php71-openssl php71-mcrypt php71-mbstring
$ sudo port select php php71
$ cd ~
$ php -r "copy('https://getcomposer.org/installer', 'composer-setup.php');"
$ php -r "if (hash_file('SHA384', 'composer-setup.php') === '669656bab3166a7aff8a7506b8cb2d1c292f042046c5a994c43155c0be6190fa0355160742ab2e1c88d40d5be660b410') { echo 'Installer verified'; } else { echo 'Installer corrupt'; unlink('composer-setup.php'); } echo PHP_EOL;"
$ php composer-setup.php
$ php -r "unlink('composer-setup.php');"


建立專案:

$ time php ~/composer.phar create-project --prefer-dist laravel/laravel my-project

運行:

$ php artisan serve
Laravel development server started: <http://127.0.0.1:8000>


收工。

若有碰到奇怪的問題,通常是 php 版本問題:

Parse error: parse error, expecting `'&'' or `"variable (T_VARIABLE)"' in /Users/id/my-project/vendor/laravel/framework/src/Illuminate/Foundation/helpers.php on line 478
Script php artisan optimize handling the post-update-cmd event returned with error code 255


這是因為一開始偷懶不改變 php 環境變數,例如只用 alias php=php71 或是都用 php71 ~/composer.phar create-project --prefer-dist laravel/laravel my-project 這種執行方式所產生出來的,最後還是靠設定 PATH 或是 port select php php71 來解。