cotswoldjam icon indicating copy to clipboard operation
cotswoldjam copied to clipboard

Shrinking images on a MacBook

Open jjo93sa opened this issue 7 years ago • 33 comments

Hi,

Thanks so much for these scripts, particularly the raspbian-shrink! I wanted to use this on my MacBook, but of course it isn't directly useable because things like e2fs aren't supported. However, it was possible to get it working in Docker for Mac.

This is how I did it, which I leave here in case it is useful for anyone else:

  1. Clone your repo: mkdir raspbian-shrink && cd !$ && git clone https://github.com/aoakley/cotswoldjam.git

  2. Create a file called "Dockerfile" in directory raspbian-shrink with the following content:

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y install dcfldd

# Copy the raspbian-shrink script file into somewhere on the $PATH
COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

# Change the script permissions
RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ]
CMD ["original.img", "shrunk.img"]
  1. Build the docker image, in the same directory, here represents a repository name and the image will be called raspbian-shrink:

docker build -t <name>/raspbian-shrink .

  1. To run the container and shrink our image (large.img) we need some extra permissions to execute the mounts and we must map the loop0 device (which actually comes from the underlying linux VM running in xhyve that is part of Docker for Mac AFAIK). This command assumes that large.img is in the current working directory, but you can change this behaviour by modifying this command to insert the path to the image instead of $(pwd):

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink <name>/raspbian-shrink large.img shrunk.img

  1. Since this command will be used a lot, I created a function in my .bashrc file like this:
DOCKER_REPO_PREFIX=<name>

shrink()
{
   docker run --rm \
       -it \
       -v `pwd`:/work-dir \
       --device=/dev/loop0:/dev/loop0:rw \
       --cap-add=SYS_ADMIN \
       --name shrink \
       ${DOCKER_REPO_PREFIX}/rapbian-shrink "$@"
}

So, after re-loading .bashrc, I can now shrink an image thus: shrink large.img small.img directly from my terminal in Mac OS X

jjo93sa avatar Jun 02 '17 11:06 jjo93sa

Brilliant, I'll add that to the page. Thanks,

On 2 Jun 2017 12:58, "James Osborne" [email protected] wrote:

Hi,

Thanks so much for these scripts, particularly the raspbian-shrink! I wanted to use this on my MacBook, but of course it isn't directly useable because things like e2fs aren't supported. However, it was possible to get it working in Docker for Mac.

This is how I did it, which I leave here in case it is useful for anyone else:

Clone your repo: mkdir raspbian-shrink && cd !$ && git clone https://github.com/aoakley/ cotswoldjam.git 2.

Create a file called "Dockerfile" in directory raspbian-shrink with the following content:

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y install dcfldd

Copy the raspbian-shrink script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ] CMD ["original.img", "shrunk.img"]

  1. Build the docker image, in the same directory, here represents a repository name and the image will be called raspbian-shrink:

docker build -t /raspbian-shrink .

  1. To run the container and shrink our image (large.img) we need some extra permissions to execute the mounts and we must map the loop0 device (which actually comes from the underlying linux VM running in xhyve that is part of Docker for Mac AFAIK). This command assumes that large.img is in the current working directory, but you can change this behaviour by modifying this command to insert the path to the image instead of $(pwd):

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink /raspbian-shrink large.img shrunk.img

  1. Since this command will be used a lot, I created a function in my .bashrc file like this:

DOCKER_REPO_PREFIX=

shrink() { docker run --rm
-it
-v pwd:/work-dir
--device=/dev/loop0:/dev/loop0:rw
--cap-add=SYS_ADMIN
--name shrink
${DOCKER_REPO_PREFIX}/rapbian-shrink "$@" }

So, after re-loading .bashrc, I can now shrink an image thus: shrink large.img small.img directly from my terminal in Mac OS X

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4, or mute the thread https://github.com/notifications/unsubscribe-auth/ACJqETtdeaG1n6FPZSYKv3CPmwAxjE08ks5r__jxgaJpZM4NuMGE .

