Monday, July 29, 2013

FreeNAS Resize Root Partition

FreeNAS designed to fix into sdcard, the default root partition only come with 1GB, it's insufficient if you try install mediatomb + transcoder. Below is steps on how to resize the root partition of FreeNAS.
Before proceed, boot into option 4 (single user mode) for all steps below.

1. )  Add another disk to NAS machine or virtual disk if running on VM.

2. )  Backup the current partitions for reference, type:
# gpart backup da0
<da0> is the geom label, your case may difference.

3. ) Backup all partition to new added disk.
# dd if=/dev/da0s1 of=<new disk path>/da0s1
# dd if=/dev/da0s2 of=<new disk path>/da0s2
# dd if=/dev/da0s3 of=<new disk path>/da0s3
# dd if=/dev/da0s4 of=<new disk path>/da0s4

4. ) Delete and re-create the partition so that we have the free space for 1st partition.

# gpart show da0
=>      63  18874305  da0  MBR  (9.0G)
        63  1930257    1  freebsd  [active]  (942M)
  1930320   1930257    2  freebsd  (942M)
  3860577      3024    3  freebsd  (1.5M)
  3863601     41328    4  freebsd  (20M)
  3904929       14969376       - free -  (7.1G)

# gpart delete -i 4 da0

you need calculate the start index of your last partition, your last partition index should be 18874305 - 128 - 41328.

# gpart add -t freebsd -b 18832849 -s 41328 da0

5. ) Repeat step 4. for partition 3 and 2. You should get something like below
# gpart show da0
=>      63  18874305  da0  MBR  (9.0G)
        63  1930320    1  freebsd  [active]  (942M)
  1930383       14969241       - free -  (7.1G)
  16899624   1930257    2  freebsd  (942M)
  18829881      3024    3  freebsd  (1.5M)
  18832905     41328    4  freebsd  (20M)

6. ) Restore the partition 2,3 and 4.
# dd of=/dev/da0s2 if=<new disk path>/da0s2
# dd of=/dev/da0s3 if=<new disk path>/da0s3
# dd of=/dev/da0s4 if=<new disk path>/da0s4

7. )  Resize root partition
# gpart resize -i 1 da0

8. ) Correct the label
# bsdlabel /dev/da0s1
# /dev/da0s1:
8 partitions:
#          size     offset    fstype   [fsize bsize bps/cpg]
  a:   1930241         16    unused        0     0  
  c:   1930257          0    unused        0     0     # "raw" part, don't edit

Edit the label to correct size.
#  gpart show da0
=>      63  18874305  da0  MBR  (9.0G)
        63  16899561    1  freebsd  [active]  (8.1G)
  16899624   1930257    2  freebsd  (942M)
  18829881      3024    3  freebsd  (1.5M)
  18832905     41328    4  freebsd  (20M)
  18874233       135       - free -  (67k)

 Mine da0s1 size is 16899561, I edit my label as below:
# /dev/da0s1:
8 partitions:
#          size     offset    fstype   [fsize bsize bps/cpg]
  a:   16899545         16    unused        0     0   
  c:   16899561          0    unused        0     0     # "raw" part, don't edit


9. ) Now grow the file system
# growfs /dev/da0s1a

10. ) Reboot the machine and you will find that the root / not able to mount
type ufs:/dev/da0s1a to mount root.

11. ) Correct the mount path for root /
# mount -uw /
# vi /conf//base/etc/fstab
change /dev/ufs/FreeNASs1a / ufs ro 1 1
to /dev/da0s1a / ufs ro 1 1

12.) Reboot and done.




Tuesday, July 23, 2013

Android - DBHelper

Android using Sqlite as primary storage for android application, below is the sample code for create, upgrade or delete database.


Usually you will require to create one Helper for 1 database, we create our first helper class name "DBHelper", this class extends SQLiteOpenHelper and you need implement at least 2 methods


  • onCreate()
  • onUpgrade()
Always create only single instance for any DBHelper to avoid open & close connection of DB.


   public static DBHelper getInstance() {
        if(instance == null) {
            instance = new UserDatabaseHelper();
        }
        return instance;
    }



After install and start the app, verify if the db created.

root@android:/data/data/com.androidtutorial/databases # ls
Songs.db
Songs.db-journal
e3 Songs.db                                                                   <
SQLite version 3.7.16 2013-03-18 11:39:23 [www.ptsoft.org] [www.ptdave.com]
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
Songs             android_metadata
sqlite> select * from Songs;
sqlite> select * from Songs;
song1
sqlite> 


The songs.db is created.


One confusing about onUpgrade, if you upgrade the db version from 2~10, don't expect onUpgrade will handle for you, you need do some checking as below.

   @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
        Log.d(TAG, "onUpgrade ..." + oldVersion ); 
        oldVersion += 1;
        if(oldVersion < newVersion) {
            onUpgrade(db, oldVersion, newVersion);
        }
    }