clockworkpi

[All In One Image Provided] Turning Clockworkpi into Backup & Sharing Center

Updated 16th Mar, 2020

As lots of people are hard to understand the whole process hence I made an AIO Image with latest CPI OS (v0.5) here for your convinence.

Download Here!!

What you need to do

  • Download the image
  • Flash to a TF card larger than 16GB
  • Extend Root partition to full size (No auto expand script included in this version)
  • Amend SSID and WIFI password in /etc/hostapd/hostapd.conf (Optional)
  • Get an OTG cable(mentioned below) ready!

How it works

  1. Once boot into system and get above mentioned things ready, you can attach your external storage drive into microUSB by OTG cable. A cron script will check if any external storage exist every minute.

  2. Be aware that XBOX layout is set as default.

  3. Backup the external drive accordingly.

How to access integrated Owncloud

Once the backup process is done, you will be asked to enable WiFi HotSpot. Press A to enable.

After Hotspot enabled, you can connect to the SSID showing on the screen, password is set to 12345678 by default.

Once connected, visit http://192.168.1.1/ in browser and login with username cpi password cpi

Press B to exit the sharing.

That’s all!

Troubleshooting

In case of not responding keyboard input you may use sudo killall dwm-mod through SSH.

If you don’t have access to SSH by this moment you are suggested to perform a hardware rest instead.

All error message during OS startup and WiFi Sharing can by ignored safely.


Technical Details (Released before AIO Image)

0x01 Why am I doing this

At the very first beginning I bought CPI is not for gaming, as extraordinary extension ability is with this device I wondering how to hack the HW and turn it into something useful with me.

What can you achieve after read through this article is, once you connect a storage device to CPI, CPI will automatically backup all files inside, and you can visit them by connecting to WiFi HotSpot activated by CPI and transfer your file through OwnCloud service inside CPI.

0x02 Prerequisites

Hardware

To connect external device such as card reader or external hard drive, a microUSB OTG cable is a must

To enable larger space inside CPI, a 128G TF card is recommended.

Software

  • A new kernel with USB Support Enabled
  • Filesystem Support for FAT/NTFS/exFAT
  • A cron script for backup periodically
  • A backup script and rsync
  • A brightness control script (preventing from black screen during backup)
  • Apache, PHP installed on CPI
  • Owncloud installed on CPI
  • Hostapd for WiFi HotSpot Support
  • Dnsmasq as replacement for dhcpd

0x03 Before next step…


This is NOT a step-by-step guideline, which means ALL Non-critical steps will NOT be provided.

This is NOT a guideline for beginners, please make sure you know exactly what you are doing.

Basic operations such as disk extension, permission setting will be omitted.

I know nothing about python developing hence all scripts are written by bash. Be my guest to convert into application in launcher if you can.


0x04 Requirement Fulfillment

Cool, you made it. Let’s move on.

Hardware Fulfillment

Get a OTG cable from wherever you could find, it won’t be an issue as it’s not requiring special pins for this cable.

I’ve do a lot of search regarding this topic but seems nothing found here. Someone may mentioned special-pin-version OTG cable is needed, but as my test, for CPI3, it’s not.

After you got your cable, try to connect with a keyboard. If it works, congrats.

Try dmesg during your debug. If you boot with original kernel version, the keyboard should work but external drive will not. We will discuss this later.

A larger TF card is recommended here as you are making a backup server.

Make sure you created DOS partition table with fdisk, or otherwise you won’t be able to boot up

Software Fulfillment

Kernel Replacement

Thanks to @shell I compiled the new kernel with USB support and for a easier solution you can just download the uImage file here.

To install new kernel, mount /dev/mmcblk0p1 somewhere in CPI, and backup old uImage file, replace with it.

After a reboot you should be able to find more information in dmesg during inserting a USB drive.

Filesystem Support

Filesystem support is quite easy as same as other Deiban distribution works. No details here.

Cron Script

A script that should periodically executed to monitor if any new drive inserted. A example here:

cpi@DEOT:~/imgbackup$ cat monitor.sh
#!/bin/bash

loop(){

    ls /dev/sd*1 &> /dev/null
    [ $? -ne 0 ] && return 1;
    return 0;

}

main(){

    cd `dirname $0`
    loop
    [ $? -eq 0 ] && xterm -display :0 -e ./copy.sh

}
main

Add a crontab item here:

*/1 * * * * flock -xen /tmp/rsync.lock -c '/home/cpi/imgbackup/monitor.sh'

Make sure flock is used here to prevent multiple instances launched.

Main Backup Script

cpi@DEOT:~/imgbackup$ cat copy.sh 
#!/bin/bash