aoakley avatar Jun 02 '17 17:06 aoakley

Thanks again - your hints are now online at: https://github.com/aoakley/cotswoldjam/blob/master/raspbian-shrink/raspbian-shrink-mac.txt

Cheers,

-- Andrew Oakley [email protected]

On 2 June 2017 at 18:01, Andrew Oakley [email protected] wrote:

Brilliant, I'll add that to the page. Thanks,

On 2 Jun 2017 12:58, "James Osborne" [email protected] wrote:

Hi,

Thanks so much for these scripts, particularly the raspbian-shrink! I wanted to use this on my MacBook, but of course it isn't directly useable because things like e2fs aren't supported. However, it was possible to get it working in Docker for Mac.

This is how I did it, which I leave here in case it is useful for anyone else:

Clone your repo: mkdir raspbian-shrink && cd !$ && git clone https://github.com/aoakley/cotswoldjam.git https://github.com/aoakley/cotswoldjam.git 2.

Create a file called "Dockerfile" in directory raspbian-shrink with the following content:

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y install dcfldd

Copy the raspbian-shrink script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ] CMD ["original.img", "shrunk.img"]

  1. Build the docker image, in the same directory, here represents a repository name and the image will be called raspbian-shrink:

docker build -t /raspbian-shrink .

  1. To run the container and shrink our image (large.img) we need some extra permissions to execute the mounts and we must map the loop0 device (which actually comes from the underlying linux VM running in xhyve that is part of Docker for Mac AFAIK). This command assumes that large.img is in the current working directory, but you can change this behaviour by modifying this command to insert the path to the image instead of $(pwd):

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink /raspbian-shrink large.img shrunk.img

  1. Since this command will be used a lot, I created a function in my .bashrc file like this:

DOCKER_REPO_PREFIX=

shrink() { docker run --rm
-it
-v pwd:/work-dir
--device=/dev/loop0:/dev/loop0:rw
--cap-add=SYS_ADMIN
--name shrink
${DOCKER_REPO_PREFIX}/rapbian-shrink "$@" }

So, after re-loading .bashrc, I can now shrink an image thus: shrink large.img small.img directly from my terminal in Mac OS X

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4, or mute the thread https://github.com/notifications/unsubscribe-auth/ACJqETtdeaG1n6FPZSYKv3CPmwAxjE08ks5r__jxgaJpZM4NuMGE .

aoakley avatar Jun 03 '17 17:06 aoakley

Excellent - thanks for letting me know.

James

On 3 June 2017 at 18:28, Andrew Oakley [email protected] wrote:

Thanks again - your hints are now online at: https://github.com/aoakley/cotswoldjam/blob/master/ raspbian-shrink/raspbian-shrink-mac.txt

Cheers,

-- Andrew Oakley [email protected]

On 2 June 2017 at 18:01, Andrew Oakley [email protected] wrote:

Brilliant, I'll add that to the page. Thanks,

On 2 Jun 2017 12:58, "James Osborne" [email protected] wrote:

Hi,

Thanks so much for these scripts, particularly the raspbian-shrink! I wanted to use this on my MacBook, but of course it isn't directly useable because things like e2fs aren't supported. However, it was possible to get it working in Docker for Mac.

This is how I did it, which I leave here in case it is useful for anyone else:

Clone your repo: mkdir raspbian-shrink && cd !$ && git clone https://github.com/aoakley/cotswoldjam.git https://github.com/aoakley/cotswoldjam.git

Create a file called "Dockerfile" in directory raspbian-shrink with the following content:

FROM ubuntu:latest

RUN apt-get -y update && apt-get -y install dcfldd

Copy the raspbian-shrink script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ] CMD ["original.img", "shrunk.img"]

  1. Build the docker image, in the same directory, here represents a repository name and the image will be called raspbian-shrink:

