2015年8月24日 星期一

利用 overlayfs 將 filesystem.squafs 與 casper-rw 合併成新的 filesystem.squasfs

參考文章:
http://davstott.me.uk/index.php/2013/09/05/ubuntu-13-04-on-a-usb-flash-drive-and-merging-its-persistent-storage.


太實用了! 改天來玩玩,
這樣就無需將 filesystem.squashfs 解壓縮再重裝, 那麼麻煩, 直接用 iso 檔開機, 刪刪減減, 再將他合併, 改成符合自己所適用的作業系統

重點節錄:
假設 persistent storage 放在 /dev/sdb2
sudo su
cd /mnt
mkdir  ro.dir  rw.dir  new.dir
mount /dev/sdb2  rw.dir
mount  -o  loop  filesystem.squashfs  ro.dir
mount  -o  lowerdir=ro.dir,upperdir=rw.dir  -t  overlayfs  none  new.dir
mksquashfs new.dir filesystem.squashfs

chroot new.dir dpkg-query -W --showformat='${Package} ${Version}\n' > filesystem.manifest
printf $(du -sx --block-size=1 new.dir | cut -f1) > filesystem.size

p.s. Linux mint 17.2 已經內建 overlayfs, 可以利用它來運作

2015年8月23日 星期日

Linux mint 無需安裝, 直接使用 LiveCD iso 檔開機, 並讓它擁有永久存放檔案的能力

1. 先規劃好一塊硬碟分區, 因為我的 DRAM 有 8G 空間, 原先 /dev/sdb2 預備當成 swap 分區, 我想 Livecd 應該不需要, 於是我直接將此分區約 2G bytes 空間廢棄, 直接將他改成 ext3 分區來使用, 於是用先 fdisk 將 partition type 0x82 改成 0x83, 接著將他 format 成 ext3 檔案系統(mkfs.ext3 /dev/sdb2). 最後重要的是將他 label 命名成 casper-rw  
tune2fs -L casper-rw /dev/sdb2
或者
e2label /dev/sdb2 casper-rw  
2. 我將整個 cdrom 直接複製到 /dev/sda2/boot/ 目錄內
mount /dev/sda2 /mnt  
dd if=/dev/sr0      of=/mnt/boot/linuxmint172.iso
3. 我使用 Grub 2 的 boot loader, 在 boot option 增加 persistent 字眼
menuentry "Linux Mint 17.2" {  
set root=(hd0,2)  
loopback loop (hd0,2)/boot/linuxmint172.iso
linux        (loop)/casper/vmlinuz boot=casper persistent iso-scan/filename=/boot/linuxmint172.iso ro quiet splash locale=zh_TW.UTF-8    
initrd        (loop)/casper/initrd.lz    
}
4. 大功告成, Linux mint 無需安裝到硬碟中, 只要將 cdrom 的轉成 iso 檔, 複製到 /boot/ 目錄內就好, 且以後任何 linux 系統內任何修改過的資料, 全部都會更新到 /dev/sdb2 中了

2015年8月16日 星期日

試用 linux mint 17.2

重裝 Linux Mint 的方式大致上與重裝 Ubuntu 一樣,
可參考:How to customize Linux Mint live cd/dvd:
http://community.linuxmint.com/tutorial/view/1784
感覺還不錯用, 但是在 zh.TW-UTF8 中文介面, 開啟 terminal, 有些英文字(m 或 w)會與旁邊的字擠成一起, 造成辨識困難.
後來上 google 終於解決了, 只要直接下兩條命令來更改字型便可(特別注意: 千萬不要使用 root 來執行):
gconftool-2 --set /apps/gnome-terminal/profiles/Default/use_system_font --type=boolean false
gconftool-2 --set /apps/gnome-terminal/profiles/Default/font --type string "Liberation Mono 11"
整個操作很像在使用 Windows XP 的感覺, 但無內建中文輸入法, 只好自行再重新封裝加入 gcin:
apt-get install gcin
之後變可以很順利的使用了. 免錢的 OS, 值得推荐.
另外找到 Monaco 字型也可以直接套用, 需先將字型放入 ~/.fonts 或 /usr/share/fonts/truetype 目錄內,並執行 fc-cache -vf 後, 再套用上述兩條命令
gconftool-2 --set /apps/gnome-terminal/profiles/Default/use_system_font --type=boolean false
gconftool-2 --set /apps/gnome-terminal/profiles/Default/font --type=string "Monaco 11"
最後我用 isolinux 製作開機光碟, 將所有檔案放入 isodir 目錄裡, 之後執行 mkisofs:
sudo mkisofs -r -V "Linux Mint 17.2" -cache-inodes -J -l -o linux172.iso -b isolinux/isolinux.bin -c isolinux/boot.cat -no-emul-boot -boot-load-size 4 -boot-info-table isodir
附註1. isolinux 網址: http://www.syslinux.org/wiki/index.php/The_Syslinux_Project
使用 ISOLINUX 時的目錄及檔案結構:
isodir
isodir/isolinux
isodir/isolinux/isolinux.bin
isodir/isolinux/ldlinux.c32
isodir/isolinux/isolinux.cfg
casper
casper/vmlinuz
casper/initrd.lz
casper/filesystem.squashfs
casper/filesystem.manifest
.disk
.disk/casper-uuid-generic
如果是用 GRUB 2 來製作開機光碟, 將所有檔案放入 isodir 目錄裡, 之後再執行 grub-mkrescue
sudo grub-mkrescue -o linux172.iso isodir
附註2. apt-get install xorriso 安裝後, 才可以使用 grub-mkrescue
參考 grub-mkrescue 網址 https://www.gnu.org/software/grub/manual/html_node/Invoking-grub_002dmkrescue.html
使用 GRUB 2 時的目錄及檔案結構:
isodir
isodir/boot
isodir/boot/grub
isodir/boot/grub/grub.cfg
casper
casper/vmlinuz
casper/initrd.lz
casper/filesystem.squashfs
casper/filesystem.manifest
.disk
.disk/casper-uuid-generic

