Solved Installing FreeBSD from mfsBSD

I've put together the following script for installing FreeBSD 13.2-RELEASE from mfsBSD :-

Bash:
#!/bin/sh                                                                                                                                                                     
cat <<EOF > FreeBSD-install.sh                                                                                                                                                 
mount -t ext2fs /dev/ada0p1 /mnt                                                                                                                                               
tar xf /mnt/mfsbsd-se-13.2-RELEASE-amd64.iso -C /media                                                                                                                         
umount /mnt                                                                                                                                                                   
gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0                                                                                                                           
#set -- $(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0)                                                                                                                 
PART='da0s1'                                                                                                                                                                   
echo $PART                                                                                                                                                                     
newfs /dev/$PART                                                                                                                                                               
mount /dev/$PART /mnt                                                                                                                                                         
tar zxf /media/13.2-RELEASE-amd64/base.txz -C /mnt                                                                                                                             
tar zxf /media/13.2-RELEASE-amd64/kernel.txz -C /mnt                                                                                                                           
EOF                                                                                                                                                                           
chmod +x FreeBSD-install.sh                                                                                                                                                   
scp -P 22 FreeBSD-install.sh root@192.168.1.4:/root/FreeBSD-install.sh                                                                                                         
ssh 192.168.1.4 'sh -x ./FreeBSD-install.sh'

Unfortunately, I can't get $PART to resolve... maybe a simple oversight on my part. Can any see my error?
The above is hard coded for da0. I wanted to get the 'set --' function to work but it wouldn't.

The code above is designed to work with the mfsBSD.iso residing on ada0p1 as an ext4 partition of a Ventoy installation.

I hope someone can figure out what is supposed to happen here...
 
Don't you need to escape your "$"?
It should be echo \$PART for example.
EDIT: unless you want to resolve $PART during your cat - but then your PART= needs to go before the cat command. You're writing a file, so those commands are not interpreted while they are being written.
 
Don't you need to escape your "$"?
It should be echo \$PART for example.
EDIT: unless you want to resolve $PART during your cat - but then your PART= needs to go before the cat command. You're writing a file, so those commands are not interpreted while they are being written.
Yes, I did overlook escaping $. Thanks for pointing that out.

In a previous Thread 90552 I learnt that I could find which partition has just been added using this:

Code:
set -- $(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0)
PART=$1

But can't get this to work now. Is this technique reliable?
 
why don't you do this instead?
Code:
PART=$(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0|awk '{print $1}')
I recommend checking $? for an error code that was returned before proceeding, however.
Remember to escape those "$" in your script. Inside the EOF-block it should probably read
Code:
PART=\$(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0|awk '{print \$1}')
 
As I understand from your first message you are using the here document construct inside a sh(1) script to generate another shell script. That means you are writing code generating code; that comes with difficulties of its own (I don't know if that is the easiest way to go about it).

In your script using here-document you are confronted constantly with the fact that the text stuff between your delimiters EOF as in:
sh:
cat <<EOF > FreeBSD-install.sh
  <text stuff>
EOF
is being parsed by the shell for special characters before you are writing the generated script code out to your file FreeBSD-install.sh. To prevent that you have to be on your guard when a special character appears in the <text stuff>. You have to take measures to defeat that special meaning, for example, use: echo \$PART (not echo $PART). That holds also for:
  • PART=\$(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0 | awk '{print \$1}')
    (the suggestion from cmoerz), or
  • PART=\$(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0 | sed 's/\(.*\) /\1/' )
(or for the string set -- $(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test da0) )
Compare the resulting text in your output file FreeBSD-install.sh against your input text in the here-document part of the code in message #1.

To go about it in a slightly different way, I recommend having a look at Here document - Unix_shells or Here Document. From the last reference (it is not relevant what you use as delimiter, in this example WORD, or EOF in your example as long as they match, and the ending delimiter first occurrence appears on a seperate line with a return immediately after it) :
If we want to avoid shell substitutions, we can quote the delimiter word:
Code:
somecommand <<'WORD'
your data
$go
`here`
WORD
Or as sh(1) states:
Redirections
[...]
The following redirection is often called a "here-document".
[...]
If the delimiter as specified on the initial line is quoted, then the here-doc-text is treated literally, otherwise the text is subjected to parameter expansion, command substitution, and arithmetic expansion (as described in the section on "Word Expansions").


P.S. Please note that, from what you have shown, you have no error checking at all (cmoerz hinted at this too); that can be dangerous, depending on the commands that are to be executed.
 
Erichans, thanks for your comments. I really enjoy the 'challenge' of writing 'heredocs', and it sometimes annoys me in some documentation where it suggests that you need to edit some file and insert some lines, whereas IMV, it would be simpler to include a 'heredoc' as an example in some configuration.

As far as the error above is concerned, I wrote the script originally without escaping the '$'s but then decided not to risk it trying to add partitions to my hard disk which already has 18. It would be very painful, if I managed to destroy that somehow, so I thought I'd better test it on a USB stick, but the 'gpart' command wouldn't work!

Eventually it dawned on me that it was an MBR device, not GPT!

As for error checking, I'll try adding that when I know the process actually works.
 
This works for me now. Many thanks to those that helped.

Bash:
#!/bin/sh                                                                                                                                                                     
cat <<EOF > FreeBSD-install.sh                                                                                                                                                 
mount -t ext2fs /dev/ada0p1 /mnt                                                                                                                                               
tar xf /mnt/mfsbsd-se-13.2-RELEASE-amd64.iso -C /media                                                                                                                         
umount /mnt                                                                                                                                                                   
set -- \$(gpart add -t freebsd-ufs -s 10G -l FreeBSD-test ada0)                                                                                                               
PART=\$1                                                                                                                                                                       
newfs /dev/\$PART                                                                                                                                                             
mount /dev/\$PART /mnt                                                                                                                                                         
tar zxf /media/13.2-RELEASE-amd64/base.txz -C /mnt                                                                                                                             
tar zxf /media/13.2-RELEASE-amd64/kernel.txz -C /mnt                                                                                                                           
EOF                                                                                                                                                                           
chmod +x FreeBSD-install.sh                                                                                                                                                   
scp -P 22 FreeBSD-install.sh root@192.168.1.4:/root/FreeBSD-install.sh                                                                                                         
ssh 192.168.1.4 'sh -x ./FreeBSD-install.sh'
 
Back
Top