docker build -t /raspbian-shrink .

  1. To run the container and shrink our image (large.img) we need some extra permissions to execute the mounts and we must map the loop0 device (which actually comes from the underlying linux VM running in xhyve that is part of Docker for Mac AFAIK). This command assumes that large.img is in the current working directory, but you can change this behaviour by modifying this command to insert the path to the image instead of $(pwd):

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/ loop0:rw --cap-add=SYS_ADMIN --name shrink /raspbian-shrink large.img shrunk.img

  1. Since this command will be used a lot, I created a function in my .bashrc file like this:

DOCKER_REPO_PREFIX=

shrink() { docker run --rm
-it
-v pwd:/work-dir
--device=/dev/loop0:/dev/loop0:rw
--cap-add=SYS_ADMIN
--name shrink
${DOCKER_REPO_PREFIX}/rapbian-shrink "$@" }

So, after re-loading .bashrc, I can now shrink an image thus: shrink large.img small.img directly from my terminal in Mac OS X

— You are receiving this because you are subscribed to this thread. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4, or mute the thread <https://github.com/notifications/unsubscribe-auth/ ACJqETtdeaG1n6FPZSYKv3CPmwAxjE08ks5r__jxgaJpZM4NuMGE> .

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4#issuecomment-305989467, or mute the thread https://github.com/notifications/unsubscribe-auth/APNM07dOO7mXzH8BRXnM2sd-XMmvB9yGks5sAZe8gaJpZM4NuMGE .

jjo93sa avatar Jun 08 '17 13:06 jjo93sa

I've tried to setup the docker file but I run into a problem:

***** Checking image ***** ***** Copying image ***** 965888 blocks (30184Mb) written. 966112+0 records in 966112+0 records out ***** Mounting filesystem ***** ***** Checking filesystem ***** e2fsck 1.42.13 (17-May-2015) /dev/loop0 has unsupported feature(s): metadata_csum e2fsck: Get a newer version of e2fsck! fsck failed. Check if the image is corrupt, e.g. partial download.

svh1985 avatar Jul 23 '17 18:07 svh1985

This is a weird bug which I can't reproduce but you are the second person to report it.

As per the error message, please can you try a newer version of e2fsck .

For example, if you're on Ubuntu 16.04, try e2fsk.static from 16.10: https://packages.ubuntu.com/yakkety/e2fsck-static

This is a separate, static package so after installation you'll need to change my script to specify e2fsck.static rather than e2fsck .

Make sure you've backed up everything important. If you're not sure, don't continue.

aoakley avatar Jul 23 '17 19:07 aoakley

That is odd, I ran my container only today and it worked just fine. I checked the e2fsck version, and it is the same one that @svh1985 is reporting as problematic. It might be worth changing the line in the Dockerfile:

FROM ubuntu:latest to something like: FROM ubuntu:17.10 or FROM ubuntu:14.04

This would give you an image based on the level version of Ubuntu, or on an older version & see what happens.

It might also be worth checking the permissions of the file you are trying to shrink: ls -al name.img

BTW - what system are you running this on?

jjo93sa avatar Jul 23 '17 20:07 jjo93sa

Yeah, I'm also running the same e2fsck version and no problems.

This makes me suspect that there is something unusual about the image file. Not wrong, just unusual.

@svh1985 can you tell me more information about the .img file that you are trying to shrink, please?

  • What OS is on that .img ? If it's Raspbian, which version of Raspbian - when did you download it from raspberrypi.org ?

  • Have you added or changed any partitons on the image?

  • Anything else you can tell us about what's in your .img file please?

Thanks,

aoakley avatar Jul 23 '17 21:07 aoakley

I don't think this error is specific to Docker, as someone else had the exact same problem. They had a physical installation of Ubuntu 16.04 which had been release-upgraded from 14.04 . However they had the exact same version of e2fsck .

Perhaps it is one of the libraries or associated programs for e2fsck rather than e2fsck itself. Can you try updating e2fsprogs and e2fslibs as per this solution, please:

https://askubuntu.com/questions/747656/ext4-broken-file-system-on-ubuntu-14-04-4/868737#868737

