Creating Ubuntu 16.04 Mirror on a Synology Diskstation
I am constantly installing Ubuntu with various configurations. I find having a local Ubuntu 16.04 Mirror on a Synology Diskstation speeds up the installation and updates, making the time spent setting it up well worth while.
Backup your diskstation before doing anything else, just in case.
Configuring Diskstation
To configure your diskstation when using DSM 6.0 is reasonably simple.
Create a new share
Create new share ubuntu-mirror. I assume you can do this without help, if not stop now. This will hold around 90-150GB of packages for one version of Ubuntu.
Enable NFS for the new share, again I assume you know how to do this on your own system.
Configure Web Station
Install the the package Web Station
General settings tab
HTTP backend server: Nginx
PHP: any version will do, we will not be using it.
Personal websites: disable
PHP Setting:
No need for PHP.
Virtual hosts
Create a new name based virtual host, I use the hostname ubuntu-mirror. (Remember to add ubuntu-mirror to your DNS configuration).
Leave 80/443 ticked. The other ports just leave unticked.
HTTP Backend server: I use Nginx but Apache works just as well.
PHP: can be unticked we do not use it.
Installing apt-mirror
Back on the Ubuntu machine you will use to maintain your Ubuntu mirror.
sudo apt-get update sudo apt-get install apt-mirror
apt-mirror stores its repositories in /var/spool/apt-mirror. As we are going to be using our diskstation this directory needs to be a mount point to ubuntu-mirror on the Synology diskstation. An example mount command is below:
sudo mount diskstation:/volume1/ubuntu-mirror /var/spool/apt-mirror
Sort out the permissions on your own diskstation so that the user apt-mirror has read and write permissions to the files and directories in the diskstation share ubuntu-mirror. This can get tricky with too many different permutations for this post. (Or I do not want to get into diskstation permissions here 🙂 )
If the permissions are wrong apt-mirror will not run!
Update the file /etc/apt/mirror.list to list all the repositories you want to mirror. In the example below I also mirror the ubuntu-wine repo. I also mirror the i386 repos, without them you will see errors if you have enables i386 architecture. I do not mirror the source repo files. At the end of the file there are some clean configurations lines you need one line per repo. Play around and you will see what I mean.
When you want to download a new version simply copy/paste the existing lines again and change xenial to the name of the version you want to mirror. You will then have a local mirror with BOTH Ubuntu version available. To remove a version from your local mirror. Remove it from the mirror list leaving the clean line. When you rung apt-mirror it will clean up and remove the old version. You can then remove the clean lines.
############# config ################## # # set base_path /var/spool/apt-mirror # # set mirror_path $base_path/mirror # set skel_path $base_path/skel # set var_path $base_path/var # set cleanscript $var_path/clean.sh # set defaultarch# set postmirror_script $var_path/postmirror.sh # set run_postmirror 0 #set nthreads 20 #set limit_rate 200m set _tilde 0 # ############# end config ############## ######################################################### # Xenial 16.04 LTS ######################################################### deb https://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse deb https://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse #deb https://extras.ubuntu.com/ubuntu xenial main deb-i386 https://archive.ubuntu.com/ubuntu xenial main restricted universe multiverse deb-i386 https://archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse deb-i386 https://archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse deb-i386 https://archive.ubuntu.com/ubuntu xenial-proposed main restricted universe multiverse deb-i386 https://archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse #deb https://extras.ubuntu.com/ubuntu xenial main deb https://archive.ubuntu.com/ubuntu xenial main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb https://archive.ubuntu.com/ubuntu xenial-security main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb https://archive.ubuntu.com/ubuntu xenial-updates main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb https://archive.ubuntu.com/ubuntu xenial-backports main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb-i386 https://archive.ubuntu.com/ubuntu xenial main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb-i386 https://archive.ubuntu.com/ubuntu xenial-security main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb-i386 https://archive.ubuntu.com/ubuntu xenial-updates main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer deb-i386 https://archive.ubuntu.com/ubuntu xenial-backports main/debian-installer restricted/debian-installer universe/debian-installer multiverse/debian-installer # WINE deb https://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial main deb-i386 https://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial main ######################################################### # Clean up ######################################################### clean https://archive.ubuntu.com/ubuntu clean https://extras.ubuntu.com/ubuntu clean https://ppa.launchpad.net/ubuntu-wine/ppa/ubuntu clean https://ppa.launchpad.net/ubuntu-mate-dev/ppa/ubuntu
Create a new subdirectory and file /var/spool/apt-mirror/var/postmirror.sh. This file must be executable by the user apt-mirror. If should contain the following to cleanup your local mirror. There are examples, to be found on the Internet, that are more complex.
#!/bin/sh set -e /var/spool/apt-mirror/var/clean.sh /bin/echo -e "\nPackages downloaded were:" if [ -e /var/spool/apt-mirror/var/archive-urls.0 ]; then cat /var/spool/apt-mirror/var/archive-urls* rm /var/spool/apt-mirror/var/archive-urls* fi
You also need to create an empty file called /var/spool/apt-mirror/var/clean.sh. The permissions on this file must allow apt-mirror read – write – execute permissions. It will be recreated each time you run apt-mirror.
When you have the mount point and the permissions all sorted out you can run the following command to mirror the repos listed in mirror-list.
Depending on your internet speed the initial download will take a few hours to a few days, it is about 90 – 150GB of data. So you may want to comment out most of the repos listed while testing this out. The wine repo is reasonable small compared to the Ubuntu repos at around 300MB.
su - apt-mirror -c '/usr/bin/apt-mirror | tee /var/log/apt-mirror.log'
When apt-mirror runs correctly the output shows what is going on and how long each set took.
How to use your local mirror
After waiting for the initial download to complete. You are now wondering how to I use the local mirror.
Simply change the file /etc/apt/sources.list to point at your Ubuntu 16.04 Mirror on a Synology Diskstation as shown below. You may have to remove any additional repos you have added which can be found in /etc/apt/sources.list.d. You do not want to duplicate repos locally and remotely.
deb https://ubuntu-mirror/mirror/archive.ubuntu.com/ubuntu xenial main restricted universe multiverse deb https://ubuntu-mirror/mirror/archive.ubuntu.com/ubuntu xenial-updates main restricted universe multiverse deb https://ubuntu-mirror/mirror/archive.ubuntu.com/ubuntu xenial-backports main restricted universe multiverse deb https://ubuntu-mirror/mirror/archive.ubuntu.com/ubuntu xenial-security main restricted universe multiverse deb https://ubuntu-mirror/mirror/ppa.launchpad.net/ubuntu-wine/ppa/ubuntu xenial main
If all is working when you run the following it will download the release files very quickly. Installing new packages and updating existing packages will be done in a blink of an eye. (If you blink slowly 🙂 )
sudo apt-get update
Keep your mirror up to date
Keeping your local mirror up to date is simple are really quick. just add the following to the crontab jobs for root. This one runs at midnight. The pipe tee part at the end means you should get an email of the output from cron. If you have not set up email forwarding, just remove “| tee /var/log/apt-mirror.log” from the end.
01 0 * * * apt-mirror -c '/usr/bin/apt-mirror | tee /var/log/apt-mirror.log'