Humm, very charged question :-). Well, to answer directly to your question - No, we do not know how viruses were formed. We have only hypotheses. Obviously, all are essentially speculations. What we know are the structures and function of lot many viruses. We know that present viruses cannot reproduce independently of some sort of cells. The hypotheses on their origin are:
Degeneracy hypothesis: Viruses were originally independent cellular forms, but later on stick to some hosts and gradually lost many of their own structures and functions that were available from the hosts. The model is easily understandable in the light of multi-cellular parasites, who also lost features of independent living. Thus viruses are just degenrates.
Cellular origin hypothesis or escape hypothesis: We know cells have many "mobile DNA" or "Jumping genes" that can jump out from a part of DNA molecule and insert in another part. Since they have some free existence and mobility within the cell, it might be speculated that could escape out of the cell and come in again with help of some proteins (coat proteins). - Can we call it wild speculation? May be. I would not bet.
Co-evolution hypothesis: Viruses evolved as simple DNA or RNA molecules (A cell must have started in the same way. The cellular DNA could not have got all the fortunate combinations of enzymes, proteins and membranes in one fine morning. Cellular DNA must have spent millions of years of naked membraneless lives for millions or billions of years. So, why could not the viruses? When some of their fellow DNA and RNA developed an more organized forms they just started piggy-backing. Why not possible?
Given the complexity and marvellous variety of life forms, I believe, may be all are possible. We may find examples from all the pathways. For handy references please see:
- The Origins of Viruses: https://www.nature.com/scitable/topicpage/the-origins-of-viruses-14398218/
- Virus: https://en.wikipedia.org/wiki/Virus#Origins