Monday, January 23, 2012

Yet Another AppleTV Upgrade Strategy

(following in the long tradition of repeating history I am manually moving some of the potentially useful or particularly dear to me blog posts from my server to blogger. This entry was originally posted on May 12th, 2009 and may still be of interest)

I purchased an AppleTV when they first came out as it was perfect for playing all the kids movies which were on DVD's that were becoming increasingly scratched and getting lost. Sadly I discovered that the internal drive was less than adequate to hold them all, but luckily I have a house server running all the time anyway for XTension anyway so setting up streaming was no big deal. The interface for finding the streamed videos is less than perfect and the kids sometimes have trouble finding things but it does work. Lately though I've taken to lugging the AppleTV along with us on vacations and trips to hook to hotel or relatives TV's so it's necessary now to get as much of that stored locally as possible. The disk drive inside is a regular 2.5" IDE (note: NOT SATA) drive and can be fairly easily replaced physically by anybody with a set of torx drivers. Other people online have described it as stuck to the case via a pice of foam tape like stuff that was easy to remove. Mine was NOT easy to remove. After removing the screws it stayed stuck tight so much so that I was worried about damaging the drive to pry it loose. I finally resorted to using a small screwdriver to push through the mounting holes one after another working around and around until it finally came loose. Save that piece of foam as you'll need it when replacing the next drive to keep the metal case from shorting against the bottom of the disk controller board if it should flex at all. Now the problems started. the disk has a very interesting partition setup that cannot be easily re-created or altered with the regular OSX partitioning tools. If you simply image the entire disk via the "dd" command as engadget and others suggest and restore to a larger disk then disk utility will refuse to resize the Media partition as the partition table is too small to allow it. If you use "gpt" from the command line as they suggest it will do it, but it will create a corrupt partition that overlaps other data and the disk will become corrupt and have to be reformatted. What I finally had to do was to image the individual partitions on the disk separately, manually from the command line create the appropriate partitioning on the new disk and then restore the images to it. this is not complicated really, just involves some typing at the command line, but if you do these things to the wrong disk you will totally destroy whatever disk is connected to your system. So be sure to follow those instructions to make sure you're working on the right disk! First thing to do is to connect the original AppleTV disk to your Mac via some kind of IDE to USB or Firewire adaptor. I used an old firewire portable case that used to hold the disk I was replacing it with. When you connect the old drive 2 partitions will mount on the desktop, "OSBoot" and "Media" for this example I'm not going to preserve the "Media" directory, but you can do that if you wish to save whats on it, I decided that would take too long and just resynced over night after I got it working again. In addition to the 2 mounted partition the disk has 2 others that need to be imaged and moved to the new disk. The first partition is called "EFI" and is the firmware or bootloader or other such. The second holds the factory restore information so that you can boot into restore mode and rebuild the OSBoot partition if that ever becomes corrupted. You do not actually HAVE to restore the restore partition, the machine will run without it, but you'll be sunk if you ever need it. The finder will continue to remount the OSBoot and Media partitions as we work so you'll have to eject them after about every step or get errors that the disks are in use.

Step 1: figure out what the disk entry is

If you have only 1 disk on your system, and no images or other things mounted then your boot disk will be called disk0 and the newly mounted apple tv disk will be called disk1. If you have other disks mounted first they will get the lower numbers. You can figure out for sure what the disk number is by running the command diskutil list in the terminal. The output will look something like this:

[pasta:~] james% diskutil list
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *465.8 Gi   disk0
   1:                        EFI                         200.0 Mi   disk0s1
   2:                  Apple_HFS Meteor                  465.4 Gi   disk0s2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *298.1 Gi   disk1
   1:                        EFI                         200.0 Mi   disk1s1
   2:                  Apple_HFS BigMedia                297.8 Gi   disk1s2
   #:                       TYPE NAME                    SIZE       IDENTIFIER
   0:      GUID_partition_scheme                        *37.3 Gi    disk2
   1:                        EFI                         34.0 Mi    disk2s1
   2:             Apple_Recovery                         400.0 Mi   disk2s2
   3:                  Apple_HFS OSBoot                  900.0 Mi   disk2s3
   4:                  Apple_HFS Media                   35.8 Gi    disk2s4