My guess is:

  • An image which uses a cutting-edge feature of ext4; CRC metadata checksums
  • Older e2fsprogs and/or e2fslibs (for example perhaps you are running Ubuntu 16.04 without xenial-updates universe , or some similar issue whereby lots of Ubuntu users would normally get the updated programs, but you are not)

aoakley avatar Jul 23 '17 21:07 aoakley

You're right @aoakley, this has nothing to do with your scripts.

Some version of the Rasbpian image (June, maybe July) shipped with the metadata_csum feature, that broke older e2fsck versions. See https://github.com/RPi-Distro/pi-gen/issues/84.

martignoni avatar Jul 24 '17 08:07 martignoni

Oh, thank goodness.

So it looks like the Raspbian images from June and early July 2017 used metadata_csum which is not compatible with e2fsck on several long-term-support distributions of Linux, notably Ubuntu 16.04 .

https://www.raspberrypi.org/forums/viewtopic.php?t=186769&p=1178842

The image dated 21 June 2017 is definitely known to have this problem.

There are two possible solutions:

  1. Use a Raspbian image dated 5th July 2017 or newer, or use one dated May 2017 or earlier.

or

  1. Upgrade your version of e2fsck to 1.43.4-2 or newer. A safe way of doing this is to install e2fsck.static (which won't overwrite your existing e2fsck) and then change my script to replace e2fsck with e2fsck.static

I think I'm ready to close this issue unless anyone else has got something to say.

Thanks for all your help diagnosing this. As I use my own script for my own archival of Cotswold Jam images, I am extremely sensitive to it possibly going wrong and losing lots of hard work!

aoakley avatar Jul 24 '17 13:07 aoakley

Yes pi-gen was fixed before release of the image dated 5th July 2017 (see https://github.com/RPi-Distro/pi-gen/commit/234c4b79371dd2f4531482e1045752de9e183a68), so the only image that suffers of this problem is the one dated 21 June 2017.

martignoni avatar Jul 24 '17 16:07 martignoni

Thanks for all the reactions and help guys!

I've used a VM with Ubuntu 17.10 to shrink. The Docker "FROM ubuntu:17.10" unfortunately didn't work:

user@Users-MBP  ~/Developer/raspbian-shrink  docker build -t cotswoldjam/raspbian-shrink .
Sending build context to Docker daemon  252.4kB
Step 1/8 : FROM ubuntu:17.10
Get https://registry-1.docker.io/v2/library/ubuntu/manifests/14.04: unauthorized: incorrect username or password

I think it's also possible to disable the metadata_csum: tune2fs -O ^metadata_csum /dev/path/to/disk https://ext4.wiki.kernel.org/index.php/Ext4_Metadata_Checksums

svh1985 avatar Jul 25 '17 10:07 svh1985

Your error message

unauthorized: incorrect username or password

seems to indicate a problem not related to current issue. You should IMHO open another issue.

martignoni avatar Jul 25 '17 10:07 martignoni

That looks like a Docker hub authentication issue, and nothing to do with the scripts or original problem reported. This is strange because you should be able to pull images without any login to the hub.

What version of docker are you running?

If you want to try 14.04, try:

FROM ubuntu:trusty

You can see the tags available here: https://hub.docker.com/_/ubuntu/ trusty is a synonym for 14.04.

However, it seems the original e2fsck problem stems from a build issue in Raspian, if I've understood properly? It might be worth rebuilding your RPi with a newer version of Raspian, or trying to disable the csum values in your current ext4 partition. Of course, it might also be possible to create a docker image with an e2fsck version that supports this meta data. I've not had time to figure out that process yet.

60% of the time, it works every time

On 25 Jul 2017, at 11:09, Nicolas Martignoni [email protected] wrote:

Your error message

unauthorized: incorrect username or password

seems to indicate a problem not related to current issue. You should IMHO open another issue.

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jjo93sa avatar Jul 25 '17 10:07 jjo93sa

My docker client was signed in with my Docker ID. Once I've signed out, the installation magically works!

Running the manual commandline works but when I setup the .bashrc and run shrink large.img small.img I get this:

Unable to find image 'svh1985/raspbian-shrink:latest' locally
docker: Error response from daemon: repository svh1985/raspbian-shrink not found: does not exist or no pull access.
See 'docker run --help'.

But it exists in the repo:

REPOSITORY                TAG                 IMAGE ID            CREATED             SIZE
svh1988/raspbian-shrink   latest              644346ca7838        2 minutes ago       133MB
ubuntu                    devel               c8cdcb3740f8        4 days ago          93.9MB

ps. the docker file has a typo, raspbian misses the S: ${DOCKER_REPO_PREFIX}/raspbian-shrink "$@"

svh1985 avatar Jul 25 '17 11:07 svh1985

Glad you got it to build. Can you run a list of your images?

docker images | grep shrink

and see if your image has been tagged properly?

60% of the time, it works every time

On 25 Jul 2017, at 12:47, svh1985 [email protected] wrote:

My docker client was signed in with my Docker ID. Once I've signed out, the installation magically works!

Running the manual commandline works but when I setup the .bashrc and run shrink large.img small.img I get this:

Unable to find image 'svh1985/raspbian-shrink:latest' locally docker: Error response from daemon: repository svh1985/raspbian-shrink not found: does not exist or no pull access. See 'docker run --help'. ps. the docker file has a typo, raspbian misses the S: ${DOCKER_REPO_PREFIX}/raspbian-shrink "$@"

— You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jjo93sa avatar Jul 25 '17 11:07 jjo93sa

I removed all the images, recreated it and saved the shrink() function with the typo correction and everything works!

May I suggest you to correct the typo in the shrink() function:

DOCKER_REPO_PREFIX=<name>

shrink()
{
   docker run --rm \
       -it \
       -v `pwd`:/work-dir \
       --device=/dev/loop0:/dev/loop0:rw \
       --cap-add=SYS_ADMIN \
       --name shrink \
       ${DOCKER_REPO_PREFIX}/raspbian-shrink "$@"
}

svh1985 avatar Jul 25 '17 13:07 svh1985

That's awesome. Would you please post how you fixed the csum problem? Did you change Raspbian version? Or was the change of ubuntu version all that was needed?

60% of the time, it works every time

On 25 Jul 2017, at 14:02, svh1985 [email protected] wrote:

I removed all the images, recreated it and saved the shrink() function with the typo correction and everything works!

May I suggest you to correct the typo in the shrink() function:

DOCKER_REPO_PREFIX=

shrink() { docker run --rm
-it
-v pwd:/work-dir
--device=/dev/loop0:/dev/loop0:rw
--cap-add=SYS_ADMIN
--name shrink
${DOCKER_REPO_PREFIX}/raspbian-shrink "$@" } — You are receiving this because you authored the thread. Reply to this email directly, view it on GitHub, or mute the thread.

jjo93sa avatar Jul 25 '17 18:07 jjo93sa

@jjo93sa can you push an update to your script please? I could probably edit it given svh's comment, but I'm not (yet) a Docker user, so I have no way of testing the script. Thanks all.

aoakley avatar Jul 25 '17 19:07 aoakley

Can do. First though, I would like to get to the bottom of why svh's shrink didn't work then did, in case there's anything else I need to fix.

60% of the time, it works every time

On 25 Jul 2017, at 20:05, Andrew Oakley [email protected] wrote:

@jjo93sa can you push an update to your script please? I could probably edit it given svh's comment, but I'm not (yet) a Docker user, so I have no way of testing the script. Thanks all.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub, or mute the thread.

jjo93sa avatar Jul 25 '17 20:07 jjo93sa

I think docker automatically downloads the Ubuntu LTS version which doesnt work with the csum image.

So I changed the Dockerfile and corrected the typo in the script. Dockerfile:

FROM ubuntu:devel

RUN apt-get -y update && apt-get -y install dcfldd

# Copy the raspbian-shrink script file into somewhere on the $PATH
COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

# Change the script permissions
RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ]
CMD ["original.img", "shrunk.img"]

ps. the Docker unauthorized: incorrect username or password error had to do with my Docker hub account so that can be ignored.

svh1985 avatar Jul 26 '17 09:07 svh1985

That's great. I'll send over the new files with the typo fixed.There's also some improvements to the Dockerfile that can be made.

@svh1985 - if you have a copy of the image with the csum enabled, and don't mind sharing it, it would be very useful for me to test with the new version of the container. I can send a link for you to upload it to me.

On 26 July 2017 at 10:28, svh1985 [email protected] wrote:

I think docker automatically downloads the Ubuntu LTS version which doesnt work with the csum image.

So I changed the Dockerfile and corrected the typo in the script. Dockerfile:

FROM ubuntu:devel

RUN apt-get -y update && apt-get -y install dcfldd

Copy the raspbian-shrink script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ] CMD ["original.img", "shrunk.img"]

