Category Archives: Mac

Mac tricks – command line

Strange wake up of your Mac
To disable Bonjour (mDNSResponder – NoMulticastAdvertisements ):

sudo defaults write /System/Library/LaunchDaemons/com.apple.mDNSResponder ProgramArguments -array-add "-NoMulticastAdvertisements"

To re-enable Bonjour:

sudo defaults write /System/Library/LaunchDaemons/com.apple.mDNSResponder ProgramArguments -array "/usr/sbin/mDNSResponder" "-launchd"

Disable Creation of Metadata Files on Network Volumes

defaults write com.apple.desktopservices DSDontWriteNetworkStores -bool true

Re-enable:

defaults write com.apple.desktopservices DSDontWriteUSBStores -bool true

Save to Disk by Default and not on Cloud products

defaults write NSGlobalDomain NSDocumentSaveNewDocumentsToCloud -bool false

Power off memory during standy
hibernatemode supports values of 0, 3, or 25. Whether or not a hiberna-
tion image gets written is also dependent on the values of standby and
autopoweroff

For example, on desktops that support standby a hibernation image will be
written after the specified standbydelay time. To disable hibernation
images completely, ensure hibernatemode standby and autopoweroff are all
set to 0.

hibernatemode = 0 by default on desktops. The system will not back memory
up to persistent storage. The system must wake from the contents of mem-
ory; the system will lose context on power loss. This is, historically,
plain old sleep.

hibernatemode = 3 by default on portables. The system will store a copy
of memory to persistent storage (the disk), and will power memory during
sleep. The system will wake from memory, unless a power loss forces it to
restore from hibernate image.

hibernatemode = 25 is only settable via pmset. The system will store a
copy of memory to persistent storage (the disk), and will remove power to
memory. The system will restore from disk image. If you want “hiberna-
tion” – slower sleeps, slower wakes, and better battery life, you should
use this setting.

It is suggested to power off memory at stand-by with the following command:

sudo pmset hibernatemode 25

Source: http://docs.hardentheworld.org/OS/OSX_10.11_El_Capitan/

Regex – Grep reference

 

Regex operator

Meaning

.

Matches any single character.

?

The preceding item is optional and will be matched, at most, once.

*

The preceding item will be matched zero or more times.

+

The preceding item will be matched one or more times.

{N}

The preceding item is matched exactly N times.

{N,}

The preceding item is matched N or more times.

{N,M}

The preceding item is matched at least N times, but not more than M times.

Represents the range if it’s not first or last in a list or the ending point of a range in a list.

^

Matches the empty string at the beginning of a line; also represents the characters not in the range of a list.

$

Matches the empty string at the end of a line.

\b

Matches the empty string at the edge of a word.

\B

Matches the empty string provided it’s not at the edge of a word.

\<

Match the empty string at the beginning of word.

\>

Match the empty string at the end of word.

(a|b)

Match a or b

[abc]

Range (a or b or c)

[^abc]

NOT a or b or c

[a-z]

Any letter (lowercase) from a to z

[A-Z]

Any letter (uppercase) from a to z

[a-zA-Z]

Any letter (upper and lowercase) from a to z

[0-9]

Any digit from 0 to 9

A most exhaustive cheat sheet is available here

Mac – Enable syntax highlight VIM

Instert these lines below the “set backspace=2” in your /usr/share/vim file:

set ai                  " auto indenting
set history=100         " keep 100 lines of history
set ruler               " show the cursor position
syntax on               " syntax highlighting
set hlsearch            " highlight the last searched term
filetype plugin on      " use the file type plugins

" When editing a file, always jump to the last cursor position
autocmd BufReadPost *
\ if ! exists("g:leave_my_cursor_position_alone") |
\ if line("'\"") > 0 && line ("'\"") <= line("$") |
\ exe "normal g'\"" |
\ endif |
\ endif

Source

Backup Raspberry Pi SD on your Mac… and restore.

Plug the SD in your Mac.

In the Terminal, as root, use diskutil to identify your SD.
Generally it’s the last in the list, if you’ve just plugged in.

root:~ # diskutil list

You will see something like this:
diskutil_list_pi_sd

In my case, the SD is /dev/disk4. For this reason, I run the following to unmount the whole disk.

root:~ # diskutil umountDisk /dev/disk4
Unmount of all volumes on disk4 was successful

Once done, you can create the backup using dd utility, but make sure to change the device from /dev/diskX to /dev/rdiskX, adding the “r“.

