clone my linux
The snippet can be accessed without any authentication.
Authored by
Jakob Kirsch
Edited
clone.py 1.94 KiB
import os, subprocess
def get_size(path):
os.system("du --exclude=/proc --exclude=/sys --exclude=/tmp --exclude=/mnt --exclude=" + device_real + " -sl " + path + " > size.tmp")
with open("size.tmp", "r") as f:
size = int(f.read().replace("\t/\n", ""))
os.unlink("size.tmp")
return size
device_root = input("File: ")
device_real = os.path.abspath(device_root)
assert input("Are you sure?(n/y): ") == "y"
print("Getting image size")
size = get_size("/")
alloc = int(input("Factor(110%): ")) / 100
print("Image will be {} bytes big".format(int(size * alloc)))
print("Creating image")
os.system("dd if=/dev/zero of=" + device_root + " bs=1024 count=" + str(int(size * alloc)))
print("Mapping image")
os.system("(\necho o\necho n\necho\necho\necho\necho\necho w\n) | fdisk " + device_root)
print("Looping")
loop = subprocess.check_output(["losetup", "--show","-fP", device_root]).decode().replace("\n", "")
device = loop + "p1"
device_root = loop
print("Formatting partition")
os.system("mkfs.ext4 " + device)
print("Mounting image")
os.system("mount " + device + " /mnt")
print("Copying data")
for src in ["/bin", "/sbin", "/usr", "/lib*", "/boot", "/etc", "/initrd*", "/vmlinuz*", "/opt", "/var", "/home", "/root"]:
os.system("rsync --links -rv --exclude=" + device_real + " " + src + " /mnt/")
print("Binding necesseraly directorys")
for dir in ["dev", "proc", "sys", "tmp", "media", "run", "srv"]:
os.system("mkdir /mnt/" + dir)
os.system("mount --bind /" + dir + " /mnt/" + dir)
os.system("mkdir /mnt/root_mirror")
os.system("mount --bind / /mnt/root_mirror/")
print("Installing grub")
os.system("chroot /mnt /sbin/grub-install --recheck --no-floppy --root-directory=/ " + device_root)
os.system("chroot /mnt /sbin/update-grub")
print("Starting bash")
os.system("chroot /mnt /bin/bash")
print("Unmounting")
for dir in ["root_mirror", "dev", "proc", "sys", "tmp", "media", "run", "srv"]:
os.system("umount /mnt/" + dir)
os.system("umount /mnt")
Please register or sign in to comment