ps. the Docker unauthorized: incorrect username or password error had to do with my Docker hub account so that can be ignored.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4#issuecomment-318000762, or mute the thread https://github.com/notifications/unsubscribe-auth/APNM0zPCqC7basOQVRQrs-My2LUJgpn_ks5sRwbNgaJpZM4NuMGE .

jjo93sa avatar Jul 26 '17 09:07 jjo93sa

Hi,

So this is what I'm using now.

In .bashrc:

export DOCKER_REPO_PREFIX=

Shrink a RPi disk image, source is $1, dest is $2

Dockerfile: Docker/shrink

shrink()

{

docker run --rm \

    -it \

    -v `pwd`:/work-dir \

    --device=/dev/loop0:/dev/loop0:rw \

    --cap-add=SYS_ADMIN \

    --name shrink \

    ${DOCKER_REPO_PREFIX}/shrink "$@"

}

And the Dockerfile is:

#If e2fsck error, change to

#FROM ubuntu:devel

FROM ubuntu:latest

LABEL maintainer "some string"

RUN apt-get -y update && apt-get -y install dcfldd

Clone the repo into the same directory as this dockerfile

git clone https://github.com/aoakley/cotswoldjam.git

Copy the script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ]

CMD ["original.img", "shrunk.img"]

All other steps and commands stay the same. I can send the files over as attachments if that helps. I could also create a repo for them and send a pull-request. Although, that might be way more hassle than simply creating the files yourself!