root:~ # dd if=/dev/<span style="color: #ff0000;">rdisk4</span> of=/path/to/mypibackup.img bs=1m

To restore, of course… invert if (input file) with of (output file)… 🙂

root:~ # dd if=/path/to/mypibackup.img of=/dev/rdisk4 bs=1m

Mac – Time Machine on a Raspberry Pi

Requirements

First of all, you need the following:

  • a raspberry pi installed with 1 USB port available,
  • 1 external USB drive (I’ve used a 3TB external USB drive), pre-formatted under your Mac in HSF+, labelled as TimeMachine
  • internet connection 🙂
  • …and some time to perform all the steps and the initial backup.

Installation of your pi

As root, run the following to upgrade the system and install the required packages:

apt-get update && apt-get upgrade && apt-get -y install hfsplus hfsutils hfsprogs avahi-daemon libavahi-client3 db-util libgcrypt11

Now all the required debian packages are installed. We will install netatalk from the source code, because the debian packages are very old.

Setup the hard drive

Create a mount point as root:

mkdir /TimeMachine

Connect your external USB drive to your pi and run as root the following:

blkid

The output should be something like this:

 # blkid
/dev/mmcblk0p1: SEC_TYPE="msdos" UUID="xxxx-xxxx" TYPE="vfat"
/dev/mmcblk0p2: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" TYPE="ext4"
/dev/sda1: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" TYPE="ext3"
/dev/sdb1: LABEL="EFI" UUID="xxxx-xxxx" TYPE="vfat"
/dev/sdb2: UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" LABEL="TimeMachine" TYPE="hfsplus"

Please take note of the UUID where you can see your hard drive installed and add the following line in your /etc/fstab file:

UUID="xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxx" /TimeMachine hfsplus rw,force,exec,auto,users 0 0

Now your drive will be automatically mounted at each reboot of the machine.

Install and configure Netatalk demon

Here you have 3 options:

  1. You can follow this guide if you want to create a .deb package which is easier manageable, especially on Debian;
  2. (For the lazy ones) download my precompiled deb file of version 3.1.1 from here, or 3.1.7 version from here, unzip and install it using dpkg -i
  3. Otherwise… just follow the next steps 🙂
Compile and install Netatalk

Download the latest stable code from here.
I’ve personally used version 3.0.4 for a while, and now I’ve upgraded to version 3.1.7.

Decompress the file, enter the folder and run the following:

./configure --with-init-style=debian --with-zeroconf

If no errors:

make

As root:

make install
cd /etc/init.d/
update-rc.d netatalk defaults

Configure afp.conf

Based on your version, you could have the configuration files in different locations.
Use the command afpd -v | grep "afp.conf"  to locate your afp.conf file.

Edit afp.conf based on your network configuration:

I’ve setup also an extra bit called [Homes] which shares the user /home directory using the same protocol. Quite handy to move around files 🙂 But this last part is NOT required for the scope of this post.

[Global]
mimic model = TimeCapsule6,106
; Global server settings
; Name of your computer in apple devices network
hostname = <your raspberry pi HOSTNAME>
; IP of your Pi
afp listen = <your raspberry pi IP>
hosts allow = <your network or the Mac's IP, for example 192.168.1.0/24>
; logging config
log file = /var/log/netatalk.log
log level = default:warn

[TimeMachine]
; Our Time Machine volume
path = /TimeMachine
cnid scheme = dbd
file perm = 0660
directory perm = 0770
time machine = yes

[Homes]
basedir regex = /home
cnid scheme = dbd
; Display each user home directory in this format
home name = Home $u

Now we can mount the drive and restart the services:

mount /TimeMachine && /etc/init.d/netatalk restart && /etc/init.d/avahi-daemon restart

Now the disk should be visible from your Mac and Time Machine application 🙂

First Time Machine backup

For the first Time Machine backup, I would really recommend to connect the external drive directly to the USB port of your Mac and follow these steps.

Before disconnect the drive from your pi, make sure to stop the services and unmount properly the disk:

/etc/init.d/avahi-daemon stop && /etc/init.d/netatalk stop && umount /TimeMachine && mount

Make sure the output does NOT show /TimeMachine mounted.

Create a sparsebundle manually

Connect the USB drive to your Mac and from the Terminal do the following:

cd /Volumes/TimeMachine/
hdiutil create -size 1500g -fs HFS+J -volname "TMachine" NAME_XXXXXXXXXXXX.sparsebundle

