# Copy /boot to hard disk?



## fredvs (Aug 11, 2015)

Hello.

After have done with lot of success a ISO bootable CD-ROM with mfsBSD,
I want to copy this distro to a hard disk.

First I did use `dd if=/myimage.img of=/dev/da0`

It works, the image is transferred, but with same partition size of the CD-ROM.
This is not what I want, I want to use the entire hard disk.

So I googled to find a other solution.

I find this =>

```
...
While you can use dd to copy a disk like that, doing so has a number of drawbacks:

  The destination must be exactly the same size or larger than the source.
  After copying, you will need to resize the partitions to use any additional space.
  You will waste time copying free space.
  Any fragmentation present in the old disk is preserved.

You can also simply format the new disk, mount it, and copy all of the files over
with cp -ax ( as root ), and then reinstall the boot loader on the new drive.
This method does not suffer from any of the above drawbacks.
```
It seems to be perfectly what I want.

So, for formatting the hard disk I use that script (and it works) =>

```
#!/bin/sh
echo "This will format and add FreeBSD system to /dev/da0 (main disk)"
echo "..."
echo "Blank out the first chunk of the disk to destroy any MBR partition tables that might exist."
umount /media/disk
dd if=/dev/zero of=/dev/da0 bs=1m count=128
echo "Done ..."
echo "Creating a single (bootable/active) partition spanning the entire main disk."
fdisk -B /dev/da0
echo "Done ..."
echo "Writing a standard (bootable) freebsd disk label to the 1st partition."
echo "The standard label has the entire space usable as 'a'"
bsdlabel -w /dev/da0s1
echo "Done ..."
echo "Formating the disk for FreeBSD."
newfs -O2 -U /dev/da0s1a
echo "Done ..."
echo "Mounting the disk."
mount /dev/da0s1a /media/disk
echo "All done ..."
echo "You may access the disk in /media/disk."
echo "..."
echo "Press [Enter] to quit."
read something
```
OK, I have a bootable hard-disk.

But for next step, how to re-install the boot loader?

Thanks.

Fre;D


----------



## wblock@ (Aug 11, 2015)

This is not about /boot, which is just a directory.

Please do not use that script, it uses obsolete programs and makes assumptions that might not be correct.

See Disk Setup on FreeBSD to see how to do that with modern tools that are less dangerous.


----------



## fredvs (Aug 12, 2015)

Hello.

Some news from the front.

Before all, I want to thank Warren Block (wblock@) for his perfect Disk Setup on FreeBSD.

After a big and hard fight, I get it.

1) Preparing the hard-disk (format and create partition).

```
#!/bin/sh
clear
echo "Create a new GPT partition."
echo "..."

gpart create -s gpt da0

echo "Done."
echo "Create a new boot partition."
echo "..."

gpart add -t freebsd-boot -l gpboot -b 40 -s 512K da0

echo "Done."
echo "Install the GPT bootcode."
echo "..."

gpart bootcode -b /boot/pmbr -p /boot/gptboot -i 1 da0

echo "Done."
echo "Create a partition for /."
echo "..."

gpart add -t freebsd-ufs -l gprootfs -b 1M -s 2G da0

echo "Done"
echo "Create a partition for swap, /var, /tmp, /usr."
echo "..."

gpart add -t freebsd-swap -l gpswap -s 512M da0
gpart add -t freebsd-ufs  -l gpvarfs -s 1G da0
gpart add -t freebsd-ufs  -l gptmpfs -s 256M da0
gpart add -t freebsd-ufs  -l gpusrfs -a 1M da0

echo "Done."
echo "Show partition."
echo "..."

gpart show -l da0

echo "Done."
echo "Format the new filesystems."
echo "..."

newfs -U /dev/gpt/gprootfs
newfs -U /dev/gpt/gpvarfs
newfs -U /dev/gpt/gptmpfs
newfs -U /dev/gpt/gpusrfs

echo "All format done."
echo "..."
```

2) Mount the new formatted disk.

```
#!/bin/sh
MKDIR=/bin/mkdir
echo "Mount disk."
echo "..."
${MKDIR} /media/disk/
mount /dev/gpt/gprootfs /media/disk
${MKDIR} /media/disk/var
mount /dev/gpt/gpvarfs /media/disk/var
${MKDIR} /media/disk/tmp
mount /dev/gpt/gptmpfs /media/disk/tmp
${MKDIR} /media/disk/usr
mount /dev/gpt/gpusrfs /media/disk/usr
echo "..."
```

3) Copy files from working system to new disk.