I've tested this on one of the RPi images I have available on my MacBook, and it worked OK for me, using Docker version 17.06.1-ce-rc1. I've not been able to test it on an image with the sum value that caused the original issue though.

Cheers,

James

On 26 July 2017 at 10:36, James Osborne [email protected] wrote:

That's great. I'll send over the new files with the typo fixed.There's also some improvements to the Dockerfile that can be made.

@svh1985 - if you have a copy of the image with the csum enabled, and don't mind sharing it, it would be very useful for me to test with the new version of the container. I can send a link for you to upload it to me.

On 26 July 2017 at 10:28, svh1985 [email protected] wrote:

I think docker automatically downloads the Ubuntu LTS version which doesnt work with the csum image.

So I changed the Dockerfile and corrected the typo in the script. Dockerfile:

FROM ubuntu:devel

RUN apt-get -y update && apt-get -y install dcfldd

Copy the raspbian-shrink script file into somewhere on the $PATH

COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

Change the script permissions

RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

VOLUME [ "/work-dir" ]

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ] CMD ["original.img", "shrunk.img"]

ps. the Docker unauthorized: incorrect username or password error had to do with my Docker hub account so that can be ignored.

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4#issuecomment-318000762, or mute the thread https://github.com/notifications/unsubscribe-auth/APNM0zPCqC7basOQVRQrs-My2LUJgpn_ks5sRwbNgaJpZM4NuMGE .

jjo93sa avatar Jul 27 '17 17:07 jjo93sa

@jjo93sa unfortunately I can not share the image because it contains company data, sorry.