NAME is the hostname
XXXXXXXXXX is your MAC Address without “:”
200g means 1.5 TB (1500 GB). Please make sure it’s big enough to contain the first backup. After it will expand automatically. But if you set it less than the space required for the first backup, Time Machine might ignore it and create a normal folder structure, typical of USB hard drives.
Best practise is set about 80% of the capacity of your hard drive 😉

Start the first backup

Then, add this disk to Time Machine and let’s the backup start and finish.
For the fist backup, if it’s around 1Tb, be ready to leave the PC on all day.

Make sure that NO directories are created in the USB disk.
Just check the size of the sparsebundle file during the initial backup process. If it increases, it’s working! 🙂

I have to admit that I was doing something else when I’ve launched the backup and I’ve realised (I guess) that the sparsebundle file got renamed by Time Machine in a format like <hostname>.sparsebundle. Not sure why but… well, all worked so… from now on, I will use this new name for that file. :-p

If the backup doesn’t start automatically using the sparsebundle file, you can try to force it following these steps:

1) double click on your file sparsebunle to mount it
2) From Terminal, as superuser, run

tmutil setdestination /Volumes/TMachine/

3) Now force to run a new backup

Once completed, just take notes of the current permission of that file. You can check via terminal running ls -l /Volumes/TimeMachine/.
Now unmount the hard drive and reconnect it to your Raspberry Pi.

Final setup on the Raspberry Pi

Before mounting, do a quick check running the following (/dev/sdb2 is my Time Machine drive – please review the output of blkid if unsure):

fsck.hfsplus -f /dev/sdb2

If all is fine, you can mount the drive:

mount /TimeMachine/

And now, do a little trick to make sure to avoid permissions issues, and allow Time Machine to be able to write into the sparsebundle “file” (you will realise on Linux that it’s in fact a directory tree with files).

We’re going to create the same user and group that we have on the Mac on our Raspberry Pi.
Run a handy ls -l /TimeMachine and take note of the UID and GID.
If the group shows as numeric value, as root, you can run groupadd -g GID MACGROUP .

NOTE:GID is the value that you’ve seen with the previous command and MACGROUP is the name of the group that you saw running ls -l /Volumes/TimeMachine/ on you Mac Terminal.

Now, as root, create a new user that matches with the Mac user.

useradd -u UID -g GID MACUSER -m

NOTE: Same as before, UID is the value that you’ve seen with the previous command and MACUSER is the name of the user that you saw running ls -l /Volumes/TimeMachine/ on you Mac Terminal.

Now check again with ls -l /TimeMachine and you should see something like this: drwxr-xr-x 1 macuser staff 11 Apr 2 10:22 HOSTNAME.sparsebundle

NOTE: The group might be displayed as ‘dialout’ (or something else) instead of ‘staff’ as on your Mac. This is not a problem, as long as the numeric GID matches. If the group still shows as numeric value, it groupadd -g GID ‘staff’

All good? Then let’s restart netatalk:

/etc/init.d/netatalk restart

And now, we’re ready to:

  1. Add the remote disk “TimeMachine” to Time Machine;
  2. Initialise a second backup, just to make sure that all works fine (this will be probably less than 1GB);
  3. Have a drink as reward after all this work 🙂

Sources:
http://garmoncheg.blogspot.com.au/2012/11/time-capsule-for-25.html
http://administeria.com/?s=time+machine
http://buffalo.nas-central.org/wiki/Time_Machine_&_Time_Capsule_support_on_your_LinkStation

Mac – Upgrade rsync to version 3

Quick method:

change /private/etc/paths putting ahead /usr/local/...

<span style="color: #ff0000;">/usr/local/bin</span>
<span style="color: #ff0000;">/usr/local/sbin</span>
/bin
/usr/bin
/usr/sbin
/sbin

Then, unzip this precompiled rsync into /usr/local/bin

Restart the Terminal and run

rsync --version

This should be the output:

rsync version <span style="color: #ff0000;">3.0.9</span> protocol version 30
Copyright (C) 1996-2011 by Andrew Tridgell, Wayne Davison, and others.
Web site: http://rsync.samba.org/
Capabilities:
64-bit files, 64-bit inums, 64-bit timestamps, 64-bit long ints,
socketpairs, hardlinks, symlinks, IPv6, batchfiles, inplace,
append, ACLs, xattrs, iconv, symtimes