附註3. 如要移除桌面上的 "安裝" 按鈕需要執行在重新封裝(remaster)時將 ubituity 移除
apt-get remove ubiquity

改裝的 Linux mint 17.2 iso 檔案

附註4. 如果要 compile grub-2.0 時出現 stdio.in.h 錯誤訊息, 使用 sed 修改檔案內容變可解決:
sed -i -e ‘/gets is a security/d’ grub-core/gnulib/stdio.in.h








































(initramfs) Uable to find a medium containing a live file system

重裝 Ubuntu 時使用 GRUB 2或 ISOLINUX 當 boot loader 時, CDROM 根目錄 忘記將 .disk 目錄從原公版內複製過來時, 裏面有一個重要的檔案叫作 casper-uuid-generic, 內含當初 build 此公版時所創建的 uuid,如缺乏此檔將造成casper無法從此 CDROM 啟動 Ubuntu linux, 解決的方式是在 boot option 增加 ignore_uuid 便 可, 修改 grub.cfg 如下:

menuentry "Ubuntu Linux from CDROM" {

kernel /casper/vmlinuz.efi boot=casper ignore_uuid

initrd /casper/initrd.lz

}

如果是用 isolinux 則直接在 boot: 下命令

boot: kernel /casper/vmlinuz.efi initrd=/casper/initrd.lz boot=casper ignore_uuid

或修改 isolinux.cfg 內容如下:

label ubuntu

kernel /casper/vmlinuz.efi

append initrd=/casper/initrd.lz boot=casper ignore_uuid

應該可以解決

2013年9月29日 星期日

使用 wkhtmltopdf 將網頁轉成 PDF檔

1. 先下載 wkhtmltopdf: http://code.google.com/p/wkhtmltopdf/
2. 再編輯一串文字檔, 每一行的內容為皆為想要抓取的 URL , 後面緊跟要存成的 PDF 檔名, 將此黨命名為 note.txt
3. note.txt 範例:
http://madalgo.au.dk/~jakobt/wkhtmltoxdoc/wkhtmltopdf-0.9.9-doc.html 0
... 1
以此類推
3. 用 python 寫個 batch 程式, 將上述連結 URL 一行一行轉成 PDF:
url2pdf.py
----------------------------
import os
ff=open('note.txt','rt')
while True:
str=ff.readline()
if not str: break
sss=os.popen('"C:\\Program Files (x86)\\wkhtmltopdf\\wkhtmltopdf" '+str.strip('\n')+'.pdf')
sss.read()
sss.close()
ff.close
-----------------------------
4. 將會獲得 0.pdf, 1.pdf, 2.pdf, ...

2013年7月13日 星期六

scilab 呼叫 share library(Windows DLL)

使用 link & call

a=link("sharelib.dll","dllfunction","c");

o=0;

o=call("dllfunction",arg1,1,"i",arg2,2,"i","out",[length(o) 1],3,"i");

unlink(a);

其中 call 有幾個關鍵須先明瞭

1. 是 "out" 字眼,他是指明當呼叫 DLL 的函數時,等號左邊右邊的變數的分界點,"out" 之前的參數稱作 Righthand side input variables, "out" 之後的參數稱作 Lefthand side output variables.

2. 參數 arg(n) 須擺在當呼叫 DLL 時第幾(m)個參數必須被明確指出, 否則會出現記憶體分配錯誤的情形.(如上例:參數 arg1 當第一個, 參數 arg2 當第2 個, 第3個被當成傳回值且放到等號左邊第一個變數,且記憶體需要[lenghth(o) 1] 個陣列的空間

3. 當作 Lefthandside 的記憶體數量須先自行分配好足夠空間以便容納所傳回的陣列(或變數)

4. scilab 都是call by reference(也就是說它會傳變數的記憶體位址過去給DLL函數)

5. 資料型態共有 4 種: "i" 是 integer, "r" 是 real, "d" 是 double, "c" 是 character 註:

// 當'out'存在時的呼叫方式:

[y1,...,yk] = call("ident",x1,px1,"tx1",...,xn,pxn,"txn","out",[ny1,my1],py1,"ty1",...,[nyl,myl],pyl,"tyl")


使用 tinyCC 建立 DLL 讓 freemat 呼叫

download tiny C Compiler

//calldll.c
#include "windows.h"
typedef void (*FreematPrint)(const char*);
FreematPrint FreematConsole;
__declspec(dllexport) void freemat_io_handler(FreematPrint pname) { FreematConsole = pname; }
__declspec(dllexport) void calldll(int i)
{
char sbuf[1024];
sprintf(sbuf,"Freemat console print. You are passing an integer %d:sizeof(int)=%d bytes\n",i,sizeof(int));
FreematConsole(sbuf);
}

%%calldll.m
import('calldll.dll','calldll','mCalldll','void', 'int32 st');
mCalldll(1232138);

REM makedll.bat
tcc -shared calldll.c -ocalldll.dll

----- Freemat console snapshot --------
--> calldll
Freemat console print. You are passing an integer 1232138:sizeof(int)=4 bytes
-->