in my case the /dev/disk2 entry is the AppleTV disk, you can see 4 partitions listed, EFI, Apple_Recovery, Apple_HFS OSBoot and Apple_HFS Media. The whole disk is mounted as disk2 in my case, and the other partitions are given names like disk2s1, disk2s2 etc... we're going to make images of the EFI, recovery and OSBoot partitions next given the disk number as 2.
Step 2: Image the necessary partitions

As of this writing all AppleTV's in the wild appear to be the same as far as what the partitions are numbered and sized. This could change however so if your output from the diskutil list command is too different from mine you may have to change the partitions numbers I'm using in order to get things to work. now we're going to image the first 3 partitions or the disk, assuming that the disk2s1, disk2s2 and disk2s3 are the same on my system as yours. They should be as long as the AppleTV versions aren't too different. Some tutorials out there suggest that it is necessary to run all these commands below through "sudo" if you get permission errors when running them that aren't caused by the disks being mounted just add "sudo" before each command and enter your password when it asks. we'll use the command line dd command to do the image saving and restoring. eject the disks by typing:

diskutil eject disk2

image the EFI Partition:
dd if=/dev/disk2s1 of=./efi.dmg bs=1024k

saves the entire partition to a disk image file called efi.dmg in the current working directory, you can change that path if you'd like it somewhere else. 

Image the apple recovery partition:
dd if=/dev/disk2s2 of=./recovery.dmg bs=1024k

image the OSBoot partition:
dd if=/dev/disk2s3 of=./osboot.dmg bs=1024k

Step 3: Partition the new disk

This is where things get tricky and where the other tutorials fell down for me. So we're going to completely destroy the partition on the new disk and create a new one from scratch. Unmount and remove the old AppleTV disk and connect and mount your new disk. It will probably already be formatted and will mount to the desktop, if you get the finder popup asking what you want to do with this unformatted disk just click "ignore" what we're doing here would completely destroy any other disk and data that you might have connected if you operate on the wrong disk number. Again verify that you've found your new disk by using the diskutil listcommand. If you didn't change any other disk mounting between ejecting the AppleTV disk and putting the new one on it will probably be the same. If need be eject the new disk, run the command, then re-connect it and see what changed that will show what the new disk is. If you get it wrong you'll destroy the wrong disk! in the command line you'll have to unmount it if it mounted by entering:
diskutil eject disk2

now we're going to totally wipe out the partition table on this new disk and start from scratch:
gpt destroy /dev/disk2
and create a new partition table:
gpt create /dev/disk2

Once you start creating partitions the finder may try to do something with them so you should run the diskutil eject disk2 between each of the following. This is also very similar to what the tutorials on Awkward TV suggest but very different from the Engadget tutorial. It is a good idea to say again at this point that as of this writing all disks in AppleTV's in the wild are the same and that these sector numbers shoudl work for everything as of this time. Future upgrades to the hardware might change these numbers. 

Create the EFI partition:
gpt add -b 40 -i 1 -s 69632 -t efi /dev/disk2

eject the disk again if necessary Create the recovery partition. This one is odd, I dont know if it's actually necessary to give it the same ID number as in the examples, but doing it this way did work so thats what I'll pass on again:
gpt add -b 69672 -i 2 -s 819200 -t 5265636F-7665-11AA-AA11-00306543ECAC /dev/disk2

eject the disk again Create the OSBoot Partition:
gpt add -b 888872 -i 3 -s 1843200 -t hfs /dev/disk2

eject the disk again create the new Media partition. This is the one that you need to calculate the size of to use the whole new disk. Thats not hard to do though as the size of the empty space can be shown by the gpt command. So at this point show the partitions on the disk:
gpt show /dev/disk2

