Can I change the dalvik cache location e.g. with a symbolic link? I currently use Link2SD since my phone's onboard storage is insufficient for more than a few essential apps. It is very effective but limited to when the phone is on.

While changing ROMs, the dalvik cache is wiped and when the phone restarts you see a progress bar saying Android is optimising applications. So given a reasonable number of applications, the dalvik cache fills up the storage long before link2sd can do anything about it.

So is there a way that mounts a sd card folder as the appropriate cache location before the system gets on with booting and doing its business? I know it might involve creating a symbolic link but I just don't know when things are mounted and in what order.
Of course you can. Although it seems like there's no problem to migrate dalvik-cache to a normal SD card filesystem (the most common, vfat), it still is highly recommended that you format your SD card to ext4 (or the same as your /data) to ensure everything works fine.  

So let's go. A full backup of all your apps is recommended before proceeding though if you feel lucky, it isn't required. Assuming that you have a terminal emulator to do the necessary jobs.

First, create a folder that you want to migrate your dalvik-cache to. Then chmod and chown the new folder to match /data/dalvik-cache. If you're running a SELinux-enabled Android OS, don't forget to change security contexts correspondingly. Run

ls -dlZ /data/dalvik-cache
ls -lZ /data/dalvik-cache


to see the original permissions, owner/groups and security contexts of dalvik-cache folder.

Next, copy all files from /data/dalvik-cache to your new location, for example /storage/sdcard1/dalvik-cache. You can use cp -a command to preserve file attributes, which would make things simpler.

Ensure no new apps start during the process of the following step.

Then, rm -r /data/dalvik-cache and create a symbolic link to the new location using

ln -s /storage/sdcard1/dalvik-cache /data/dalvik-cache


That's all. We're done. Reboot your phone (not required but recommended).  

Some notices about this process:


The busybox may not always work well as Android is based on a highly modified Linux kernel. For example almost all SELinux-related functions do not work properly. You can test it out as shown below.  

toolbox ls -Z /data
busybox ls -Z /data
toolbox getenforce
busybox getenforce


However these may work properly, depending on Android version:

busybox chcon &lt;context&gt; &lt;files...&gt;
busybox runcon &lt;context &lt;program&gt; [args...]

Never remove your SD card. If you re-format your SD card, make sure it's still ext4 AND create the dalvik-cache folder back before booting your phone for the first time.