```
#!/bin/sh
echo "Create directories of system ."
echo "..."
mkdir  -p /media/disk/mnt
mkdir -p /media/disk/dev

echo "Copy files to disk."
echo "..."

echo "Copy /usr/ "
echo "..."

cp -afv  /usr/  /media/disk/usr/


echo "Copy /bin/ "
echo "..."
mkdir /media/disk/bin
cp -afv  /bin/  /media/disk/bin/

echo "Copy /boot/ "
echo "..."
mkdir /media/disk/boot
cp -afv  /boot/  /media/disk/boot/

echo "Copy /compat/ "
echo "..."
mkdir /media/disk/compat
cp -afv  /compat/  /media/disk/compat/
st
echo "Copy /dist/ "
echo "..."
mkdir /media/disk/dist
cp -afv  /dist/  /media/disk/dist/

echo "Copy /etc/ "
echo "..."
mkdir /media/disk/etc
cp -afv  /etc/  /media/disk/etc/

echo "Copy /lib/ "
echo "..."
mkdir /media/disk/lib
cp -afv  /lib/  /media/disk/lib/

echo "Copy /libexec/ "
echo "..."
mkdir /media/disk/libexec
cp -afv  /libexec/  /media/disk/libexec/

echo "Copy /proc/ "
echo "..."
mkdir /media/disk/proc
cp -afv  /proc/  /media/disk/proc/

echo "Copy /root/ "
echo "..."
mkdir /media/disk/root
cp -afv  /root/  /media/disk/root/

echo "Copy /sbin/ "
echo "..."
mkdir /media/disk/sbin
cp -afv  /sbin/  /media/disk/sbin/

echo "Copy /stand/ "
echo "..."
mkdir /media/disk/stand
cp -afv  /stand/  /media/disk/stand/

echo "Copy /boot/kernel/ "
echo "..."
mount -t cd9660 /dev/cd0 /media/cdrom

mkdir /media/disk/boot/kernel
cp -afv /media/cdrom/boot/kernel/  /media/disk/boot/kernel/

cp -afv /dist/boot/loader.conf  /media/disk/boot/loader.conf
cp -afv /dist/etc/fstab  /media/disk/etc/fstab

cp -afv /dist/root/jwmrc  /media/disk/root/.jwmrc
cp -afv /dist/root/xinitrc  /media/disk/root/.xinitrc

echo "All copy done."
echo "..."

echo "System transfered to disk."
echo "You may reboot to feel it.  ;-)"

echo "Press [Enter] to quit."

read something
```

4) Changing /boot/loader.conf
Add this =>

```
vfs.root.mountfrom="ufs:/dev/gpt/gprootfs"
vfs.usr.mountfrom="ufs:/dev/gpt/gpusrfs"
vfs.var.mountfrom="ufs:/dev/gpt/gpvarfs"
vfs.tmp.mountfrom="ufs:/dev/gpt/gptmpfs"
```

5) Changing /etc/fstab

```
# Device  Mountpoint  FStype  Options  Dump  Pass#
/dev/gpt/gpswap  none  swap  sw  0  0
/dev/gpt/gprootfs  /  ufs  rw  1  1
/dev/gpt/gptmpfs  /tmp  ufs  rw  2  2
/dev/gpt/gpusrfs  /usr  ufs  rw  2  2
/dev/gpt/gpvarfs  /var  ufs  rw  2  2
```

Yep, all done. ;-)

After rebooting, I get a hyper-fast booting system, working perfectly.

Many thanks.

Fre;D

PS: Of course, remarks are very welcome (for example, is `cp -afv` the right option ?)


----------



## wblock@ (Aug 12, 2015)

Compare sizes occupied by the original / versus the copy.  The copy is probably larger, because hard links were copied multiple times instead of once.

For example:

```
% du -hd0 /rescue
7.9M   /rescue
% mkdir /tmp/rescue
% cp -afv /rescue/ /tmp/rescue/
% du -hd0 /tmp/rescue
1.1G   /tmp/rescue
```

Oops, went from 8M to 1.1G, because /rescue is just one hardlinked copy of a single file.

As far as I know, there is no way to prevent that with cp(1).  net/rsync can do it, but a better option for copying filesystems is dump(8) and restore(8).  See Copying Filesystems in Backup Options for FreeBSD.


----------



## wblock@ (Aug 12, 2015)

Oh, and step 4 should not be needed at all.


----------



## alphaniner (Aug 12, 2015)

wblock@ said:


> As far as I know, there is no way to prevent that with cp(1).  net/rsync can do it, but a better option for copying filesystems is dump(8) and restore(8).  See Copying Filesystems in Backup Options for FreeBSD.



I'm new to FreeBSD, but I got the impression bsdtar was recommended for this kind of situation: `bsdtar -cf - -C / rescue | bsdtar -xf - -C /tmp`


----------



## wblock@ (Aug 12, 2015)

Sometimes recommended, but not usually by me (although that example does work correctly).  If I need file-by-file copy, I prefer rsync with the right options.  Complicated ones, usually.  For copying UFS filesystems, dump(8) is the winner.  It knows and understands everything UFS supports, so the result matches the original.


----------



## fredvs (Aug 12, 2015)

wblock@ said:


> Oh, and step 4 should not be needed at all.


Hum, ok, but without step 4, at boot, system ask to manually write where is the root partition...


----------



## fredvs (Aug 12, 2015)

wblock@ said:


> As far as I know, there is no way to prevent that with cp(1). net/rsync can do it, but a better option for copying filesystems is dump(8) and restore(8). See Copying Filesystems in Backup Options for FreeBSD.


OK, thanks, I will study it.