the output will look something like:
gpt show disk1
      start       size  index  contents
          0          1         PMBR
          1          1         Pri GPT header
          2         32         Pri GPT table
         34          6         
         40      69632      1  GPT part - C12A7328-F81F-11D2-BA4B-00A0C93EC93B
      69672     819152      2  GPT part - 5265636F-7665-11AA-AA11-00306543ECAC
     888824    1843192      3  GPT part - 48465300-0000-11AA-AA11-00306543ECAC
    2732016  309849759         
  312581775         32         Sec GPT table
  312581807          1         Sec GPT header

see the 3 indexed "GPT parts" and following that an empty line before the "sec GPT Table" that is where we'll get our start and size parameters for creating the full sized media partition. So in my case as above the -b (begin) parameter is 2732016 and the -s (size) parameter would be 309849759. So the command would be:
gpt add -b 2732016 -i 4 -s 309849759 -t hfs /dev/disk2

Since we're not restoring the media partition we need to reformat it as a standard HFS+ volume and we can do that here, eject the disks first though "diskutil eject disk2”
newfs_hfs -J -v Media /dev/disk2s4

the -J tells it to use journaling and the -v passes the volume name which needs to be Media. and eject the disks again before continuing.

Step 4: Restore the image files

we need to put back the data from the efi, apple recovery and osboot partitions. We made the partitions above but they are empty so we'll put those image files we made in step 2 back into them. If you used different names for the dmg files we created above or put them in a different place you'll have to use the appropriate names and paths different from the example below. First the efi partition:
dd if=./efi.dmg of=/dev/disk2s1 bs=1024k

eject the disks again "diskutil eject disk2" Then the apple recovery partition:
dd if=./recovery.dmg of=/dev/disk2s2 bs=1024k

eject the disks again "diskutil eject disk2" Finally the OSBoot partition:
dd if=./osboot.dmg of=/dev/disk2s3 bs=1024k

This time you DONT have to unmount the disks as the next, and last, thing we have to do to this is remove the spotlight index files as they can evidently cause problems when placed back into the apple tv.

Step 5: Remove the Spotlight Index Files
Spotlight will try to index these new disks as they are mounted. It will create files at the root level of each one called ".Spotlight-vt100" and you need to delete those. You can do it from the finder by turning on the show invisibles option and just dragging them to the trash (it may ask you for your password) or you can do it from the command line by switching to those directories like cd /Volumes/OSBoot and then doing
rm -R ./Spotlight-vt100

Careful with that -R switch there, if you enter the wrong path it will delete all the way up any folder hierarchy without asking you any questions. So if in doubt use the finder. You may also wish to delete the file and the .fsevents file. That is used by timemachine and the system to write file system events to so that it knows what files changed. But AppleTV doesn't use it so it's a waste of space. Probably have to leave the .Trashes file that is there, that might be important, at least it caused no trouble for my own system. I removed the 3 above only. Now eject the disk, re-insert into the AppleTV remembering to put that piece of green foam tape like material under the new disk to keep the circuit board from shorting against the metal back and close it up. It took a few moments longer for mine to reboot after putting in the new disk, perhaps it knew things had changed and was running fsck or something. But it did come up. I had to re-pair with my iTunes library, but it was back up and running very quickly. Now with 150 gig drive, I could have gone larger but I had this disk here not really being used for anything so this upgrade was free except for the hours it took to sort out the myriad of tutorials out there. The Engadget tutorial that got me started and then failed at the creating of the new media partition is here A really good page bringing together many things that I used above is here

(notes from the future: That AppleTV is still running happily and I plan to keep it for as long as it will keep running. The older AppleTV models have a couple of advantages over the new ones not including the onboard disk drive. You can hack them to watch hulu content and they have component video output instead of HDMI. You can set the output to 480i which is just component standard resolution video. So if you have an older TV that you still have somewhere that has component inputs then you can use this to play movies on it. We have one older TV that is so heavy that I couldn’t carry it out to throw it away if I wanted to, this lets me put off the inevitable a little longer.) 

No comments:

Post a Comment