There are 3rd party utilities that can be used in a script to change the icon of folders and files, but they don't, to my knowledge work on the Macintosh HD icon on the Desktop. The hard drive is not your typical file or folder, so it doesn't always play by the same rules as those other containers.
One thing I do know is that when you paste a custom icon onto a volume, like Macintosh HD, it creates an invisible ".VolumeIcon.icns" file at the root of the drive. If the icon gets removed or has never been applied, that .VolumeIcon.icns file isn't present. You can see this by doing ls -al /
before and after applying your custom icon. I suspect your pkg may not be properly applying the .VolumeIcon.icns file to the HD when the pkg is run, but if one is already present, it's able to change it something else.
In short, I don't know of a good way to change the hard drive icon.
But this all leads me to ask why this is really necessary? Is it that important to change the Macintosh HD icon? Just wondering how much the effort is worth it in this case.
The package I created does install a .VolumeIcon.icns at the root level. Something gets switched on or off that allows this change to happen and I'm hoping someone has the answer.

Why does Apple change the icons and why do they give us a method to do it? I've been making an original OS hard drive icon which ties into our legal login screen for over two decades for several reasons. 1) My users are graphic artists who look forward to seeing each new OS icon. 2) It makes it much easier identifying the hard drive on a messy desktop 3) For support a user can identify what OS they are running from halfway across the room. 4) I'm trying to duplicate what I use to be able to do easily when making a traditional image. I was told I could and after spending a lot of money and time I'm finding out I can't... another topic.
Have you tried this? I just did a quick test and miraculously this works.
sudo cp /path/to/icon.icns /Volumes/Macintosh HD/.VolumeIcon.icns
Basically a simple copy command of the custom icon in .icns form (likely must be an icns to work) to the root volume as .VolumeIcon.icns
This actually worked on my 10.12.6 machine. The Finder immediately recognized the change and the HD icon was updated!
I haven't tested it on HS, but it might still work there too.
If that works, you can probably create a pkg that drops your custom icon into /tmp/ and then runs a postinstall script with something like the above without the sudo since it shouldn't be needed.
I'm using 10.13.6 and it didn't work. I can see that the command did replace the .VolumeIcon.icns files with my new icon image but the hard drive image icon didn't change. It also didn't change after I rebooted or running my installer made from Composer after I ran your script. Thanks for trying and have a great weekend!