I wonder why people feel the need to sleep with a secured companionway hatch. Most of the time, except for cool weather, my hatches and windows are open. I even leave my ladder down in the water in case someone falls off of the boat during the night. I never pay much of a thought to being boarded during the night by people with bad intentions.
As far as having your boat searched, Law Enforcement agencies are more likely to view your boat with the same search and seizure laws as a car. That means that since it can be moved the probably don't need to secure a search warrant to go into it if they have the probable cause to do so. This would be under the Carol Doctrine. The probable cause can be developed by many different factors. Remember that Probable Cause is not the same standard as beyond a reasonable doubt.
One might argue that there is definitely a lesser expectation of privacy in a boat than a house. This is another thing to consider and past rulings have definitely favored this notion. Even if the boat is your primary residence this is not taken into consideration.
None the less, the issue of carrying a firearm aboard a boat is a serious one. Thanks to Federal Law, I can conceal carry anywhere in the U.S. I don't frequently carry aboard and the last time I was boarded I was asked if I was carrying. If I was I would simply declare the firearm and nothing could be done about it (I am a certified law enforcement officer).
If you choose to carry aboard then honesty is the best policy. If you have it, let them know. If they find it mistakenly and they weren't told about it then they won't be too happy about it and they will definitely look at you differently. LEOs would much rather see a weapon in the hands of the right person but adding a weapon to any encounter changes the dynamics of the event.
The idea of carrying a weapon on board for defense is a serious decision that can carry major consequences. You have to determine what the threat is that you feel may happen. If you are just worried about property theft and not personal safety, then having a gun aboard may not be the best bet. If you are worried about personal safety, you have to be able to articulate the need for deadly force before you use it. I teach use of force, firearms, and defensive tactics at my department and at a police academy and there is a ton of materials, discussions, and thoughts in this area. This discussion is a very lengthy one and this may not be the place for it.
If you want to deter someone then have something that makes noise when they get into your boat. Then blast a boat horn constantly in their direction. A loud noise like that when someone is trying to be expected is very jarring. It also gets other people's attention. Furthermore, there are so many items aboard that could be great defensive weapons (rigging knives, tools, and things of that nature).