----------



## fredvs (Aug 12, 2015)

alphaniner said:


> 'm new to FreeBSD, but I got the impression bsdtar was recommended for this kind of situation:  bsdtar -cf - -C / rescue | bsdtar -xf - -C /tmp



Hum, thinking about it, maybe a other solution...

Here re-explain of the thing:

There is a bootable iso and I want to transfer the system of the iso to a new disk.

In the bootable iso, there are:

/[Boot] (directory)
/boot  (directory)
/baseroot.gz (file)

This first idea (that works) was to first boot from the iso and then copy (or `dump`) the iso-installed system to the new bootable formatted disk.

But what about directly decompress /baseroot.gz into the new bootable hard disk and then apply modifications ?
Instead of copy (or `dump`) the already temporally installed system (who is, in fact, baseroot.gz decompressed)?

Thanks.


----------



## fredvs (Aug 14, 2015)

Hello.

I have used `dump` with lot of success for / and /var.

Sadly, I was not able to do the same for /usr => error message ;-(


```
mount /dev/gpt/gprootfs /media/disk/disk0
dump -C16 -b64 -0uanL -h0 -f - /  | (cd /media/disk/disk0 && restore -ruf -)
```
 => OK


```
mount /dev/gpt/gpvarfs /media/disk/disk0/var
dump -C16 -b64 -0uanL -h0 -f - /var | (cd /media/disk/disk0/var && restore -ruf -)
```
 => OK


```
mount /dev/gpt/gpusrfs /media/disk/disk0/usr
dump -C16 -b64 -0uanL -h0 -f - /usr | (cd /media/disk/disk0/usr && restore -ruf -)
```
 => NOT OK => Error => Could not do on unmounted....bla,... bla,...
But both /dev/gpt/gpusrfs and /media/disk/disk0/usr exist....

Could it be that it is not possible to dump /usr if /usr was compressed?
Or must I use other parameter for `dump /usr`?
Because the system I want to dump comes from a bootable iso and /usr was compressed on ISO and decompressed at boot.

PS: I do then, for backup /usr => `cp /usr/ /media/disk/disk0/usr/`

(But I would prefer to dump /usr too.)

Thanks.

Fred


----------



## wblock@ (Aug 14, 2015)

fredvs said:


> => NOT OK => Error => Could not do on unmounted....bla,... bla,...


It would be useful to see the exact error.


----------



## fredvs (Aug 14, 2015)

wblock@ said:


> It would be useful to see the exact error.













Thanks.

Fre;D


----------



## wblock@ (Aug 15, 2015)

There is some confusion over whether you are trying to dump the current running system or copy filesystems that are not mounted.  Which is it?

dump(8) only works on filesystems.  The stock FreeBSD install does not make separate filesystems for /var or /usr any more, they are just subdirectories of /.  So that could be a problem here also.

It is not necessary to mount a filesystem to copy it with dump(8).  Just give it the path: `dump -0an -f - /dev/gpt/grootfs | (cd /media/disk/disk0 && restore -rf -)`
There is also no reason to snapshot an unmounted filesystem.  Leaving -u out of the restore(8) options can speed up a restore, and it is not needed when restoring to an empty filesystem.


----------



## fredvs (Aug 15, 2015)

wblock@ said:


> here is some confusion over whether you are trying to dump the current running system or copy filesystems that are not mounted. Which is it?


Hello and many thank to help.

About confusion...

What I want to do is transfer a running system (a bootable cdrom) into a other device (hard disk or usb stick).

What I do not understand is why `dump /`  (grootfs) did transfer all the system (`/var` included) but let /usr empty.
It is why i do a `cp` for /usr.

Thanks.


----------



## wblock@ (Aug 15, 2015)

It depends on how the disk is set up.  /usr could be a separate filesystem.  It might just be a mdmfs(8) image file instead of a filesystem.


----------



## fredvs (Aug 15, 2015)

wblock@ said:


> It depends on how the disk is set up. /usr could be a separate filesystem. It might just be a mdmfs(8) image file instead of a filesystem.



Huh, here other confusion, are you talking of /usr of the running cdrom or /usr of the new formatted disk ?
In case you are talking about of /usr of the running cdrom, how can I know if /usr is mdmfs(8) image file instead of a filesystem ?

And if it is  a mdmfs(8) image, what is the solution, using `cp` for /usr like I did (and that works) ?

Thanks.


----------



## wblock@ (Aug 15, 2015)

I can't see it from here, you're going to have to hold it higher. 

How was this "bootable CD-ROM" created?


----------



## fredvs (Aug 15, 2015)

wblock@ said:


> How was this "bootable CD-ROM" created?



Following your perfect advices, it was created with msfBSD (some things where changed in Makefile but the idea is the same).

PS: I am nearly ready to publish the alpha release, but do not talk about it, it is still a secret.
There is a problem with fpc compiler and output messages, all the rest is working perfectly (net, sound, xorg, automount,...)
Even for transferring the system to disk, works like charm and fast.

Missing configuration of locale, I did not find doc to help how to configure locale with FreeBSD...

Fre;D


----------