init(){

SDRIVE=`ls /dev/sd*1`
DEST="/backup/OwnCloudUserName/files/"
MP="/mnt/bak_disk"
echo "Found External Drive $SDRIVE."
read -n1 -sp "Press A to start copy, B to abort." CHOICE
[[ $CHOICE == "j" ]] && echo "Confirmed! Copy in progress!" && copypic
[[ $CHOICE == "k" ]] && echo "Aborted!" && sleep 5 && exit;

}


initsetup(){

    sudo groupadd bakgrp
    sudo usermod -aG bakgrp cpi
    sudo mkdir -p $DEST
    sudo chmod 2770 $DEST
    sudo chown root:bakgrp $DEST
    sudo mkdir -p $MP
    echo "Initization Completed. Keep Syncing..,"
    copypic
}
copypic(){

    which rsync > /dev/null
    [ $? -ne 0 ] && echo "Rsync not found, installing.." && sudo apt install -y rsync
    rsync=`which rsync`
    [ ! -d $DEST ] && echo "Destination folder missing, initiating..." && initsetup
    echo "Mounting Drive."
    sudo mount $SDRIVE $MP
    mount | grep "$MP" &>/dev/null
    [ $? -ne 0 ] && echo "Mount drive failed, exiting..." && sleep 5s && exit;
    echo "Mounted on $MP, now copy started."
    rsync -avz --delete $MP $DEST
    sudo umount $MP
    echo "Now refreshing Owncloud files..."
    sudo systemctl start apache2
    sudo -u www-data php /var/www/html/occ files:scan --all
    sleep 5
    [ $? -eq 0 ] && echo "Backup Succeed. Now loading Hotspot Module." && sleep 2  && hotspot;
    [ $? -ne 0 ] && echo "Backup Failed, exiting...." && sleep 5 && kill `pgrep setbright` && killall dwm-mod && exit;

}

#loop(){
#
#    ls /dev/sd*1 > /dev/null
#    [ $? -ne 0 ] && return 1;
#    return 0;
#
#}
hotspot(){

    read -n1 -sp "Do you want to enable WiFi HotSpot for sharing?" CHOICE
    [[ $CHOICE == "k" ]] && echo "OK, bye." && kill `pgrep setbright` && sleep 5 && killall dwm-mod && exit;
    echo "Enabling WiFi HotSpot Module!"
    echo "Reconfiguring dnsmasq..."
    sudo cp /etc/dnsmasq-wifi.conf /etc/dnsmasq.conf
    sudo systemctl stop wicd
    sudo systemctl restart dnsmasq

    echo "Pulling up hostapd."
    (sudo hostapd /etc/hostapd/hostapd.conf &)
    sleep 2s
    sudo ifconfig wlan0 192.168.1.1 netmask 255.255.255.0

    while true; do
    CHOICE=""
    read -n1 -sp "Press B to exit" CHOICE
    while [[ $CHOICE == "k" ]]; do
    
        sudo cp /etc/dnsmasq-wire.conf /etc/dnsmasq.conf
        sudo systemctl start wicd
        sudo systemctl restart dnsmasq
        sudo killall hostapd
        sudo systemctl restart networking
        sleep 5
        kill `pgrep setbright`
        killall dwm-mod
        exit;

    done
    done

}
main(){

#    loop
#    [ $? -eq 0 ] && init
    #Set Brightness to always on
    [ -f setbright.sh ] && (./setbright.sh &) || echo "Bright Control script is missing."
    init

}
main

Make sure you have amend all path mentioned and all folder are created.

Brightness control script

cpi@DEOT:~/imgbackup$ cat setbright.sh 
#!/bin/bash

while true
do

#pgrep rsync >/dev/null
#
#    while [ $? -eq 0 ]
#    do
        sudo echo 8 > /proc/driver/backlight
#    done

sleep 10s

done

Quite simple, but not easy to find out the control interface.

Apache, PHP Installation

As same as all Debian-like distribution, no details here.

make sure your website root path is /var/www/html/

Owncloud Installation

During the first time you visit your owncloud you will be asked to set the backup directory, point it to /backup/, and make sure appropriate permission is given.

Add www-data to bakgrp group, make sure folder /backup/ is with permission 2770 and group is set to bakgrp for common owning.

Enable Hostapd Support

Be advised the wicd will conflict will hostapd hence please shutdown wicd before continue by systemctl stop wicd, but once it’s shutdown, the launcher will stop responding. (Only for troubleshooting, in script it will be handled automatically)

You can restart the launcher by killall dwm-mod

After hostapd is installed, put a config here:

root@DEOT:/etc/hostapd# cat /etc/hostapd/hostapd.conf 
interface=wlan0
hw_mode=g
channel=7
wmm_enabled=0
macaddr_acl=0
auth_algs=1
ignore_broadcast_ssid=0
wpa=2
wpa_key_mgmt=WPA-PSK
wpa_pairwise=TKIP
rsn_pairwise=CCMP
ssid=DEOT
wpa_passphrase=YourWiFiPassword

Once done, you should start hostapd with hostapd /etc/hostapd/hostapd.conf

Enable Dnsmasq Support

As CPI comes with dhcpd to support DHCP over USB, we should disable dhcpd here but I didn’t found it as a service. I handle it by rename the binary file directly.

which dhcpd

and mv it directly.

For dnsmasq we have two config setup here:

root@DEOT:~# ll /etc/dnsmasq*
-rw-r--r-- 1 root root 27455 Dec 10 19:04 /etc/dnsmasq.conf
-rw-r--r-- 1 root root 27527 Dec 10 13:47 /etc/dnsmasq-wifi.conf
-rw-r--r-- 1 root root 27455 Dec 10 15:38 /etc/dnsmasq-wire.conf

/etc/dnsmasq.d:
total 20
drwxr-xr-x   2 root root  4096 Dec  9 15:26 .
drwxr-xr-x 117 root root 12288 Dec 10 21:15 ..
-rw-r--r--   1 root root   211 Sep 18  2018 README
root@DEOT:~# 

root@DEOT:~# tail /etc/dnsmasq-*.conf
==> /etc/dnsmasq-wifi.conf <==

# If a DHCP client claims that its name is "wpad", ignore that.
# This fixes a security hole. see CERT Vulnerability VU#598349
#dhcp-name-match=set:wpad-ignore,wpad
#dhcp-ignore-names=tag:wpad-ignore

interface=wlan0
dhcp-range=192.168.1.190,192.168.1.199,255.255.255.0,1h
interface=usb0
dhcp-range=192.168.10.190,192.168.10.199,255.255.255.0,1h

==> /etc/dnsmasq-wire.conf <==
# Include all files in a directory which end in .conf
#conf-dir=/etc/dnsmasq.d/,*.conf

# If a DHCP client claims that its name is "wpad", ignore that.
# This fixes a security hole. see CERT Vulnerability VU#598349
#dhcp-name-match=set:wpad-ignore,wpad
#dhcp-ignore-names=tag:wpad-ignore

interface=usb0
dhcp-range=192.168.10.190,192.168.10.199,255.255.255.0,1h
root@DEOT:~# 

Make sure a currently using config exist on /etc/dnsmasq.conf

Make sure dnsmasq is auto-bootup: systemctl enable dnsmasq

0x05 Troubleshooting

Google


Update on 9th Mar

Thank you all for keep waiting, I am still working on this topic - build an All-In-One image to help most of you engage more easier. Due to the Coronavirus it somehow get postponed but I am still WORKING HARD on this!

8 Likes

When OTG Devices detected

Confirmed to proceed copy

2

With WiFi HotSpot Enabled

Visit Owncloud for retrieving files

4 Likes

This project is very interesting, do you mind to share a .img of your work? since its not a step-by-step guideline and it requests a lot of work, anyway, congratulations, such a nice idea!

On the note of doing it yourself, the Kernel may possibly need go be updated, if you want to use some of the more recent additions.
@0x0c Which version of the kernel does this use? And dare I ask, but do you have a run down of how you compiled it, so we can compile our own kernels the same way? I’m almost thinking, this should be standardised. :wink:

I could try to build a latest img from mainline in early Feb. as I am in trip for now.
I bring CPI on my trip and act as a backup & sharing server and it works as expected.
Will keep you posted once the img is done.

1 Like

please enable items below during you compile the kernel :

  1. Device Drivers->SCSI device support->SCSI disk support
  2. Device Drivers->USB support->Support for Host-side USB
  3. Device Drivers->USB support->USB Mass Storage support
  4. Enable filesystem support as per your own requirements.

it was based on my memory and I don’t have laptop by me hence it might be slightly different.

it doesn’t matter what kernel are you using because USB support is integrated in all under maintained versions. For me I build from the version found on CPI github page.

For sure it should be standardized as well as the I2C support!

1 Like

> “I bring CPI on my trip and act as a backup & sharing server and it works as expected.”

Wow! will be wonderful to have it, especially to carry it on trips with limited baggage. All photos will be saved, the good thing is you already test it! :smile:

I will be waiting for it! Thanks for the reply!

AIO image is fully completed, you can get your cable ready and testing soon!

1 Like

AIO Image is now released! Thanks to have a test and let me know if any issue.

1 Like

That’s amazing, thank you so much @0x0c
I’ll test it when I have the opportunity, and sure let you know!