5

I have inherited a Ubuntu 14.04 production server which needs to be upgraded to 20.04, and I would like a sandboxed version to experiment with first, hence I want to dump and restore the filesystems over the network from either a MacOS or another 14.04 virtualbox instance. An earlier version of this question is at https://askubuntu.com/q/1314747/963.

The server cannot "see" my machines so I cannot easily run dump and push the result remotely to my machine, but need to invoke ssh from my machine to run dump.

ssh -t me@there "echo MYPASSWORD | sudo -S dump -y -f - /boot 2>/dev/null " > boot.dump 

Problem is that I've found that running this command inserts a lot of \r characters in front of \n characters which ruins the dump file so restore cannot use it. I understand that this is probably due to a driver translating linefeeds to the characters needed for printing, but I do not see where this is triggered.

How should I do this to get the correct binary dump file?

2 Answers2

12

It's the ONLCR .c_oflag termios setting which is causing the newline (\n) to be turned into carriage-return/newline (\r\n) by the pseudo-terminal allocated by ssh on the remote machine (because of ssh's -t option).

Turn it off with stty -onlcr:

ssh -t me@there 'stty -onlcr; ...' > output
2

The official ASCII line ending is CR LF (i.e., return to line start and go to next line, \r\n in C-ish). To shave off a byte for each line (very important when yor memory is measured in KiB and disks are a few hundred MiB), Unix just uses \n to mark line end. Some systems (notably Microsoft) did go with the standard, so when moving text files among systems you have sometimes a translation task at hand.

AdminBee
  • 22,803
vonbrand
  • 18,253
  • Thank you. I understand the mechanism and the history behind it (seen it since the CP/M days) but not how it applied in my scenario. – Thorbjørn Ravn Andersen Feb 09 '21 at 16:52
  • 1
    @vonbrand ASCII defines the control characters Carriage Return and Line Feed. Unix does use these when talking to a device that requires them. However, internally Unix uses a newline character a a separator between lines in files and in memory. The Line Feed character was chosen for this, because one is enough. Internally, Unix handles line breaks at a higher level of abstraction than the characters controlling the movement of a mechanical print head. – Johan Myréen Feb 09 '21 at 19:20
  • 3
    Not the "ASCII" line ending, the "Internet" one. – Paŭlo Ebermann Feb 10 '21 at 01:40
  • 3
    The official ASCII line ending is CR LF But this is just false. LF is also known as "newline", and its use as a single-character line ending is well established. See https://en.wikipedia.org/wiki/Newline, in particular the History section. – Steve Summit Feb 10 '21 at 13:23