To solve this issue we just need to change the Dockerfile to FROM ubuntu:devel and fix the typo in the attach() script. :)

svh1985 avatar Jul 27 '17 18:07 svh1985

Sorry, apparently markdown formatting isn't supported in email replies. So, here it is formatted better.

In .bashrc:

export DOCKER_REPO_PREFIX=<name>

# Shrink a RPi disk image, source is $1, dest is $2
# Dockerfile: Docker/shrink
shrink()
{
    docker run --rm \
        -it \
        -v `pwd`:/work-dir \
        --device=/dev/loop0:/dev/loop0:rw \
        --cap-add=SYS_ADMIN \
        --name shrink \
          ${DOCKER_REPO_PREFIX}/shrink "$@"
}

And the Dockerfile is now:

FROM ubuntu:latest
#If you get an e2fsck error referencing csum issues, change to
#FROM ubuntu:devel

LABEL maintainer "some string"

RUN apt-get -y update && apt-get -y install dcfldd

# Clone the cotswoldjam repo into the same directory as this dockerfile
# git clone https://github.com/aoakley/cotswoldjam.git

# Copy the script file into somewhere on the $PATH
COPY ./cotswoldjam/raspbian-shrink /usr/local/bin

# Change the script permissions
RUN chmod a+x /usr/local/bin/raspbian-shrink

WORKDIR /work-dir

ENTRYPOINT [ "/usr/local/bin/raspbian-shrink", "-m", "256" ]

CMD ["original.img", "shrunk.img"]

jjo93sa avatar Jul 28 '17 13:07 jjo93sa

Hey, Hope I am not too late. Pretty cool to see a docker implementation. In CustomPiOS I did a similar thing using vagrant. The main advantage in using vagrant is that it also works on windows and 32bit systems

guysoft avatar Jul 28 '17 19:07 guysoft

Please forgive. I'm a complete NOOB and trudging through this as best I can. I have succeeded in building and running with

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink raspbian-shrink/raspbian-shrink RasPi.img shrunk.img

This works fine.

But when I try to use the quicker .bashrc solution, I run into some trouble. I'm using Mac High Sierra.

Should I be adding this...

DOCKER_REPO_PREFIX=raspbian-shrink

shrink()
{
   docker run --rm \
       -it \
       -v `pwd`:/work-dir \
       --device=/dev/loop0:/dev/loop0:rw \
       --cap-add=SYS_ADMIN \
       --name shrink \
       raspbian-shrink/raspbian-shrink "$@"
}

to /private/etc/bashrc ?? (notice there is no preceding period '.' before bashrc) or create a .bashrc in my home directory (Users/SAR) ??

I've tried both and then run source {directory}/[.]bashrc to reinitialize bashrc for either location to test and I keep getting this error:

docker: invalid reference format. See 'docker run --help'.

And i noticed that the .bashrc code shows "-v pwd:..." while the longer code shows "-v $(pwd):...". Ive tried both, but still no joy. And I suspect that the " ` " symbols should be " ' " instead, but again, I have no idea.

I can live with the long command if I have to, but it would be nice to be able to properly configure the .bashrc solution.

Thanks!

sarossell avatar Mar 13 '18 10:03 sarossell

Hi,

It is difficult to tell from your code output what is going wrong, but here's a few things to try:

  1. You should chand 'pwd' to $(pwd)

  2. If you've split the command in .bashrc over several lines, then you need to terminate each line before the end of the command with ' ', that is a space to separate it from the text and then an immediate carriage return;

`DOCKER_REPO_PREFIX=raspbian-shrink

shrink() { docker run --rm
-it
-v $(pwd):/work-dir
--device=/dev/loop0:/dev/loop0:rw
--cap-add=SYS_ADMIN
--name shrink
raspbian-shrink/raspbian-shrink "$@" }` This tells the shell that the line continues.

  1. Check that your image is tagged properly, execute: docker images and see if the raspbian-shrink image has a TAG of raspbian-shrink, if not change the DOCKER_REPO_PREFIX to the value you've found

  2. You can safely put this command in a .bashrc file in your home directory. And source it from there - it is probably easier to find out what's going on if you do that. Check is one exists already: cd ~; ls -la If you see a .bashrc file, just append the above text to it, and then source it: . .bashrc

  3. You can see if the function has been declared properly by running:

typset -f | grep shrink

This should give you some output like the command above

Hope that helps, if not ask again!

james

On 13 March 2018 at 10:01, sarossell [email protected] wrote:

Please forgive. I'm a complete NOOB and trudging through this as best I can. I have succeeded in building and running with

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink raspbian-shrink/raspbian-shrink RasPi.img shrunk.img

This works fine.

But when I try to use the quicker .bashrc solution, I run into some trouble. I'm using Mac High Sierra.

Should I be adding this...

`DOCKER_REPO_PREFIX=raspbian-shrink

shrink() { docker run --rm -it -v pwd:/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink raspbian-shrink/raspbian-shrink "$@" }`

to /private/etc/bashrc ?? (notice there is no preceding period '.' before bashrc) or create a .bashrc in my home directory (Users/SAR) ??

I've tried both and then run source {directory}/[.]bashrc to reinitialize bashrc for either location to test and I keep getting this error:

docker: invalid reference format. See 'docker run --help'.

I can live with the long command if I have to, but it would be nice to be able to properly configure the .bashrc solution.

Thanks!

— You are receiving this because you were mentioned. Reply to this email directly, view it on GitHub https://github.com/aoakley/cotswoldjam/issues/4#issuecomment-372610603, or mute the thread https://github.com/notifications/unsubscribe-auth/APNM01YBkeuCY33QHai8jA1SKJBUbTs-ks5td5jkgaJpZM4NuMGE .

jjo93sa avatar Mar 13 '18 11:03 jjo93sa

Thank you for your help James. I greatly appreciate it. Unfortunately, I'm still having the same issue. I did follow your directions as precisely as possible and triple checked my work.

I changed the "pwd" part of the .bashrc code as follows:

DOCKER_REPO_PREFIX=raspbian-shrink

shrink()
{
docker run --rm \
-it \
-v `$(pwd):/work-dir` \
--device=/dev/loop0:/dev/loop0:rw \
--cap-add=SYS_ADMIN \
--name shrink \
${DOCKER_REPO_PREFIX}/raspbian-shrink "$@"
}

I had to create a ~/.bashrc file because it didn't exist previously. I then sourced it and checked it with typeset. It displayed this:

docker run --rm -it -v $(pwd):/work-dir --device=/dev/loop0:/dev/loop0:rw --cap-add=SYS_ADMIN --name shrink ${DOCKER_REPO_PREFIX}/raspbian-shrink "$@"

(I don't know why there's space ahead of the command docker though - or if it matters).

And finally, I checked the tag of the Docker image and it was listed as "latest". So, used the docker tag command to have it match the .bashrc entry of "raspbian-shrink" as follows:

docker tag raspbian-shrink/raspbian-shrink:latest raspbian-shrink/raspbian-shrink:raspbian-shrink

Then I had two images. One tagged "latest" and the other tagged "raspbian-shrink".

  • Although, when I used the long command directly instead of the .bashrc shortcut, the image ran fine previously without changing the tag.

This is very perplexing. Although, I'm not near as smart as my hat and some say I'm about as useful as a wicker bowling ball.

Still, thanks for your help!

sarossell avatar Mar 13 '18 18:03 sarossell

I even tried just forcing the file names in .bashrc and making sure my image file was named "original.img". Still, same error.

shrink()
{
docker run --rm \
-it \
-v $(pwd):/work-dir \
--device=/dev/loop0:/dev/loop0:rw \
--cap-add=SYS_ADMIN \
--name shrink \
raspbian-shrink/raspbian-shrink original.img shrunk.img
}

sarossell avatar Mar 13 '18 18:03 sarossell