Mac/Linux – Terminal customisation .profile/.bashrc

For Mac, you need to create a .profile file in your home directory.

This is what I’ve done on mine…

For normal user:

user_bashrc

export PS1="\[\e[00;32m\]\u\[\e[0m\]\[\e[00;37m\]@\[\e[0m\]\[\e[01;37m\]\h\[\e[0m\]\[\e[00;37m\]:\[\e[0m\]\[\e[00;33m\]\w\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;32m\]\\$\[\e[0m\]\[\e[00;37m\] \[\e[0m\]"
# Customisation of 'ls' command
export CLICOLOR=1
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
alias ls='ls -GF'

 

For root:

root_bashrc

export PS1="\[\e[00;31m\]\u\[\e[0m\]\[\e[00;37m\]:\[\e[0m\]\[\e[00;33m\]\w\[\e[0m\]\[\e[00;37m\] \[\e[0m\]\[\e[00;31m\]\\$\[\e[0m\]\[\e[00;37m\] \[\e[0m\]"
# Customisation of 'ls' command
export CLICOLOR=1
export LSCOLORS="ExGxBxDxCxEgEdxbxgxcxd"
alias ls='ls -GF'

Sources:
http://bashrcgenerator.com/
http://geekology.co.za/article/2009/04/how-to-enable-terminals-directory-and-file-color-highlighting-in-mac-os-x
http://apple.stackexchange.com/questions/33677/how-can-i-configure-mac-terminal-to-have-color-ls-output
http://osxdaily.com/2013/02/05/improve-terminal-appearance-mac-os-x/

Mac /Users partition on RAID1

This is what I’ve created:

  • Fresh new installation of Mac OS X Lion on a new 128SSD hard drive (this should work with Mavericks too);
  • Create a RAID1 with the 2 TB disks, and mounted in /Users;
  • Restored apps/confs/data from Time Machine using Migration Assistant.

Create RAID1

This is quite handy to have the user’s data safe.
I’ve simply created a RAID1 using Disk Utility and set this new device to auto mount in /Users folder 😉
Call the new device “Users“. I will use this name as reference in this guide.
Simply drop the hard drives in the Raid section and select Mirrored Raid Set – Mac OS Extended (journaled)
Once created, CMD+I on the new device and take note of Universal Unique Identifier the famous UUID

It might be interesting to follow this guide to create an encrypted logical volume on the RAID partition to save your data 😉

How to mount this new drive in /Users?

First of all you need to enable “root” user

Very easy… open Terminal, got superuser privilegies (sudo su - command) and once the prompt shows “#“, type passwd command.
Set a new password (it will ask to insert it twice).
Now, go to Users and Group and in Login Options select Show Input menu in login window (this can be rolled back once finished).
Logout (better if you do a full reboot).
Once the OS is up again you will have an “old fashion” login window, where you need to enter username and password.
Fantastic!
Insert root as username and the password is the one you’ve just set via Terminal: you will login as super user.

Second, copy the current data

Now… back to the Terminal!
Open it (and you will be already root user! ), have handy the UUID you’ve saved before and digit the following commands:

cd /
cp -a /Users/* /Volumes/Users/
cp -a /Users /Users.orig
rm -rf /Users/*
vifs

Now you need to press SHIFT+g
Then press o
Then insert the following, of course replacing the XXX with your UUID

UUID=XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX /Users hfs auto

Once finished, to save press these in sequence esc : wq

For more info check any vim editor guide.

What we’ve done is coping the full content of /Users in our new RAID device (called “Users” during the RAID setup).
Created a copy on the local drive as well (just to stay from the safe side 😛 )
Removed all the content of /Users and make it ready to get the new RAID1 device mounted inside (sounds a bit xxx but it’s not 😀 )
Edited /etc/fstab to auto mount the RAID1 device automatically at the boot.

Third… reboot and login

Now it’s all ready for the reboot and login with your normal user!
Once confirmed that all works, you can safely remove Users.orig folder and restore the original login window.

Forth (optional): restore from Time Machine

Now you can finally restore your data manually using Time Machine.
Migration Assistant will probably fail if the amount of data is bigger than the size of your main OS disks. The reason is that It’s not able to understand that there are extra 2TB of space under Users.
What I’ve done is restoring ALL my OS except the users, using Migration Assistant, and then, I’ve used Time machine and copied manually all the folders inside /Users … and all worked! 😉